19
1 Lecture 35 • CFG --> PDA construction – Shows that for any CFL L, there exists a PDA M such that L(M) = L – The reverse is true as well, but we do not prove that here

Lecture 35

Embed Size (px)

DESCRIPTION

Lecture 35. CFG --> PDA construction Shows that for any CFL L, there exists a PDA M such that L(M) = L The reverse is true as well, but we do not prove that here. CFL subset LPDA. Let L be an arbitrary CFL Let G be the CFG such that L(G) = L G exists by definition of L is CF - PowerPoint PPT Presentation

Citation preview

Page 1: Lecture 35

1

Lecture 35

• CFG --> PDA construction– Shows that for any CFL L, there exists a PDA

M such that L(M) = L– The reverse is true as well, but we do not prove

that here

Page 2: Lecture 35

2

CFL subset LPDA

• Let L be an arbitrary CFL

• Let G be the CFG such that L(G) = L– G exists by definition of L is CF

• Construct a PDA M such that L(M) = L– M is constructed from CFG G

• Argue L(M) = L

• There exists a PDA M such that L(M) = L

• L is in LPDA– By definition of L in LPDA

Page 3: Lecture 35

3

Visualization

CFLLPDA

CFG’s PDA’s

L L

G M

•Let L be an arbitrary CFL•Let G be the CFG such that L(G) = L

•G exists by definition of L is CF•Construct a PDA M such that L(M) = L

•M is constructed from CFG G•Argue L(M) = L•There exists a PDA M such that L(M) = L•L is in LPDA

•By definition of L in LPDA

Page 4: Lecture 35

4

Algorithm Specification

• Input– CFG G

• Output– PDA M such that L(M) = L(G)

CFG G PDA MA

Page 5: Lecture 35

5

Construction Idea

• The basic idea is to have a 2-phase PDA– Phase 1:

• Derive all strings in L(G) on the stack nondeterministically

• Do not process any input while we are deriving the string on the stack

– Phase 2: • Match the input string against the derived string on the stack

– This is a deterministic process

• Move to an accepting state only when the stack is empty

Page 6: Lecture 35

6

Illustration• Input Grammar G

– V = {S}– = {a,b}– S = S– P:

S --> aSb |

• What is L(G)?– {anbn | n >= 0}

1. Derive all strings in L(G) on the stack2. Match the derived string against input

(q0, aabb, Z)/* put S on stack */

(q1, aabb, SZ)/* derive aabb on stack */

(q1, aabb, aSbZ)(q1, aabb, aaSbbZ)(q1, aabb, aabbZ)

/* match stack vs input */(q2, aabb, aabbZ)(q2, abb, abbZ)(q2, bb, bbZ)(q2, b, bZ)(q2,, Z)

(q3, , Z)

Illustration of how the PDA might work,though not completely accurate.

Page 7: Lecture 35

7

Difficulty 1. Derive all strings in L(G) on the stack2. Match the derived string against input

(q0, aabb, Z)/* put S on stack */

(q1, aabb, SZ)/* derive aabb on stack */

(q1, aabb, aSbZ)(q1, aabb, aaSbbZ)(q1, aabb, aabbZ)

/* match stack vs input */(q2, aabb, aabbZ)(q2, abb, abbZ)(q2, bb, bbZ)(q2, b, bZ)(q2,, Z)

(q3, , Z)

What is wrong with the computation graph on the left?

Page 8: Lecture 35

8

Construction• Input Grammar

– G=(V,, S, P)

• Output PDA – M=(Q, , , q0, Z, F, )

– Q = {q0, q1, q2}

– = – = V union union {Z}

– Z = Z

– q0 = q0

– F = {q2}

• :– (q0, , Z) = (q1, SZ)

– (q1, , Z) = (q2, Z)

– For all productions A --> • q1, , A) = (q1, )

– For all a in • q1, a, a) = (q1, )

Page 9: Lecture 35

9

Examples

Page 10: Lecture 35

10

Palindromes• PALG:

– V = {S}

– = {a,b}

– S = S

– P:• S --> aSa | bSb | a | b |

• Output PDA M=(Q,,q0,Z,F,)

– Q = {q0, q1, q2}

– = {a,b,S,Z}

– q0 = q0

– Z = Z

– F = {q2}

• :

– (q0, , Z) = (q1, SZ)

– (q1, , Z) = (q2, Z)

– Production Transitions• q1, , S) = (q1, aSa)

• q1, , S) = (q1, bSb)

