PUSHDOWN AUTOMATA
UNITY UNIVERSITY
DEPT. OF COMPUTER SCI.
SECTION : N1-2/12
1. Zelalem Zerihun
2. Rewina Zerou
3. Welid Yasin
4. Natnael Legesse
5. Yonathan Dejene
6. Medhin Abraham
OUTLINE
• Introduction
• Definition of PDA
• The Language of a PDA
• Equivalence of PDA’s and CFG’s
• Deterministic PDA
Introduction
• Basic concepts:
• CFL’s may be accepted by pushdown automata (PDA’s)
• A PDA is an e-NFA with a stack.
• The stack can be read, pushed, and popped only at the top.
• Two different versions of PDA’s ---
• Accepting strings by “entering an accepting state”
• Accepting strings by “emptying the stack”
Introduction
• Basic concepts (cont’d)
• The original PDA is nondeterministic.
• There is also a subclass of PDA’s which are deterministic in
nature.
• Deterministic PDA’s (DPDA’s) resembles parsers for CFL’s in
compilers.
Introduction
• Basic concepts (cont’d)
• It is interesting to know what “language constructs” which a DPDA
can accept.
• The stack is infinite in size, so can be used as a “memory” to
eliminate the weakness of “finite states” of NFA’s, which cannot
accept languages like L = {anbn | n 1}.
Definition
• Informal Introduction
• Advantage of the stack --- the stack can “remember” an infinite
amount of information.
• Weakness of the stack --- the stack can only be read in a first-in-
last-out manner.
• Therefore, it can accept languages like Lwwr = {wwR | w is in (0 +
1)*}, but not languages like L = {anbncn | n 1}.
Definition of PDA
• Informal Introduction
• A graphic model of a PDA
• Automata img
Definition of PDA
• Informal Introduction
• The input string on the “tape” can only be read.
• But operations applied to the stack is complicated; we may replace
the top symbol by any string ---
• By a single symbol
• By a string of symbols
• By the empty string e which means the top
stack symbol is “popped up (eliminated).”
Definition of PDA
• Informal Introduction
• Example - Design a PDA to accept the
language Lwwr = {wwR | w is in (0 + 1)*}.
• In start state q0, copy input symbols onto the stack.
• At any time, nondeterministically guess whether
the middle of wwR is reached and enter q1, or
continue copying input symbols.
• In q1, compare remaining input symbols with those
on the stack one by one.
• If the stack can be so emptied, then the matching
of w with wR succeeds.
Definition of PDA
• Formal Definition
A PDA is a 7-tuple P = (Q, S, G, d, q0, Z0, F) where
• Q: a finite set of states
• S: a finite set of input symbols
• G: a finite stack alphabet
• d: a transition function such that d(q, a, X) is a set of pairs (p, g)
where
• qQ (the current state)
• aS or a = e (an input symbol or an empty string)
• XG
• pQ (the next state) (cont’d in the next page)
Definition of PDA
• Formal Definition
(continued from last page)
• gG* which replaces X on the top of the stack:
when g = e, the top stack symbol is popped up
when g = X, the stack is unchanged
when g = YZ, X is replaced by Z, and Y is pushed to the top
when g = aZ, X is replaced by Z and string a is pushed to the top
• q0: the start state
• Z0: the start symbol of the stack
• F: the set of accepting or final states
Definition of PDA
• Formal Definition
• Example (cont’d from Example ) - Designing a PDA to
accept the language Lww . R
• Need a start symbol Z of the stack and a 3rd state q2 as the accepting state.
• P = ({q0, q1, q2}, {0, 1}, {0, 1, Z0}, d, q0, Z0, {q2}) such that
• d(q0, 0, Z0) = {(q0, 0Z0)}, d(q0, 1, Z0) = {(q0, 1Z0)}
(initial pushing steps with Z0 to mark stack bottom)
• d(q0, 0, 0) = {(q0, 00)}, d(q0, 0, 1) = {(q0, 01)}, d(q0, 1, 0) =
{(q0, 10)}, d(q0, 1, 1) = {(q0, 11)}
(continuing pushing)
Definition of PDA
• Formal Definition
• Example (cont’d)
• d(q0, e, Z0) = {(q1, Z0)}
(check if input is e which is in L )
wwR
• d(q0, e, 0) = {(q1, 0)}, d(q0, e, 1) = {(q1, 1)}
(check the string’s middle)
• d(q1, 0, 0) = {(q1, e)}, d(q1, 1, 1) = {(q1, e)}
(matching pairs)
• d(q1, e, Z0) = {(q2, Z0)}
(entering final state)
Definition of PDA
• A Graphic Notation for PDA’s
• The transition diagram of a PDA is easier to follow.
• We use “a, X/a” on an arc from state p to q to represent that
“transition d(q, a, X) contains (p, a)”
q
q a,x/a p
• Example The transition diagram of the PDA of example is as
shown in fig .
Contd…
•A Graphic Notation for PDA’s
0,zo/0zo(push 0 on top of zo)
1,zo/1zo
0,0/00
0,1/01
1,0/10 0,0/E
1,1/11 1,1/E
qo q1 q2
• e,zo/zo e,zo/zo
• Where is the nondeterminism?
Definition of PDA
• Instantaneous Descriptions of a PDA
• The configuration of a PDA is represented by a 3-tuple (q, w, g)
where
• q is the state;
• w is the remaining input; and
• g is the stack content.
• Such a 3-tuple is called an instantaneous description (ID) of the
PDA.
Definition of PDA
• Instantaneous Descriptions of a PDA
• The change of an ID into another is called a move, denoted by the
symbol p, or when P is understood.
• So, if d(q, a, X) contains (p, a), then the following is a
corresponding move:
(q, aw, Xb) (p, w, ab)
• We use *p or * to indicate zero or more moves.
Definition of PDA
• Instantaneous Descriptions of a PDA
• Example (cont’d from example) -
• Moves for the PDA to accept input w = 1111:
(q0, 1111, Z0) Ւ (q0, 111, 1Z0)
Ւ (q0, 11, 11Z0) Ւ (q1, 11, 11Z0)
Ւ (q1, 1, 1Z0)
Ւ (q1, e, Z0) Ւ (q2, e, Z0)
• There are other paths entering dead ends (not shown).
Definition of PDA
• Instantaneous Descriptions of a PDA
• Theorem
If P = (Q, S, G, d, q0, Z0, F) is a PDA, and
(q, x, a) Ւ*p (p, y, b),
then for any string w in S* and g in G*, it is also true that
(q, xw, ag) Ւ*p (p, yw, bg).
(The reverse is not true; but if g is taken away, the reverse is true, as shown by the next theorem)
Definition of PDA
• Instantaneous Descriptions of a PDA
• Theorem
If P = (Q, S, G, d, q0, Z0, F) is a PDA, and
(q, xw, a) Ւ*p (p, yw, b),
then it is also true that
(q, x, a) Ւ*p (p, y, b).
The Language of a PDA
• Some important facts:
• Two ways to define languages of PDA’s: by final state and by
empty stack, as mentioned before.
• It can be proved that a language L has a PDA that accepts it by
final state if and only if L has a PDA that accepts it by empty stack.
• For a given PDA P, the language that P accepts by final state and
by empty stack are usually different.
• In this section, we show conversions between the two ways of
language acceptances.
Contd…
• Acceptance by Final State
• Definition:
If P = (Q, S, G, d, q0, Z0, F) is a PDA. Then L(P), the
language accepted by P by final state, is
{w | (q0, w, Z0) Ւ*p (q, e, a), qF}
for any a.
• Example - Proving the PDA shown in Example indeed accepts the
language LwwR
Contd…
Acceptance by Empty Stack
• Definition:
If P = (Q, S, G, d, q0, Z0, F) is a PDA. Then N(P), the
language accepted by P by empty stack, is
{w | (q0, w, Z0) Ւ*p (q, e, e)}
for any q.
• The set of final states F may be dropped to form a 6-tuple, instead
of a 7-tuple, for a PDA.
Contd…
Acceptance by Empty Stack
• Example
The PDA of Example may be modified to accept LwwR by
empty stack:
simply change the original transition
d(q1, e, Z0) = {(q2, Z0)}
to be
d(q1, e, Z0) = {(q2, e)}.
(just eliminate Z0)
Contd…
• From Empty Stack to Final State
• Theorem (1/3)
If L = N(PN) for some PDA PN = (Q, S, G, dN, q0,
Z0), then there is a PDA PF = such that L = L(PF).
• From Empty Stack to Final State
• Theorem (2/3)
If L = N(PN) for some PDA PN = (Q, S, G, dN, q0,
Z0), then there is a PDA PF = such that L = L(PF).
Contd…
• From Empty Stack to Final State
• Theorem (3/3)
If L = N(PN) for some PDA PN = (Q, S, G, dN, q0,
Z0), then there is a PDA PF = such that L = L(PF).
Contd…
• From Empty Stack to Final State
• Example - Design a PDA which accepts the if/else
errors by empty stack.
• Let i represents if; e represents else.
• The PDA is designed in such a way that
if the number of else (#else) > the number of if (#if), then the stack will
be emptied.
Contd…
• From Empty Stack to Final State
• Example (cont’d)
• A PDA by empty stack for this is as follows:
PN = ({q}, {i, e}, {Z}, dN, q, Z)
when an “if” is seen, push a “Z”;
when an “else” is seen, pop a “Z”;
when (#else) > (#if + 1), the stack is emptied and the input sting is
accepted.
• For example, for input string w = iee, the moves are:
(q, iee, Z) Ւ (q, ee, ZZ) Ւ (q, e, Z) Ւ (q, e, e) accept !
(how about w = eei?)
Contd…
• From Empty Stack to Final State
• Example (cont’d)
• A PDA by final state as follows:
PF = ({p, q, r}, {i, e}, {Z, X0}, dF, p, X0, {r})
e,Z/e
i,Z/ZZ
start Q R
P
e,xo/zxo e,xo/e
• For input w = iee, the moves are:
(p, iee, X0) Ւ (q, iee, ZX0) Ւ (q, ee, ZZX0) Ւ (q, e, ZX0) Ւ (q, e, X0) Ւ (r, e, e)
accept !
Contd…
• From Final State to Empty Stack
• Theorem
Let L be L(PF) for some PDA PF = (Q, S, G, dF, q0,
Z0, F). Then there is a PDA PN such that L = N(PN).
Proof. The idea is to use Fig. below (in final states of
PF, pop up the remaining symbols in the stack).
Equivalence of PDA’s and CFG’s
• Equivalences to be proved:
1) CFL’s defined by CFG’s
2) Languages accepted by final state by some PDA
3) Languages accepted by empty stack by some PDA
• Equivalence of 2) and 3) above have been proved.
CFG to PDA
• Given a CFG G = (V, T, Q, S), construct a PDA P that accepts L(G)
by empty stack in the following way:
• P = ({q}, T, V∪T, d, q, S) where the transition function d is defined
by:
• for each nonterminal A,
d(q, e, A) = {(q, b) | A b is a production of G};
• for each terminal a, d(q, a, a) = {(q, e)}.
Contd…
• From Grammars to PDA’s
• Theorem
If PDA P is constructed from CFG G by the construction above,
then N(P) = L(G).
Contd…
• From Grammars to PDA’s
• Example - Construct a PDA from the expression grammar of Fig. :
I a | b | Ia | Ib | I0 | I1;
E I | E*E | E+E | (E).
The transition function for the PDA is as follows:
a) d(q, e, I) = {(q, a), (q, b), (q, Ia), (q, Ib), (q, I0), (q,
I1)}
b) d(q, e, E) = {(q, I), (q, E+E), (q, E*E), (q, (E))}
c) d(q, d, d) = {(q, e)} where d may any of the terminals
a, b, 0, 1, (, ), +, *.
From PDA to CFG
• Theorem
Let P = (Q, S, G, d, q0, Z0) be a PDA. Then there
is a context-free grammar G such that L(G) = N(P).
Proof. Construct G = (V, T, P, S) where the set of
nonterminals consists of:
• the special symbol S as the start symbol;
• all symbols of the form [pXq] where p and q are states in Q and X is a
stack symbol in G.
Contd…
• From PDA’s to Grammars
• Theorem
Proof. (cont’d)
The productions of G are as follows.
(a) For all states p, G has the production S [q0Z0p].
(b) Let d(q, a, X) contain the pair (r, Y1Y2 … Yk), where
• a is either a symbol in S or a = e;
• k can be any number, including 0, in which case the pair
is (r, e).
Then for all lists of states r1, r2, …, rk, G has the production
[qXrk] a[rY1r1][r1Y2r2]…[rk1Ykrk].
Contd…
• From PDA’s to Grammars
• Example --- Convert the PDA of Example 6.10 (below) to a grammar.
e, z/e
i,z/zz
Nonterminals include only two symbols, S and [qZq].
Productions:
1. S [qZq] (for the start symbol S);
2. [qZq] i[qZq][qZq] (from (q, ZZ)dN(q, i, Z))
3. [qZq] e (from (q, e)dN(q, e, Z))
Contd…
From PDA’s to Grammars
• Example --- (cont’d)
If we replace [qZq] by a simple symbol A, then the
productions become
1. S A
2. A iAA
3. A e
Obviously, these productions can be simplified to be
1. S iSS
2. S e
And the grammar may be written simply as
G = ({S}, {i, e}, {S iSS | e}, S)
1.4 Deterministic PDA
• Definition of a Deterministic PDA
• A PDA is said to be deterministic if it satisfies two conditions
• first if there is only one move for a state on every input symbol and
stack symbol and
• The second condition is that when a λ-move is possible for some
configuration, no input-consuming alternative is available.
• Formally ;
d(q, a, X) has at most one element for any qQ, aS or a = e, and
XG. If d(q, a, X) is nonempty for some aS, then d(q, e, X) must be
empty.
Contd…
A DPDA is a 7-tuple P = (Q, S, G, d, q0, Z0, F)
d : a transition function takes as argument a triple
d(q, a, X) where
i. q is a state in Q
ii. a is either input symbol in S, or e
iii. X is a stack symbol
The output of the transition function is finite set of pairs (p, g) where
P : is a new state
g : is a string of stack symbols that replaces X at the top of the
stack
If g = e the stack is popped
If g = X the stack is unchanged
If g = YZ then X is replaced by Z and Y is pushed to the stack
Contd…
• Example : construct a DPDA for the language L={anbn , n>0}
• Solution: The language generates a string like
ab,aabb,aaabbb …
• Initially our stack contains only z
• As the number of a’s and b’s are equal,
whenever we get a we push to the stack and
whenever we get b we pop a out of the stack
Z
Contd…
The equivalence DPDA will be ;
(a,a/aa)
(a,z/az) (b,a/ e)
(b,a/e) (e,z/z)
q0 q1 qf
At the end we will have only
z in the stack, and as it
takes us to the final state
the string is accepted
a
a a
Z Z Z
Contd…
Transition function
d(q0,a,z)=(q0,az)
d(q0,a,a)=(q0,aa)
d(q0,b,a)=(q1, e)
d(q1,b,a)=(q1, e)
d(q1, e,z)=(qf,z)
Regular Languages and DPDA
• The DPDA’s accepts a class of languages that is between the
RL’s and the CFL’s, as proved in the following.
• Theorem
If L is an RL, then L = L(P) for some DPDA P (accepting by
final state).
Proof. Easy. Just use a DPDA to simulate a DFA as follows.
If DFA A = (Q, S, dA, q0, F) accepts L, then construct DPDA P
= (Q, S, {Z0}, dP, q0, Z0, F) where dP is such that dP(q, a, Z0) =
{(p, Z0)} for all states p and q in Q such that dA(q, a) = p.
Contd…
• Regular Languages and DPDA’s
• The language-recognizing capability of the DPDA by empty stack is
rather limited.
• Theorem
A language L is N(P) for some DPDA P if and only if L has the
prefix property and L is L(P') for some DPDA P'
A language L is said to have the prefix property if there are no two
different strings x and y in L such that x is a prefix of y.
DPDA and CFL
• DPDA’s can be used to accept non-RL’s, for example, LwcwR
mentioned before.
• It can be proved by the pumping lemma that LwcwR is not an RL
• On the other hand, DPDA’s by final state cannot accept
certain CFL’s, for example, LwwR.
• It can be proved that LwwR cannot be accepted by a DPDA by final state.
Contd…
• DPDA’s and CFL’s
• Conclusion:
The languages accepted by DPDA’s by final state
properly include RL’s, but are properly included in
CFL’s.
DPDA and Ambiguous Grammars
• DPDA’s and Ambiguous Grammars
• Theorem
If L = N(P) (accepting by empty stack) for some DPDA P, then L
has an unambiguous CFG.
Theorem
If L = L(P) for some DPDA P (accepting by final state), then L has
an unambiguous CFG.
THE END