...

/

From PDA to CFG (General Case)

From PDA to CFG (General Case)

Learn about the general case of conversion of a PDA to a CFG.

Converting an arbitrary PDA to CFG

To convert an arbitrary PDA to a CFG, we somehow need to relate the states and transitions of the PDA, including stack operations, to variables and rules in a CFG. This is a tall order but a doable one. To understand the algorithm, we need to take a deeper look at stack behavior in PDAs. We start with the simple, one-state PDA for Leq={na(w)=nb(w)w(a+b)}L_{eq} = \{n_a(w)\:=\:n_{b}(w)\:|\:w \in (a+b)^* \} found in the figure below.

Press + to interact
Accepts the language Leq
Accepts the language Leq

While referring to this figure, let’s study the following trace of aaababbbbbaaaaababbbbbaa.

Step Input Stack
0 aaababbbbbaaaaababbbbbaa λ1\lambda_1
1 aababbbbbaaaababbbbbaa X1X_1
2 ababbbbbaaababbbbbaa X2X1X_2X_1
3 babbbbbaababbbbbaa X3X2X1X_3X_2X_1
4 abbbbbaaabbbbbaa X2X1X_2X_1
5 bbbbbaabbbbbaa X4X2X1X_4X_2X_1
6 bbbbaabbbbaa X2X1X_2X_1
7 bbbaabbbaa X1X_1
8 bbaabbaa λ2\lambda_2
9 baabaa Y1Y_1
10 aaaa Y2Y1Y_2Y_1
11 aa Y1Y_1
12 λ\lambda λ\lambda

We have numbered the stack variables (and the lambdas) with subscripts according to the order in which they appear on the stack so we can inspect their stack lifetimes. As always, the stack begins and ends empty, and in between, it grows and shrinks in size repeatedly. A stack variable’s lifetime is the number of steps from when it is first pushed until it is removed from the stack. The lifetimes of the stack variables in the above table appear in the following table.

Stack Variable Lifetime (In Steps) Step Range
X1X_1 7 1–7
X2X_2 5 2–6
X3X_3 1 3–3
X4X_4 1 5–5
Y1Y_1 3 9–11
Y2Y_2 1 10–10

From the time the first variable, X1X_1, appears on the stack until it is removed, three other XX’s (X2X_2 ...