Chapter 7 - Pushdown Automata

Chapter 7
Pushdown Automata
10/31/03

Up to now CFL’s were specified by grammars

Want a class of automata which is associated with CFL’s
-
Not all CFL’s are recognized by DFA’s or NFA’s
-
Generally, an automaton for CFL’s requires more memory than the states
themselves
Example:
L = {anln : n  0} requires keeping a count of number of a’s and
number of l’s to see if they are equal.
But n is unbounded and the automaton has finite number of states
Example:
In L = {wwR} need the ability to store and match a sequence of
symbols in reverse order.
==> storage mechanism is unbounded pushdown stack

CS373
==>
Pushdown automata
==>
Non-deterministic PDA accepts the family of CFL’s
==>
Will show deterministic and non-deterministic PDA’s are not
equivalent.
NPDA
Fall 2003
Chapter 7
N. Guydosh
Page 1

Definition of NPDA
Septuple:
M = (Q, , , , q0, Z, F)
Compare with DFA p. 38 and NFA p. 5
 and Z are “new”
where:
Q = Finite set of internal states of C. U.
 = Input alphabet
 = Stack alphabet (finite set of symbols)
 : Q x ({}) x   finite subsets of Qx*

top of stack
q0  Q initial state of Control Unit

Z
stack start symbol
FQ
set of final states
Notes
-
Only top of stack is “read”
Stack read is destructive

-
CS373
(top of stack at start)
(stack “popped”)
input to  is:
Current state of CU
Current symbol of input string
Top of stack
String read L  R
No move possible if stack is empty (needs at least z)
Undefined transitions allowed. They take us to the empty set or a “dead
configuration” from which there is no return
Fall 2003
Chapter 7
N. Guydosh
Page 2

Note:
When a transition is done (using ), the result is not only the new set of states, but also:
A string of symbols from  is “pushed” onto the stack
The string is pushed onto the stack symbol by symbol starting at the right end of
the string.
If nothing is pushed on stack, the net effect is a pop caused by the last read.

Example 7.1
(q1 , a, b) = {(q2 , cd), (q3 , )}
where:
a is leftmost unused input symbol
b is content of top of stack
If we go to q2 , b is popped, push d, push c. c is now top.
If we go to q3 , only b is popped, nothing is pushed.


Putting  on stack does not affect it.
Example 7.2
Acceptor for CFL
L = {anbn : 0}  {a}
Q = {q0 , q1 , q2 , q3}
 = {a, b}
 = {0, 1} // 0 is end of stack, and remember reading an “a” by pushing a 1
Z=0
F = {q3}
(q0 , a, 0) = {(q1 , 10), (q3 , )}
(q3 , ) accepts {a}
(q0 , , 0) = {(q3 , )}
guess empty string, if not dead end
(q1 , a, 1) = {(q1 , 11)}
process the a’s (pop 1, push 11)
(q1 , b, 1) = {(q2 , )}
detect 1st b
(q2 , b, 1) = {(q2 , )}
process the b’s
(q2 , , 0) = {(q3 , )}
detect top stack, guess end of string, if more stuff  
Question: why not take the  transition to q3  F for any string and call it a day.
The idea: Reading an “a” pushes a 1, Reading a “b” pops a 1
Rejection: too many a’s, we halt in in q2 a nonfinal state
too many b’s: there is no rule (q2 , b, 0), thus go to empty set. Also when
(q2 , , 0) = {(q3 , )} will “lead” to  on subsequent b’s – we end in a .-
CS373
Fall 2003
Chapter 7
N. Guydosh
Page 3

dead configuration.
Instantaneous Description:
(q, w, u)

q
current state
w
unread part of input string
u
current stack contents, left most symbol = top
Instantaneous Description Definition:
(q1 , aw, bx) |- (q2 ,w, yx)
a is next to be read in
b is top of stack
y is pushed string
is possible if, and only if, (q2 , y)  (q1 ,a, b).
Because (q2 , y) is but one member of the set (q1 ,a, b) which could potentially have
other members (ordered pairs), there may be multiple paths started from (q1 , aw, bx):
(q1 , aw, bx) |- (q2 ,w, yx)
|- (q3 ,w, vx)
|- (q4 ,w, zx)
etc.
where q2 , q2 , q2 , … are next states, and y, v, z, … are corresponding pushed strings.
Language Acceptance: Definition 7.2
Given M = (Q, , , , q0 , Z, F)
NPDA
Language accepted by M is
L(M) = {w  * : (q0 , w, Z) |- M* (p, , u), p  F, u  *}

String consumed – important!
In words: the language accepted by M is the set of all strings that can put M into a final
state at the end of the string. The state of the stack at the end of the string is irrelevant.
Caution: some other books have alternate but equivalent definition, for example some
require that the stack be empty.
See examples 7.3 and 7.4
CS373
Fall 2003
Chapter 7
N. Guydosh
Page 4
Pushdown Automata and CFL’s

