Come back from Shanghai this two days after reading the semantic analysis, the previous paragraph is almost, originally prepared to see Code generation, people suddenly do not want to see, old habits and committed: a long time did not knock code, preface, so decided to move forward side to achieve the front part ... Think finally can start to realize the compiler heart excited not, then so happy to start ...
First of all, of course, the language of the word French law, because the first time to write the compiler, do not want to be too complicated, I am going to write a simplified version of the C language, most of the lexical and grammatical generalship adopt the C language standard ...
The first is the basic type:
int, String,bool (none of the two C languages, but I added), in order to simplify the design, temporarily or without floating point number, and only support lowercase, uppercase will be treated as an identifier ...
Then the Process Control statement:
If-else If-else
While
(temporarily not supported for, Do-while, switch, break, continue)
Finally, we provide support for the function, but the function does not support the argument--! :
(But return is not supported)
The specific lexical requirements are as follows (not strictly in accordance with the C standard, for ease of implementation, it is Simplified):
ID: [a-za-z_][0-9a-za-z_]*int :-? [0-9]+string"[\" |.] *"
Next is the syntax:
Progfunc Funcs|func-type ID () Blockblock-{stmts}stmts-stmt Stmts|stmt-type ID; | Type ID =expr; | Type ID =judge; |if(judge) block|if(judge) blockElseBlock| while(judge) Blocktype-int|BOOL|stringExprExpr + Term| Expr- Term|TermtermTerm *unary| Term/unary|unaryunary!unary| -unary|Factorfactor-Num|Boolean|(expr) JudgeJudge | |Join|JoinjoinJoin &&Equality|equalityequalityEquality = =rel| Equality! =rel|RelrelExpr <Expr| Expr <=Expr| Expr >=Expr| Expr >Expr|Expr
For the time being think so much, not enough after ... The next section begins the code ...
Read Dragon Book compilation Principle hand-written compiler (1) ...