0% found this document useful (0 votes)
12 views

10 Pda

Uploaded by

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

10 Pda

Uploaded by

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

Pushdown Automata (PDA)

1
PDA - the automata for CFLs
 What is it?
 FA to Reg Lang, PDA is to CFL
 PDA == [  -NFA + “a stack” ]
 Why a stack?

Input -NFA Accept/reject


string

A stack filled with “stack symbols”


2
Pushdown Automata (PDA) - Definition

A PDA is a seven tuple P = (Q, , , , q0, F) where:


 Q is a finite set of states,

  is the input alphabet,

  is the stack alphabet,

 q0 in Q is the initial state,

 F  Q is a set of final states, and

  is the transition function

pop symbol state push symbol


: Q  (  {})   → subsets of Q  *
state input symbol
new state(s)
old state input symb. Stack top new Stack symbol(s).
δ : Q x ∑ x  => Q x *
δ : The Transition Function
δ(q,a,X) = {(p, α), …}
1. state transition from q to p
2. a is the next input symbol a X Y
3. X is the current stack top symbol q p
4. α is the replacement for X;
it is in * (a string of stack α=? Action
symbols)
i. Set α =  for: Pop(X) i) α= Pop(X)
ii. If α =X: stack top is ii) α=X Pop(X)
unchanged Push(X)
iii. If α =Z1Z2…Zk: X is
popped and is replaced by α iii) α=Z1Z2..Zk Pop(X)
in reverse order (i.e., Z1 will Push(Zk)
be the new stack top) Push(Zk-1)

Push(Z2)
Push(Z1)

4
The language of a PDA

 A PDA is nondeterministic
Multiple transitions on same pop/input allowed
 Transitions may but do not have to push or pop

The language of a PDA is the set of all strings in


* that can lead the PDA to an accepting state
Example
Let L= {wwR | w is in (0+1)*}
 CFG for L : S → 0S0 | 1S1 | 
 PDA for L :

 P := ( Q,∑, , δ,q0,Z0,F )

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

6
Initial state of the PDA:

Stack q0
top
PDA for wwR Z0

1. δ(q0,0, Z0)={(q0,0Z0)}
First symbol push on stack
2. δ(q0,1, Z0)={(q0,1Z0)}

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

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


Shrink the stack by popping matching
11. δ(q1,1, 1)={(q1, )}
symbols (wR-part)
12. δ(q1, , Z0)={(q2, Z0)} Enter acceptance state
7
PDA as a state diagram

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

Next Current Stack


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

a, X / Y Next
qi qj state

8
PDA for L = wwR : Transition Diagram

Grow stack ∑ = {0, 1}


0, Z0/0Z0 = {Z0, 0, 1}
1, Z0/1Z0 Pop stack for Q = {q0,q1,q2}
0, 0/00
0, 1/01
matching symbols
1, 0/10 0, 0/ 
1, 1/11 1, 1/ 

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

This would be a non-deterministic PDA 9


