Test 1 Post Mortem
description
Transcript of Test 1 Post Mortem
Test 1 Post MortemTest 1 Post Mortem
Topics Topics Questions and points Grade Distribution Answers
February 20, 2006
CSCE 531 Compiler Construction
– 2 – CSCE 531 Spring 2006
Questions and Point EvaluationsQuestions and Point Evaluations
1a. Reg expr1a. Reg exprNFA 14 ptsNFA 14 pts
2.2. Subset Construction 14 Subset Construction 14 ptspts
33 Grammars, derivations Grammars, derivations a. Flex/Lex patterns 4 pts b. L( (a|b)*aa(a|b) ) 5 ptsc. Reg expr a’s before b’s
5pts
44 Left-recusionLeft-recusiona. Left recursion 4 ptsb. Left factor 4 ptsc. LL(1) 2ptsd. M[A,a] 4pts
55 Recursive Descent parsingRecursive Descent parsing
a. Transition diagram 7pts
b. recursive parsing 7pts
66 LL(1) table construction ptsLL(1) table construction pts
a. Modifications 1 pt
b. FIRST 4 pts
c. FOLLOW 4 pts
d. Table 5 pts
77 LR(0) sets of items 14 ptsLR(0) sets of items 14 pts
– 3 – CSCE 531 Spring 2006
Grade DistributionGrade Distribution
100100
94 93 9094 93 90
89 88 83 83 81 80 8089 88 83 83 81 80 80
79 78 78 78 77 77 75 74 73 73 73 71 71 7079 78 78 78 77 77 75 74 73 73 73 71 71 70
68 68 66 66 63 63 68 68 66 66 63 63
59 59 56 54 52 5159 59 56 54 52 51
49 46 4649 46 46
3434
1 2 3ai ii iii iv 3b 3c 4a 4b 4c 4d 5a 5b 6a 6b 6c 6d 7 PointsOff Grade1 2 3ai ii iii iv 3b 3c 4a 4b 4c 4d 5a 5b 6a 6b 6c 6d 7 PointsOff Grade
AVERAGE 1.6 4.5 0.2 0.7 0.2 0.4 0.2 2.3 0.5 0.9 0.2 2.7 3.4 3.9 0.0 1.0 1.6 2.1 4.0 30.5 69AVERAGE 1.6 4.5 0.2 0.7 0.2 0.4 0.2 2.3 0.5 0.9 0.2 2.7 3.4 3.9 0.0 1.0 1.6 2.1 4.0 30.5 69
– 4 – CSCE 531 Spring 2006
1. Thompson Construction Reg Expr NFA1. Thompson Construction Reg Expr NFA(a | b)(ab)*a solution built from innermost out(a | b)(ab)*a solution built from innermost out
aεbεaε
ε
ε
ε
a
b
ε ε
ε
ε ε
– 5 – CSCE 531 Spring 2006
2. Subset Construction NFA DFA2. Subset Construction NFA DFAstatestate aa bb
q0 = e-closure({s0}) = q0 = e-closure({s0}) = {s0}{s0}
e-closure({s1})e-closure({s1})
= {s1, s2 }= {s1, s2 }
e-closure({s0, s4 })e-closure({s0, s4 })
= {s0, s4, s1, s2}= {s0, s4, s1, s2}
q1={s1,s2}q1={s1,s2} e-closure({s1,s3 }) = e-closure({s1,s3 }) = {s1, s2, s3}{s1, s2, s3}
ΦΦ
q2={s0, s4, s1, s2}q2={s0, s4, s1, s2} e-closure({s1,s3}) = e-closure({s1,s3}) = {s1, s2, s3} = q3{s1, s2, s3} = q3
e-closure({s0,s4,s5}) e-closure({s0,s4,s5}) = {s0,s1,s2,s4,s5}= {s0,s1,s2,s4,s5}
q3 = {s1, s2, s3}q3 = {s1, s2, s3} e-closure({s1,s3})= e-closure({s1,s3})= {s1, s2, s3} = q3{s1, s2, s3} = q3
e-closure({s6}) = {s6}e-closure({s6}) = {s6}
q4 = q4 = ΦΦ ΦΦ ΦΦ
q5 = {s0,s1,s2,s4,s5}q5 = {s0,s1,s2,s4,s5} e-closure({s1,s3,s6}) e-closure({s1,s3,s6}) = {s1, s2, s3,s6}= {s1, s2, s3,s6}
e-closure({s4,s5,s6}) e-closure({s4,s5,s6}) ={s4,s1,s2,s5,s6}={s4,s1,s2,s5,s6}
q6 = {s6}q6 = {s6} ΦΦ ΦΦ
q7={s1, s2, s3, s6}q7={s1, s2, s3, s6} e-closure({s1,s3}) = e-closure({s1,s3}) = {s1, s2, s3} = q3{s1, s2, s3} = q3
e-closure({s6}) = {s6}e-closure({s6}) = {s6}
Accepting states those that contain s6:Accepting states those that contain s6:
– 6 – CSCE 531 Spring 2006
3. Regular Languages3. Regular Languages3a. What do the following Lex/Flex patterns match?3a. What do the following Lex/Flex patterns match?
x? --- zero or one x’s (spaces on the dynamically bound question)
[^abc] --- any single character not ‘a’, ‘b’ or ‘c’ ab+ --- a single ‘a’ followed by one or more ‘b’s’ ab{3,5} --- a single ‘a’ followed by 3, 4, or 5 b’s
3b. Describe the language denoted by the regular 3b. Describe the language denoted by the regular expression (a|b)*aa(a|b)expression (a|b)*aa(a|b) Any string of a’s and b’s such that the third and second to last
characters are a’s Or strings of a’s and b’s that end in aaa or aab
3c. Regular expression for strings from {a,b,c}* all a’s 3c. Regular expression for strings from {a,b,c}* all a’s before any bbefore any b (a | c)* (b | c)*
– 7 – CSCE 531 Spring 2006
3a. Mistakes; imprecise answers, etc.3a. Mistakes; imprecise answers, etc.
x? --- zero or one x’s (spaces )x? --- zero or one x’s (spaces ) An optional match Matches anything that is not an “a”, “b” or “c”
[^abc] --- any single character not ‘a’, ‘b’ or ‘c’[^abc] --- any single character not ‘a’, ‘b’ or ‘c’ Negated character class any thing other than [abc] Any string other than abc Any string that does not contain the characters ‘a’, ‘b’ and ‘c’
ab+ --- a single ‘a’ followed by one or more ‘b’s’ab+ --- a single ‘a’ followed by one or more ‘b’s’
ab{3,5} --- a single ‘a’ followed by 3, 4, or 5 b’sab{3,5} --- a single ‘a’ followed by 3, 4, or 5 b’s Matches 3 to 5 b’s
– 8 – CSCE 531 Spring 2006
3b. Mistakes 3b. Mistakes A string of a’s and b’s which contain two consecutive a’s (a|
b)*aa(a|b)*
3c. Mistakes, etc. Notations 3c. Mistakes, etc. Notations L3c – the language specified in 3c
La – the language specified by the student’s answer
MistakesMistakes a*b*c* | c* a* b* | a* c* b* --- acacb is not in La .
(a* | c*) b* c* -- close but acbcb is not in La .
((a* | c*) b c*)* -- close but abcabc is in La .
– 9 – CSCE 531 Spring 2006
4. Points(4/4/2/4) Left Recursion 4. Points(4/4/2/4) Left Recursion SS Sa | bL Sa | bL
L L ScL | Sd | a ScL | Sd | a
4a. Equivalent non left recursive grammar4a. Equivalent non left recursive grammar
The only left recursive production is SThe only left recursive production is S Sa Sa
The problem would have been more interesting if The problem would have been more interesting if SSLb were to replace SLb were to replace SbL because then we would bL because then we would have some non-direct recursion to eliminatehave some non-direct recursion to eliminate
To handle STo handle SSSαα | | ββ ( (αα=a, =a, ββ=bL) replace with S=bL) replace with S ββS’ S’ and S’and S’ ααS’ | S’ | ЄЄ, so, so
S S bLS’ bLS’
S’ S’ aS’ | aS’ | ЄЄ
L L ScL | Sd | a ScL | Sd | a
– 10 – CSCE 531 Spring 2006
Common error: rewrite Eliminate LCommon error: rewrite Eliminate L
SS Sa | bL Sa | bL
L L ScL | Sd | a ScL | Sd | a
The idea some students used is to substitute for L the The idea some students used is to substitute for L the right hand sides of the L productions to obtainright hand sides of the L productions to obtain
SS Sa | bScL | bSd | ba Sa | bScL | bSd | ba
But what’s wrong with this?But what’s wrong with this?
We still have an L!!!We still have an L!!!
This is because the L productions are recursive; so no This is because the L productions are recursive; so no matter how many times you rewrite it you can never matter how many times you rewrite it you can never get rid of the L.get rid of the L.
– 11 – CSCE 531 Spring 2006
4b Left Factor this resulting grammar4b Left Factor this resulting grammarS S bLS’ bLS’
S’ S’ aS’ | aS’ | ЄЄ
L L ScL | Sd | a ScL | Sd | a
The only portion needing factoring is LThe only portion needing factoring is LScL | SdScL | Sd
SoSo
L L SL’ | a SL’ | a
L’ L’ cL | d cL | d
With the S and S’ productions from aboveWith the S and S’ productions from above
S S bLS’ bLS’
S’ S’ aS’ | aS’ | ЄЄ
– 12 – CSCE 531 Spring 2006
4. continued4. continued
4c.4c. (2pts) a grammar is LL(1) if in constructing the (2pts) a grammar is LL(1) if in constructing the predictive parse table there are no multiply defined predictive parse table there are no multiply defined entriesentries
There is an alternate definition in the text page 192.There is an alternate definition in the text page 192.
G is LL(1) if and only if if AG is LL(1) if and only if if Aαα | | ββ are two productions are two productions then the following conditions holdthen the following conditions hold1. FIRST*(α) ∩ FIRST*(β) = Φ
2. At most one of α and β can derive the empty string Є
3. If β Є then α does not derive any string beginning with a token in FOLLOW(A)
4d.4d.(4 pts) Pop A off the stack and push X Y and Z in (4 pts) Pop A off the stack and push X Y and Z in reverse orderreverse order
– 13 – CSCE 531 Spring 2006
5 Recursive Descent Parsing5 Recursive Descent Parsing5a. Construct a transition diagram for X given the 5a. Construct a transition diagram for X given the
productions X productions X aAB | bB | aAB | bB | ЄЄ
BAa
b B
ЄЄ
– 14 – CSCE 531 Spring 2006
5b. Recursive Descent Parsing5b. Recursive Descent Parsing5b. Construct a recursive parsing routine for X assuming 5b. Construct a recursive parsing routine for X assuming
routines for A and B.routines for A and B.
Reference Transition diagrams in fig 4.12 used to Reference Transition diagrams in fig 4.12 used to generate parsing routine in Chapter 2, page 75.generate parsing routine in Chapter 2, page 75.
ParseX (){ParseX (){
if (current_token == ‘a’){if (current_token == ‘a’){
parseA();parseA();
parseB();parseB();
}else if (current_token == ‘b’){}else if (current_token == ‘b’){
parseB();parseB();
}else return; }else return; /* epsilon*//* epsilon*/
}}
– 15 – CSCE 531 Spring 2006
6 FIRST, FOLLOW Predictive Parsing 6 FIRST, FOLLOW Predictive Parsing 6a. Non required6a. Non required6b. FIRST - FIRST (t6b. FIRST - FIRST (tαα) ={t} for any token t) ={t} for any token t
NonterminalNonterminal FIRST(N)FIRST(N)
ZZ { d, a, c, (FIRST(X)) }{ d, a, c, (FIRST(X)) }
YY { c, { c, ЄЄ, ,
XX { a, c, { a, c, ЄЄ, , (FIRST(Y))(FIRST(Y))
Considering productionsConsidering productions ZZd add d to FIRST(Z)d add d to FIRST(Z) YYc add c to FIRST(Y)c add c to FIRST(Y) YYЄЄ add add ЄЄ to FIRST(Y) to FIRST(Y) XXa add a to FIRST(X)a add a to FIRST(X) XXY add FIRST*(Y) to Y add FIRST*(Y) to
FIRST(X)FIRST(X)
Then consideringThen considering ZZXYZ add FIRST*(X) to XYZ add FIRST*(X) to
FIRST(Z)FIRST(Z) Since XSince XYYЄЄ add to add to
FIRST*(Y) to FIRST(Z)FIRST*(Y) to FIRST(Z) And since XAnd since XЄЄ and Y and YЄЄ
add FIRST(Z) to FIRST(Z) add FIRST(Z) to FIRST(Z) duh!duh!
– 16 – CSCE 531 Spring 2006
6c. FOLLOW6c. FOLLOW6c. Add $ to FOLLOW(Z)6c. Add $ to FOLLOW(Z)Here we will use FIRST*(N) to denote FIRST(N) – {Here we will use FIRST*(N) to denote FIRST(N) – {ЄЄ}}
NonterminalNonterminal FOLLOW(N)FOLLOW(N)
ZZ { $, }{ $, }
YY {FIRST*(Z)=d, a, c, } ={d, a, c, }{FIRST*(Z)=d, a, c, } ={d, a, c, }
XX {FIRST*(Y)= c, FIRST*(Z)=d, a, c, } ={d, a, c, }{FIRST*(Y)= c, FIRST*(Z)=d, a, c, } ={d, a, c, }
Considering productionsConsidering productions ZZXYZ add XYZ add
FIRST*(Y) to FOLLOW(X) FIRST*(Z) to FOLLOW(Y)
Since ZSince Z XYZ XYZ X XЄЄZZ FIRST*(Z) to FOLLOW(X)
And consideringAnd considering XXY add FOLLOW(X) to Y add FOLLOW(X) to
FOLLOW(Y)FOLLOW(Y)
– 17 – CSCE 531 Spring 2006
6d. Predictive Parse Table6d. Predictive Parse Tableaa cc dd $$
ZZ ZZXYZXYZ ZZXYZXYZ ZZXYZ,XYZ,
Z Z d d
YY YYЄЄ YYc, Yc, YЄЄ YYЄЄ
XX XX a, X a, XYY XXYY XXYY
– 18 – CSCE 531 Spring 2006
6d. Predictive Parse Table Analysis6d. Predictive Parse Table AnalysisConsider Productions in order and add them to M[N,t] according to Consider Productions in order and add them to M[N,t] according to algorithm 4.4algorithm 4.4
ZZXYZXYZ Using rule 2 add ZXYZ for each a in FIRST*(XYZ)
But since Є is in FIRST(X) and FIRST(Y) FIRST*(XYZ) = FIRST*(X) U FIRST*(Y) U FIRST*(Z) = {a, c, d }
Rule 3 does not apply as XYZ does not derive Є
Z Z d d Using rule 2 again add Zd for M[Z, d] double entry not LL(1)
YYc similarly adds Yc similarly adds Yc to M[Y, c]c to M[Y, c]
And XAnd X a adds X a adds X a to M[X, a] a to M[X, a]
YYЄЄ, , using rule3 add to M[Y, b] foreach token b in FOLLOW(Y)={a,c,d}using rule3 add to M[Y, b] foreach token b in FOLLOW(Y)={a,c,d}
XXY using rule 2 add to a to M[X, a] for each a in FIRST*(Y)={c}Y using rule 2 add to a to M[X, a] for each a in FIRST*(Y)={c}
XXYY, , using rule3 add to M[X, b] for each token b in FOLLOW(X)={a,c,d}using rule3 add to M[X, b] for each token b in FOLLOW(X)={a,c,d}
– 19 – CSCE 531 Spring 2006
7. LR(0) Sets of Items7. LR(0) Sets of Items
D D T L ; | T L ; | ЄЄ Tokens = {‘;’, id, ‘,’, int, float}Tokens = {‘;’, id, ‘,’, int, float}
L L L , id | id L , id | id
T T int | float int | float
Augment with S’ Augment with S’ D D
JJ00 = closure({S’ = closure({S’ ● D}) ● D})
= { [S’ = { [S’ ● D], [D ● D], [D ● T L ;], [D ● T L ;], [D●], [T●], [T●int], [T●int], [T ●float]} ●float]}
Now consider GOTO(JNow consider GOTO(J00, X) for each X just to right of “dot”, X) for each X just to right of “dot”
JJ11 = GOTO(J = GOTO(J00, D) = closure({[S’ , D) = closure({[S’ D ● ]} = {[S’ D ● ]} = {[S’ D ● ]} D ● ]}
JJ22 = GOTO(J = GOTO(J00, T) = closure({[D, T) = closure({[D T ● L ;]} T ● L ;]}
= {[D= {[D T ● L ;], [L T ● L ;], [L ● L , id], [L ● L , id], [L ● id]} ● id]}
– 20 – CSCE 531 Spring 2006
7. LR(0) Sets of Items7. LR(0) Sets of Items
JJ33 = GOTO(J = GOTO(J00, int) = closure({[T, int) = closure({[T int ● ]} = {[T int ● ]} = {[T int ● ]} int ● ]}
JJ44 = GOTO(J = GOTO(J00, float) = closure({[T, float) = closure({[T float ●]} = {[T float ●]} = {[Tfloat ● ]}float ● ]}
JJ55 = GOTO(J = GOTO(J22, L) = closure({[D, L) = closure({[D T L ● ;], [L T L ● ;], [L L ● , id]}) L ● , id]})
= ({[D= ({[D T L ● ;], [L T L ● ;], [L L ● , id]}) L ● , id]})
JJ66 = GOTO(J = GOTO(J22, id) = closure({[L , id) = closure({[L id ● ]}) = {[L id ● ]}) = {[L id ● ]} id ● ]}
JJ77 = GOTO(J = GOTO(J55, ; ) = closure( {[L , ; ) = closure( {[L T L ; ● ]}) = {[L T L ; ● ]}) = {[L T L ; ● ]} T L ; ● ]}
JJ88 = GOTO(J = GOTO(J55, , ) = closure( {[L , , ) = closure( {[L L , ● id]}) = {[L L , ● id]}) = {[L L , ● id]} L , ● id]}
JJ99 = GOTO(J = GOTO(J88, id ) = closure( {[L , id ) = closure( {[L L , id ●]}) = {[L L , id ●]}) = {[L L , L , id●]}id●]}
– 21 – CSCE 531 Spring 2006
What I should have also asked .What I should have also asked .
Leftmost derivations, parse treesLeftmost derivations, parse trees
Ambiguous grammarsAmbiguous grammars