Pushdown Automata
Pushdown Automata
PDA
Can visualize a PDA with the schematic
input Finite State Control Accept or reject
Reads input symbol by symbol Can write to stack Makes transitions based on input, top of stack
Stack
Implementing a PDA
In one transition the PDA may do the following:
Consume the input symbol. If is the input symbol, then no input is consumed. Go to a new state, which may be the same as the previous state. Replace the symbol at the top of the stack by any string.
If this string is then this is a pop of the stack The string might be the same as the current stack top (does nothing) Replace with a new string (pop and push) Replace with multiple symbols (multiple pushes)
We showed this is not regular A finite automaton is unable to recognize this language because it cannot store an arbitrarily large number of values in its finite memory.
As each 0 is read, push it onto the stack As soon as 1s are read, pop a 0 off the stack If reading the input is finished exactly when the stack is empty, accept the input else reject the input
Start in state q0 that represents the state where we havent yet seen the reversed part of the string. While in state q0 we read each input symbol and push them on the stack. At any time, assume we have seen the middle; i.e. fork off a new branch that assumes we have seen the end of w. We signify this choice by spontaneously going to state q1. This behaves just like a nondeterministic finite automaton
Well continue in both the forked-branch and the original branch. One of these branches may die, but as long as one of them reaches a final state we accept the input.
In state q1 compare input symbols with the top of the stack. If match, pop the stack and proceed. If no match, then the branch of the automaton dies. If we empty the stack then we have seen wwR and can proceed to a final accepting state.
The output of is the finite set of pairs (p, ) where p is a new state and is a new string of stack symbols that replaces X at the top of the stack.
If = then we pop the stack if = X the stack is unchanged if = YZ then X is replaced by Z and Y is pushed on the stack. Note the new stack top is to the left end. If X = then we push on
0 $
q2,0
0
q3, q3,
1 $
$
q2,$ q4,
Graphical Format
Uses the format
Input-Symbol, Top-of-Stack Any of these may be empty!
0, 0
String-to-replace-top-of-stack
Start
q1
q2
1, 0
q4
,$
q3
1, 0
Example 2
Here is the graphical description of the PDA that accepts the language
L = { wwR | w is in (0+1)* }
Stays in state q0 when we are reading w, saving the input symbol. Every time we guess that we have reached the end of w and are beginning wR by going to q1 on an epsilon-transition. In state q1 we pop off each 0 or 1 we encounter that matches the input. Any other input will die for this branch of the PDA. If we ever reach the bottom of the stack, we go to an accepting state.
0, 1,
0 1
0,0 1,1
Start
q0
q1
,$
q2
Moves of a PDA
To describe the process of taking a transition, we can adopt a notation similar to like we used for DFAs. In this case we use the turnstile symbol which is used as: (q, aw, X) (p, w, )
In other words, we took a transition such that we went from state q to p, we consumed input symbol a, and we replaced the top of the stack X with some new string . We can extend the move symbol to taking many moves: * represents zero or more moves of the PDA.
Language of a PDA
The PDA consumes input and accepts input when it ends in a final state. We can describe this as:
L(P) = {w | (q0, w, Z0) * (q, , ) } where q F
That is, from the starting state, we can make moves that end up in a final state with any stack values. The stack values are irrelevant as long as we end up in a final state.
But the PDA can access only the top symbol on the stack and that might be a terminal
But if our PDA makes transitions only for variables, we we wont know to do
To get around this problem, well use the non-determinism of the PDA to match terminal symbols on the stack with symbols in the input string before the first variable
This chomps any leading terminals until we can process a variable
qstart
, S ,A Y a,a S is the start symbol For rule A Y, Y is a string For terminal symbol a $ initially the top of the stack
qloop
, $/
qaccept
Example
Consider the grammar S aTb | b T Ta |
Start
qstart
, S , S aTb , T Ta ,S b ,T a, a b, b
Given the string aab derived by S aTb aTab aab We have the corresponding moves: (qs, aab, $) (qloop, aab, S$) (qloop, aab, aTb$) (qloop, ab, Tb$) (qloop, ab, Tab$) (qloop, ab, ab$) (qloop, b, b$) (qloop, , $) (qaccept, , )
qloop
,$
qaccept
10
Deterministic PDA
A DPDA is simply a pushdown automata without non-determinism.
i.e. no epsilon transitions or transitions to multiple states on same input Only one state at a time This machine accepts a class of languages somewhere between regular languages and context-free languages. For this reason, the DPDA is often skipped as a topic In practice the DPDA can be useful since determinism is much easier to implement.
Parsers in programs such as YACC are actually implemented using a DPDA.
11