0% found this document useful (0 votes)
23 views42 pages

Push Down Automata

Uploaded by

Rakshith P CSE
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views42 pages

Push Down Automata

Uploaded by

Rakshith P CSE
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 42

PUSH DOWN AUTOMATA

(PDA)

By,
Mr.R.Karunamoorthi,
AP / CSE
Pushdown automata are machines to accept context-free
languages.
For a context-free grammar G, there is an equivalent
pushdown automaton M to recognize the language
generated by the grammar G.
Description of pushdown automaton (PDA)
A PDA has

an input tape,

a finite control, and

a stack.

0 0 0 1 0 0 0 (input tape)

step1: read 0, push 0, and move to right

q1 q1 q1 step2: read 0, push 0, and move to right


0

(finite control) 0

(stack)
There are 2 types of moves (Non-determinstic):

1. According to an input symbol, current state, and top


symbol of the stack, a decision is made, then the input
head is moved ahead one symbol.

2. Next move is decided by the current state and the top


symbol of the stack only. The input symbol is not used.
Hence the input head is not moved at all after the
decision is made. This type of move is called ε-move
that allows the PDA to manipulate the stack without
reading input symbol.
Languages accepted by PDA's

There are two ways to accept inputs:

1. The PDA accepts an input if after reading the input


and the machine empty its stack.
The set of inputs accepted by the PDA is the language
accepted by empty stack.

2. Some states of the PDA are final states. The PDA


accepts an input if the machine enters a final state.
The set of inputs accepted by the PDA is the language
accepted by final state.
Definition of PDA's

A pushdown automaton M is a system (Q, Σ, Γ, δ, q0, Z0 , F),


where
1. Q is a finite set of states;

2. Σ is an alphabet called the input alphabet;

3. Γ is an alphabet, called the stack alphabet;

4. q0 in Q is the initial state;

5. Z0 in Γ is a particular stack symbol called the start symbol;

6. F, a subset of Q, is the set of final states;

7. mapping δ: Q×(Σ∪{ε}) ×Γ → finite subset of Q×Γ*.


Moves:

δ(q, a, Z) = {(p1, r1), (p2, r2), ..., (pm, rm)}, where


q and pi, 1≦i ≦ m, are states, a is in , Z is a stack
symbol, and ri is in Γ*, 1≦i ≦ m,
means
PDA in state q, reading input symbol a with top symbol Z on the
stack, can enter any of the state pi and replaces top symbol Z by
ri, and advances reading head one symbol.

Example: δ(q1, 0, G) = {(q1, BG)}

0 0 1 0 0 0 1 0
δ B

G G
q1 q1
R R
-Moves:

δ(q, , Z) = {(p1, r1), (p2, r2), ..., (pm, rm)}, where


q and pi, 1≦i ≦ m, are states, Z is a stack symbol, and ri
is in Γ*, 1≦i ≦ m,
means
PDA in state q, without reading any input symbol with top
symbol Z on the stack, can enter any of the state pi and replaces
top symbol Z by ri, and the reading head remains at the same
place.
Example: δ(q1, , G) = {(q1, BG)}

0 0 1 0 0 0 1 0
δ B

G G
q1 q1
R R
Example: N(M) = {wwR| w in (0+1)*}, where

M = ({q1, q2}, {0, 1}, {Z0, 0, 1}, δ, q1, Z0, {}), and δ is as follows:

δ(q1, 0, Z0) ={(q1, 0Z0)}, δ(q2, 0, 0) ={(q2, )},


δ(q1, 1, Z0) ={(q1, 1Z0)}, δ(q2, 1, 1) ={(q2, )},

δ(q1, 0, 0) ={(q1, 00), (q2, )}, δ(q2, , Z0) ={(q2, )}.

δ(q1, 0, 1) ={(q1, 01)}, δ(q2, , Z0) ={(q3, )}.

δ(q1, 1, 0) ={(q1, 10)},


