Examples of Converting PDA to CFG (General Case)
Explore some examples of converting arbitrary PDA to CFG.
We'll cover the following...
Let’s start by looking at a few examples of converting an arbitrary PDA to CFG.
First example
The following PDA accepts the language .
To convert this PDA to a CFG, we first normalize it by adding the transition on state .
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: . 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 , we pop nothing, consume , push , and move in one step to state .” Where we eventually end up after multiple moves is reflected in the third slot of each variable, and by the definition of the notation , those slots must represent the same state—consuming , pushing , and staying in 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 . 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 have vanished from the stack. Part of those aftereffects is that two additional '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 's is where we must continue from to process the second . Therefore, we have four possible grammar rules for this transition:
One of those rules, set off in a different typeface, has an invalid variable: . It is not possible to move from state to state 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 from to yields the following possibilities, one of which we immediately discard because it also reflects an invalid path in the PDA:
Finally, the transition on ...