240 likes | 258 Views
Learn about PDAs, CFL properties, grammar construction, deterministic parsing in CSCE 355 Foundations. Topics covered include algorithms, LL(1) grammars, and PDA construction. Homework assignments included.
E N D
Lecture 20DPDA / Review CSCE 355 Foundations • Topics • Given a PDA construct a grammar for the language it accepts • Deterministic PDAs • Properties of CFLs • Readings: November 5, 2008
Last Time: • Top-down parsing • Algorithms • Left-factoring • From Grammars to PDA Section 6.3 • Formal PDA Construction from CFL • New: • When will our parsing algorithm be deterministic? • Homework • …
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.
Homework from Last time • 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? • 6.2.1b) Strings of 0’s and 1’s such that no prefix has more 1’s than 0’s • 6.2.5b,c
%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");} • ; • expr : expr PLUS term {printf("Using Prod expr -> expr PLUS 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 : LPAREN expr RPAREN {printf("Using Prod factor -> LP expr RP \n");} • | INTEGER {printf("Using Prod factor -> INTEGER \n");} • ; • %%
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
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 0,Z0/0Z0 0,0/00 1,0/ε 1,Z0/Z0 q dead
6.2.5b • (q0, abb, Z0)├ (rule 1) • (q1, bb, AAZ0)├ (rule 5) • (q1, b, AZ0)├ (rule 5) • (q1, ε, Z0)├ (rule 6) • (q0, ε, Z0)├ (rule 3) • (f, ε, ε)
6.2.5c • (q0, b7a4, Z0)├ (rule 2) • (q2, b6a4, BZ0)├ (rule 8) • (q2, b5a4, BBZ0)├ (rule 8) • … • (q2, ba4, BBBBBBZ0)├ (r8) • (q2, a4, BBBBBBBZ0)├ (r7) • (q3, a3, BBBBBBZ0)├ (rule10) • (q2, a3, BBBBBZ0)├ (rule7) • (q3, a2, BBBBZ0)├ (rule10) • (q2, a2, BBBZ0)├ (r7) • (q3, a1, BBZ0)├ (rule10) • (q2, a1, BZ0)├ (rule7) • (q3, ε, Z0)├ (rule11) • (q1, ε, AZ0)├ ( No rule!?)
Dump of Distinguished States Array • 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
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
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
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
Formal PDA Construction from CFL • Let G = (V, T, Q, S) • P = ({q}, T, (V U T), δ, q, S) • where δ is defined by: • δ(q, ε, A) = { (q, β) | A βis a production of G} • δ(q, a, a) = { (q, ε) }
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 Σ • δ(q, a, a) = { (q, ε) } • δ(q, b, b) = { (q, ε) } • δ(q, ε, A) = { (q, β) | Aβin G} • δ(q, ε, S) = { (q, aAA) } • δ(q, ε, A) = { (q, aS), (q, bS), (q, a) }
HW 10/29 6.1.1b,c PDA trace (tree of IDs) • δ(q, 0, Z0) = { (q, XZ0) } • δ(q, 0, X) = { (q, XX) } • δ(q, 1, X) = { (q, X) } • δ(q, ε, X) = { (p, ε) } • δ(p, ε, X) = { (p, ε) } • δ(p, 1, X) = { (p, XX) } • δ(p, 1, Z0) = { (p, ε) } ε,X/ε 1,X/XX 1,Z0/ ε 0,Z0/XZ0 0,X/XX 1,X/X ε,X/ε q p
ε,X/ε 1,X/XX 1,Z0/ ε 0,Z0/XZ0 0,X/XX 1,X/X 6.1.1b (q, 0011, Z0) (q, 011, XZ0) (p, 011, Z0) ε,X/ε q p (q, 11, XXZ0) (p , 11, XZ0) (p , 11, Z0) (q, 1, XXZ0) (p , 1, XXZ0) (q, ε, XXZ0) (p , 1, XZ0) (p , 1, ε) (p, 1, XZ0) (p , ε, XXXZ0) (p, 1, Z0) (p , ε, XZ0) (p, 1, Z0) (p , ε, XZ0) (p , ε, XXZ0) (p,ε, ε) (p,ε, ε) (p , ε, XZ0) (p , ε, Z0)