Stanford CS143 Compiler Fall2014 个人笔记
introduction
-
Deference between Compilers with Interpreters
Compiler is off-line, and it’s imput is “program” then compiler it to exec witch can coculate Data to Output.
Interpreters is on-line, and it’s input is Program and Data, it interpreter the Program line by line, and excute the code. -
History
1954 IBM develops the 704. In this time, software is more expensive than hardware. - Speedcoding
1954 John Backus.today’s interperters. very slow.
than he invented Fortran1(1954-1957). It’s the first compiler.
(好吧,英语记笔记有点慢)并且编译的核心由此确定为“理论(Theory)+练习(Pratice)” - 编译原理分为:lexical analysis, parsing, sematic analysis, optimization, code generation
lexical analysis和parsing注重句法分析,sematic注重语义,
1.1 编译器的结构
人类是如何理解一段英语的
理解语句 | 对应编译过程 |
---|---|
This is a sentence. | if x == y then z = 1; else z = 2; |
理解单词 将句子分割成单词集 {‘This’, ‘is’, ‘a’, ‘sentence’} |
词法分析 将独立的程序文本分割成单词或tokens(标记) 可以识别出关键字{‘if’, ‘and’, ‘else’}、变量名{‘x’, ‘y’, ‘z’}、常量{‘1’, ‘2’}、操作符{’==’, ‘’=’, ‘=’} |
理解句子结构 |
语法分析(语法树) |
理解句子意思 这是会有歧义的 |
语义分析 强制规定,避免模糊语言 |
简化语言表示 | 自动代码优化 但当Y==NaN时不能这么做 |
翻译成其他语言 | code生成 |