Context-free languages and parsing theory
도입됨이 수업의 내용
| 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 |
어떤 문제가 있었나요? 사이트 팀만 이 내용을 볼 수 있습니다.