Compiler Development Series--ocelot language 1. Abstract syntax tree

Source: Internet
Author: User

Start today by researching and developing your own programming language Ocelot, starting with the self-made compiler, and then perfecting the function and optimizing it.

Compiler front-end simple, do not delve into, directly with a ready-made tool called JAVACC, it can generate abstract syntax tree, abstract syntax tree is the key to generate intermediate code, and intermediate code is the key to generate back-end code.

The entire compiler code is written in Java language, the main function is to JAVACC generated abstract syntax tree for semantic analysis, optimization, and finally generate the optimized assembly code, and then use the assembler to assemble the assembly code to generate machine code, and finally the command chain to deliver the Linux executable file, You can run it directly on Linux.

The syntax used by the compiler is basically the C language syntax, to remove some of the syntax into the C language simplified version, and the original project is not optimized. What I want to do is to optimize and support garbage collection on the basis of the original project. - -! Some have played.

The abstract syntax tree and its nodes are inherited from the node class. Introduce the hierarchy of inheritance for node groups:

To see the structure of the abstract syntax tree using a simple HelloWorld demo, the demo is as follows:

int main (int argc, char **argv) {int I, j = 5;if (i) {return (J * 1-j);} else {exit (1);}}

The abstract syntax tree that is generated after the compiler project runs is as follows:

<<AST>> (g:\ compiling principle \ homemade compiler \ Source \test\hello.cb:1) Variables:functions: <<DefinedFunction>> (g:\ Compiling principle \ self-made compiler \ Source \test\hello.cb:1) Name: "Main" Isprivate:false Params:parameters: <<parame Ter>> (g:\ compilation principle \ self-compiler \ Source \test\hello.cb:1) Name: "ARGC" Typenode:int <<paramet Er>> (g:\ compilation principle \ self-compiler \ Source \test\hello.cb:1) Name: "argv" typenode:char** Body: << Blocknode>> (g:\ compilation principle \ self-compiler \ Source \test\hello.cb:2) Variables: <<DefinedVariable>> (g:\ compiling principle \ self-made compiler \ Source \test\hello.cb:3) Name: "I" Isprivate:false Typenode:int Initialize R:null <<DefinedVariable>> (g:\ compiling principle \ homemade compiler \ Source \test\hello.cb:3) Name: "J" Ispr Ivate:false typenode:int initializer: <<IntegerLiteralNode>> (g:\ compilation principle \ Self        system compiler \ Source \test\hello.cb:3)        Typenode:int value:5 Stmts: <<IfNode>> (g:\ compiling principle \ Homemade compiler \ Source code \test\hello . Cb:4) Cond: <<VariableNode>> (g:\ compiling principle \ homemade compiler \ Source \test\hello.cb:4) name : "I" Thenbody: <<BlockNode>> (g:\ compilation principle \ Homemade compiler \ Source \test\hello.cb:4) vari                    Ables:stmts: <<ReturnNode>> (g:\ compilation principle \ self-made compiler \ Source \test\hello.cb:5)                        Expr: <<BinaryOpNode>> (g:\ compiler principle \ self-compiler \ Source \test\hello.cb:5) Operator: "-" Left: <<BinaryOpNode>> (g:\ compiler principle \ self-compiler \ Source \test \HELLO.CB:5) Operator: "*" Left: <                            <VariableNode>> (g:\ compiling principle \ homemade compiler \ Source \test\hello.cb:5) Name: "J"            Right                    <<IntegerLiteralNode>> (g:\ compiling principle \ homemade compiler \ Source \test\hello.cb:5) t Ypenode:int value:1 Right: <<vari                Ablenode>> (g:\ compilation principle \ self-compiler \ Source \test\hello.cb:5) Name: "J" Elsebody:                    <<BlockNode>> (g:\ compiler principle \ self-compiler \ Source \test\hello.cb:7) Variables:stmts: <<ExprStmtNode>> (g:\ compiling principle \ homemade compiler \ Source \test\hello.cb:8) Expr: & Lt;<funcallnode>> (g:\ compilation principle \ self-compiler \ Source \test\hello.cb:8) Expr: <                        <VariableNode>> (g:\ compiling principle \ homemade compiler \ Source \test\hello.cb:8) Name: "Exit"                            Args: <<IntegerLiteralNode>> (g:\ compiler principle \ self-compiler \ Source \test\hello.cb:8)  Typenode:int                          Value:1 

1.<<ast>> and <<DefinedFunction>> represent the class name of the node.

2. The right side of the display (G:\ compilation principle \ self-compiler \ Source \test\hello.cb:1) is the corresponding syntax of the node file name and line number.

3. Indentation means that the node is referenced by the previous node.

Compiler Development Series--ocelot language 1. Abstract syntax tree

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.