Read Dragon Book compilation Principle hand-written compiler (1) ...

Source: Internet
Author: User

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) ...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.