lecture 20 dpda / review topics given a pda construct a grammar for the language it accepts...
TRANSCRIPT
Lecture 20DPDA / Review
Lecture 20DPDA / Review
Topics Topics Given a PDA construct a grammar for the
language it accepts Deterministic PDAs Properties of CFLs
Readings:Readings:
November 5, 2008
CSCE 355 Foundations
– 2 – CSCE 355 Fall 2008
Last Time: Top-down parsing
AlgorithmsLeft-factoring
From Grammars to PDA Section 6.3 Formal PDA Construction from CFL
New: New: When will our parsing algorithm be deterministic?
HomeworkHomework1. …
– 3 – CSCE 355 Fall 2008
When will our parsing algorithm be deterministic?When will our parsing algorithm be deterministic?First(First(αα ) = { x in T such that ) = { x in T such that αα * x* xββ } }
Then if AThen if A αα and A and A β β are two productions we are are two productions we are choosing from and the current input character is a which choosing from and the current input character is a which is in is in First(First(αα) but not in First() but not in First(ββ ) )
Then we choose the production Then we choose the production AA αα..
So if So if First(First(αα) ) ∩∩ First( First(ββ ) = ) = ϕϕ then we can always then we can always deterministically choosedeterministically choose
There is a type of grammar that is LL(1) for which this works There is a type of grammar that is LL(1) for which this works well. A grammar with no well. A grammar with no εε-productions is LL(1) if it satisfies -productions is LL(1) if it satisfies the disjointness property above.the disjointness property above.
– 4 – CSCE 355 Fall 2008
Homework from Last time Homework from Last time
1.1. Given the grammar simple4.y (and simple4.l)Given the grammar simple4.y (and simple4.l)a. Modify the semantic actions so that it produces a trace of
productions involved in the parse.
b. If you look at the sequence do you notice anything?
2.2. 6.2.1b) Strings of 0’s and 1’s such that no prefix has 6.2.1b) Strings of 0’s and 1’s such that no prefix has more 1’s than 0’smore 1’s than 0’s
3.3. 6.2.5b,c6.2.5b,c
– 5 – CSCE 355 Fall 2008
%token INTEGER PLUS TIMES LPAREN RPAREN%token INTEGER PLUS TIMES LPAREN RPAREN
%%%%
line : expr '\n' {printf("Using Prod line -> expr \\n \n");}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");}expr : expr PLUS term {printf("Using Prod expr -> expr PLUS term \n");}
| term | term {printf("Using Prod expr -> term \n");}{printf("Using Prod expr -> term \n");}
;;
term : term TIMES factor {printf("Using Prod term -> term TIMES factor\n");}term : term TIMES factor {printf("Using Prod term -> term TIMES factor\n");}
| factor | factor {printf("Using Prod term -> factor \n");}{printf("Using Prod term -> factor \n");}
;;
factor : LPAREN expr RPAREN {printf("Using Prod factor -> LP expr RP \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 – CSCE 355 Fall 2008
cerberus> ./simpleHWcerberus> ./simpleHW
2+3*72+3*7
Using Prod factor -> INTEGER Using Prod factor -> INTEGER
Using Prod term -> factor Using Prod term -> factor
Using Prod expr -> term Using Prod expr -> term
Using Prod factor -> INTEGER Using Prod factor -> INTEGER
Using Prod term -> factor Using Prod term -> factor
Using Prod factor -> INTEGER Using Prod factor -> INTEGER
Using Prod term -> term TIMES factorUsing Prod term -> term TIMES factor
Using Prod expr -> expr PLUS term Using Prod expr -> expr PLUS term
Using Prod line -> expr \n Using Prod line -> expr \n
– 7 – CSCE 355 Fall 2008
6.2.1b) no prefix has more 1’s than 0’s6.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 6.2.1b) Strings of 0’s and 1’s such that no prefix has more 1’s than 0’smore 1’s than 0’s
q dead
0,Z0/0Z0
0,0/001,0/ε
1,Z0/Z0
– 8 – CSCE 355 Fall 2008
6.2.5b6.2.5bSt Σ Γ NextSt Γ* indx
q0 a Z0 q1 AAZ0 1
q0 b Z0 q2 BZ0 2
q0 ε Z0 f ε 3
q1 a A q1 AAA 4
q1 b A q1 ε 5
q1 ε Z0 q0 Z0 6
q2 a B q3 ε 7
q2 b B q2 BB 8
q2 ε Z0 q0 Z0 9
q3 ε B q2 ε 10
q3 ε Z0 q1 AZ0 11
(q(q00, abb, Z, abb, Z00))├ ├ (rule 1)(rule 1)
(q(q11, bb, AAZ, bb, AAZ00))├ ├ (rule 5)(rule 5)
(q(q11, b, AZ, b, AZ00))├ ├ (rule 5)(rule 5)
(q(q11, , εε, Z, Z00))├ ├ (rule 6)(rule 6)
(q(q00, , εε, Z, Z00))├ ├ (rule 3)(rule 3)
(f, (f, εε, , εε))
– 9 – CSCE 355 Fall 2008
6.2.5c6.2.5cSt Σ Γ NextSt Γ* indx
q0 a Z0 q1 AAZ0 1
q0 b Z0 q2 BZ0 2
q0 ε Z0 f ε 3
q1 a A q1 AAA 4
q1 b A q1 ε 5
q1 ε Z0 q0 Z0 6
q2 a B q3 ε 7
q2 b B q2 BB 8
q2 ε Z0 q0 Z0 9
q3 ε B q2 ε 10
q3 ε Z0 q1 AZ0 11
(q(q00, b, b77aa44, Z, Z00))├ ├ (rule 2)(rule 2)
(q(q22, b, b66aa44, BZ, BZ00))├ ├ (rule 8)(rule 8)
(q(q22, b, b55aa44, BBZ, BBZ00))├ ├ (rule 8)(rule 8)
……
(q(q22, ba, ba44, BBBBBBZ, BBBBBBZ00))├ ├ (r8)(r8)
(q(q22, a, a44, BBBBBBBZ, BBBBBBBZ00))├ ├ (r7)(r7)
(q(q33, a, a33, BBBBBBZ, BBBBBBZ00))├ ├ (rule10)(rule10)
(q(q22, a, a33, BBBBBZ, BBBBBZ00))├ ├ (rule7)(rule7)
(q(q33, a, a22, BBBBZ, BBBBZ00))├ ├ (rule10)(rule10)
(q(q22, a, a22, BBBZ, BBBZ00))├ ├ (r7)(r7)
(q(q33, a, a11, BBZ, BBZ00))├ ├ (rule10)(rule10)
(q(q22, a, a11, BZ, BZ00))├ ├ (rule7)(rule7)
(q(q33, , εε, Z, Z00))├ ├ (rule11)(rule11)
(q(q11, , εε, AZ, AZ00))├ ├ ( No rule!?)( No rule!?)
– 10 – CSCE 355 Fall 2008
Sample Test 2 Minimize DFA problemSample Test 2 Minimize DFA problem
1
– 11 – CSCE 355 Fall 2008
Dump of Distinguished Dump of Distinguished States ArrayStates Array
A B C D E F G H A B C D E F G H
A X A X
B X B X
C X C X
D X D X
E X E X
F X X X X X X X F X X X X X X X
G X G X
H X H X
– 12 – CSCE 355 Fall 2008
Iteration 0 a=0Iteration 0 a=0
Iteration 0 a=1Iteration 0 a=1
Adding dist[A,D] since delta(A, 1) = B, delta(D, 1) = F and dist[B,F]=XAdding 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]=XAdding 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]=XAdding 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]=XAdding 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]=XAdding 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]=XAdding 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]=XAdding 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]=XAdding 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]=XAdding 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]=XAdding 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]=XAdding 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]=XAdding dist[H,B] since delta(H, 1) = H, delta(B, 1) = D and dist[H,D]=X
– 13 – CSCE 355 Fall 2008
Iteration 1 a=0Iteration 1 a=0
Iteration 1 a=1Iteration 1 a=1
Adding dist[A,B] since delta(A, 1) = B, delta(B, 1) = D and dist[B,D]=XAdding 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]=XAdding 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]=XAdding 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]=XAdding 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]=XAdding dist[B,C] since delta(B, 1) = D, delta(C, 1) = E and dist[D,E]=X
Iteration 2 a=0Iteration 2 a=0
Iteration 2 a=1Iteration 2 a=1
– 14 – CSCE 355 Fall 2008
A B C D E F G H
A . X X X X X X X
B X . X X X X X X
C X X . X X X
D X X X . X X X
E X X X . X X
F X X X X X . X X
G X X X X X . X
H X X X X X .
Equivalence ClassesEquivalence Classes
A's equivalence class is: A A's equivalence class is: A
B's equivalence class is: B B's equivalence class is: B
C's equivalence class is: C E H C's equivalence class is: C E H
D's equivalence class is: D G D's equivalence class is: D G
E's equivalence class is: C E H E's equivalence class is: C E H
F's equivalence class is: F F's equivalence class is: F
G's equivalence class is: D G G's equivalence class is: D G
H's equivalence class is: C E HH's equivalence class is: C E H
– 15 – CSCE 355 Fall 2008
Formal PDA Construction from CFLFormal PDA Construction from CFL
Let G = (V, T, Q, S)Let G = (V, T, Q, S)
P = ({q}, T, (V U T), P = ({q}, T, (V U T), δδ, q, S), q, S)
where where δ δ is defined by:is defined by:
1.1. δδ(q, (q, εε, A) = { (q, , A) = { (q, ββ) | A) | A ββ is a production of G}is a production of G}
2.2. δδ(q, a, a) = { (q, (q, a, a) = { (q, εε) }) }
– 16 – CSCE 355 Fall 2008
Homework CFG PDAHomework CFG PDA
Convert the grammarConvert the grammar• S aAA • A aS | bS | a
To a PDA that accepts the same language by empty stackTo a PDA that accepts the same language by empty stack
P = ({q}, {a,b}, {S,A,a,b}, P = ({q}, {a,b}, {S,A,a,b}, δδ, q, S), q, S)
where where δ δ is defined by: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}
• δ(q, ε, S) = { (q, aAA) }
• δ(q, ε, A) = { (q, aS), (q, bS), (q, a) }
– 17 – CSCE 355 Fall 2008
HW 10/29 6.1.1b,c PDA trace (tree of IDs)HW 10/29 6.1.1b,c PDA trace (tree of IDs)
1.1. δδ(q, 0, Z(q, 0, Z00) = { (q, XZ) = { (q, XZ00) }) }
2.2. δδ(q, 0, X) = { (q, XX) }(q, 0, X) = { (q, XX) }
3.3. δδ(q, 1, X) = { (q, X) }(q, 1, X) = { (q, X) }
4.4. δδ(q, (q, εε, X) = { (p, , X) = { (p, εε) }) }
5.5. δδ(p, (p, εε, X) = { (p, , X) = { (p, εε) }) }
6.6. δδ(p, 1, X) = { (p, XX) }(p, 1, X) = { (p, XX) }
7.7. δδ(p, 1, Z(p, 1, Z00) = { (p, ) = { (p, εε) }) }
q p
0,Z0/XZ0
0,X/XX1,X/X
ε,X/ε
ε,X/ε1,X/XX1,Z0/ ε
– 18 – CSCE 355 Fall 2008
6.1.1b6.1.1b(q, 0011, Z0)
(q, 011, XZ0) (p, 011, Z0)
(q, 11, XXZ0) (p , 11, XZ0)
(q, 1, XXZ0) (p , 1, XXZ0)
(q, ε, XXZ0) (p , 1, XZ0)
(p, 1, Z0) (p , ε, XZ0)
q p
0,Z0/XZ0
0,X/XX1,X/X
ε,X/ε
ε,X/ε1,X/XX1,Z0/ ε
(p, ε, ε)
(p, 1, Z0) (p , ε, XZ0)
(p, ε, ε)
(p, 1, XZ0)
(p , 11, Z0)
(p , 1, ε)(p , ε, XXXZ0)
(p , ε, XXZ0)
(p , ε, XZ0)
(p , ε, Z0)
– 19 – CSCE 355 Fall 2008
– 20 – CSCE 355 Fall 2008
– 21 – CSCE 355 Fall 2008
– 22 – CSCE 355 Fall 2008
– 23 – CSCE 355 Fall 2008
– 24 – CSCE 355 Fall 2008