δ(q1, 1, 1) ={(q1, 11), (q2, )},
δ(q1, , Z0) = {(q2, )},
M = ({q1, q2, q3}, {0, 1}, {Z0, 0, 1}, δ, q1, Z0, {q3})
Let Lwwr = {wwR | w is in (0+1)*}
• CFG for Lwwr : S==> 0S0 | 1S1 | 
• PDA for Lwwr :
• P := ( Q,∑, , δ,q0,Z0,F )

= ( {q0, q1, q2},{0,1},{0,1,Z0},δ,q0,Z0,{q2})


Initial state of the PDA:

PDA for Lwwr Stack


q0

top
Z0
1. δ(q0,0, Z0)={(q0,0Z0)}
2. δ(q0,1, Z0)={(q0,1Z0)} First symbol push on stack
3. δ(q0,0, 0)={(q0,00)}
4. δ(q0,0, 1)={(q0,01)} Grow the stack by pushing
5. δ(q0,1, 0)={(q0,10)} new symbols on top of old
6. δ(q0,1, 1)={(q0,11)}
(w-part)
7. δ(q0, , 0)={(q1, 0)}
8. δ(q0, , 1)={(q1, 1)}
Switch to popping mode
9. δ(q0, , Z0)={(q1, Z0)} (boundary between w and wR)

10. δ(q1,0, 0)={(q1, )}


11. δ(q1,1, 1)={(q1, )}
Shrink the stack by popping matching

12. δ(q1, , Z0)={(q2, Z0)} symbols (wR-part)


Enter acceptance state
11
PDA as a state diagram

δ(qi,a, X)={(qj,Y)}

Next Current Stack


input stack Top
Current symbol top Replacement
state (w/ string Y)

Next
qi
a, X / Y q state
j

12
PDA for Lwwr: Transition Diagram
∑ = {0, 1}
= {Z0, 0, 1}
Q = {q0,q1,q2}
0, Z0/0Z0
Grow stack Pop stack for
1, Z0/1Z0 matching symbols
0, 0/00
0, 1/01
1, 0/10 0, 0/ 
1, 1/11 1, 1/ 

q0 q1 q2
, Z0/Z0 , Z0/Z0 , Z0/Z0
, 0/0 Go to acceptance
, 1/1

Switch to
popping mode
This would be a non-deterministic PDA 13
Instantaneous descriptions:

If δ(q, a, Z) = contains (p, ), we say


(q, a, Z)  M(p, ,  ).

Example: δ(q1, 0, G) = {(q1, BG)}

0 0 1 0 0 0 1 0
δ B

G G
q1 q1
R R

For the above case, we have that


(q1, 010, GR)  M(q1, 01, BGR).

Let  * be the reflexive and transitive closure of  M.


M
PDA’s Instantaneous Description (ID)
A PDA has a configuration at any given instance: (q,w,y)
– q - current state
– w - remainder of the input (i.e., unconsumed part)
– y - current stack contents as a string from top to bottom of
stack
If δ(q,a, X)={(p, A)} is a transition, then the following are also
true:
– (q, a, X ) |--- (p,,A)
– (q, aw, XB ) |--- (p,w,AB)
|--- sign is called a “turnstile notation” and represents one move
|---* sign represents a sequence of moves

15
How does the PDA for Lwwr work on input
“1111”?
All moves made by the non-deterministic PDA

(q0,1111,Z0)

(q0,111,1Z0) (q1,1111,Z0) Path dies…

(q0,11,11Z0) (q1,111,1Z0) Path dies…

(q0,1,111Z0) (q1,11,11Z0)
Acceptance by
(q0,,1111Z0) (q1,1,111Z0) (q1,1,1Z0) final state:

(q1, ,1111Z0) (q1, ,11Z0) (q1, ,Z0) = empty input


AND
Path dies… (q2, ,Z0)
Path dies… final state
16
There are two types of PDAs that one can design:
those that accept by final state or by empty stack

Acceptance by…
• PDAs that accept by final state:
– For a PDA P, the language accepted by P, denoted by L(P)
by final state, is:
• {w | (q0,w,Z0) |---* (q,, A) }, s.t., q  F Checklist:
- input exhausted?
- in a final state?

