...

/

Examples of Converting PDA to CFG (General Case)

Examples of Converting PDA to CFG (General Case)

Explore some examples of converting arbitrary PDA to CFG.

Let’s start by looking at a few examples of converting an arbitrary PDA to CFG.

First example

The following PDA accepts the language Ln={anbn    n0}L_n = \{a^nb^n \;|\; n\geq 0\}.

Press + to interact
PDA accepting Ln
PDA accepting Ln

To convert this PDA to a CFG, we first normalize it by adding the transition a,XXXa,X\to XX on state q0q_0.

Press + to interact

There are two accepting states, so the first boilerplate rules are:

As usual, we have nullity rules for each state:

With multiple states, we must entertain all possible combinations of states as we convert each transition to grammar rules. This is where things get tedious. Consider the first transition: a,λXa,\lambda\to X. We begin to extract a grammar rule for this transition as follows:

We have not yet populated the third slot (marked by a dash) of each grammar variable. There are examples with only one state to consider, but now there are two states. Recall that the interpretation of the incomplete rule above is, “Starting in state q0q_0, we pop nothing, consume aa, push XX, and move in one step to state q0q_0.” Where we eventually end up after multiple moves is reflected in the third slot of each variable, and by the definition of the notation pVq\langle pVq\rangle, those slots must represent the same state—consuming aa, pushing XX, and staying in q0q_0 is just the first step in that journey. Since we must consider all possibilities of target states, we form two rules, one for each state in the machine:

Remember: The state in the third slot in the variable on the left of each line must be identical to the third slot of the last variable on the right.

Now consider the rule a,XXXa,X\to XX. This time there will be two variables on the right-hand side of the rule. We begin with the following incomplete rule pattern:

The identical patterns above indicate that the states in those positions must be the same. The single-dash pattern was explained above—it represents where we end up when the aftereffects of popping XX have vanished from the stack. Part of those aftereffects is that two additional XX's are pushed and must be subsequently processed. The box patterns must be identical because the state we end up in after processing the first of the two additional XX's is where we must continue from to process the second XX. Therefore, we have four possible grammar rules for this transition:

One of those rules, set off in a different typeface, has an invalid variable: q1Xq0\langle \mathsf{q_1Xq_0}\rangle. It is not possible to move from state q1q_1 to state q0q_0 in this PDA, so we remove this rule from further consideration, leaving only three possible rules for the associated transition.

In a similar fashion, the transition b,Xλb,X\to\lambda from q0q_0 to q1q_1 yields the following possibilities, one of which we immediately discard because it also reflects an invalid path in the PDA:

Finally, the transition b,Xλb,X\to\lambda on q1q_1 ...

Access this course and 1400+ top-rated courses and projects.