编译原理
引言: 如果你不能理解,那么你记不住
学习书籍:Compilers Principles,Techniques & tools
Programming languages are notations for describing computations to people and to machines.
程序语言就是给人看的,就是向人描述计算过程的,并且能够被 编译成机器运行的语言
The software systems that do this translation are called compilers.
做这种转译的软件系统就被称为编译器
the diference between compilers and intepretors
编译器和解释器的区别
- 同样作为语言处理器,
- 编译器是将一段高级语言的程序转换为低级语言程序
,或者是将一个接受输入的可执行的机器语言程序直接运行输出结果。
- 解释器是直接执行的用户输入
an intepreters appears to directly execute the operation specified in the source program on input supplied by the user.
比如 java 语言就会首先被编译成字节码,然后由 Java 的虚拟机执行
编译器的结构
up to this point we have treated a compiler as a single box that maps source program into semantically equivalent target program. if we open this box a little, we see that there are two parts to this mapping: analysis and synthesis.
简而言之,分析 和 合成。 分析阶段别被称为编译器的前端部分,将源程序转换为一种中间表示形式,分析其中是否有句法错误,并把一些数据信息存储在一个叫做 symbol table 的地方。 合成被称为编译器的后端部分,将这种中间形式的代码生成机器代码。
往更细了说,可以分为下面这些阶段 词法分析->句法分析->语义分析->中间代码->机器代码优化->代码生成
比如如下的代码
position = initial + rate * 60
会经历下面的处理过程
编译器的应用
编译器可以被用在以下方面
- 高级程序语言的实现。高级程序语言相比而言容易书写和理解,但是执行却相对较慢。对面向对象的支持,也是应用之一。
- 计算机体系结构的优化,并行,内存层次结构。
- 软件生产效率工具。类型检查,边界检查。
- 程序设计语言的基础。动态静态语言类型。