Equivalence

Equivalence of ndpa and cfg
• For any npda M, there is a cfg G such that L(G)=L(M)
• For any cfg G, there is an npda M such that L(M)=L(G)
1
Convert cfg to pda
• Example
– CFG in Greibach normal form
– Productions in P: SaSA|a, AbB, Bb
– w=aabb
• Idea: use the stack to hold the derivation process,
and match the input with the derivation
2
• SaSA|a, AbB, Bb, w=aabb
S  aSA
w=aabb is accepted by PDA
 aaA
 aabB

z
S
z
a
S
A
z
a
 aabb
A
z
b
B
z

b
z
3
Convert cfg to pda
• Construct M
– Initial: (q0, , z)={ (q1, Sz) }
– Process the input:
• (q1, a, S)={ (q1, SA), (q1, )}: for SaSA|a
• (q1, b, A)={ (q1, B) }: for AbB
• (q1, b, B)={ (q1, )}: for Bb
– In the end: (q1, , z)={ (q2, )}
4
• P: SaSA|a, AbB, Bb
• Run M on the input aabbb
• See the relation between
S * aabb
*(q0, aabb, z)
5
Convert pda to cfg
• Idea: the grammar simulates the move of pda
• Assumptions for an npda
– Only one final state qf
– All transitions must have form, a{},
(qi, a, A)={ c1, c2, …, cn}, where ci=(qj, ) or ci=(qi, BC)
6
• Simulation
– each variable is of form (qiAqj) -- generating string w
– corresponding move in pda
•
•
•
•
•
Starting at state qi
Top stack symbol is A
Read in string w=a1a2…am
Ending at state qj
Stack symbol is popped out.
qi
A

z
qj
qk
a1
B
C

z
a2
D
E
C

z
. . .
C

z
F
G

z
. . .
H

z
am

z
7
• The construction
– Start variable: S=(q0zqf)
– For transitions a{},
(qi, a, A)={ c1, c2, …, cn}, where ci=(qj, ) or ci=(qi, BC)
• If ci=(qj, ), add production (qiAqj)a
qi
A

z
qj
a

z
8
• If ci=(qj, BC), add production (qiAqk)a(qjBql)(qlCqk),
for all qk, qlQ
qj
qi
A

z
a
B
C

z
qk
ql
D
E
C

z
. . .
C

z
F
G

z
. . .
H

z

z
9
• Example
–
–
–
–
(q0, a, z)={ (q0, Az) }
(q0, a, A)={ (q0, A) }
(q0, b, A)={ (q1, ) }
(q1, , z)={ (q2, ) }
• Converted to satisfy the requirements
–
–
–
–
–
(q0, a, z)={ (q0, Az) }
(q3, , z)={ (q0, Az) }
(q0, a, A)={ (q3, ) }
(q0, b, A)={ (q1, ) }
(q1, , z)={ (q2, ) }
10
• Work
11
(q0, a, z)={ (q0, Az) }
(q3, , z)={ (q0, Az) }
12
• The final result
13
Comments on dpda and dcfl
• For efficient parsing, we need “deterministic
cfl”.
• The corresponding deterministic pda
– (q, a, b) contains one element
– If (q, , b) is not empty, (q, c, b) is empty for
c
14