• PDAs that accept by empty stack:


– For a PDA P, the language accepted by P, denoted by N(P)
by empty stack, is:
• {w | (q0,w,Z0) |---* (q, , ) }, for any q  Q.

Q) Does a PDA that accepts by empty stack Checklist:


need any final state specified in the design? - input exhausted? 18
- is the stack empty?
Example 2: language of balanced paranthesis
∑ = { (, ) }
= {Z0, ( }
Q = {q0,q1,q2}
Pop stack for
Grow stack matching symbols

(, Z0 / ( Z0
(, ( / ( ( ), ( / 

q0 q1 q2
, Z0 / Z0 ), ( /  , Z0 / Z0
, Z0 / Z0 Go to acceptance (by final state)
Switch to when you see the stack bottom symbol
(, ( / ( (
popping mode (, Z / ( Z
0 0

To allow adjacent
19
Example 2: language of balanced paranthesis
(another design)
∑ = { (, ) }
(,Z0 / ( Z0 = {Z0, ( }
(,( / ( ( Q = {q0,q1}
), ( / 

start ,Z0/ Z0
q0 q1
,Z0/ Z0

20
Example: L of balanced parenthesis
An equivalent PDA that
PDA that accepts by final state accepts by empty stack
(,Z0 / ( Z0
PF: PN:
(,Z0 / ( Z0 (, ( / ( (
(,( / ( ( ), ( / 
), ( /  ,Z0 / 
start ,Z0/ Z0 start
q0 q1 q0
,Z0/ Z0 ,Z0/ Z0

How will these two PDAs work on the input: ( ( ( ) ) ( ) ) ( ) 21


PDAs accepting by final state and empty stack are
equivalent
• PF <= PDA accepting by final state
– PF = (QF,∑, , δF,q0,Z0,F)
• PN <= PDA accepting by empty stack
– PN = (QN,∑, , δN,q0,Z0)
• Theorem:
– (PN==> PF) For every PN, there exists a PF s.t. L(PF)=L(PN)

– (PF==> PN) For every PF, there exists a PN s.t. L(PF)=L(PN)

23
How to convert an empty stack PDA into a final state PDA?
PN==> PF construction
• Whenever PN’s stack becomes empty, make PF go to a final state
without consuming any addition symbol. new state pf is needed.
• To detect empty stack in PN: PF pushes a new stack symbol X0 (not in 
of PN) initially before simulating PN
• New start state p0 in Pf that pushes Z0 onto TOS and goes to PN

PF: PN: , X0/ X0


, X0/Z0X0 , X0/ X0
New , X0/ X0
start p0 q0 … pf
, X0 / X0
, X0/ X0

PN PN = (QN,∑, , δN,q0,Z0)

PF = (QN U {p0,pf}, ∑,  U {X0}, δF, p0, X0, {pf}) 24


Example: Matching parenthesis “(” “)”
PN: ( {q0}, {(,)}, {Z0,Z1}, δN, q0, Z0 )
Pf: ( {p0,q0 ,pf}, {(,)}, {X0,Z0,Z1},
δf, p0, X0 , pf)
δN: δN(q0,(,Z0) = { (q0,Z1Z0) }
δN(q0,(,Z1) = { (q0, Z1Z1) }
δf: δf(p0, ,X0) = { (q0,Z0) }
δN(q0,),Z1) = { (q0, ) }
δf(q0,(,Z0) = { (q0,Z1 Z0) }
δN(q0, ,Z0) = { (q0, ) }
δf(q0,(,Z1) = { (q0, Z1Z1) }
δf(q0,),Z1) = { (q0, ) }
δf(q0, ,Z0) = { (q0, ) }
(,Z0 /Z1Z0 δf(p0, ,X0(,Z
) = /Z
{ (pZf, X0 ) }
0 1 0
(,Z1 /Z1Z1
(,Z1/Z1Z1
),Z1 / 
),Z1/ 
,Z0 / 
 ,Z0/ 
start start ,X0/Z0X0 ,X0/ X0
p0 q0 pf
q0

Accept by empty stack Accept by final state 25


How to convert an final state PDA into an empty stack PDA?
PF==> PN construction
• Main idea:
– Whenever PF reaches a final state, just make an  -transition into a new end state, clear
out the stack and accept
– Danger: What if PF design is such that it clears the stack midway without entering a final
state?
 to address this, add a new start symbol X0 (not in  of PF)

PN = (Q U {p0,pe}, ∑,  U {X0}, δN, p0, X0)

PN:
, X0/Z0X0 , any/  , any/ 
New p0 q0 , any/  p
start … e

, any/ 
PF
26
Equivalence of PDAs and CFGs

27
CFGs == PDAs ==> CFLs

PDA by PDA by

final state empty stack

?
CFG

28
This is same as: “implementing a CFG using a PDA”
Converting CFG to PDA
Main idea: The PDA simulates the leftmost derivation on a
given w, and upon consuming it fully it either arrives at
acceptance (by empty stack) or non-acceptance.

accept

OUTPUT
PDA
INPUT

w (acceptance by
empty stack)
reject
implements

CFG
29
Main idea: The PDA simulates the leftmost derivation on a given w, and
upon consuming it fully it either arrives at acceptance (by empty stack)
or non-acceptance.

Steps:
1. Push the right hand side of the production onto the stack, with
leftmost symbol at the stack top
2. If stack top is the leftmost variable, then replace it by all its
productions (each possible substitution will represent a distinct
path taken by the non-deterministic PDA)
3. If stack top has a terminal symbol, and if it matches with the next
symbol in the input string, then pop it
State is inconsequential (only one state is needed)

30
Formal construction of PDA from CFG
Note: Initial stack symbol (S)
• Given: G= (V,T,P,S) same as the start variable
in the grammar
• Output: PN = ({q}, T, V U T, δ, q, S)
• δ:
– For all A  V , add the following transition(s) in the
Before: PDA: After:
A • δ(q,  ,A) = { (q, ) | “A ==>”  P} 


Before: – For all a  T, add the following
After: a…
a
transition(s) in the PDA:
a pop
• δ(q,a,a)= { (q,  ) }


31
Example: CFG to PDA
1,1 / 
• G = ( {S,A}, {0,1}, P, S) 0,0 / 
• P: ,A / 01
,A / A1
– S ==> AS | 
,A / 0A1
– A ==> 0A1 | A1 | 01
,S / 
• PDA = ({q}, {0,1}, {0,1,A,S}, δ, q, S) ,S / AS
,S / S
• δ:
q
– δ(q,  , S) = { (q, AS), (q,  )}
– δ(q,  , A) = { (q,0A1), (q,A1), (q,01) }
– δ(q, 0, 0) = { (q,  ) }
– δ(q, 1, 1) = { (q,  ) }
How will this new PDA work?
Lets simulate string 0011
32
Simulating string 0011 on the new PDA

PDA (δ): Leftmost deriv.:
δ(q,  , S) = { (q, AS), (q,  )} 1,1 / 
0,0 / 
δ(q,  , A) = { (q,0A1), (q,A1), (q,01) } ,A / 01
S => AS
,A / A1 => 0A1S
δ(q, 0, 0) = { (q,  ) } ,A / 0A1
=> 0011S
δ(q, 1, 1) = { (q,  ) } ,S / 
,S / AS => 0011
,S / S
Stack moves (shows only the successful path): q

0 0
A A 1 1
A 1 1 1 1 1 Accept by
S S S S S S S S
empty stack
0 0 1 1 

S =>AS =>0A1S =>0011S => 0011


33
Example 2 L ={anbn | n≧1} is a context-free language .

L = L(G), G = (V, T, P, S) a CFG in Greiback normal form, where


S  aSB, S  aB,B  b.

Let M = ({q}, T, V, δ, q, S , {}), a PDA, where


δ(q, a, S) contains (q, SB), where SaSB is in P,

δ(q, a, S) contains (q, B), where SaB is in P,

δ(q, b, B) contains (q, ), where Bb is in P.


a a b b δ(q, a, S) := (q, SB) a a b b
S
q S q B
δ(q, a, S) := (q, B)

a a b b a a b b
δ(q, b, B) := (q, )
B
q q B
B

δ(q, b, B) := (q, )

a a b b

q
Converting a PDA into a CFG

• Main idea: Reverse engineer the productions from


transitions
If δ(q,a,Z) => (p, Y1Y2Y3…Yk):
1. State is changed from q to p;
2. Terminal a is consumed;
3. Stack top symbol Z is popped and replaced with a sequence of k
variables.
– Action: Create a grammar variable called “[qZp]”
which includes the following production:
– [qZp] => a[pY1q1] [q1Y2q2] [q2Y3q3]… [qk-1Ykqk]

– Proof discussion (in the book)


37
Example: Bracket matching
PDA to CFG
• To avoid confusion, we will use b=“(“ and e=“)”

PN: ( {q0}, {b,e}, {Z0,Z1}, δ, q0, Z0 ) 0. S => [q0Z0q0]


1. [q0Z0q0] => b [q0Z1q0] [q0Z0q0]
1. δ(q0,b,Z0) = { (q0,Z1Z0) } 2. [q0Z1q0] => b [q0Z1q0] [q0Z1q0]
2. δ(q0,b,Z1) = { (q0,Z1Z1) } 3. [q0Z1q0] => e
3. δ(q0,e,Z1) = { (q0,  ) } 4. [q0Z0q0] => 
4. δ(q0,  ,Z0) = { (q0,  ) }
Let A=[q0Z0q0] Simplifying,
If you were to directly write
Let B=[q0Z1q0]
a CFG:
0. S => b B S | 
0. S => A 1. B => b B B | e
S => b S e S | 
1. A => b B A
2. B => b B B
3. B => e 38
4. A => 
Two ways to build a CFG

Build a PDA Construct (indirect)


CFG from PDA

Derive CFG directly (direct)

Similarly… Two ways to build a PDA


Derive a CFG Construct
PDA from CFG (indirect)

Design a PDA directly (direct)

39
Deterministic PDAs

40
This PDA for Lwwr is non-deterministic

Grow stack
0, Z0/0Z0
Why
Whydoes
doesitit
1, Z0/1Z0 Pop stack for have
haveto
tobe
be
0, 0/00 matching symbols non-
non-
0, 1/01 deterministic?
deterministic?
1, 0/10 0, 0/ 
1, 1/11 1, 1/ 

q0 q1 q2
, Z0/Z0 , Z0/Z0
, 0/0
, 1/1 Accepts by final state
Switch to To
Toremove
removeguessing,
guessing,
popping mode impose
imposethe theuser
userto
to
insert
insertccininthe
the
middle
middle
41
Example shows that: Nondeterministic PDAs ≠ D-PDAs

D-PDA for Lwcwr = {wcwR | c is some special


symbol not in w}
Note:
Note:
••all
alltransitions
transitions
Grow stack have
0, Z0/0Z0
Pop stack for havebecome
become
matching symbols deterministic
deterministic
1, Z0/1Z0
0, 0/00
0, 1/01 0, 0/ 
1, 0/10 1, 1/ 
1, 1/11

q0 q1 q2
c, Z0/Z0 , Z0/Z0
c, 0/0
c, 1/1 Accepts by
Switch to final state
popping mode

42
Deterministic PDA: Definition

• A PDA is deterministic if and only if:


δ(q,a,X) has at most one member for any
a  ∑ U {}

 If δ(q,a,X) is non-empty for some a∑, then


δ(q, ,X) must be empty.

43
PDA vs DPDA vs Regular languages

Lwcwr Lwwr

D-PDA
Regular languages

non-deterministic PDA

44
Summary
• PDAs for CFLs and CFGs
– Non-deterministic
– Deterministic
• PDA acceptance types
1. By final state
2. By empty stack
• PDA
– IDs, Transition diagram
• Equivalence of CFG and PDA
– CFG => PDA construction
– PDA => CFG construction

45

You might also like