14
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

Equivalence of ndpa and cfg - National Chiao Tung …wgtzeng/courses...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

  • Upload
    others

  • View
    8

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Equivalence of ndpa and cfg - National Chiao Tung …wgtzeng/courses...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

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

Page 2: Equivalence of ndpa and cfg - National Chiao Tung …wgtzeng/courses...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

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

Page 3: Equivalence of ndpa and cfg - National Chiao Tung …wgtzeng/courses...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

• 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

Page 4: Equivalence of ndpa and cfg - National Chiao Tung …wgtzeng/courses...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

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

Page 5: Equivalence of ndpa and cfg - National Chiao Tung …wgtzeng/courses...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

• P: SaSA|a, AbB, Bb

• Run M on the input aabbb

• See the relation between

5

S * aabb *(q0, aabb, z)

Page 6: Equivalence of ndpa and cfg - National Chiao Tung …wgtzeng/courses...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

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

Page 7: Equivalence of ndpa and cfg - National Chiao Tung …wgtzeng/courses...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

• 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

Page 8: Equivalence of ndpa and cfg - National Chiao Tung …wgtzeng/courses...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

• 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

Page 9: Equivalence of ndpa and cfg - National Chiao Tung …wgtzeng/courses...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

• 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

Page 10: Equivalence of ndpa and cfg - National Chiao Tung …wgtzeng/courses...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

• 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

Page 11: Equivalence of ndpa and cfg - National Chiao Tung …wgtzeng/courses...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

• Work

11

Page 12: Equivalence of ndpa and cfg - National Chiao Tung …wgtzeng/courses...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

12

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

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

Page 13: Equivalence of ndpa and cfg - National Chiao Tung …wgtzeng/courses...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

• The final result

13

Page 14: Equivalence of ndpa and cfg - National Chiao Tung …wgtzeng/courses...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

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