Intro Compiler
Intro Compiler
Sept. 2007
Program
Interpreter Output
Input
Source Target
Compiler input
Program Program
Editor
• Editor Absolute Machine Code
Ls Lt
Li
• Self extending
– Problem: if there is no any compiler available, we want
to develop a compiler for a programming language L;
- Solution:
- Define L0 as a sub-language of L;
- Manually write a compiler for L0;
- Make some extensions to L0, which is called L1,
- Develop L1’s compiler with L0;
- ……
- Develop Ln(=L)’s compiler with Ln-1;
• Preprocessing
– Problem: if we have a programming Language L and its
compiler, we want to develop a compiler for a
programming languageL1 which makes some extensions
to L;
- Solution:
- Develop a preprocessor: Translating L1 into L
- Use L’s compiler: from L to Target code
- For example: C++ C
• Porting
– Problems:
• source language L
• L’s compiler for machine M1
• we wan to develop another compiler of L for machine M2;
– Same source language, Different target languages
– Two ways
• Develop a program for translating from machine code for M1 to
machine code for M2;
• Rebuild the back-end of the compiler
You can put your dream into reality through your efforts!
你 能够 通过你的努力 实现你的梦
想!
General process of translation: analysis
Recognize Grammatical
meaningful translation
words Analysis
中 中 目
Source 词 语 语 间 间 标 Target
program 法 法 义 代 代 代 Program
分 分 分 码 码 码
析 析 析 生 优 生
成 化 成
Error Handling
Intermediate Code
Syntax Analysis Optimization
Parsing
Intermediate Code
Semantic Analysis Generation
• Sample program
var
int x, y;
{
read(x);
read(y);
if x>y then write(1) else write(0);
}
v a r i n t △ x
, y ; {
r e a d ( x ) ; r e a d ( y
)
; i f △ x > y △ t h e n △
w r i t e ( 1 ) △ e l s e △ w
r i t e ( 0 ) ; }
x read-statement if-statement
int x, y
write-statement
y expression
expression
x 〉 y write-statement
0
expression
1
Compiler Construction Principles & Implementation Techniques -41-
College of Computer Science & Technology
Semantic Analysis
x varKind int ……
y varKind int ……