Theory of Computation and Compilers
University Computer Science Topic 11 5:58 English narration · English and 中文 subtitles you can switch
Chapters
Transcript
This course asks what machines can decide, and how text becomes meaning.
这门课探讨机器能够判定什么,以及文本如何变成有意义的结构。
The first three units are theory: automata and regular languages, context-free languages, and computability.
前三个单元是理论:自动机与正则语言、上下文无关语言,以及可计算性。
The last three are practice, the stages of a compiler: lexing, parsing, and semantic analysis with code generation.
后三个单元是实践,也就是编译器的各个阶段:词法分析、语法分析,以及语义分析和代码生成。
Every trace, table and token position in the course is checked by the suite.
课程中的每条运行轨迹、每张表和每个词法单元的位置都经过测试套件检验。
A deterministic finite automaton reads its input one symbol at a time and accepts only if it ends in an accepting state.
确定有限自动机每次读取一个输入符号,只有最终停在接受状态才接受输入。
The machine for strings ending in a b runs on a a b a b through q0, q1, q1, q2, q1, q2, and accepts.
识别以 a b 结尾的字符串的自动机,处理 a a b a b 时依次经过 q0、q1、q1、q2、q1、q2,并接受该字符串。
The pumping lemma shows a to the n b to the n is not regular: finite memory cannot count.
抽引引理表明 a 的 n 次方后接 b 的 n 次方所构成的语言不是正则语言:有限的存储空间无法计数。
Walk the arrows of this three-state machine as you read the trace.
阅读运行轨迹时,沿着这个三状态自动机的箭头走一遍。
Each a takes you to q1, and a b straight after an a takes you to q2.
每个 a 都会让你进入 q1,紧跟在 a 后面的 b 会让你进入 q2。
The string a a b a b ends at q2, so it is accepted.
字符串 a a b a b 最终停在 q2,所以被接受。
The string a b a ends at q1, so it is rejected.
字符串 a b a 最终停在 q1,所以被拒绝。
The machine remembers only its state.
自动机只记住自己当前的状态。
A context-free grammar can nest.
上下文无关文法可以形成嵌套。
With E goes to E plus T, T goes to T times F, and F goes to id, the string id plus id times id has only one parse tree, and multiplication binds tighter.
对于 E → E + T、T → T × F 和 F → id,字符串 id + id × id 只有一棵语法分析树,并且乘法的优先级更高。
A flat grammar, E goes to E plus E or E times E, gives two trees: it is ambiguous.
扁平文法 E → E + E 或 E → E × E 则产生两棵树:它是二义的。
Layering the grammar cures it.
将文法分层可以解决这个问题。
Compare the two pictures.
比较这两张图。
With the layered grammar, there is exactly one tree, with multiplication deeper than addition.
使用分层文法时,恰好只有一棵树,乘法位于比加法更深的层次。
With the unlayered grammar, the same string has two trees, one meaning a plus b, times c, and the other meaning a plus, b times c.
使用未分层的文法时,同一个字符串有两棵树,一棵表示 a 加 b 的和再乘以 c,另一棵表示 a 加上 b 与 c 的乘积。
A string must pick exactly one tree, because the tree is the meaning.
一个字符串必须恰好对应一棵树,因为树决定了它的含义。
Turing machines formalise algorithms, and some questions no machine can answer.
图灵机将算法形式化,而有些问题是任何机器都无法回答的。
Suppose H decides whether any program halts.
假设 H 能判定任意程序是否停机。
Build D, which asks H about itself and does the opposite.
构造 D,让它向 H 询问自身的情况,并做出相反的行为。
Running D on D is a contradiction, so H cannot exist.
用 D 处理 D 会产生矛盾,因此 H 不可能存在。
Rice's theorem extends this to every nontrivial property of a program's behaviour.
Rice 定理将这一结论推广到程序行为的所有非平凡性质。
DFA questions stay decidable, because they are finite.
DFA 的相关问题仍然是可判定的,因为 DFA 是有限的。
The diagonal is the whole proof.
对角线就是整个证明的关键。
D takes a machine description, asks H whether that machine halts on itself, and then does the opposite: it loops if H says halts, and halts if H says loops.
D 接收一台机器的描述,询问 H 这台机器以自身为输入时是否停机,然后做出相反的行为:如果 H 说停机,它就循环;如果 H 说循环,它就停机。
Feed D its own description, and whatever H answers is wrong.
把 D 自己的描述输入 D,无论 H 如何回答都是错的。
So no general halting decider can exist.
因此,通用的停机判定器不可能存在。
A lexer turns characters into tokens, each with a type, its text and its position.
词法分析器将字符转换为词法单元,每个单元都有类型、文本和位置。
The line count equals a1 plus b2 semicolon gives six tokens, at columns 1, 7, 9, 12, 14 and 16.
代码行 count = a1 + b2; 产生六个词法单元,分别位于第 1、7、9、12、14 和 16 列。
Longest match wins, so i f x is one identifier, while if alone is a keyword.
采用最长匹配原则,因此 ifx 是一个标识符,而单独的 if 是关键字。
Positions travel with every token for error messages.
每个词法单元都携带位置信息,用于生成错误消息。
Characters go in, tokens come out.
字符进入,词法单元输出。
At each point, the lexer takes the longest match it can, and when two rules match the same length, the one with priority wins, so keywords beat identifiers.
在每个位置,词法分析器都尽可能采用最长匹配;如果两条规则匹配的长度相同,就由优先级较高的规则胜出,因此关键字优先于标识符。
Count the six tokens in the example line and check each position.
数一数示例代码行中的六个词法单元,并检查每个位置。
Words come before sentences.
先有单词,才有句子。
An LL(1) parser uses a table and a stack.
LL(1) 语法分析器使用一张表和一个栈。
FIRST and FOLLOW sets fill the table, and the stack runs the derivation: id plus id parses in eight stack snapshots.
用 FIRST 集和 FOLLOW 集填表,再通过栈完成推导:分析 id + id 需要八个栈快照。
If a table cell holds two productions, one symbol of lookahead cannot choose, so the grammar is not LL(1).
如果表格的一个单元格中有两条产生式,就无法仅凭向前查看的一个符号作出选择,因此该文法不是 LL(1) 文法。
Left recursion is removed by rewriting it as a tail.
可以将左递归改写为尾部形式,从而消除左递归。
Read the table, then the stack, then the tree.
先看表,再看栈,最后看树。
For each step, look at the top of the stack and the next input token, find the cell where they meet, and replace the stack top with that production.
每一步都查看栈顶和下一个输入词法单元,找到它们对应的表格单元格,然后用其中的产生式替换栈顶。
Matching terminals are popped.
匹配的终结符会从栈中弹出。
After eight snapshots the stack is empty and the input is consumed: parsed.
经过八个快照,栈为空,输入也已全部读完:语法分析完成。
A symbol table binds each name to its type, and type rules walk the tree.
符号表将每个名称与其类型关联起来,类型规则沿着树进行检查。
For count equals a1 plus b2 times 3, with all integers, the compiler emits three instructions: t1 equals b2 times 3, t2 equals a1 plus t1, and count equals t2.
对于 count = a1 + b2 × 3,如果所有变量都是整数,编译器会生成三条指令:t1 = b2 × 3、t2 = a1 + t1,以及 count = t2。
If b2 were a string, the type check would reject it, with the position from the lexer.
如果 b2 是字符串,类型检查就会拒绝该表达式,并利用词法分析器提供的位置信息报告错误。
The symbol table binds a1, b2 and count, all as integers.
符号表将 a1、b2 和 count 都关联为整数类型。
Semantic analysis decorates the tree with these types and checks every operation.
语义分析用这些类型标注语法树,并检查每个运算。
Code generation then walks the decorated tree and emits intermediate code: three bindings, three instructions.
随后代码生成遍历标注后的树,生成中间代码:三个绑定,三条指令。
Constant folding turns 2 times 3 into 6, but leaves b2 times 3 alone.
常量折叠将 2 × 3 化为 6,但保留 b2 × 3 不变。
The source code first passes through lexical analysis, which splits it into tokens.
源代码首先经过词法分析,被拆分成词法单元。
Syntax analysis builds an abstract syntax tree.
语法分析构建抽象语法树。
Semantic analysis checks types and declarations.
语义分析检查类型和声明。
Code generation and optimisation then produce the executable.
随后代码生成和优化产生可执行文件。
Now test yourself from memory.
现在凭记忆自测。
Where does the course DFA end on the string a b a, and is it accepted?
课程中的 DFA 处理字符串 a b a 时最终停在哪里,它是否被接受?
How many steps does the leftmost derivation of id plus id times id take?
id + id × id 的最左推导需要多少步?
How does i f x lex?
ifx 会被切分成什么词法单元?
What intermediate code does count equals a1 plus b2 times 3 produce?
count = a1 + b2 × 3 会产生什么中间代码?
Pause, answer, then check.
暂停,回答,然后核对。
Comments
No comments yet. Be the first to ask or share something.