Context-free languages and parsing theory
導入済み
5:58
計算理論とコンパイラ
This course asks what machines can decide, and how text becomes meaning. The first three units are theory: automata and regular languages, context-free…
英語のナレーション・英語と中国語の字幕を切り替え可能
このレッスンの内容
| English | 意味 |
|---|---|
| context-free grammar/ˈkɒntekst friː ˈɡræmə/ | rules that rewrite one nonterminal at a time; they describe most programming-language syntax |
| nonterminals/ˌnɒnˈtɜːmɪnlz/ | grammar symbols that are rewritten by rules, as opposed to the tokens of the input |
| derivation/ˌderɪˈveɪʃn/ | 導出 |
| parse tree/pɑːs triː/ | 構文解析木 |
| ambiguity/ˌæmbɪˈɡjuːɪti/ | a grammar giving some string two or more different parse trees |
| pushdown automaton/ˈpʊʃdaʊn ɔːˈtɒmətn/ | A finite automaton plus a stack, and it recognises exactly the context-free languages. |
Grammars that nest
- A context-free grammar 上下文无关文法 (CFG) rewrites nonterminals 非终结符: $E \to E + T \mid T$, $T \to T \times F \mid F$, $F \to (E) \mid \mathbf{id}$.
- A derivation 推导 applies the rules one by one; a parse tree 语法分析树 records the result.

In the layered grammar, id + id * id has — parse tree(s)
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
Layering enforces precedence: x binds under T, deeper than +.
Ambiguity and precedence
- With the layered grammar,
id + id * idhas exactly one parse tree: × sits under T, deeper than +. The unlayered grammar $E \to E + E \mid E \times E \mid \mathbf{id}$ gives two trees for the same string. Good grammar design removes this ambiguity 二义性. - The leftmost derivation of
id + id * idtakes eight steps, one rule each, always expanding the leftmost nonterminal:
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.
The next limit
- $\{a^n b^n c^n\}$ needs two independent counts, but a CFG can keep only one, so a context-sensitive machine is needed. This is the next level of the Chomsky hierarchy, and it is why type checking happens after parsing.
- A classic trap is to confuse the order of a derivation 推导 (leftmost or rightmost) with the shape of the tree. Different derivation orders can produce the same tree, but an ambiguous grammar can give a string more than one tree.
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 |
何が問題でしたか?この内容はサイトの運営チームだけが確認します。