当前位置:网站首页>Self made compiler learning 2: compilation process

Self made compiler learning 2: compilation process

2022-06-09 08:25:00 I_ belong_ to_ jesus

compile 4 Stages

Narrow sense compilation can be roughly divided into 4 Stages : Syntax analysis -> Semantic analysis -> Generate intermediate code -> Code generation

Syntax analysis (syntax analyzing)

The first step in compiling is to... The code analysis parse) Also known as Syntax analysis syntax analyzing), The program module that parses the code is called Parser parser) or parsers syntax analyzer). The final result of parsing is the syntax tree , As shown in the figure below :

Semantic analysis

Syntax analysis Just analyze the appearance of the code , Semantic analysis (semantic analysis) Is to analyze the part beyond the representation , for example : Distinguish between local variables and global variables 、 Distinguish whether a variable is declared or referenced 、 Whether the variable is initialized, etc . The syntax tree just copies the structure of the code , Semantic analysis generates Abstract syntax tree Abstract Syntax Tree, AST) It also includes Semantic information , Links are added before the references and definitions of variables , Add commands such as type conversion to make the expression types consistent , in addition , Parentheses outside the expression 、 Semicolon at the end of line, etc , stay Abstract syntax tree Are omitted from .

Generate intermediate code

Intermediate code Intermediate Representation, IR) Mainly to support multi programming languages and multi machine languages , Generating uniform intermediate code will greatly simplify the subsequent operations .

Code generation

Intermediate code to assembly code .

原网站

版权声明
本文为[I_ belong_ to_ jesus]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/160/202206090812499743.html