4.6.11 · D3Theory of Computation

Worked examples — Equivalence of CFGs and PDAs

4,023 words18 min readBack to topic

This page is a drill hall. The parent note built the two constructions; here we run them on every kind of input you could ever meet: normal cases, empty-string edge cases, degenerate grammars, and an exam twist. Before each solution you'll see a Forecast — pause and guess, then check yourself.

Everything here uses only pieces the parent defined: CFGs, PDAs, the single-state "guess-the-derivation" PDA (Direction 1), and the " triple trick" (Direction 2).


The scenario matrix

Think of "every scenario" as a grid. Each row is a class of situation the construction can face; the worked examples below fill in every cell.

Cell Situation class What makes it tricky Covered by
C1 CFG → PDA, ordinary string, accepts the happy path Example 1
C2 CFG → PDA, string rejected a path must exist for no guess Example 2
C3 CFG → PDA, the empty string degenerate input, length 0 Example 3
C4 CFG with a rule having multiple RHS (real branching) nondeterminism actually matters Example 4
C5 PDA → CFG, the split rule fires stack returns to floor mid-run Example 5
C6 PDA → CFG, the match rule fires one bracket wraps the whole run Example 6
C7 Degenerate PDA: empty language / no accepting path limiting case, Example 7
C8 Real-world word problem (balanced brackets) modelling from scratch Example 8
C9 Exam twist: why determinism breaks here conceptual trap Example 9

Read the matrix once. By the end, you'll have seen a live run for each row.


Warm-up conventions (so no symbol is a surprise)

Before any example, pin down exactly how we draw and finish a run — the figure below is our reference for the whole page.

  • The stack is drawn with its top on the left. So stack aSb means " is on top, then , then at the bottom". In the figure, the leftmost coral box marked TOP is where all the action happens.
  • (epsilon) means the empty string — a string of length zero, "nothing".
  • "Expand" = replace a variable on top by a rule's right-hand side (reads no input). This is the lavender arrow in the figure.
  • "Match" = a terminal on top must equal the next input letter; both get consumed. This is the coral arrow in the figure.
  • Acceptance conventions (both are used on this page, they recognize the same class):
    • In the single-state CFG→PDA (Direction 1) we accept by empty stack: a run accepts when the stack is empty AND all input is read. In the reference figure this is the pale-yellow (butter) rounded box on the right, labelled "ACCEPT when…".
    • In the PDA→CFG direction (Examples 5–7) we also normalize to accept by empty stack. When a PDA instead uses accept states, we first convert it to empty-stack acceptance (always possible), so on this page "accept" always means "empty stack + input exhausted".

Study this reference before moving on — every later step ("Match ", "Expand ") points back to one of these two arrows.

Figure — Equivalence of CFGs and PDAs

Example 1 — C1: the happy path


Example 2 — C2: a rejected string


Example 3 — C3: the empty string (degenerate, length 0)


Example 4 — C4: genuine branching, nondeterminism does real work


Example 5 — C5: PDA → CFG where the SPLIT rule fires

Now the other direction. Recall (from the boxed schemas above) the meaning of : " goes from state to reading , starting and ending at the same stack height, never dipping below it."

The figure below plots stack height against time for two kinds of balanced run. The mint curve on the left touches the floor in the middle — that is precisely the situation the Split rule handles. The coral arch on the right stays above the floor until the very end — that is the Match rule (Example 6). Keep this picture beside you for both examples.

Figure — Equivalence of CFGs and PDAs
Figure — Equivalence of CFGs and PDAs

Example 6 — C6: PDA → CFG where the MATCH rule wraps the whole run

Figure — Equivalence of CFGs and PDAs

Example 7 — C7: the empty language (limiting / degenerate PDA)


Example 8 — C8: real-world word problem (balanced brackets)


Example 9 — C9: the exam twist (why determinism fails here)


Recall

Recall Cover the answers
  • Reject in a nondeterministic PDA means… ::: every path dies, not just one.
  • Does the CFG→PDA accept for ? ::: Yes — one expand empties the stack with no input read.
  • vs ? ::: has zero strings; has one (the empty string).
  • When does the PDA→CFG Split rule fire vs Match? ::: Split when the stack returns to floor mid-run; Match when the first push is undone at the very end.
  • Why doesn't always add to ? ::: it only counts if is reachable from the start variable in a genuine accepting run; otherwise it is a useless production, pruned.
  • Why can't be done deterministically? ::: nothing in the input marks the middle, so the push→pop switch must be guessed.