Theorem 7.1
For any CFL,  an NPDA such that L = L(M)

Theorem 7.2
If L = L(M) for some NPDA M, then L is a CFL.

The constructive proof techniques are useful in solving specific problems (Examples 7.5
and 7.6) – the constructive proof of Theorem 7.1 is easy, but the proof of Theorem 7.2 is
as hairy as hell and will be left for independent reading in the book.
Constructing An NPDA From A CFG


Will have 3 states:
q0 , q1 , q2
q0
is start state
q1
emulates production substitutions
q2
is final state
Let G = (V, T, S, P)
Transition Table:
Transition for q0
(q0 , , z) = {q1 , Sz}
Push start state on stack and go to q1
Transition from q1 (next page)
CS373
Fall 2003
Chapter 7
N. Guydosh
Page 5
Transition from q1
As long as unused input string not empty, all transitions are from q1 to q1:
Each transition simulates a production.
For each production:
A  au
where a  T, u  V*
(Assume GNF <== thanks to chapter 6!)
We have transition:
(q1 ,u) (q1 , a, A)
a is GNF prefix from T
u (push GNF suffix on to stack)
if u =  , (A a) nothing pushed, A popped
q1 is current state
Finally, when input string is used up and stack empty (all variables have been
resolved), we go to final state:
(q1 , , z) = {(qf , z)}, where  indicates input string is used up
See examples 7.5 and 7.6 (below)

Don’t have to be in GNF:
Can use arbitrary CFG’s
“q1” rules are same except
u is any sentential form u  {VT}*
A  VT
See extended example 7.5
CS373
Fall 2003
Chapter 7
N. Guydosh
Page 6
Example 7.6
CS373
Fall 2003
Chapter 7
N. Guydosh
Page 7
Deterministic Pushdown Automata
and
Deterministic CFL’s
Definition 7.3:
M = (Q, , , , q0 , z, F) is said to be deterministic if, and only if,
A PDA

it is an NPDA (definition 7.1) subject to the restrictions:
-
q  Q, a  {} and b  ,
1.
(q, a, b) is a set of one
“a” is input string symbol
“b” is from top of stack
Remember:
2.
input string symbols  {}
If (q, , b) is not empty,
then (q, c, b) is empty
c  
Notes:
CS373
1.
For any given input symbol and any stack top, at most one move can be made.
2.
When a  move is possible, for some configuration, no input consuming
alternative is allowed.
Fall 2003
Chapter 7
N. Guydosh
Page 8
More Notes:

The domain of the transition function of a DPDA is the same as for NPDA.
That is, it is Q x ({}) x 
and not Q x x 
 transitions are retained.

Top stack plays role in next move
Thus  on input does not necessarily imply non-determinism

Some transitions of DPDA  , i.e., undefined
Thus  dead configurations, see p. 195, 3rd ed. (202, 2nd ed)

The only criterion for determinism is that, at most, one possible move exists.
Definition:
Deterministic CFL
Language L is DCFL if, and only if,  a DPDA M such that L = L(M)
Notes:
 Just because a particular PDA for a language is non-deterministic, it does not necessarily
mean that the language itself is non-deterministic. ... maybe you did not try hard enough and
there still exist a DPDA.

Demonstrating a DPDA is necessary and sufficient to show a language is deterministic.

To show that a language is non-deterministic, you must first show that there DOES NOT
EXIST an equivalent DPDA – not easy – maybe a proof by contradiction
.... logic has echoes of the pumping lemma!
CS373
Fall 2003
Chapter 7
N. Guydosh
Page 9

Examples 7.8, 7.9
L1 = {anbn : n 0} ... our old friend is deterministic context free, can demonstrate a DPDA
see p. 196 3rd ed. (202, 2nd ed):
For L1: M = ({q0, q1, q2}, {a, b}, {a, z}, , z, {q0})
(q0, a, z) = { (q1, az) }
(q1, a, a) = { (q1, aa) }
(q1, b, a) = { (q2,) }
(q2, b, a) = { (q2,) }
(q2, , z) = { (q0,) }
L2 = {anb2n : n 0} is also deterministic CF. ... simple modification of previous DPDA
But L = L1  L2 is non-deterministic.
Let P1 be the productions for L1, and P2 the productions for L2. The variable set for each
set of productions sets are disjoint with S1 and S2 being the starting variables for P1 and
P2 respectively. The productions for the L are then:
P = P1  P2  {S -> S1 | S2}
Because of the “choice” of starting variables, it should be plausible that L is
nondeterministic.
We shall see late that Non-deterministic CFL’s are closed under 
But deterministic CFL’s are not as demonstrated above.
CS373
Fall 2003
Chapter 7
N. Guydosh
Page 10