Recall warm-up
- Standard CFL that is not a DCFL?
Stuck? Peek at 3.3.3 Designing PDAs; DPDA vs NPDA; DCFL ⊂ CFL
- Recipe: stack stores what is unbounded; states store the phase; Z₀ detects empty; accept via ε, Z₀.
- DPDA: ≤ 1 move per (q, a|ε, X), and no ε-move competing with input moves.
- Regular ⊊ DCFL ⊊ CFL. wwᴿ, aⁿbⁿ ∪ aⁿb²ⁿ, aⁱbʲcᵏ (i=j or j=k): CFL not DCFL.
- DCFL ⇒ unambiguous grammar; not conversely.
- Derivation steps for length n in GNF?
Stuck? Peek at 3.2.2 Chomsky and Greibach normal forms
- CNF: A → BC | a (+ S → ε). Length n ⇒ 2n − 1 steps; n − 1 binary nodes. Height h ⇒ |w| ≤ 2^(h−1).
- Conversion: simplify, replace terminals in long bodies, split long bodies.
- GNF: A → aα. Length n ⇒ n steps. No left recursion.
- How does a DFA become a right-linear grammar?
Stuck? Peek at 2.3.2 Right-linear and left-linear grammars
- Right-linear: A → wB | w. Left-linear: A → Bw | w. Regular grammar = one of the two (not mixed).
- DFA → grammar: δ(A,a)=B ⇒ A → aB; A accepting ⇒ A → ε.
- Grammar → NFA: A → aB is an arrow; A → a goes to a new accepting state.
- Mixed linear can give aⁿbⁿ. Non-regular grammar does not imply non-regular language.
The Hook
For regular languages you saw machines turn into grammars and back (2.3.2). The same happens one level up: CFGs and PDAs describe exactly the same languages. GATE asks this at the concept level — which direction is easy, what the PDA built from a grammar looks like, and what follows.
Predict First
A leftmost derivation always rewrites the leftmost variable. If you kept the unfinished part of a sentential form on a stack, what would be on top?
Core Concept
Prediction check
The leftmost symbol. If it is a terminal, it must match the next input symbol; if it is a variable, rewrite it by some rule. That is the whole CFG → PDA construction.
CFG → PDA
One working state q (plus start and accept states):
- Start: push S on Z₀.
- For each rule A → α: move (q, ε, A) → (q, α) — expand.
- For each terminal a: move (q, a, a) → (q, ε) — match.
- Accept when Z₀ is back on top and the input is finished.
It simulates leftmost derivations; nondeterminism picks the rule.
States: p, q, f
Transitions: p>q: ε,Z/SZ; q>q: ε,S/aSb | ε,S/ε | a,a/ε | b,b/ε; q>f: ε,Z/Z
PDA → CFG (idea)
Make variables [p X q] meaning “starting in p with X on top, the PDA can read some string and end in q having popped X (net)”. Rules come from moves: a move that pops X goes straight into a terminal rule; a move that replaces X by Y₁…Yₖ splits into k nested pops through intermediate states. The construction is mechanical and gives a CFG for N(P).
| variable | meaning |
|---|---|
| [p X q] | from p with X on top, read some w, end in q with X gone |
| [q₀ Z₀ q] for every q | start rules: S → [q₀ Z₀ q] |
| δ(p,a,X) ∋ (r, ε) | [p X r] → a |
| δ(p,a,X) ∋ (r, Y₁Y₂) | [p X s] → a [r Y₁ t][t Y₂ s] for all t, s |
Consequences
- CFL = languages of CFGs = languages of NPDAs (final state) = languages of NPDAs (empty stack).
- The PDA from a grammar has one main state: states add no power to PDAs beyond one (the stack does all the work).
- Closure proofs can use whichever side is easier: grammars for union/concatenation/star, PDAs for intersection with regular languages.
Putting it together
- A PDA can simulate leftmost derivations of any CFG with one working state.
- A PDA can be turned into a CFG with [p X q] variables.
- CFGs and NPDAs describe exactly the CFLs.
Visual
CFG → PDA for E → E+T | T, T → id input id+id stack (top first) move E Z expand E → E+T E + T Z expand E → T T + T Z expand T → id id + T Z match id + T Z match + T Z expand T → id id Z match id Z accept
Worked Examples
In the CFG → PDA construction, what move handles the terminal b?
Show the full solution
δ(q, b, b) = {(q, ε)}: if b is on top and the input is b, pop it.
How many states does the basic CFG → PDA construction need?
Show the full solution
One working state (often plus a start state that pushes S and an accepting state). The stack carries everything.
Which are true? (i) Every PDA with many states has an equivalent PDA with one state (empty-stack acceptance). (ii) The PDA built from a CFG is deterministic. (iii) CFL = languages accepted by NPDAs. (iv) The CFG → PDA construction simulates rightmost derivations.
Show the full solution
(i) true (PDA → CFG → one-state PDA). (ii) false in general: choosing the rule is a guess. (iii) true. (iv) false: leftmost. Answer: (i) and (iii).
GATE Traps
- The grammar-built PDA is usually nondeterministic.
- It simulates leftmost derivations, not rightmost.
- Equivalence is with NPDAs; DPDAs only cover the DCFLs.
Memory Hook
The PDA is a to-do list: the top item is the next thing to produce — a word to say (match) or a task to break down (expand).
Connect the Dots
Grammars and stack machines are two views of one family. Now: what the family is closed under (and the famous failures), which questions have algorithms, and how to prove a language is not context-free.
Self-Test
- MCQ
In the CFG → PDA construction, a variable A on top of the stack is:
- matched against the input
- replaced by the body of some rule for A
- popped and ignored
- pushed twice
- MSQ
Which describe exactly the CFLs?
- CFGs
- NPDAs by final state
- NPDAs by empty stack
- DPDAs by final state
- MCQ
The CFG → PDA construction simulates:
- rightmost derivations
- leftmost derivations
- any derivation order
- CYK
Rule Card
- CFG → PDA: push S; expand (q, ε, A) → (q, α); match (q, a, a) → (q, ε); accept on Z₀. Simulates leftmost derivations; nondeterministic.
- PDA → CFG: variables [p X q] (net pop of X from p to q).
- CFG = NPDA (final state) = NPDA (empty stack). One working state suffices.
Concept Ledger
- CFG → PDA (expand / match)
- PDA → CFG (triple variables [pXq])
- CFG ≡ NPDA
Answer Vault
Reveal answers when you’re ready
Warm-up answers
- wwᴿ.
- n.
- Each arrow δ(A,a)=B becomes A → aB; accepting states get A → ε.
Self-test answers
- B — Expand step.
- A: Only terminals are matched.
- C: It must be expanded.
- D: No.
- ABC — DPDAs give DCFLs only.
- D: Misses wwᴿ.
- B — The top of the stack is the leftmost unfinished symbol.
- A: No.
- C: A fixed order: leftmost.
- D: Different algorithm.
Flashcards
- CFG → PDA: two kinds of moves?
Reveal answer
Expand a variable by a rule (ε-move); match a terminal with the input. - How many working states does the grammar-built PDA need?
Reveal answer
One. - Which derivation does it simulate?
Reveal answer
Leftmost.