Context-free languages and parsing theory
引入
5:58
计算理论与编译器⟦number⟧
这门课探讨机器能够判定什么,以及文本如何变成有意义的结构。前三个单元是理论:自动机与正则语言、上下文无关语言,以及可计算性。后三个单元是实践,也就是编译器的各个阶段:词法分析、语法分析,以及语义分析和代码生成。课程中的每条运行轨迹、每张表和每个词法单元的位置都经过测试套件检验。
英文讲解 · 可切换中英文字幕
本课内容
| English | 释义 | 拼音 |
|---|---|---|
| context-free grammar/ˈkɒntekst friː ˈɡræmə/ | 上下文无关文法 | shàng xià wén wú guān wén fǎ |
| nonterminals/ˌnɒnˈtɜːmɪnlz/ | 非终结符 | fēi zhōng jié fú |
| derivation/ˌderɪˈveɪʃn/ | 推导 | tuī dǎo |
| parse tree/pɑːs triː/ | 语法分析树 | yǔ fǎ fēn xī shù |
| ambiguity/ˌæmbɪˈɡjuːɪti/ | 二义性 | èr yì xìng |
| pushdown automaton/ˈpʊʃdaʊn ɔːˈtɒmətn/ | 下推自动机 | xià tuī zì dòng jī |
嵌套语法
- 上下文无关文法重写非终结符:$E \to E + T \mid T$、$T \to T \times F \mid F$、$F \to (E) \mid \mathbf{id}$。推导展开过程;分析树记录该展开过程。


In the layered grammar, id + id * id has — parse tree(s)
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
Layering enforces precedence: x binds under T, deeper than +.
歧义与优先级
- 采用分层文法时,
id + id * id仅有一棵语法树:× 绑定于 T 之下,层级深于 +。非分层 $E \to E + E \mid E \times E \mid \mathbf{id}$ 对同一字符串生成两棵树——歧义通过文法设计消除。 id + id * id的最左推导:$E \Rightarrow E + T \Rightarrow T + T \Rightarrow \mathbf{id} + T \Rightarrow \mathbf{id} + T \times F \Rightarrow \mathbf{id} + \mathbf{id} \times F \Rightarrow \mathbf{id} + \mathbf{id} \times \mathbf{id}$ — 共六步,经套件验证。
With E → E + T | T, T → T * F | F and F → id, the leftmost derivation of id + id * id takes — steps
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
E, E+T, T+T, F+T, id+T, id+TF, id+FF, id+idF, id+idid - eight steps, one rule each.
下一个上限
- $\{a^n b^n c^n\}$ 需要两个独立计数:上下文无关文法只能泵入一个,因此需上下文敏感机器 — 这是乔姆斯基层级中的下一阶,也是类型检查器位于词法分析器之上的原因。
- 混淆推导顺序(最左/最右)与树形结构是经典陷阱:必须由字符串本身决定对应的树。
The language a^n b^n c^n requires —
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
Two independent counts: CFGs pump one, not both.
For an unambiguous grammar, the leftmost and rightmost derivations of a string give different parse trees.
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
They expand the nonterminals in a different order but build the same tree. Only an ambiguous grammar lets one string have two trees.
这是一张新卡片,翻译完成前会显示英文。
The stack machine
- A pushdown automaton 下推自动机 is a finite automaton plus a stack, and it recognises exactly the context-free languages. For $\{a^n b^n\}$: push on each $a$, pop on each $b$, and accept if the stack is empty when the input ends. After
aaabthe stack holds $2$ symbols. - Any CFG in Chomsky normal form can be parsed by CYK in $\Theta(n^3)$ time. Real compilers use restricted grammars, LL or LR, that parse in linear time.
The PDA for a^n b^n reads aaaabb. How many symbols are on the stack?
难度 2/5 · 今日首次完成可获 140 金币及少量额外奖励
Four pushes and two pops leave 2; the input is not accepted yet.
Which machine recognises exactly the context-free languages?
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
A finite control plus one stack matches the nesting that CFGs describe.
这是一张新卡片,翻译完成前会显示英文。
The CYK table
- CYK fills a triangle that records, for each substring, which nonterminals 非终结符 can derive it. A string of length $n$ has $n(n+1)/2$ substrings, so $n = 5$ gives $\mathbf{15}$ cells.
- Each cell tries every split point, so the work is $\Theta(n^3)$. The string is in the language if the start symbol is in the top cell.
How many cells does the CYK table have for a string of length 6?
难度 2/5 · 今日首次完成可获 140 金币及少量额外奖励
$6 \times 7/2 = 21$.
这是一张新卡片,翻译完成前会显示英文。
Worked example: balanced brackets
- $S \to (S)S \mid \varepsilon$ generates every balanced bracket string. For
(())():
- The first bracket and its partner split the string into "inside" and "after", which is why the grammar has no ambiguity 二义性. No regular language can do this, because the nesting depth has no limit.
Which grammar generates exactly the balanced bracket strings?
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
The second allows unbalanced strings; the last two miss strings like (())().
Every regular language is context-free.
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
A DFA converts to a right-linear grammar.
这是一张新卡片,翻译完成前会显示英文。
Marks that slip away
- The unlayered grammar for expressions. $E \to E + E \mid E \times E \mid \mathbf{id}$ gives
id + id * idtwo trees; layering by precedence gives one. - Expecting a CFG to keep two counts. $\{a^n b^n c^n\}$ is not context-free.
- Forgetting the stack. Nested brackets need a pushdown automaton 下推自动机; a finite automaton cannot match them.
这是一张新卡片,翻译完成前会显示英文。
Recap
- A CFG rewrites nonterminals 非终结符; a parse tree records the derivation.
- The layered grammar gives
id + id * idone tree; $E \to E + E \mid E \times E$ gives two. - $\{a^n b^n c^n\}$ is not context-free: it needs two independent counts.
| Language | Example | Machine |
|---|---|---|
| Regular | identifiers, numbers | finite automaton |
| Context-free | $a^n b^n$, nested brackets | finite automaton + a stack |
| Context-sensitive | $a^n b^n c^n$ | linear-bounded automaton |
出了什么问题?只有网站团队能看到这些信息。