Hello! Welcome back to your study of the Theory of Computation.
In our last lesson, we developed the conceptual foundation for converting a Pushdown Automaton (PDA) into an equivalent Context-Free Grammar (CFG). We saw how the nested, recursive nature of a PDA's stack operations could be modeled by three types of grammar rules, establishing that L(PDA) ⊆ L(CFG).
Today, we transition from the "why" to the "how". Our learning outcome is to apply the formal construction to generate CFG production rules from the transitions of a given PDA. This is a systematic, algorithmic process. Think of it as compiling a machine's operational description (the PDA's transitions) into a declarative specification (the CFG's rules). Your background in designing and building complex software systems will be an asset here, as this process requires careful, step-by-step translation of formal rules.
1. The Formal Construction Algorithm
While our previous lesson's three rule types (A_pp → ε, A_pq → A_pr A_rq, A_pq → a A_{rs} b) are conceptually sound, there is a more direct and mechanical algorithm that is often used in practice. This algorithm uses a different, but equivalent, style of variable.
CFG Variable Definition
The variables (non-terminals) in our new grammar will be of the form [p, A, q], where:
pandqare states of the PDA.Ais a stack symbol.
The intuitive meaning of the variable [p, A, q] is:
The language of all input strings
wthat take the PDA from statepto stateq, where the net result of the computation is the consumption of the single symbolAfrom the top of the stack.
The stack can grow and shrink arbitrarily during the computation for w, but it must end up exactly one symbol shorter than it started.
The Algorithm Steps
Given a PDA P = (Q, Σ, Γ, δ, q₀, Z₀), we construct an equivalent CFG G = (V, T, P, S) as follows:
-
Terminals (T): The terminals of the grammar are the input symbols of the PDA:
T = Σ. -
Variables (V): The variables include a new start symbol
Sand one variable for every possible triple:V = {S} ∪ { [p, A, q] | p, q ∈ Q, A ∈ Γ }. -
Start Productions: We add productions for the start symbol
Sof the form:
for every stateqinQ. This rule captures the overall goal: a string is in the language if it takes the PDA from the start stateq₀to any stateq, with the net effect of emptying the stack (i.e., consuming the initial stack symbolZ₀). -
Transition Productions: For each transition in the PDA, we generate a set of production rules. Consider a generic transition:
δ(p, a, A) = (r, B₁B₂...B_k). This means "in statep, reading inputawithAon the stack, go to staterand replaceAwith the stringB₁B₂...B_k".-
Case 1: Pop (
k = 0)
If the transition is a pop,δ(p, a, A) = (r, ε), the generated rule is simple:
This means: "To go fromptorby consumingA, you can just read inputa." -
Case 2: Push/Replace (
k > 0)
If the transition pushes symbolsB₁B₂...B_k, we need to account for the sub-computations that will eventually pop each of these new symbols. The generated rule is:
This rule is created for every possible combination of intermediate statesq₁, q₂, ..., q_kfromQ. This rule is the heart of the construction. It says:"To go from
ptoq_kconsumingA, you can:- Read input
a. - This puts you in state
rwithB₁...B_kon the stack. - Now, find a path from
rto some stateq₁that consumesB₁. - Then, find a path from
q₁toq₂that consumesB₂. - ...and so on, until you find a path from
q_{k-1}toq_kthat consumesB_k."
- Read input
-
This decomposition is what gives the construction its power. The large number of rules generated accounts for the non-determinism of both the PDA and the resulting CFG.

2. A Concrete Example: Step-by-Step Conversion
Let's see this algorithm in action. We'll use a PDA that recognizes the language {a^m b^m | m ≥ 1}.
The following video walks through an almost identical problem. The presenter uses a slightly different notation for variables (e.g., q₀Aq₁ instead of [q₀, A, q₁]), but the logic is precisely the same as the algorithm we just defined.
PDA to CFG Conversion || TOC || FLAT || Theory of Computation
Watch this video to see how the formal rules are applied to a specific PDA. Pay close attention to how the number of generated productions depends on the number of symbols pushed by a PDA transition.
Watch the video from the beginning to 17:11. The sections cover: (00:00 - 04:44): Defining the CFG variables and the start symbol productions. (04:44 - 11:34): Generating productions for transitions that push two symbols. (11:34 - 14:48): Generating productions for transitions that push one symbol. (14:48 - 17:11): Generating productions for transitions that pop a symbol (pushing ε).
Let's recap the key points from the video, mapping them to our algorithm:
-
Variables and Start Symbol (00:00 - 04:44): The video correctly identifies that the CFG variables are formed from combinations of
(state, stack_symbol, state). The start productionsS → [q₀, Z₀, q₀]andS → [q₀, Z₀, q₁]are created, setting up the goal of consuming the initial stack symbolZ₀and ending in any state. -
Two-Symbol Push (04:44 - 11:34): The PDA transition
δ(q₀, a, A) = (q₀, AA)pushes two symbols. Our algorithm states this creates rules of the form[q₀, A, q₂] → a [q₀, A, q₁][q₁, A, q₂]. Since there are two states (q₀,q₁), the intermediate stateq₁can be eitherq₀orq₁, and the final stateq₂can also be eitherq₀orq₁. This gives2 * 2 = 4total productions for this single PDA transition, exactly as shown in the video. -
One-Symbol Push (11:34 - 14:48): The PDA transition
δ(q₀, b, A) = (q₁, A)replaces one symbol with one other. This is ak=1push. The rule form is[q₀, A, q₁] → b [q₁, A, q₁]. Since there is only one final state to choose (q₁), and no intermediate states, this generates2rules (one for each possibleq₁in{q₀, q₁}), just as shown. -
Pop / Epsilon-Push (14:48 - 17:11): The PDA transition
δ(q₁, a, A) = (q₁, ε)is a pop. This is ourk=0case. The rule is[q₁, A, q₁] → a. This is a direct mapping and generates only one production.
Test your understanding!
Suppose you have a PDA with states {p, q, r} and a transition δ(p, 0, X) = (q, Y). This is a "replace" operation, which is equivalent to a pop followed by a push, but our algorithm handles it as a k=1 push.
According to the formal construction, what production rule(s) would this generate?
Show answer
The general form for a k=1 transition δ(p, a, A) → (r, B₁) is [p, A, q₁] → a [r, B₁, q₁] for all states q₁.
In this case, p=p, a=0, A=X, r=q, B₁=Y. The possible end states q₁ are {p, q, r}. Therefore, this single PDA transition generates three production rules:
[p, X, p] → 0 [q, Y, p][p, X, q] → 0 [q, Y, q][p, X, r] → 0 [q, Y, r]
Alternative Formalisms
It's worth noting that the algorithm we've studied is not the only one. The conceptual method from our previous lesson (using variables A_pq) is also a valid and powerful construction. The resources below lay out this alternative. You don't need to master it, but it's good to be aware that different, equivalent paths exist to prove the same result.
Pushdown Automata (PDA) to Context-Free Grammars (CFG)
This document from the University of Texas at El Paso provides a concise summary of the alternative construction method, which we discussed conceptually in the last lesson.
Skim through the sections "Variables of the resulting grammar" and "Rules of the resulting grammar." Notice how the variables are defined as A_pq and the rules are broken into four distinct families. This is the same method explained in the Easy Theory video from the last lesson.
This alternative method often feels more intuitive but can be harder to apply mechanically. The algorithm we focused on today is less intuitive but is a straightforward, deterministic procedure for generating the grammar. Both arrive at an equivalent CFG.
Conclusion
You have now mastered the formal algorithm for converting any PDA into an equivalent CFG. This is a significant result that completes the proof of equivalence between these two models of computation.
Key Takeaways:
- The conversion is an algorithmic process that translates each PDA transition into one or more CFG production rules.
- The variables of the CFG,
[p, A, q], elegantly capture the idea of a "sub-computation" that consumes a specific stack symbolAwhile transitioning between statespandq. - The number of rules generated by a single PDA transition depends on the number of symbols it pushes onto the stack and the total number of states in the PDA, reflecting the non-determinism involved.
- While the resulting grammar can be very large and contain many useless or unreachable productions, it is guaranteed to be equivalent to the source PDA. Simplification algorithms can be applied afterward.
Preview of the Next Lesson:
Now that we've established that PDAs and CFGs are equivalent, we'll shift our focus to exploring the limits of this class of languages. We will learn about Deterministic Pushdown Automata (DPDA) and see that, unlike with finite automata, adding nondeterminism to PDAs gives them more power. We will identify specific languages that can be recognized by a nondeterministic PDA but not by any deterministic one.