equivalence of ndpa and cfg - national chiao tung …wgtzeng/courses...equivalence of ndpa and cfg...

Post on 02-Apr-2020

9 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

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: SaSA|a, AbB, Bb

– w=aabb

• Idea: use the stack to hold the derivation process, and match the input with the derivation

2

• SaSA|a, AbB, Bb, w=aabb

3

S aSA

aaA

aabB

aabb

zSz

SAz

Az

Bz z

a

a

b b

w=aabb is accepted by PDA

Convert cfg to pda

• Construct M

– Initial: (q0, , z)={ (q1, Sz) }

– Process the input:

• (q1, a, S)={ (q1, SA), (q1, )}: for SaSA|a

• (q1, b, A)={ (q1, B) }: for AbB

• (q1, b, B)={ (q1, )}: for Bb

– In the end: (q1, , z)={ (q2, )}

4

• P: SaSA|a, AbB, Bb

• Run M on the input aabbb

• See the relation between

5

S * aabb *(q0, aabb, z)

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.

7

A

z

BC

z

z

DEC

z

H

z

qi qj

a1 a2am. . . C

z

qk

. . .FG

z

• 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

8

A

z

z

qi

a

qj

• If ci=(qj, BC), add production (qiAqk)a(qjBql)(qlCqk), for all qk, qlQ

9

A

z

BC

z

z

DEC

z

H

z

qiqk

a. . . C

z

ql

. . .FG

z

qj

• 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

12

(q0, a, z)={ (q0, Az) }

(q3, , z)={ (q0, Az) }

• 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

top related