270 likes | 421 Views
Dr. torng. CFG → PDA construction Shows that for any CFL L, there exists a PDA M such that L(M) = L The reverse is true, but we skip the proof Parsing. 1. 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
E N D
Dr. torng • CFG → PDA construction • Shows that for any CFL L, there exists a PDA M such that L(M) = L • The reverse is true, but we skip the proof • Parsing 1
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(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 2
Visualization L L G M CFG’s PDA’s • 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 CFL LPDA 3
A Algorithm Specification • Input • CFG G • Output • PDA M such that L(M) = CFG G PDA M 4
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 5
Illustration 1. Derive all strings in L(G) on the stack2. Match the derived string against input • Input Grammar G • V = {S} • = {a,b} • S = S • P: S → aSb | • What is L(G)? Illustration of how the PDA might work, though not completely accurate. (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) 6
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 illegal with the computation graph on the left? 7
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} • : • Fixed Transitions • (q0, , Z) = (q1, SZ) • (q1, , Z) = (q2, Z) • Production Transitions • For all productions A → , add q1, , A) = (q1, ) • Matching Transitions • For all a in , add q1, a, a) = (q1, ) 8
Examples 9
Balanced Parentheses • Fixed Transitions • δ(q0, λ, Z) = (q1, SZ) • δ(q1, λ, Z) = (q2, Z) • Production Transitions • δ(q1, λ, S) = (q1, SS) • δq1, λ, S) = (q1, (S)) • δq1, λ, S) = (q1, λ) • Matching transitions • δq1, (, ( = (q1, λ) • δq1, ), ) = (q1, λ) • BALG: • V = {S} • = {(,)} • S = S • P: • S → SS | (S) | λ • Output PDA M=(Q,,q0,Z,F, δ) • Q = {q0, q1, q2} • = {(,),S,Z} • q0 = q0 • Z = Z • F = {q2} • δ: 10
BALG Transition Table Transition Current Input Top of Next Stack Number State Symbol Stack State Update --------------------------------------------------------------------------------- 1 q0 Z q1 SZ 2 q1 Z q2 Z 3 q1 S q1 SS 4 q1 S q1 (S) 5 q1 S q1λ 6 q1 ( ( q1 7 q1 ) ) q1 11
Partial Computation Graph (q0, ()(), Z) (q1, ()(), SZ) (q1, ()(), SSZ) (other branches not shown) (q1, ()(), (S)SZ) (other branches not shown) (q1, )(), S)SZ) (q1, )(),)SZ) (other branches not shown) (q1, (), SZ) (q1, (), (S)Z) (other branches not shown) (q1, ), S)Z) (q1, ), )Z) (other branches not shown) (q1, , Z) (q2, , Z) 12
Palindromes • Fixed Transitions • (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, ) • 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} • δ: 13
Palindrome Transition Table Transition Current Input Top of Next Stack Number 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 14
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 strings not in the language and see that they are not accepted. 15
{anbn | n ≥ 0} • Fixed Transitions • (q0, , Z) = (q1, SZ) • (q1, , Z) = (q2, Z) • Production Transitions • Matching transitions • 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} • : 16
{anbn | n ≥ 0} Transition Table Transition Current Input Top of Next Stack Number State Symbol Stack State Update --------------------------------------------------------------------------------- 1 q0 Z 2 q1 Z 3 q1 S 4 q1 S 5 q1 a a 6 q1 b b 17
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) 18
{aibj | i = j or i = 2j} • • Fixed Transitions • (q0, , Z) = (q1, SZ) • (q1, , Z) = (q2, Z) • Production Transitions • Matching transitions • 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} 19
{aibj | i = j or i = 2j} Transition Table Transition Current Input Top of Next Stack Number 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 20
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) 21
Parsing 22
Eliminating Nondeterminism • Lets revisit the BALG grammar • S → SS | (S) | λ • Whenever S is on top of the stack, we do not look at the input and nondeterministically select one of the 3 productions • Think about the PDA parsing ( ) • How might we try to eliminate the nondeterminism to choose between the productions? • Will that work for this grammar? 23
Another Grammar • BALG2 grammar • S → (S)S | λ • Now try parsing ( ) • Any issues? • Need an end marker, say $ • T → S$, S → (S)S | λ 24
Resulting Transition Table * Transition Current Input Top of Next Stack Number State Symbol Stack State Update --------------------------------------------------------------------------------- 1 q0 Z q1 TZ 2 q1 Z q2 Z 3 q1 T q1 S$ 4 q1 S q1 (S)S 5 q1 S q1λ 6 q1 ( ( q1 7 q1 ) ) q1 8 q1 $ $ q1 4’ q1 ( S q1 S)S 5’ q1 ) S q)λ 5’’ q)λ ) q1λ 5’’’ q1 $ S q$λ 5’’’’ q$λ $ q1λ 25
Deterministic Parsers • BALG2 grammar {S → (S)S | λ} is called an LL(1) grammar • The nondeterministic top-down PDA can be converted into a deterministic top-down parser by “looking ahead” 1 character • Generalize to LL(k) grammar with k characters of lookahead • LR(k) grammars correspond to bottom-up parsers using “shift” and “reduce” operations • Shift: read and push input symbol onto stack • Reduce: replace string on stack with variable that derives it 26
Comments • You should be able to execute the 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 answer-preserving input transformations between decision problems about CFL’s. • You should have a basic intuition about parsing 27