Professional Documents
Culture Documents
(
)
Reading: Chapter 6
What is?
FA to Reg Lang,
Lang PDA is to CFL
-NFA
Accept/reject
Q:
:
:
:
q0:
Z0:
F:
old state
: Q x x => Q x
Y=?
Action
i)
Y=
Pop(X)
ii)
Y=X
Pop(X)
Push(X)
iii)
Y=Z
Y
Z1Z2..Z
Zk
Pop(X)
Push(Zk)
Push(Zk-1)
Push(Z2)
Push(Z1)
4
Example
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})
3.
4.
5.
6.
(q0,0, Z0)={(q0,0Z0)}
(q0,1, Z0)={(q0,1Z0)}
(q0,0,
0 0)={(q
0) {(q0,00)}
00)}
(q0,0, 1)={(q0,01)}
(q0,1, 0)={(q0,10)}
(q0,1, 1)={(q0,11)}
Stackk
St
top
Z0
q0
11.
(q1,0, 0)={(q1, )}
(q1,1, 1)={(q1, )}
12.
7.
8.
9
9.
10.
Current
state
Next
input
symbol
Current Stack
stack
Top
top
Replacement
(w/ string Y)
a, X / Y
qi
qj
Next
state
Grow stack
0, Z0/0Z0
1, Z0/1Z0
0, 0/00
0, 1/01
1, 0/10
1, 1/11
, Z0/Z0
q0
, Z0/Z0
, 0/0
, 1/1
q1
, Z0/Z0
q2
G to acceptance
Go
Switch to
popping mode
This would be a non-deterministic PDA
Example
p 2: language
g g of
balanced paranthesis
Pop stack for
matching symbols
Grow stack
(, Z0 / ( Z0
(, ( / ( (
, Z0 / Z0
q0
= { (, ) }
= {Z0, ( }
Q = {q0,q
q1,q
q2}
), ( /
), ( /
q1
, Z0 / Z0
Switch to
popping mode
(, ( / ( (
(, Z0 / ( Z0
, Z0 / Z0
q2
Go to acceptance (by
G
(b fi
finall state))
when you see the stack bottom symbo
To allow adjacent
blocks of nested paranthesis
(,Z0 / ( Z0
(,( / ( (
), ( /
start
,Z0/ Z0
q0
,Z0/ Z0
q1
10
PDAs 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
off stack
t k
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)
(
)
(q1,1111,Z0)
(q1,111,1Z
111 1Z0)
Path dies
Path dies
dies
(q1,11,11Z0)
(q1,1,111Z0)
(q1,1,1Z0)
Acceptance by
final state:
( 1, ,1111Z
(q
1111Z0)
(q1, ,11Z
11Z0)
(q1, ,Z
Z0)
Path dies
Path dies
= empty
t input
i
t
AND
final state
(q0,,1111Z0)
(q2, ,Z0)
12
13
Acceptance by
- input exhausted?
- in a final state?
{{w | (q0,,w,Z
, 0) ||---* (q, ,, )) }, for anyy q Q
Q.
Checklist:
Q) Does a PDA that accepts by empty stack
need any final state specified in the design? - input exhausted?
Example:
p L of balanced
parenthesis
PDA that accepts by final state
PF:
start
Z0/ Z0
,Z
q0
((,Z
Z0 / ( Z0
(, ( / ( (
), ( /
,Z0 /
PN:
(,Z0 / ( Z0
(,( / ( (
), ( /
,Z0/ Z0
start
q1
Z0/ Z0
,Z
q0
(if-part)
(if
part) If the string is of the form wwR then there
exists a sequence of IDs that leads to a final state:
(q0,wwR,Z0) |---* (q0,wR,wZ0) |---* (q1,wR,wZ0) |---*
(q1, ,Z
Z0) |--| * (q2, ,Z
Z0)
(only-if part)
PF = (QF,, , F,q0,Z0,F)
PN = (QN,, , N,q0,Z0)
Th
Theorem:
17
PN==> PF construction
PF:
New
start
, X0 / X0
, X0/ X0
, X0/Z0X0
p0
q0
, X0/
, X0/ X0
, X0/ X0
pf
X0
18
Pf:
N :
N(q0,(,Z0) = { (q0,Z1Z0) }
N(q0,(,Z
( Z1) = { (q0, Z1Z1) }
f:
N(q0,),Z1) = { (q0, ) }
(,Z0 /Z1Z0
(,Z1 /Z1Z1
),Z1 /
,Z0 /
start
(,Z0/Z1Z0
(,Z1/Z1Z1
),Z1/
,Z
Z0/
start
q0
p0
,X /Z X
0
q0
,X / X
0
pf
19
PF==> PN construction
Main idea:
PN:
New
start
, X0/Z0X0
p0
q0
, any/
, any/
, any/
pe
, any/
PF
20
21
PDA by
empty stack
?
CFG
22
PDA
(
(acceptance
by empty
stack)
accept
reject
OUTP
PUT
INPU
UT
implements
CFG
23
Steps:
1.
2.
3.
Push the right hand side of the production onto the stack,
with leftmost symbol at the stack top
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
non-deterministic
deterministic PDA)
If stack top has a terminal symbol, and if it matches with the
next symbol in the input string, then pop it
S
State
iis iinconsequential
i l ((only
l one state iis needed)
d d)
24
Before:
Given: G= (V,T,P,S)
Output: PN = ({q}
({q}, T
T, V U T
T, ,
q,
q S)
:
Before:
a
(q ,A)
(q,
A) = { (q
(q, ) | A
A ==>
==> P}
(q,a,a)= { (q, ) }
After:
a pop
After:
25
G = ( {S,A}, {0,1}, P, S)
P:
1,1 /
0,0 /
,A / 01
,A / A1
,A / 0A1
S/
,S
,S / AS
S ==> AS |
A ==> 0A1 | A1 | 01
,S / S
PDA = ({q},
({ } {0,1},
{0 1} {0
{0,1,A,S},
1 A S} ,
q, S)
:
(q, , S) = { (q
(q
(q, AS)
AS), (q
(q, )}
(q, , A) = { (q,0A1), (q,A1), (q,01) }
(q, 0, 0) = { (q, ) }
(q, 1, 1) = { (q, ) } How will this new PDA work?
Simulating
g string
g 0011 on the
new PDA
Leftmost deriv.:
1,1
1
1/
0,0 /
,A / 01
,A / A1
,A / 0A1
,S
, /
,S / AS
PDA ():
(q, , S) = { (q, AS), (q, )}
(q, , A) = { (q,0A1), (q,A1), (q,01) }
(q, 0, 0) = { (q, ) }
(q, 1, 1) = { (q, ) }
,S / S
A
S
0
A
1
S
A
1
S
0
1
1
S
0
S
=>AS =>0A1S
=>0011S
S => AS
=> 0A1S
=> 0011S
=> 0011
1
1
S
1
S
Accept by
empty stack
=> 0011
27
(only-if part)
(if part)
If (q,
( wx, S) |-| * (q,x,B)
(
B) then
th S =>*lm wB
B
28
1.
2.
3.
Action: Create a g
grammar variable called
[qZp] which includes the following
production:
29
PN:
1.
2.
(q0,b,Z0) = { (q0,Z1Z0) }
(q0,b,Z1) = { (q0,Z1Z1) }
3.
4.
(q0,e,Z1) = { (q0, ) }
(q
(q0, ,,Z0) = { (q0, ) }
0.
1
1.
2.
3.
S => [q0Z0q0]
[q0Z0q0] => b [q0Z1q0] [q0Z0q0]
[q0Z1q0] => b [q0Z1q0] [q0Z1q0]
[q0Z1q0] => e
4.
[q0Z0q0] =>
Let A=[q0Z0q0]
Let B=[q0Z1q0]
If you were to directly write a CFG:
S => b S e S |
0.
1.
2.
3.
4.
S => A
A => b B A
B => b B B
B => e
Simplifying,
0.
1.
S => b B S |
B => b B B | e
A =>
30
Construct
CFG from PDA
(indirect)
(direct)
(indirect)
(direct)
31
Deterministic PDAs
32
q0
Switch to
popping mode
q1
, Z0/Z0
q2
A
Accepts
b
by fi
finall state
To remove
guessing,
impose the user
to insert c in the
middle
33
0, Z0/0Z0
1, Z0/1Z0
0, 0/00
0, 1/01
1, 0/10
1, 1/11
q0
0, 0/
1, 1/
c, Z0/Z0
c, 0/0
c, 1/1
Switch to
popping mode
q1
, Z0/Z0
q2
Accepts b
A
by
final state
34
(q,a,X)
(q
a X) has at most one member for any
a U {}
35
Regular languages
Lwwr
D-PDA
non-deterministic PDA
36
Summary
PDAs for CFLs and CFGs
Non-deterministic
Deterministic
1.
2.
By final state
By empty stack
PDA
37