tags: Compiler Design
Lec1: Overview
Two Parts of a Compiler
1. Analysis(分析) part
-
Front end of the compiler
-
Divide the source program(High-level language)
-
Generate an Intermediate Representation(IR,中間語言)
2. Synthesis(合成) part
-
Back end of the compiler
- Generate the desire target program
6 Phases of the Compiler
Intermediate Code Generation In Compiler Design Notes
-
Lexical analysis(字典、辭彙分析)
-
Syntax analysis(語法、句法分析)
-
Semantic analysis(語意分析)
-
Intermediate code generation(4,5階段間IR產生)
-
Code optimization(最佳化)
-
Code generation(產生target language)
1st Phase: Lexical Analysis(辭彙分析),Scanning
- 同義詞: analysis/ scanning/ lexing/ tokenization(把string 切為 tokens)
- 定義: Look words up in the dictionary. 需要檢查有無存在於字典(Dictionary)內
in Analogy to English |
Syntax Analysis Example |
|
|
- Examlpe
由範例可知,Lexical主旨在"Tokenize",我們可以將position的公式對照Symbol Table(符號表)切分為對應<id,1> <=> <id,2> <+> <id,3> <*> <60>,其中id為identifier(辨認字)的縮寫
2nd Phase: Syntax analysis(語法分析),Parsing
- 同義詞: Parsing(剖析)
- 定義: Check if youre words do compose(組成) a sentence, find out the grammatically incorrect(文法錯誤)
in Analogy to English |
Syntax Analysis Example |
|
|
- Example
我們以能建出"Syntax Tree",作為符合文法的
3rd Phase: Semantic analysis(語意分析)
- 定義: To understand the "meaning" of a sentence
- 包含: Type checking,將錯誤的型別(Ex: int->float)作"Type Conversion"
4th Phase: Intermediate Code Generation
- A low-level IR will has following characters:
- Easy to product from syntax tree
- Easy to translate into the target assembly code
- use three-adress code
5th Phase: Code Optimization
- 優化:用更短的程式碼跑得更快
- Often takes the most of the compilation time
6th Phase: Code Generation
Whole Picture,(6 Phases整體關聯圖)