4.6.11 · D4Theory of Computation

Exercises — Equivalence of CFGs and PDAs

2,922 words13 min readBack to topic

Level 1 — Recognition

Exercise 1.1 (L1)

State the equivalence theorem precisely, and say which acceptance mode(s) of a PDA are allowed in it.

Recall Solution

Theorem. For any language : is context-free for some pushdown automaton . Acceptance modes. Either acceptance by final state or acceptance by empty stack may be used, because the two modes recognize exactly the same class of languages. We freely convert between them and pick whichever is convenient for a construction.

Exercise 1.2 (L1)

In the CFG → PDA construction, how many states does the machine need, and why that number?

Recall Solution

Exactly one state, call it . Why. A grammar has no notion of a "control state" — every bit of live information sits inside the current sentential form (the mix of terminals and variables produced so far). We store that sentential form on the stack. Since nothing else needs remembering, one state is enough. The stack does all the work.

Exercise 1.3 (L1)

For the single-state CFG → PDA, write the two transition schemas and label each as "apply rule" or "match input".

Recall Solution

Expansion (apply rule). For every grammar rule : Read no input, pop the variable , push its right-hand side . Consumes no input because applying a rule is a paper operation, not a reading operation. Matching (match input). For every terminal : Read the input symbol , pop the matching terminal , push nothing. This "cashes in" a terminal that the derivation promised.


Level 2 — Application

Exercise 2.1 (L2)

Grammar (this generates ). Using the single-state PDA, trace the run on the input . Show unread input and stack at every step, and mark whether the run accepts.

Recall Solution

Stack shown top-left; accept = empty stack with all input read.

Step Action (why) Unread input Stack
0 start
1 expand (push RHS)
2 match (top = next input)
3 expand (pop )
4 match (empty) → accept

The accepting path exists, so . Nondeterminism explored the guess "use base case now" at step 3.

Exercise 2.2 (L2)

Same and PDA, input (note: unbalanced). Argue that no path accepts.

Recall Solution

Every expansion of either produces (one before and one after the recursive ) or produces . So any string derived has the form : the count of 's equals the count of 's and all 's precede all 's. has two 's and one — the counts differ. On the stack side: to match the two leading 's we must expand twice, leaving at least or over the tail, i.e. at least two 's owed, but only one remains in the input. Some on the stack can never be matched, so the stack cannot empty. No accepting path exists ⇒ . ✓ (consistent with ).

Exercise 2.3 (L2)

For (balanced parentheses), write out all transitions of the single-state PDA. Then trace input ().

Recall Solution

Terminals are ( and ); treat them as two stack terminals. Variable: . Expansion transitions (read ): Match transitions (read the terminal): Trace on ():

Step Action Unread input Stack
0 start ()
1 expand () ()
2 match ( ) )
3 expand ) )
4 match ) empty → accept

Level 3 — Analysis

Exercise 3.1 (L3)

State the loop invariant of the CFG → PDA construction and use it to explain, in one sentence each, why acceptance implies and implies acceptance.

Recall Solution

Invariant. At every moment of a run, Accept ⇒ : if the run ends with empty stack and all of read, the invariant says is a sentential form containing no variables — i.e. a fully derived terminal string — so , hence . ⇒ accept: take a leftmost derivation of ; at each step the PDA can mirror it (expansion for a rule application, match for the exposed terminal), and nondeterminism guarantees this exact sequence of guesses is one available path, ending with empty stack.

Exercise 3.2 (L3)

In the PDA → CFG construction, the variable means " goes from state to state reading some , with net-zero balanced stack usage." A student claims just means " is reachable from ." Give a concrete reason this is wrong.

Recall Solution

"Reachable" allows the machine to pop symbols that were on the stack before — dipping below the starting stack height. That path leaves the stack lower than it started, so it does not correspond to a balanced bracket structure and is not what a single grammar variable can encode. Concretely: suppose from the only move is "pop (which was already there) → ". Then is reachable from , but the run consumed a symbol it did not itself push, so must not hold. If we treated as mere reachability, the grammar would generate strings the PDA never accepts. The balanced (never-dip-below-start) condition is exactly what makes the run mirror the nesting of grammar rules.

Exercise 3.3 (L3)

Prove that the two PDA → CFG rule schemas — split and match — are exhaustive: every balanced run is covered by exactly one case. Use the figure.

Figure — Equivalence of CFGs and PDAs
Recall Solution

