Download presentation
Presentation is loading. Please wait.
Published bySharyl Austin Modified over 9 years ago
1
Lecture 20 DPDA / Review Topics Given a PDA construct a grammar for the language it accepts Deterministic PDAs Properties of CFLsReadings: November 5, 2008 CSCE 355 Foundations
2
– 2 – CSCE 355 Fall 2008 Last Time: Top-down parsing Algorithms Left-factoring From Grammars to PDA Section 6.3 Formal PDA Construction from CFLNew: When will our parsing algorithm be deterministic?Homework 1. …
3
– 3 – CSCE 355 Fall 2008 When will our parsing algorithm be deterministic? First(α ) = { x in T such that α * x β } Then if A α and A β are two productions we are choosing from and the current input character is a which is in First(α) but not in First( β ) Then we choose the production A α. So if First(α) ∩ First( β ) = ϕ then we can always deterministically choose There is a type of grammar that is LL(1) for which this works well. A grammar with no ε-productions is LL(1) if it satisfies the disjointness property above.
4
– 4 – CSCE 355 Fall 2008 Homework from Last time 1.Given the grammar simple4.y (and simple4.l) Modify the semantic actions so that it produces a trace of productions involved in the parse. If you look at the sequence do you notice anything? 2.6.2.1b) Strings of 0’s and 1’s such that no prefix has more 1’s than 0’s 3.6.2.5b,c
5
– 5 – CSCE 355 Fall 2008 %token INTEGER PLUS TIMES LPAREN RPAREN % line : expr '\n' {printf("Using Prod line -> expr \\n \n");} | line expr '\n' {printf("Using Prod line -> line expr \\n \n");} | line expr '\n' {printf("Using Prod line -> line expr \\n \n");} ; expr : expr PLUS term {printf("Using Prod expr -> expr PLUS term \n");} | term {printf("Using Prod expr -> term \n");} | term {printf("Using Prod expr -> term \n");} ; term : term TIMES factor {printf("Using Prod term -> term TIMES factor\n");} | factor {printf("Using Prod term -> factor \n");} | factor {printf("Using Prod term -> factor \n");} ; factor : LPAREN expr RPAREN {printf("Using Prod factor -> LP expr RP \n");} | INTEGER {printf("Using Prod factor -> INTEGER \n");} | INTEGER {printf("Using Prod factor -> INTEGER \n");} ;%
6
– 6 – CSCE 355 Fall 2008 cerberus>./simpleHW 2+3*7 Using Prod factor -> INTEGER Using Prod term -> factor Using Prod expr -> term Using Prod factor -> INTEGER Using Prod term -> factor Using Prod factor -> INTEGER Using Prod term -> term TIMES factor Using Prod expr -> expr PLUS term Using Prod line -> expr \n
7
– 7 – CSCE 355 Fall 2008 6.2.1b) no prefix has more 1’s than 0’s 6.2.1b) Strings of 0’s and 1’s such that no prefix has more 1’s than 0’s qdead 0,Z 0 /0Z 0 0,0/00 1,0/ε 1,Z 0 /Z 0
8
– 8 – CSCE 355 Fall 2008 6.2.5b St ΣΓ NextSt Γ*Γ* indx q0q0 aZ0Z0 q1q1 AAZ 0 1 q0q0 bZ0Z0 q2q2 BZ 0 2 q0q0 εZ0Z0 fε3 q1q1 aAq1q1 AAA4 q1q1 bAq1q1 ε5 q1q1 εZ0Z0 q0q0 Z0Z0 6 q2q2 aBq3q3 ε7 q2q2 bBq2q2 BB8 q2q2 εZ0Z0 q0q0 Z0Z0 9 q3q3 εBq2q2 ε10 q3q3 εZ0Z0 q1q1 AZ 0 11 (q 0, abb, Z 0 ) ├ (rule 1) (q 1, bb, AAZ 0 ) ├ (rule 5) (q 1, b, AZ 0 ) ├ (rule 5) (q 1, ε, Z 0 ) ├ (rule 6) (q 0, ε, Z 0 ) ├ (rule 3) (f, ε, ε)
9
– 9 – CSCE 355 Fall 2008 6.2.5c St ΣΓ NextSt Γ*Γ* indx q0q0 aZ0Z0 q1q1 AAZ 0 1 q0q0 bZ0Z0 q2q2 BZ 0 2 q0q0 εZ0Z0 fε3 q1q1 aAq1q1 AAA4 q1q1 bAq1q1 ε5 q1q1 εZ0Z0 q0q0 Z0Z0 6 q2q2 aBq3q3 ε7 q2q2 bBq2q2 BB8 q2q2 εZ0Z0 q0q0 Z0Z0 9 q3q3 εBq2q2 ε10 q3q3 εZ0Z0 q1q1 AZ 0 11 (q 0, b 7 a 4, Z 0 ) ├ (rule 2) (q 2, b 6 a 4, BZ 0 ) ├ (rule 8) (q 2, b 5 a 4, BBZ 0 ) ├ (rule 8) … (q 2, ba 4, BBBBBBZ 0 ) ├ (r8) (q 2, a 4, BBBBBBBZ 0 ) ├ (r7) (q 3, a 3, BBBBBBZ 0 ) ├ (rule10) (q 2, a 3, BBBBBZ 0 ) ├ (rule7) (q 3, a 2, BBBBZ 0 ) ├ (rule10) (q 2, a 2, BBBZ 0 ) ├ (r7) (q 3, a 1, BBZ 0 ) ├ (rule10) (q 2, a 1, BZ 0 ) ├ (rule7) (q 3, ε, Z 0 ) ├ (rule11) (q 1, ε, AZ 0 ) ├ ( No rule!?)
10
– 10 – CSCE 355 Fall 2008 Sample Test 2 Minimize DFA problem 1
11
– 11 – CSCE 355 Fall 2008 Dump of Distinguished States Array A B C D E F G H A B C D E F G H A X B X C X D X E X F X X X X X X X G X H X
12
– 12 – CSCE 355 Fall 2008 Iteration 0 a=0 Iteration 0 a=1 Adding dist[A,D] since delta(A, 1) = B, delta(D, 1) = F and dist[B,F]=X Adding dist[A,G] since delta(A, 1) = B, delta(G, 1) = F and dist[B,F]=X Adding dist[B,D] since delta(B, 1) = D, delta(D, 1) = F and dist[D,F]=X Adding dist[B,G] since delta(B, 1) = D, delta(G, 1) = F and dist[D,F]=X Adding dist[C,D] since delta(C, 1) = E, delta(D, 1) = F and dist[E,F]=X Adding dist[C,G] since delta(C, 1) = E, delta(G, 1) = F and dist[E,F]=X Adding dist[D,E] since delta(D, 1) = F, delta(E, 1) = H and dist[F,H]=X Adding dist[D,H] since delta(D, 1) = F, delta(H, 1) = H and dist[F,H]=X Adding dist[E,B] since delta(E, 1) = H, delta(B, 1) = D and dist[H,D]=X Adding dist[E,G] since delta(E, 1) = H, delta(G, 1) = F and dist[H,F]=X Adding dist[G,H] since delta(G, 1) = F, delta(H, 1) = H and dist[F,H]=X Adding dist[H,B] since delta(H, 1) = H, delta(B, 1) = D and dist[H,D]=X
13
– 13 – CSCE 355 Fall 2008 Iteration 1 a=0 Iteration 1 a=1 Adding dist[A,B] since delta(A, 1) = B, delta(B, 1) = D and dist[B,D]=X Adding dist[A,C] since delta(A, 1) = B, delta(C, 1) = E and dist[B,E]=X Adding dist[A,E] since delta(A, 1) = B, delta(E, 1) = H and dist[B,H]=X Adding dist[A,H] since delta(A, 1) = B, delta(H, 1) = H and dist[B,H]=X Adding dist[B,C] since delta(B, 1) = D, delta(C, 1) = E and dist[D,E]=X Iteration 2 a=0 Iteration 2 a=1
14
– 14 – CSCE 355 Fall 2008 ABCDEFGH A.XXXXXXX BX.XXXXXX CXX.XXX DXXX.XXX EXXX.XX FXXXXX.XX GXXXXX.X HXXXXX. Equivalence Classes A's equivalence class is: A B's equivalence class is: B C's equivalence class is: C E H D's equivalence class is: D G E's equivalence class is: C E H F's equivalence class is: F G's equivalence class is: D G H's equivalence class is: C E H
15
– 15 – CSCE 355 Fall 2008 Formal PDA Construction from CFL Let G = (V, T, Q, S) P = ({q}, T, (V U T), δ, q, S) where δ is defined by: 1.δ (q, ε, A) = { (q, β ) | A β is a production of G} 2.δ (q, a, a) = { (q, ε) }
16
– 16 – CSCE 355 Fall 2008 Homework CFG PDA Convert the grammar S aAA A aS | bS | a To a PDA that accepts the same language by empty stack P = ({q}, {a,b}, {S,A,a,b}, δ, q, S) where δ is defined by: δ (q, a, a) = { (q, ε) } for all a in Σ 1.δ (q, a, a) = { (q, ε) } 2.δ (q, b, b) = { (q, ε) } δ (q, ε, A) = { (q, β ) | A β in G} 1.δ (q, ε, S) = { (q, aAA) } 2.δ (q, ε, A) = { (q, aS), (q, bS), (q, a) }
17
– 17 – CSCE 355 Fall 2008 HW 10/29 6.1.1b,c PDA trace (tree of IDs) 1.δ(q, 0, Z 0 ) = { (q, XZ 0 ) } 2.δ(q, 0, X) = { (q, XX) } 3.δ(q, 1, X) = { (q, X) } 4.δ(q, ε, X) = { (p, ε) } 5.δ(p, ε, X) = { (p, ε) } 6.δ(p, 1, X) = { (p, XX) } 7.δ(p, 1, Z 0 ) = { (p, ε) } qp 0,Z 0 /XZ 0 0,X/XX 1,X/X ε,X/ε 1,X/XX 1,Z 0 / ε
18
– 18 – CSCE 355 Fall 2008 6.1.1b (q, 0011, Z 0 ) (q, 011, XZ 0 )(p, 011, Z 0 ) (q, 11, XXZ 0 )(p, 11, XZ 0 ) (q, 1, XXZ 0 )(p, 1, XXZ 0 ) (q, ε, XXZ 0 )(p, 1, XZ 0 ) (p, 1, Z 0 )(p, ε, XZ 0 ) qp 0,Z 0 /XZ 0 0,X/XX 1,X/X ε,X/ε 1,X/XX 1,Z 0 / ε (p, ε, ε) (p, 1, Z 0 )(p, ε, XZ 0 ) (p, ε, ε) (p, 1, XZ 0 ) (p, 11, Z 0 ) (p, 1, ε) (p, ε, XXXZ 0 ) (p, ε, XXZ 0 ) (p, ε, XZ 0 ) (p, ε, Z 0 )
19
– 19 – CSCE 355 Fall 2008
20
– 20 – CSCE 355 Fall 2008
21
– 21 – CSCE 355 Fall 2008
22
– 22 – CSCE 355 Fall 2008
23
– 23 – CSCE 355 Fall 2008
24
– 24 – CSCE 355 Fall 2008
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.