Chapter 1
定义(Definition)
A
compiler
is a program
that reads a program written in one programming language (the
source
language
) and
translates
it into an
equivalent
program in another programming language (the
target
language
).
The source program may contain some errors
.
If errors occur, the compiler
must detect and report all errors.
![](https://i-blog.csdnimg.cn/blog_migrate/7dcde01f774397347aa1529586f1980f.png)
结构(Structure)
A compiler is not one big piece of software.
It has
a sequence of
phases
步骤/时期
.
On the high level:
•
Input:
source program
•
Source code analysis分析
(
front end前端
)
•
Intermediate code generator
•
Synthesis综合
(
back end后端
)
•
output:
target program
![](https://i-blog.csdnimg.cn/blog_migrate/b3d0cedd605db0e7104febcb2117d4be.png)
分析(Analysis----front end)
三个步骤
- lexical analysis (词法分析或者叫做扫描scanning)
- syntax analysis
- semantic analysis
词法分析(Lexical analysis)
语法分析(Syntax analysis)
语义分析(Semantic analysis)
错误类型(Error)
个人观点:
lexcial 可以看作是单词的错误,例如拼写等
syntax 则更多是结构上的错误,正如英语上的语法有误,缺少了哪些成分
semantic 就是偏理解文本意思的错误,逻辑困乱,类型不适配等
源代码分析的其他应用程序(了解)
中间代码生成(Intermediate Code Generator)
三地址代码(Three-address code)----一种中间表示形式
综合(Synthesis----back end)
代码优化(Code optimization)
代码生成(Code generation)
指令选择(Instruction selection)
指令调度(Instruction scheduling)
寄存器分配(Register allocation)
把值传给内存需要地址的转换等故需要增加运算
其他组成成分(Other components)
符号表管理(symbol table manager)
错误处理器(Error handler)
总流程(Overall flow)
其他步骤(Other phases)