Theory of Computation & Computer Graphics β Introduction to Context Free Language, NEC licence examination syllabus (Nepal Engineering Council).
Equivalence of CFL and PDA
Grammars generate, machines recognise β and for context-free languages the two are the same thing.
π Where this lives: This equivalence is what makes parser generators possible. You write a grammar; the tool builds a machine. Every time yacc, bison or ANTLR reads a .y or .g4 file and emits parsing code, it is performing the grammar-to-automaton construction of this topic β mechanically, for grammars with hundreds of productions. Without the theorem there would be no reason to believe such a translation always exists. Search "parser generator grammar to automaton construction yacc".
The theorem and the grammar β PDA direction
THE THEOREM:
A LANGUAGE IS GENERATED BY A CONTEXT-FREE GRAMMAR IF AND ONLY
IF IT IS ACCEPTED BY A PUSHDOWN AUTOMATON.
Both directions are CONSTRUCTIVE, so each is an algorithm.
Together with the earlier results this completes the picture:
CONTEXT-FREE GRAMMAR β‘ PUSHDOWN AUTOMATON
(generator) (recogniser)
exactly as REGULAR EXPRESSION β‘ FINITE AUTOMATON one level
down.
ββ DIRECTION 1: CFG β PDA ββββββββββββββββββββββββββββββββββ
THE STANDARD CONSTRUCTION BUILDS A ONE-STATE PDA THAT
ACCEPTS BY EMPTY STACK.
THE IDEA β and it is worth stating before the mechanics:
THE STACK HOLDS THE PART OF THE SENTENTIAL FORM NOT YET
MATCHED AGAINST THE INPUT.
The PDA simulates a LEFTMOST DERIVATION. At every moment the
stack contains what the derivation still has to produce, top
symbol first.
THE CONSTRUCTION. Given G = (V, T, P, S), build
M = ({q}, T, V βͺ T, Ξ΄, q, S, β ) with:
1. FOR EACH PRODUCTION A β Ξ±:
Ξ΄(q, Ξ΅, A) contains (q, Ξ±)
β replace the variable on top of the stack with the
right-hand side, consuming no input. THIS IS AN EXPANSION
STEP, and it is where the non-determinism lives: several
productions for A give several choices.
2. FOR EACH TERMINAL a β T:
Ξ΄(q, a, a) = (q, Ξ΅)
β if the input symbol matches the terminal on top of the
stack, consume both. THIS IS A MATCHING STEP.
START with S on the stack; ACCEPT by empty stack.
THE CORRESPONDENCE IS EXACT: THE STACK CONTENTS AT ANY POINT
ARE PRECISELY THE UNMATCHED SUFFIX OF THE CURRENT SENTENTIAL
FORM IN A LEFTMOST DERIVATION.
ββ A WORKED SIMULATION βββββββββββββββββββββββββββββββββββββ
Grammar: S β a S b | Ξ΅ (generating aβΏbβΏ)
Input: aabb
STACK REMAINING ACTION
ββββββββββββββββββββββββββββββββββββββββββββββ
S aabb expand S β aSb
a S b aabb match a
S b abb expand S β aSb
a S b b abb match a
S b b bb expand S β Ξ΅
b b bb match b
b b match b
(empty) Ξ΅ ACCEPT
ββββββββββββββββββββββββββββββββββββββββββββββ
READ THE EXPANSION STEPS IN ORDER: S β aSb, S β aSb, S β Ξ΅.
THAT IS EXACTLY THE LEFTMOST DERIVATION
S β aSb β aaSbb β aabb.
THE PDA HAS NOT MERELY ACCEPTED THE STRING β IT HAS
RECONSTRUCTED THE DERIVATION, which is why a parser can
output a parse tree rather than a yes/no answer.
NOTE THE NON-DETERMINISM: at the third step the machine chose
S β Ξ΅ rather than S β aSb. A real parser must decide which,
and that decision problem is precisely what LL and LR parsing
tables solve.
ββ THE GREIBACH NORMAL FORM VARIANT ββββββββββββββββββββββββ
If the grammar is first converted to GNF (A β aΞ±), the
construction becomes even tighter:
Ξ΄(q, a, A) contains (q, Ξ±) for each production A β aΞ±
EVERY MOVE NOW CONSUMES EXACTLY ONE INPUT SYMBOL β there are
no Ξ΅-moves at all, so the machine makes exactly |w| moves on
input w.
THIS IS WHY GREIBACH NORMAL FORM WAS INVENTED, as the
earlier topic said: it makes this proof immediate.
The stack contents at every moment are exactly the unmatched suffix of the current sentential form in a leftmost derivation. That is why the simulation's expansion steps, read in order, reproduce the derivation itself β and why a parser can emit a parse tree rather than merely a yes or no.
π Go further: Notice which direction of the equivalence is used in practice. Grammar β machine runs inside every parser generator, millions of times a day. Machine β grammar has essentially no practical use, because the construction produces exponentially many productions built from state triples. This asymmetry is common in computability theory: a proof of equivalence establishes that two formalisms have the same power, but the two constructions can differ enormously in cost, and only one of them ever becomes an engineering tool. Search "PDA to CFG construction triple construction complexity".
π‘ Exam angle: state the theorem and note that CFG β‘ non-deterministic PDA, not DPDA. The likely question is the CFG β PDA construction: give the two rule schemas (expansion Ξ΄(q,Ξ΅,A) β (q,Ξ±) and matching Ξ΄(q,a,a) = (q,Ξ΅)), then trace a string showing stack and remaining input, and point out that the expansions reproduce the leftmost derivation. For the reverse direction, explain the [q X p] triple idea without attempting the full construction. Finish with the decidable versus undecidable lists β this contrast with the regular level is very commonly asked.
Syllabus points
CFG β PDA construction
Create a free account to tick topics off, take notes as you read, watch the video lessons and get a day-by-day study plan built around your exam date.
Related topics in Introduction to Context Free Language