Pushdown Automata (PDA) : Reading: Chapter 6
Pushdown Automata (PDA) : Reading: Chapter 6
Pushdown Automata (PDA) : Reading: Chapter 6
Reading: Chapter 6
1
PDA - the automata for CFLs
PDA == [ -NFA + “a stack” ]
Why a stack?
2
Pushdown Automata -
Definition
A PDA P := ( Q,∑,, δ,q0,Z0,F ):
Q: states of the -NFA
∑: input alphabet
: stack symbols
δ: transition function
q0: start state
Z0: Initial stack top symbol
F: Final/accepting states
3
old state input symb. Stack top new state(s) new Stack top(s)
δ : Q x ∑ x => Q x
4
PDA as a state diagram
δ(qi,a, X)={(qj,Y)}
a, X / Y Next
qi qj state
5
Example
Let Lwwr = {wwR | w is in (0+1)*}
CFG for L
wwr : S==> 0S0 | 1S1 |
PDA for L
wwr :
P := ( Q,∑, , δ,q0,Z0,F )
6
PDA for Lwwr: Transition Diagram
q0 q1 q2
, Z0/Z0
, Z0/Z0 , Z0/Z0
, 0/0
, 1/1 Go to acceptance
Switch to
popping mode
Stack q0
PDA for Lwwr top 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)}
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)}
Switch to popping mode, nondeterministically
8. δ(q0, , 1)={(q1, 1)}
(boundary between w and wR)
9. δ(q0, , Z0)={(q1, Z0)}
q0 q1 q2
, Z0 / Z0 ), ( / , 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 9
Example 2: language of balanced
paranthesis (another design)
∑ = { (, ) }
(,Z0 / ( Z0 G = {Z0, ( }
(,( / ( (
), ( /
Q = {q0,q1}
start ,Z0/ Z0
q0 q1
,Z0/ Z0
10
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)
(q0,1,111Z0) (q1,11,11Z0)
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?
,Z0/ Z0 start
start
q0 q1 q0
,Z0/ Z0 ,Z0/ Z0
(PF==> PN) For every PF, there exists a PN s.t. L(PF)=L(PN)
15
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: P N: , X0/ X0
, X0/Z0X0 , X0/ X0
New
start p0 q0 , X0/ X0 pf
…
, X0 / X0
, X0/ X0
start
q0
start
,X /Z X
0 0 0
,X / X
0 0
p0 q0 pf
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
18
Equivalence of PDAs and
CFGs
19
CFGs == PDAs ==> CFLs
PDA by PDA by
≡
final state empty stack
?
CFG
20
This is same as: “implementing a CFG using a PDA”
accept
OUTPUT
PDA
INPUT
w (acceptance
by empty
stack) reject
implements
CFG
21
This is same as: “implementing a CFG using a PDA”
22
Formal construction of PDA
from CFG Note: Initial stack symbol (S)
same as the start variable
in the grammar
Given: G= (V,T,P,S)
Output: PN = ({q}, T, V U T, δ, q, S)
δ:
Before: After:
For all A V :
A
δ(q, ,A) = { (q, ) | “A ==>” P}
…
…
Before:
For all a T: After: a…
a
δ(q,a,a)= { (q, ) } a pop
…
…
23
Example: CFG to PDA
1,1 /
G = ( {S,A}, {0,1}, P, S) 0,0 /
,A / 01
P: ,A / A1
,A / 0A1
S ==> AS | ,S /
,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
24
Simulating string 0011 on the
new PDA … Leftmost deriv.:
1,1 /
0,0 /
PDA (δ): ,A / 01
S => AS
δ(q, , S) = { (q, AS), (q, )} ,A / A1 => 0A1S
δ(q, , A) = { (q,0A1), (q,A1), (q,01) } ,A / 0A1
=> 0011S
δ(q, 0, 0) = { (q, ) } ,S /
δ(q, 1, 1) = { (q, ) } ,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
26
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. A => b B A 1. B => b B B | e
S => b S e S | 2. B => b B B
3. B => e
4. A =>
27
Deterministic PDAs
28
Deterministic PDA: Definition
A PDA is deterministic if and only if:
1. δ(q,a,X) has at most one member for any
a ∑ U {}
29
Initial state of the PDA:
Stack q0
PDA for Lwwr top 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)}
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)}
Switch to popping mode, nondeterministically
8. δ(q0, , 1)={(q1, 1)}
(boundary between w and wR)
9. δ(q0, , Z0)={(q1, Z0)}
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 31
Example shows that: Nondeterministic PDAs ≠ D-PDAs
q0 q1 q2
c, Z0/Z0 , Z0/Z0
c, 0/0
c, 1/1 Accepts by
Switch to final state
popping mode
32
PDA vs DPDA vs Regular
languages
Lwcwr Lwwr
non-deterministic PDA
33
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
34