210 likes | 330 Vues
This document provides an in-depth post-mortem examination of the CSCE 531 Compiler Construction course, held on February 20, 2006. It includes an overview of key topics covered, including recursive descent parsing, transition diagrams, LL(1) table construction, and the construction of finite automata from regular expressions. Additionally, it addresses common student mistakes in grammar evaluation and parsing techniques, accompanied by grade distribution data and points evaluations for various exam questions.
E N D
Test 1 Post Mortem CSCE 531 Compiler Construction • Topics • Questions and points • Grade Distribution • Answers February 20, 2006
Questions and Point Evaluations • 5 Recursive Descent parsing • Transition diagram 7pts • recursive parsing 7pts • LL(1) table construction pts • Modifications 1 pt • FIRST 4 pts • FOLLOW 4 pts • Table 5 pts • LR(0) sets of items 14 pts • 1a. Reg exprNFA 14 pts • 2. Subset Construction 14 pts • Grammars, derivations • Flex/Lex patterns 4 pts • L( (a|b)*aa(a|b) ) 5 pts • Reg expr a’s before b’s 5pts • Left-recusion • Left recursion 4 pts • Left factor 4 pts • LL(1) 2pts • M[A,a] 4pts
1. Thompson Construction Reg Expr NFA • (a | b)(ab)*a solution build from innermost out ε a ε ε ε ε a ε b ε ε a ε b ε ε
2. Subset Construction NFA DFA • Accepting states those that contain s6:
3. Regular Languages • 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 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 before any b • (a | c)* (b | c)*
3a. Mistakes; imprecise answers, etc. • 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’ • 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{3,5} --- a single ‘a’ followed by 3, 4, or 5 b’s • Matches 3 to 5 b’s
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 • L3c – the language specified in 3c • La – the language specified by the student’s answer • Mistakes • 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 .
4. Points(4/4/2/4) Left Recursion • S Sa | bL • L ScL | Sd | a • 4a. Equivalent non left recursive grammar • The only left recursive production is S Sa • The problem would have been more interesting if SLb were to replace SbL because then we would have some non-direct recursion to eliminate • To handle SSα | β (α=a, β=bL) replace with S βS’ and S’ αS’ | Є, so • S bLS’ • S’ aS’ | Є • L ScL | Sd | a
Common error: rewrite Eliminate L • S Sa | bL • L ScL | Sd | a • The idea some students used is to substitute for L the right habd sides of the L productions to obtain • S Sa | bScL | bSd | ba • But what’s wrong with this? • We still have an L!!! • This is because the L productions are recursive; so no matter how many times you rewrite it you can never get rid of the L.
4b Left Factor this resulting grammar • S bLS’ • S’ aS’ | Є • L ScL | Sd | a • The only portion needing factoring is LScL | Sd • So • L SL’ | a • L’ cL | d • With the S and S’ productions from above • S bLS’ • S’ aS’ | Є
4. continued • 4c. (2pts) a grammar is LL(1) if in constructing the predictive parse table there are no multiply defined entries • There is an alternate definition in the text page 192. • G is LL(1) if and only if if Aα | β are two productions then the following conditions hold • FIRST*(α) ∩ FIRST*(β) = Φ • At most one of α and β can derive the empty string Є • If β Є then α does not derive any string beginning with a token in FOLLOW(A) • 4d. (4 pts) Pop A off the stack and push X Y and Z in reverse order
5 Recursive Descent Parsing • 5a. Construct a transition diagram for X given the productions X aAB | bB | Є Є a A B b B
5b. Recursive Descent Parsing • 5b. Construct a recursive parsing routine for X assuming routines for A and B. • Reference Transition diagrams in fig 4.12 used to generate parsing routine in Chapter 2, page 75. • ParseX (){ • if (current_token == ‘a’){ • parseA(); • parseB(); • }else if (current_token == ‘b’){ • parseB(); • }else return; /* epsilon*/ • }
6 FIRST, FOLLOW Predictive Parsing • 6a. Non required • 6b. FIRST - FIRST (tα) ={t} for any token t • Considering productions • Zd add d to FIRST(Z) • Yc add c to FIRST(Y) • YЄ add Є to FIRST(Y) • Xa add a to FIRST(X) • XY add FIRST*(Y) to FIRST(X) • Then considering • ZXYZ add FIRST*(X) to FIRST(Z) • Since XYЄ add to FIRST*(Y) to FIRST(Z) • And since XЄ and YЄ add FIRST(Z) to FIRST(Z) duh!
6c. FOLLOW • 6c. Add $ to FOLLOW(Z) • Here we will use FIRST*(N) to denote FIRST(N) – {Є} • Considering productions • ZXYZ add • FIRST*(Y) to FOLLOW(X) • FIRST*(Z) to FOLLOW(Y) • Since Z XYZ XЄZ • FIRST*(Z) to FOLLOW(X) • And considering • XY add FOLLOW(X) to FOLLOW(Y)
6d. Predictive Parse Table Analysis • Consider Productions in order and add them to M[N,t] according to algorithm 4.4 • ZXYZ • 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 d • Using rule 2 again add Zd for M[Z, d] double entry not LL(1) • Yc similarly adds Yc to M[Y, c] • And X a adds X a to M[X, a] • YЄ, using rule3 add to M[Y, b] foreach token b in FOLLOW(Y)={a,c,d} • XY using rule 2 add to a to M[X, a] for each a in FIRST*(Y)={c} • XY, using rule3 add to M[X, b] for each token b in FOLLOW(X)={a,c,d}
7. LR(0) Sets of Items • D T L ; | ЄTokens = {‘;’, id, ‘,’, int, float} • L L , id | id • T int | float • Augment with S’ D • J0 = closure({S’ ● D}) • = { [S’ ● D], [D ● T L ;], [D●], [T●int], [T ●float]} • Now consider GOTO(J0, X) for each X just to right of “dot” • J1 = GOTO(J0, D) = closure({[S’ D ● ]} = {[S’ D ● ]} • J2 = GOTO(J0, T) = closure({[D T ● L ;]} • = {[D T ● L ;], [L ● L , id], [L ● id]}
7. LR(0) Sets of Items • J3 = GOTO(J0, int) = closure({[T int ● ]} = {[T int ● ]} • J4 = GOTO(J0, float) = closure({[T float ●]} = {[Tfloat ● ]} • J5 = GOTO(J2, L) = closure({[D T L ● ;], [L L ● , id]}) • = ({[D T L ● ;], [L L ● , id]}) • J6 = GOTO(J2, id) = closure({[L id ● ]}) = {[L id ● ]} • J7 = GOTO(J5, ; ) = closure( {[L T L ; ● ]}) = {[L T L ; ● ]} • J8 = GOTO(J5, , ) = closure( {[L L , ● id]}) = {[L L , ● id]} • J9 = GOTO(J8, id ) = closure( {[L L , id ●]}) = {[L L , id●]}
What I should have also asked . • Leftmost derivations, parse trees • Ambiguous grammars