• q1, , S) = (q1, a)

• q1, , S) = (q1, b)

• q1, , S) = (q1, )

– Matching transitions• q1, a, a) = (q1, )

• q1, b, b) = (q1, )

Page 11: Lecture 35

11

Palindrome Transition Table

Transition Current Input Top of Next StackNumber State Symbol Stack State Update--------------------------------------------------------------------------------- 1 q0 Z q1 SZ 2 q1 Z q2 Z 3 q1 S q1 aSa 4 q1 S q1 bSb 5 q1 S q1 a 6 q1 S q1 b 7 q1 S q1 8 q1 a a q1 9 q1 b b q1

Page 12: Lecture 35

12

Partial Computation Graph(q0, aba, Z) (q1, aba, SZ) (q1, aba, aSaZ) (other branches not shown)(q1, ba, SaZ)(q1, ba, baZ) (other branches not shown)(q1, a, aZ)(q1, , Z)(q2, , Z)

On your own, draw computation trees for other stringsnot in the language and see that they are not accepted.

Page 13: Lecture 35

13

{anbn | n >= 0}• Grammar G:

– V = {S}

– = {a,b}

– S = S

– P:• S --> aSb |

• Output PDA M=(Q,,q0,Z,F,)

– Q = {q0, q1, q2}

– = {a,b,S,Z}

– q0 = q0

– Z = Z

– F = {q2}

• :

– (q0, , Z) = (q1, SZ)

– (q1, , Z) = (q2, Z)

– Production Transitions• q1, , S) = (q1, aSb)

• q1, , S) = (q1, )

– Matching transitions• q1, a, a) = (q1, )

• q1, b, b) = (q1, )

Page 14: Lecture 35

14

{anbn | n >= 0} Transition Table

Transition Current Input Top of Next StackNumber State Symbol Stack State Update--------------------------------------------------------------------------------- 1 q0 Z q1 SZ 2 q1 Z q2 Z 3 q1 S q1 aSb 4 q1 S q1 5 q1 a a q1 6 q1 b b q1

Page 15: Lecture 35

15

Partial Computation Graph

(q0, aabb, Z) (q1, aabb, SZ) (q1, aabb, aSbZ) (other branch not shown)(q1, abb, SbZ)(q1, abb, aSbbZ) (other branch not shown)(q1, bb, SbbZ)(q1, bb, bbZ) (other branch not shown)(q1, b, bZ)(q1, , Z) (q2, , Z)

Page 16: Lecture 35

16

{aibj | i = j or i = 2j}• Grammar G:

– V = {S,T,U}

– = {a,b}

– S = S

– P:• S --> T | U

• T --> aTb | • U --> aaUb |

• Output PDA M=(Q,,q0,Z,F,)

– Q = {q0, q1, q2}

– = {a,b,S,T,U,Z}

– q0 = q0

– Z = Z

– F = {q2}

• – (q0, , Z) = (q1, SZ)

– (q1, , Z) = (q2, Z)

– Production Transitions• q1, , S) = (q1, T)

• q1, , S) = (q1, U)

• q1, , T) = (q1, aTb)

• q1, , T) = (q1, )

• q1, , U) = (q1, aaUb)

• q1, , U) = (q1, )

– Matching transitions• q1, a, a) = (q1, )

• q1, b, b) = (q1, )

Page 17: Lecture 35

17

{aibj | i = j or i = 2j} Transition Table

Transition Current Input Top of Next StackNumber State Symbol Stack State Update--------------------------------------------------------------------------------- 1 q0 Z q1 SZ 2 q1 Z q2 Z 3 q1 S q1 T 4 q1 S q1 U 5 q1 T q1 aTb 6 q1 T q1 7 q1 U q1 aaUb 8 q1 U q1 9 q1 a a q1 10 q1 b b q1

Page 18: Lecture 35

18

Partial Computation Graph

(q0, aab, Z) (q1, aab, SZ) (q1, aab, UZ) (other branch not shown)(q1, aab, aaUbZ) (other branch not shown)(q1, ab, aUbZ)(q1, b, UbZ)(q1, b, bZ) (other branch not shown)(q1, , Z) (q2, , Z)

Page 19: Lecture 35

19

Things you should be able to do

• You should be able to execute this algorithm– Given any CFG, construct an equivalent PDA

• You should understand the idea behind this algorithm– Derive string on stack and then match it against input

• You should understand how this construction can help you design PDA’s

• You should understand that it can be used in many-one reductions between decision problems about CFL’s.