Monday, March 4, 2013

Crafting a compiler

This post that introduces a classical compiler design book - Crafting a compiler. In early stage , compiler design was regarded as an art of  computer programming . The  reason  is this domain needs high theory and   experienced programming  skills. With open sources culture became more  popular , it was not  a  mysterious and difficult topic.   Anyway , we starting break the black box via this book. 

Before introducing it , what is a compiler ?  See below about introduction of compiler 
Compiler

Usually , compiler design can be parted into two parts , one is front end process and the other one is back end process. In the front end stage , it contains several parts , they are scanner , tokenizer , and  parser . For any programming languages , they owns specific syntax and semantics .  Syntax is  the structure of programming language . Semantics is the meaning of  programming language. 
Syntax typically means context-free syntax because of the almost universal use of context-free grammars (CFGs) as a syntactic specification mechanism. wiki CFG . Basically , any programming language syntax follow a formal grammar and production .  About  more CFGs , please refer to Automata theory . 
And why it need to define semantics ? For example , a = b + c . this is an arithmetic expression. According to the rule of  syntax , the expression can be parsed into a syntax tree . the following is its parse tree . 
                                                = 
                                              /    \
                                            a      +
                                                   /   \
                                                 b     c 
But it has a problem , that is compiler doesn't know what do you want . Because we didn't declare a,b and c type , so it can understand what do you evaluate .  
Because the above  limits of CFGs ( no type specific and scoping ) ,  it need to define semantics for it.  The semantics can be classified into two kinds. 
One is semantics is static and the other is runtime. 
Static semantics - it provides a set of rules that specify which syntactically legal programs are actually valid. ( 這是語言語法上的定義) . Basically , these belong to syntax rules , such as all identifiers be declared, that operators and operands be type-compatible, and that procedures be called with the proper number of parameters .and so forth. 


No comments:

Post a Comment