学习课程的目的
想成为一名优秀的计算机专业本科生,应该对程序编译有所了解。通过这节课学习编译器的构造,理解程序编译的实现过程,实现一个编译器,提升自己的能力。
第一周 介绍和Cool编译器
Week 1: Introduction & the Cool Programming Language
编译器和解释器的区别
The difference between Compilers and Interpreters
FORTRAN I
The introduction of FORTRAN I
- The first compiler.
— Huge impact on computer science - Led to an enormous body of theoretical work.
- Modern compilers preserve the outline of FORTRAN I.
The structure of FORTRAN I
- Lexical Analysis
- Parsing
- Semantic Analysis
- Optimization
- Code Generation
Explain the above structure
- 词法分析将程序分解成“单词”或“符号”
Lexical analysis divides program text into “words” or “tokens” - 解析 = 图解句子
Parsing = Diagramming Sentences
— The diagram is a tree
- 编译器执行有限的语法分析来捕获不一致性
Compilers perform limited semantic analysis to catch inconsistencies
编程语言定义了严格的规则来避免这些歧义性。
Programming languages define strict rules to avoid such ambiguities.
- 优化过程在英语中没有十分对应的过程
Optimization has no strong counterpart in English
自动修改程序以便运行更快和使用更少内存
Automatically modify programs so the they
— Run faster
— Use less memory
- 代码生成通常生成汇编代码
Code Generation usually produce assembly code
从FORTRAN到现代编译器的改变。
The changes from FORTRAN to the modern compilers.
编程语言的经济性
The economy of programming languages
为什么有如此多的编程语言?
Why are there s many programming languages?
应用域具有独特或冲突的需要。
Application domains have distinctive/conflicting needs.
为什么有新的编程语言?
Why are there new programming languages?
- 广泛使用的语言改变缓慢。
Widely used languages are slow to change. - 开始一门新语言很简单。
Easy to start a new language. - 填补空白的语言。
Languages adopted to fill a void.
声明:程序员培训是编程语言的主要成本。
Claim: Programmer training is the dominant cost for a programming language.
什么是一个好的编程语言?
What is a good programming language?
语言设计没有普遍接受的度量标准。
There is no universally accepted metric for language design.
COOL Overview
cool的VM安装方式(推荐使用这个)
The Introduction of COOL
- Classroom Object Oriented language
- Design to be implementable in a short time
- Give a taste of implementation of modern
(1) Abstraction
(2) Static typing
(3) Reuse(inheritance)
(4) Memory management
And more … - But many things are left out
My first COOL programming
class Main{
main():Int { 1 };
};
(*需要返回值*)
class Main {
i : IO <- new IO;
main():IO { i.out_string("Hello World!\n") };
}
(*返回类型需要相等*)
class Main {
i : IO <- new IO;
main():Object { i.out_string("Hello World!\n") };
}
(*Object类型也可以*)
class Main {
main():Object { (new IO).out_string("Hello World!\n") };
}
class Main inherits IO{
main():Object { self.out_string("Hello World!\n") };
}
(*继承写法*)
COOL example II
class Main {
main():Object {
(new IO).out_string((new IO).in_string().concat("\n\n"))
};
};
(*从键盘输入字符串,输出该字符串*)
class Main inherits A2I{
main():Object {
(new IO).out_string(i2a(a2i((new IO).in_string())+1).concat("\n\n"))
};
};
(*
*将输入的字符串转换为数字串,输出数字串+1的字符串
*注:编译的时候需要将atoi.cl一同编译,Main类继承
*)
class Main inherits A2I{
main():Object {
(new IO).out_string(i2a(fact(a2i((new IO).in_string()))).concat("\n\n"))
};
fact(i: Int): Int {
if (i = 0) then 1 else i * fact(i-1) fi
};
};
(*求阶乘递归写法*)
class Main inherits A2I{
main():Object {
(new IO).out_string(i2a(fact(a2i((new IO).in_string()))).concat("\n\n"))
};
fact(i: Int): Int {
let fact: Int <- 1 in { (*fact 初始化为1*)
while (not (i = 0)) loop
{
fact <- fact * i; (*<-为赋值,=为比较*)
i <- i - 1;
}
pool;
fact;
}
};
};
(*求阶乘无递归写法*)
COOL example III
class List inherits A2I {
item: Object;
next: List;
init(i: Object, n: List): List {
{
item <- i;
next <- n;
self;
}
};
flatten(): String {
let string: String <-
case item of
i: Int => i2a(i);
s: String => s;
o: Object => { abort(); ""; };
esac
in
if (isvoid next) then
string
else
string.concat(next.flatten())
fi
};
};
class Main inherits IO {
main():Object {
let hello: String <- "Hello ",
world: String <- "World!",
newline: String <- "\n",
nil: List,
list: List <-
(new List).init(hello,
(new List).init(world,
(new List).init(newline, nil)))
in
out_string(list.flatten())
};
};
(*该样例程序比较难,跟着视频能收获更多*)