Take any balanced run from to on non-empty input (the empty case is handled by ). Look at the very first symbol that this run pushes onto the stack (red in the figure). Because the run is balanced (ends at the same height it started, never dipping below), must be popped again at some point. Two — and only two — possibilities:

  1. is popped only at the very last step of the run. Then the push at the start and the pop at the end are the outer bracket; everything strictly between them ran above without touching it — a balanced sub-run from the post-push state to the pre-pop state . This is the match rule .
  2. is popped strictly before the end, at some intermediate state . At that moment the stack is back to its starting height. So the whole run factors into two consecutive balanced runs: and . This is the split rule . There is no third option: the height either returns to start for the first time at the end (case 1) or earlier (case 2). Hence the schemas are exhaustive and non-overlapping.

Level 4 — Synthesis

Exercise 4.1 (L4)

Build a single-state PDA (by empty stack) for by first writing a CFG, then applying the CFG → PDA construction. Trace .

Recall Solution

CFG. Each must contribute two 's at the end: . PDA (single state , start symbol , accept by empty stack). Expansion: and . Match: , . Trace on :

Step Action Unread input Stack
0 start
1 expand
2 match
3 expand
4 match
5 match empty → accept

. ✓

Exercise 4.2 (L4)

Take the two-state PDA for from the parent (push per in state ; on the first pop and move to ; pop per further in ). Apply the PDA → CFG match schema to recover a grammar, and confirm it is essentially .

Recall Solution

On reading in the machine pushes and stays in . On reading the machine pops — from moving to , or from staying in . Consider the balanced run recognized by . Match schema where "push on , state " gives , and "pop on , state " gives (the inner balanced run must end just before that final pop, in state ). So: Base case (going nowhere, empty string, no net stack change). But an empty outer run also collapses : the smallest balanced run reading that starts pushing then immediately... — concretely the derivation bottoms out with via the path. Renaming : This is exactly the original grammar — the two constructions are inverse in spirit. ✓


Level 5 — Mastery

Exercise 5.1 (L5)

Explain precisely why a two-stack PDA is strictly more powerful than a one-stack PDA, and connect this to why the CFG↔PDA equivalence is special to exactly one stack.

Recall Solution

A two-stack PDA can simulate a Turing machine: let the first stack hold the tape contents to the left of the head (top = cell nearest the head) and the second stack hold the tape to the right. Moving the head right = pop from the right stack, push onto the left stack; moving left = the reverse. Reading/writing the current cell = inspecting/replacing the top of one stack. Two stacks thus give unrestricted read-write access to an unbounded tape — full Turing power. Hence two-stack machines recognize all recursively enumerable languages, far beyond context-free. The CFG↔PDA theorem hinges on a single stack because one stack encodes exactly one level of bracketed nesting — matching the single-layer recursion of a CFG's derivations. Add a second independent stack and you get two coordinated counters (or a tape), escaping the family. So "context-free = one stack" is a knife-edge, not a general "more memory, same class" trend.

Exercise 5.2 (L5)

Let and (both context-free). Is context-free? Use it to conclude a closure fact.

Recall Solution

. By the pumping lemma for CFLs, is not context-free: any pump-decomposition with can touch at most two of the three letter-blocks, so pumping unbalances the third. Therefore is not context-free even though are. Closure conclusion: context-free languages are not closed under intersection. (See Closure Properties of CFLs.) Contrast: regular languages are, and a single PDA cannot simultaneously enforce two independent balance conditions — again the "one stack" limit at work.

Exercise 5.3 (L5)

A student says: "Since every CFG has an equivalent PDA, and PDAs can be made deterministic, every context-free language is deterministic (a DCFL)." Diagnose the error precisely.

Recall Solution

The equivalence theorem produces a nondeterministic PDA, and its "guess the derivation" step is essential: the machine must guess which rule to apply / where to split. Nondeterministic PDAs recognize all context-free languages, but deterministic PDAs recognize only a proper subset, the DCFLs. Counterexample: (even-length palindromes) is context-free but not a DCFL — a deterministic machine cannot know where the string's midpoint is without guessing. So you cannot always determinize a PDA while preserving its language. The claim conflates the two machine classes. ✗


Final Recall

Recall Cover the answers

The stack in CFG→PDA holds… ::: the unmatched suffix of the current leftmost sentential form. CFG→PDA consumes input only when… ::: matching a terminal on top of the stack (expansions read ). means… ::: goes on with net-zero, never-dip-below-start (balanced) stack use. The two PDA→CFG schemas are exhaustive because… ::: the first pushed symbol is popped either at the very end (match) or earlier (split). Two stacks give… ::: full Turing power (tape = left stack + right stack), leaving the CFL class. Determinization fails for PDAs because… ::: DCFL is a proper subset of CFL; the subset construction can't track unbounded stacks.