Context-free languages and parsing theory
Đã giới thiệu
5:58
Lý thuyết tính toán và trình biên dịch
This course asks what machines can decide, and how text becomes meaning. The first three units are theory: automata and regular languages, context-free…
Thuyết minh tiếng Anh · Có thể chuyển đổi phụ đề tiếng Anh và tiếng Trung
Trong bài học này
| English | Ý nghĩa |
|---|---|
| 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/ | phép suy diễn |
| parse tree/pɑːs triː/ | cây phân tích |
| 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)
Độ khó 1/5 · 100 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
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
Độ khó 1/5 · 100 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
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 —
Độ khó 1/5 · 100 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
Two independent counts: CFGs pump one, not both.
For an unambiguous grammar, the leftmost and rightmost derivations of a string give different parse trees.
Độ khó 1/5 · 100 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
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?
Độ khó 2/5 · 140 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
Four pushes and two pops leave 2; the input is not accepted yet.
Which machine recognises exactly the context-free languages?
Độ khó 1/5 · 100 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
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?
Độ khó 2/5 · 140 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
$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?
Độ khó 1/5 · 100 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
The second allows unbalanced strings; the last two miss strings like (())().
Every regular language is context-free.
Độ khó 1/5 · 100 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
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 |
Đã xảy ra vấn đề gì? Chỉ đội ngũ trang web nhìn thấy thông tin này.