Pushdown Automata (PDA)
The PDA is an automaton equivalent to the CFG in language-defining power. Only the nondeterministic PDA defines all the CFL's. But only the deterministic version models the parser (which is implementable).
Definition of PDA
Think of an
Based on NFA, the PDA can have a choice of next moves. In each choice, the PDA can do the following (can be combined):
- Change the state.
- Pop the top of the stack.
- Push a symbol or a sequence of symbols onto the stack.
More Formally, a PDA is a 7-tuple
is a finite set of states. is the input alphabet. is the stack alphabet. is the transition function. is the start state, . is the initial stack symbol, . is the set of final states, .
The acceptance of a PDA
A PDA can accept a string only if it empties the input string and reaches a final state. The stack can be empty or not, but the input string must be empty.
Transition Function
The transition function
An Example
Consider the PDA that accepts the renowned language
. . . is the start state. is the initial stack symbol. .
The transition function can be as follows:
. We need to retain the so we write . . Also, double the . These two rules are for pushing 's onto the stack while reading 's from the input. . . Pop the 's. These two rules are for popping 's while reading 's from the input. . Accept the string when both the stack and the input are empty.
Instantaneous Description
The instantaneous description of a PDA is triple
is the current state. is the remaining input string. is the stack content.
If we have a PDA
To say one
The Previous Example
Using the previous example, take the input
. We push onto the stack. . We push another . . We pop the 's. . We pop the 's. . We accept the string.
Thus,
If we use the input
Theorem 1 Given a PDA
The trap of converse
The converse of this theorem is not true. The PDA can be stuck at some point for the stack content is not monotonically consumed.
The Language of a PDA
The common way to define the language is based on final state. If the PDA is denoted by
Another way to define the language is by empty stack. If
The two definitions are equivalent. If
Do Not Confuse...
For one PDA
Deterministic PDA
The deterministic PDA is a PDA that has only one choice at each step. The transition function is defined as
Theorem 2 If
Theorem 3 If
Generally, we have the following hierarchy:
- RE
DPDA(L(P)) NPDA - DPDA(N(P))
DPDA(L(P)) - RE and DPDA(N(P)) are incomparable.
Converting CFG to PDA
Given a CFG
- One state
. - Input symbols = terminal symbols of
. - Stack symbols = terminal symbols of
nonterminal symbols of , i.e., all the symbols of . - Start symbol = start symbol of
.
Intuitively, the snapshot of
The transition function
for all . for all .
Therefore, the PDA
Equivalence of PDA and CFG
From a PDA to a CFG
Given a PDA
First of all, we define a template variable
- Rule 1 If
, then we can have a rule in . - Rule 2 If
, then we can have a rule in . This rule means that the PDA can move a little step to a middle state , changing stack to , then may move to the final state and eliminate the stack top in future steps. Maybe this step is not necessary to each string, but we can have this rule to cover all the possibilities. - Rule 3 If
, then we can have a family of productions in for all state .
Finally, we can have this rule in
, iff in . can be any state.
So, we can construct the start symbol
An Example
Design a PDA that handles the if-else statement. It stops when the number of else exceeds the number of if. The PDA can be defined as follows:
State Graph
We can construct the CFG
- We have
, thus we have . (Rule 3) - We have
, then a rule can be added. (Rule 1) for all accepted strings , then we have a start symbol .
To sum up, the CFG
which can be simplified to