Chapter 5
Pushdown Automata
Esubalew Alemneh
Contents
Introduction
Nondeterministic Pushdown Automata
Moves by NPDA
Transition Diagram
Instantaneous Description
Acceptance of strings by NFA
Push down Automata for CFGs
CFGs for Push down Automat
Deterministic Pushdown Automata and
Introduction
Push-Down Automata (PDA) is an abstract model of a machine –
Finite State Automata.
It has a finite set of states. However, in addition, it has a
pushdown stack. Moves of the PDA are as follows:
1. An input symbol is read and the top symbol on the stack is
read.
2. Based on both inputs, the machine enters a new state and
writes zero or more symbols onto the pushdown stack.
3. Acceptance of a string occurs if the stack is ever empty.
(Alternatively, acceptance can be if the PDA is in a final
state. Both models can be shown to be equivalent.)
3
Nondeterministic Pushdown Automata(NPDA)
An NPDA is a seven-tuple:
M = (Q, Σ, Г, δ, q0, z0, F)
Q - A finite set of states,
Σ - A finite input alphabet
Г - A finite stack alphabet,
Q0 - The initial/starting state, q0 is in Q
z0 - A starting stack symbol, is in Г
F -A set of final/accepting states, which is a subset of Q
δ - A transition function, where
δ: Q x (Σ U {ε}) x Г –> finite subsets of Q x Г*
4
Moves By NPDA
Consider the various parts of δ:
Q x (Σ U {ε}) x Г –> finite subsets of Q x Г*
Q & Г on the LHS means that at each step in a computation, a PDA
must consider its’ current state & the symbol on top of its’ stack.
Σ U {ε} on the LHS means that at each step in a computation, a PDA
may or may not consider the current input symbol, i.e., it may have
epsilon transitions.
“Finite subsets” on the RHS means that at each step in a computation,
a PDA will have several options.
Q on the RHS means that each option specifies a new state.
Г* on the RHS means that each option specifies zero or more stack
symbols that will replace the top stack symbol, but in a specific
5
sequence.
Moves By NPDA…
1. δ(q, a, z) = {(p1,γ1), (p2,γ2),…, 2., ε, z) = {(p1,γ1), (p
(pm,γm)} δ(q2,γ2),…, (pm,γm)}
Current state is q
Current state is q
Current input symbol is a
Current input symbol is not
Symbol currently on top of the
stack z considered
Symbol currently on top of the
Move to state pi from q
stack z
Replace z with γi on the stack
Move to state pi from q
(leftmost symbol on top)
Replace z with γi on the stack
Move the input head to the next
input symbol (leftmost symbol on top)
No input symbol is read
6
Transition Graph
The nodes are labeled with states
The arcs are labeled as X,YV , where
X is the string processed
Y is the top element of the stack to be popped off
V is the symbol to be pushed onto stack
Example Transition graph of PDA for L(M) = {anbn:n>0}
Instantaneous Description (ID)
ID have the following format
( q, u , s )
Where,
o Q is Current state
o U is Remaining input
o S Current stack contents
A move from one move to another ID is denoted by , or
Thus (q, qw, bx) (p, w, yx) is possible iff (p, y) e δ (q, a, b)
Instantaneous Description (ID)…
Example: show how PDA for L(M) = {anbn:n>0} accepts
String w = a2b2
(q0 , aaabbb,$) (q1, aaabbb,$)
(q1, aabbb, a$) (q1, abbb, aa$) (q1, bbb, aaa$)
(q2 , bb, aa$) (q2 , b, a$) (q2 , ,$) (q3 , ,$)
Exercise
Construct a pushdown automata for the language L = {w e{a,
b}*:na(w) = nb(w)} and check how the PDA accepts string baab
Answer
Count the number of a’s and b’s
M = ({q0, q1}, {a, b}, {0, 1, z0}, δ, q0, z0, {q1} ) where δ is;
δ(q0, a, z0) = {(q0, 0z0 )}, δ(q0, a, 0) = {(q1, 00 )},
δ(q0, b, z0) = {(q1, 1z0 )}, δ(q0, b, 1) = {(q1, 11 )},
δ(q0, a, 1) = {(q1, ε )}, δ(q0, b, 0) = {(q1, ε )},
δ(q0, ε, z0) = {(q1, z0 )},
PDA for CFGs
Theorem: For any CFL L there is a PDA M such that L = L(M)
Proof;
Let G = (N, T, P, S) be a CFG such that L = L(G)
X e L(G) iff S X, using left most derivation
Define PDA that simulates these LMD on stack
M = (Q, Σ, V, δ, z0, q0, F) Where
Q = {q0 , q1 , q2}
Σ =T
V = T U NU {z0}
F = {q2}
δ is given as
δ(q0, ε, z0) = {(q1, Sz0 )},
PDA for CFGs…
δ(q1, ε, A) = {(q1, |A is in P, A e N )},
(If A is in the stack pop it & push the RHS of the production)
δ(q1, a, a) = {(q1, ε )} Ae Σ,
δ(q1, ε, z0) = {(q2, ε )} …. Accepted by Final State
Example: Construct a PDA that accepts the language
generated by the following grammar where P is
S aAA, A aA | bS | a
Define M = (Q, Σ, V, δ, z0, q0, F) Where
Q = {q0 , q1 , q2}
Σ = {a, b}
V = {S, A, a, b, z0}
F = {q2}
PDA for CFGs…
δ is given as;
δ(q0, ε, z0) = {(q1, Sz0 )},
δ(q1, ε, S) = {(q1, aAA )},
δ(q1, ε, A) = {(q1, aA), (q1, bS), (q1,b), },
δ(q1, a, a) = {(q1, ε )}, δ(q1, b, b) = {(q1, ε )},
δ(q1, ε, z0) = {(q2, ε )},
Show the sequence of moves that would be made by M in
processing string w = aba4.
CFG for PDA
Theorem: let L be a language such that L = L(M) for some
NPDA m. Then L is CFL.
Proof: Let M = (Q, Σ, V, δ, z0, q0, qf) be an NPDA such that
L=L(M)
Assume
1. It has a single final state qf that is entered iff the stack is empty (this
can always be done)
2. All transactions must have the form δ(q0, a, A) = {C1, C2, …, Cn}
(this can always be done)
Suppose variables/non-terminals of the form (qi A qj) and
whose productions are such that
(qi A qj)==>V iff the NPDA erases A from stack while
reading V and going from qi to qj .
CFG for PDA…
then construct the productions as follows
1. If δ(qi ,a, A) = {(qj, ε )} then, (qi A qj) a
2. If δ(qi ,a, A) = {(qj, BC)} then, generate rules
(qi A qk) a(qj B ql) (ql C qk) where qk and ql takes on all possible
values in Q
3. Choose (q0A qf) as start symbol
Exercises: Consider M = ({q0 , q1, q2, q3 }, {a, b}, {A, Z}, δ,
Z, q0, q2) where δ is given as
o δ(q0, a, Z) = {(q0, AZ )}, δ(q3, ε, Z) = {(q0, AZ )}, δ(q0, a, A) = {(q3,
ε )}, δ(q0, b, A) = {(q1, ε )}, δ(q1, ε, Z) = {(q2, ε )}
o Convert NPDA M to its equivalent CFG and derivate string aab using
the new grammar
CFG for PDA…
Answer
From rule one
(q0A q3 ) a … from δ(q0, a, A) = {(q3, ε )}
(q0A q1 ) b … from δ(q0, b, A) = {(q1, ε )}
(q1Zq2 ) ε … from δ(q1, ε, Z) = {(q2, ε )}
From Rule 2
Take δ(q0, a, Z) = {(q0, AZ )}
q0 Zq0 a(q0 Aq0 )(q0 Zq0 ) | a(q0 Aq1 )(q1 Zq0 ) | a(q0 Aq2 )(q2 Zq0 )
| a(q0 Aq3 )(q3 Zq0 )
q0 Zq1 a(q0 Aq0 )(q0 Zq1 ) | a(q0 Aq1 )(q1 Zq1 ) | a(q0 Aq2 )(q2 Zq1 )
| a(q0 Aq3 )(q3 Zq1 )
q0 Zq2 a(q0 Aq0 )(q0 Zq2 ) | a(q0 Aq1 )(q1 Zq2 ) | a(q0 Aq2 )(q2 Zq2 )
| a(q0 Aq3 )(q3 Zq2 )
q0 Zq3 a(q0 Aq0 )(q0 Zq3) | a(q0 Aq1 )(q1 Zq3 ) | a(q0 Aq2 )(q2 Zq3 )
| a(q0 Aq3 )(q3 Zq3)
CFG for PDA…
Take δ(q3, ε, Z) = {(q0, AZ
q3 Zq0 (q0 Aq0 )(q0 Zq0 ) | (q0 Aq1 )(q1 Zq0 ) | (q0 Aq2 )(q2 Zq0 )
| (q0 Aq3 )(q3 Zq0 )
q3 Zq1 (q0 Aq0 )(q0 Zq1 ) | (q0 Aq1 )(q1 Zq1 ) | (q0 Aq2 )(q2 Zq1 )
| (q0 Aq3 )(q3 Zq1)
q3 Zq2 (q0 Aq0 )(q0 Zq2 ) | (q0 Aq1 )(q1 Zq2 ) | (q0 Aq2 )(q2 Zq2 )
| (q0 Aq3 )(q3 Zq2 )
q3 0 Zq3 (q0 Aq0 )(q0 Zq3) | (q0 Aq1 )(q1 Zq3 ) | (q0 Aq2 )(q2 Zq3 )
| (q0 Aq3 )(q3 Zq3)
From Rule make (q0 Zq2) as Initial Symbol
Derivation of String
q0 Zq2 a(q0 Aq3 )(q3 Zq2) aa(q3 Zq2)
aa(q0 Aq1 )(q1 Zq2) aab(q1 Zq2) aab
Deterministic Push Down Automata(DPDA)
is a variation of the pushdown automaton in which has at most
one possible move.
That is
For any the set has at most one
element.
A language Accepted by DPDA is called deterministic CFL (DCFL)
DCFL are subsets of CFL
DPDA is restricted NPDA
18