Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Lecture 32 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.

Similar presentations


Presentation on theme: "1 Lecture 32 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."— Presentation transcript:

1 1 Lecture 32 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

2 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

3 3 Visualization CFL LPDA 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

4 4 Algorithm Specification Input –CFG G Output –PDA M such that L(M) = L(G) CFG GPDA M A

5 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

6 6 Illustration Input Grammar PALG –V = {S} –  = {a,b} –S = S –P: S --> aSa | bSb | a | b | What is L(PALG)? –The set of all palindromes over {a,b} 1. Derive all strings in L(G) on the stack 2. Match the derived string against input (q 0, aabaa, Z) /* put S on stack */ (q 1, aabaa, SZ) /* derive aabaa on stack */ (q 1, aabaa, aSaZ) (q 1, aabaa, aaSaaZ) (q 1, aabaa, aabaaZ) /* match stack vs input */ (q 1, abaa, abaaZ) (q 1, baa, baaZ) (q 1, aa, aaZ) (q 1, a, aZ) (q 1,, Z) (q 2,, Z) Illustration of how the PDA might work, though not completely accurate.

7 7 Construction Input Grammar –G=(V, , S, P) Output PDA –M=(Q, , , q 0, Z, F,  ) –Q = {q 0, q 1, q 2 } –  =  –  = V union  union {Z} –Z = Z –q 0 = q 0 –F = {q 2 }  : –  (q 0,, Z) = (q 1, SZ) –  (q 1,, Z) = (q 2, Z) –For all productions A -->   q 1,, A) = (q 1,  ) –For all a in   q 1, a, a) = (q 1, )

8 8 Examples

9 9 Palindromes PALG: –V = {S} –  = {a,b} –S = S –P: S --> aSa | bSb | a | b | Output PDA M=(Q, ,q 0,Z,F,  ) – Q = {q 0, q 1, q 2 } –  = {a,b,S,Z} – q 0 = q 0 – Z = Z – F = {q 2 }  : –  (q 0,, Z) = (q 1, SZ) –  (q 1,, Z) = (q 2, Z) –Production Transitions  q 1,, S) = (q 1, aSa)  q 1,, S) = (q 1, bSb)  q 1,, S) = (q 1, a)  q 1,, S) = (q 1, b)  q 1,, S) = (q 1, ) –Matching transitions  q 1, a, a) = (q 1, )  q 1, b, b) = (q 1, )

10 10 Palindrome Transition Table Transition Current Input Top of Next Stack Number State Symbol Stack State Update --------------------------------------------------------------------------------- 1 q 0 Z q 1 SZ 2 q 1 Z q 2 Z 3 q 1 S q 1 aSa 4 q 1 S q 1 bSb 5 q 1 S q 1 a 6 q 1 S q 1 b 7 q 1 S q 1 8 q 1 a a q 1 9 q 1 b b q 1

11 11 Partial Computation Tree (q 0, aba, Z) (q 1, aba, SZ) (q 1, aba, aSaZ) (other branches not shown) (q 1, ba, SaZ) (q 1, ba, baZ) (other branches not shown) (q 1, a, aZ) (q 1,, Z) (q 2,, Z) On your own, draw computation trees for other strings not in the language and see that they are not accepted.

12 12 {a n b n | n >= 0} Grammar G: –V = {S} –  = {a,b} –S = S –P: S --> aSb | Output PDA M=(Q, ,q 0,Z,F,  ) – Q = {q 0, q 1, q 2 } –  = {a,b,S,Z} – q 0 = q 0 – Z = Z – F = {q 2 }  : –  (q 0,, Z) = (q 1, SZ) –  (q 1,, Z) = (q 2, Z) –Production Transitions  q 1,, S) = (q 1, aSb)  q 1,, S) = (q 1, ) –Matching transitions  q 1, a, a) = (q 1, )  q 1, b, b) = (q 1, )

13 13 {a n b n | n >= 0} Transition Table Transition Current Input Top of Next Stack Number State Symbol Stack State Update --------------------------------------------------------------------------------- 1 q 0 Z q 1 SZ 2 q 1 Z q 2 Z 3 q 1 S q 1 aSb 4 q 1 S q 1 5 q 1 a a q 1 6 q 1 b b q 1

14 14 Partial Computation Tree (q 0, aabb, Z) (q 1, aabb, SZ) (q 1, aabb, aSbZ) (other branch not shown) (q 1, abb, SbZ) (q 1, abb, aSbbZ) (other branch not shown) (q 1, bb, SbbZ) (q 1, bb, bbZ) (other branch not shown) (q 1, b, bZ) (q 1,, Z) (q 2,, Z)

15 15 {a i b j | 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, ,q 0,Z,F,  ) – Q = {q 0, q 1, q 2 } –  = {a,b,S,T,U,Z} – q 0 = q 0 – Z = Z – F = {q 2 }  –  (q 0,, Z) = (q 1, SZ) –  (q 1,, Z) = (q 2, Z) –Production Transitions  q 1,, S) = (q 1, T)  q 1,, S) = (q 1, U)  q 1,, T) = (q 1, aTb)  q 1,, T) = (q 1, )  q 1,, U) = (q 1, aaUb)  q 1,, U) = (q 1, ) –Matching transitions  q 1, a, a) = (q 1, )  q 1, b, b) = (q 1, )

16 16 {a i b j | i = j or i = 2j} Transition Table Transition Current Input Top of Next Stack Number State Symbol Stack State Update --------------------------------------------------------------------------------- 1 q 0 Z q 1 SZ 2 q 1 Z q 2 Z 3 q 1 S q 1 T 4 q 1 S q 1 U 5 q 1 T q 1 aTb 6 q 1 T q 1 7 q 1 U q 1 aaUb 8 q 1 U q 1 9 q 1 a a q 1 10 q 1 b b q 1

17 17 Partial Computation Tree (q 0, aab, Z) (q 1, aab, SZ) (q 1, aab, UZ) (other branch not shown) (q 1, aab, aaUbZ) (other branch not shown) (q 1, ab, aUbZ) (q 1, b, UbZ) (q 1, b, bZ) (other branch not shown) (q 1,, Z) (q 2,, Z)

18 18 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.


Download ppt "1 Lecture 32 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."

Similar presentations


Ads by Google