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. Only the string and the grammar decide the 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 |
เกิดปัญหาอะไรขึ้น? เฉพาะทีมเว็บไซต์เท่านั้นที่เห็นข้อมูลนี้