Example: language of balanced
paranthesis
Pop stack for ∑ = { (, ) }
matching symbols = {Z0, ( }
Grow stack
Q = {q0,q1,q2}
(, Z0 / ( Z0
(, ( / ( ( ), ( / 

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

To allow adjacent
blocks of nested paranthesis 10
Example: language of balanced
paranthesis (another design)

∑ = { (, ) }
(,Z0 / ( Z0 = {Z0, ( }
(,( / ( (
), ( / 
Q = {q0,q1}

start ,Z0/ Z0
q0 q1

11
Building a PDA
L = {0n1n: n ≥ 1}
state control
read 0 We remember each 0
push x by pushing x onto the stack
read 1
pop x When we see a 1, we
read 1 pop an x from the stack
pop x
accept We want to accept when
we hit the stack bottom
We’ll use Z0to mark bottom
A PDA in action
state control L = {0n1n: n ≥ 1}
input
read 0
push x 0 0 0 1 1 1
read 1
pop x …
Z0 x x x
read 1
pop x stack
accept
Notation for PDAs
L = {0n1n: n ≥ 1}
read 0 0,Z0/XZ0
q0 0,X/XX
push x
read 1
pop x 1, X / 
read 1
pop x q1 1, X / 
accept when i/p exhausted &
initial stack symbol is exposed
, Z0/Z0
q2
read, pop / push
Example L = {0n1n: n ≥ 1}
 = {0, 1}
0, Z0 / XZ0
q0 0, X/XX  = {Z0, x}
(q0, 0, Z0) = {(q0, XZ0)}
1, X / 
(q0, 0, X) = {(q0, XX)}
q1 1, X / 
(q0, 1, x) = {(q1, )}
(q1, 1, X) = {(q1, )}
, Z0/Z0 (q1, , Z0) = {(q2, Z0)}
q2

: Q  (  {})   → subsets of Q  *
state input symbol pop symbol state push symbol
Example
L = {w#wR: w  {0, 1}*}  = {0, 1, #}
#, 0#0, 01#10  L  = {0, 1, $}
, 01#1, 0##0  L
,  / $
q0 #, 0 / 0 q1 , $ / $ q2
#, 1 / 1
0, $ / 0$ #, $ / $
1, $ / 1$
0, 0 / 00
0, 0 / 
0, 1 / 01 1, 1 / 
1, 0 / 10 read wR from stack &
1, 1 / 11
match with input
write w on stack
Example
L = {0n1m0m1n | n  0, m  0}
 = {0, 1}
0, 0/00
0, Z/0Z 1, 1/11
 = {0, 1, Z}

q1 , Z/Z q2
1, Z/1Z input: 0n 1m 0m 1n
1, 0/10 , Z/Z
0, 1/ stack: 0n 1m

q5 q4 , Z/Z q3
, Z/Z 1, 0/

1, 0/ 0, 1/
Exercise
L = {w: w has same number 0s and 1s}  = {0, 1}
Strategy: Stack keeps track of excess of 0s or 1s
If at the end, stack is empty, number is equal
PDA’s Instantaneous Description (ID)
A PDA has a configuration at any given instance:
(q,w,α)
 q - current state
 w - remainder of the input (i.e., unconsumed part)
 α - current stack contents as a string from top to
bottom of stack
If δ(q,a, X)={(p, α)} is a transition, then the following are also true:
 (q, a, X ) |--- (p,, α)

 (q, aw, Xβ ) |--- (p,w, αβ)

|--- sign is called a “turnstile notation” and represents


one move
|---* sign represents a sequence of moves
19
How does the PDA for wwR work on input
“1111”?
All moves made by the non-deterministic PDA
(q0,1111,Z0)

(q1,1111,Z0) Path dies…


(q0,111,1Z0)

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

(q0,1,111Z0) (q1,11,11Z0)

(q0,,1111Z0) (q1,1,111Z0) (q1,1,1Z0) Acceptance by


final state:

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


Path dies… AND
Path dies…
(q2, ,Z0) final state
20
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: Checklist:
 {w | (q0,w,Z0) |---* (q,, A) }, s.t., q  F - 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:
- input exhausted?
need any final state specified in the design?
- is the stack empty? 22
Example: L of balanced parenthesis
An equivalent PDA that
PDA that accepts by final state accepts by empty stack
(,Z0 / ( Z0
PF: (,Z0 / ( Z0
PN: (, ( / ( (
), ( / 
(,( / ( (
,Z0 / 
), ( / 

start ,Z0/ Z0 start


q0 q1 q0

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


23
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)

25
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
 To detect empty stack in PN: PF pushes a new stack
symbol X0 (not in  of PN) initially before simultating
PN

PF: PN: , X0/ X0


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

, X0/ X0

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


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) } δf: δf(p0, ,X0) = { (q0,Z0) }


δN(q0,(,Z1) = { (q0, Z1Z1) } δf(q0,(,Z0) = { (q0,Z1 Z0) }
δN(q0,),Z1) = { (q0, ) }
δf(q0,(,Z1) = { (q0, Z1Z1) }
δf(q0,),Z1) = { (q0, ) }
δN(q0, ,Z0) = { (q0, ) } δf(q0, ,Z0) = { (q0, ) }
δf(q0, ,X0) = { (pf, X0 ) }

(,Z0 /Z1Z0 (,Z0/Z1Z0


(,Z1 /Z1Z1 (,Z1/Z1Z1
),Z1 /  ),Z1/ 
,Z0 /   ,Z0/ 

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

Accept by empty stack Accept by final state 27


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
start p0 q0 , any/  pe

, any/ 
PF

28
Equivalence of PDAs and CFGs

29
CFGs == PDAs ==> CFLs

PDA by PDA by

final state empty stack

?
CFG

30
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

31
This is same as: “implementing a CFG using a PDA”

Converting a CFG into a 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.
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)

32
Formal construction of PDA from
CFG Note: Initial stack symbol (S)
same as the start variable
in the grammar
 Given: G= (N,,P,S)
 Output: PN = ({q}, , N U , δ, q, S)
 δ:
For all A  N , add the following
Before: After:

A 
transition(s) in the PDA:


 δ(q,  ,A) = { (q, ) | A →   P}
Before:
 For all a  , add the following After: a…
a transition(s) in the PDA: a pop

δ(q,a,a) = { (q,  ) }



33
Example: CFG to PDA
1,1 / 
 G = ( {S,A}, {0,1}, P, S) 0,0 / 
,A / 01
,A / A1
 P: ,A / 0A1
,S / 
 S → AS |  ,S / AS

 A → 0A1 | A1 | 01 ,S / S
q
 PDA = ({q}, {0,1}, {0,1,A,S}, δ, q, S)
 δ:
 δ(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
34
Simulating string 0011 on the new PDA …
Leftmost deriv.:
1,1 / 
0,0 / 
PDA (δ): ,A / 01 S => AS
,A / A1
δ(q,  , S) = { (q, AS), (q,  )} => 0A1S
,A / 0A1
δ(q,  , A) = { (q,0A1), (q,A1), (q,01) } ,S /  => 0011S
δ(q, 0, 0) = { (q,  ) } ,S / AS
δ(q, 1, 1) = { (q,  ) } => 0011

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


35
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
 To avoid confusion, we will use b=“(“ and e=“)”
PN: ( {q0}, {b,e}, {Z0,Z1}, δ, q0, Z0 )
0. S => [q0Z0q0]
1. δ(q0,b,Z0) = { (q0,Z1Z0) } 1. [q0Z0q0] => b [q0Z1q0] [q0Z0q0]
2. δ(q0,b,Z1) = { (q0,Z1Z1) } 2. [q0Z1q0] => b [q0Z1q0] [q0Z1q0]
3. [q0Z1q0] => e
3. δ(q0,e,Z1) = { (q0,  ) }
4. [q0Z0q0] => 
4. δ(q0,  ,Z0) = { (q0,  ) }
Let A=[q0Z0q0] Simplifying,
Let B=[q0Z1q0]
If you were to directly write a CFG: 0. S => b B S | 
0. S => A
1. B => b B B | e
1. A => b B A
S => b S e S |  2. B => b B B
3. B => e
4. A => 

38
Deterministic PDAs

39
This PDA for wwR is non-deterministic

Grow stack Why does it have


0, Z0/0Z0
1, Z0/1Z0 Pop stack for to be non-
0, 0/00
matching symbols deterministic?
0, 1/01
1, 0/10 0, 0/ 
1, 1/ 
1, 1/11

q0 q1 q2
, Z0/Z0 , Z0/Z0
, 0/0
, 1/1 Accepts by final state
Switch to To remove
popping mode guessing,
impose the user
to insert c in the
middle 40
Example shows that: Nondeterministic PDAs ≠ D-PDAs

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


symbol not in w}
Note:
Grow stack • all transitions have
Pop stack for
0, Z0/0Z0 become deterministic
1, Z0/1Z0 matching symbols
0, 0/00
0, 1/01 0, 0/ 
1, 1/ 
1, 0/10
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

41
Deterministic PDA: Definition
 A PDA is deterministic if and only if:
1. δ(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.

42
PDA vs DPDA vs Regular
languages
Lwcwr Lwwr

Regular languages D-PDA

non-deterministic PDA

43
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 44

You might also like