1 / 21

Test 1 Post Mortem

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

liora
Download Presentation

Test 1 Post Mortem

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Test 1 Post Mortem CSCE 531 Compiler Construction • Topics • Questions and points • Grade Distribution • Answers February 20, 2006

  2. 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 exprNFA 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

  3. Grade Distribution • 100 • 94 93 90 • 89 88 83 83 81 80 80 • 79 78 78 78 77 77 75 74 73 73 73 71 71 70 • 68 68 66 66 63 63 • 59 59 56 54 52 51 • 49 46 46 • 34 • 1 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 69

  4. 1. Thompson Construction Reg Expr  NFA • (a | b)(ab)*a solution built from innermost out ε a ε ε ε ε a ε b ε ε a ε b ε ε

  5. 2. Subset Construction NFA  DFA • Accepting states those that contain s6:

  6. 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)*

  7. 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

  8. 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 .

  9. 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 SLb were to replace SbL because then we would have some non-direct recursion to eliminate • To handle SSα | β (α=a, β=bL) replace with S βS’ and S’ αS’ | Є, so • S  bLS’ • S’  aS’ | Є • L  ScL | Sd | a

  10. Common error: rewrite Eliminate L • S Sa | bL • L  ScL | Sd | a • The idea some students used is to substitute for L the right hand 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.

  11. 4b Left Factor this resulting grammar • S  bLS’ • S’  aS’ | Є • L  ScL | Sd | a • The only portion needing factoring is LScL | Sd • So • L  SL’ | a • L’  cL | d • With the S and S’ productions from above • S  bLS’ • S’  aS’ | Є

  12. 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

  13. 5 Recursive Descent Parsing • 5a. Construct a transition diagram for X given the productions X  aAB | bB | Є Є a A B b B

  14. 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*/ • }

  15. 6 FIRST, FOLLOW Predictive Parsing • 6a. Non required • 6b. FIRST - FIRST (tα) ={t} for any token t • Considering productions • Zd add d to FIRST(Z) • Yc add c to FIRST(Y) • YЄ add Є to FIRST(Y) • Xa add a to FIRST(X) • XY add FIRST*(Y) to FIRST(X) • Then considering • ZXYZ add FIRST*(X) to FIRST(Z) • Since XYЄ add to FIRST*(Y) to FIRST(Z) • And since XЄ and YЄ add FIRST(Z) to FIRST(Z) duh!

  16. 6c. FOLLOW • 6c. Add $ to FOLLOW(Z) • Here we will use FIRST*(N) to denote FIRST(N) – {Є} • Considering productions • ZXYZ add • FIRST*(Y) to FOLLOW(X) • FIRST*(Z) to FOLLOW(Y) • Since Z XYZ  XЄZ • FIRST*(Z) to FOLLOW(X) • And considering • XY add FOLLOW(X) to FOLLOW(Y)

  17. 6d. Predictive Parse Table

  18. 6d. Predictive Parse Table Analysis • Consider Productions in order and add them to M[N,t] according to algorithm 4.4 • ZXYZ • Using rule 2 add ZXYZ 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 Zd for M[Z, d]  double entry  not LL(1) • Yc similarly adds Yc 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} • XY using rule 2 add to a to M[X, a] for each a in FIRST*(Y)={c} • XY, using rule3 add to M[X, b] for each token b in FOLLOW(X)={a,c,d}

  19. 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]}

  20. 7. LR(0) Sets of Items • J3 = GOTO(J0, int) = closure({[T int ● ]} = {[T int ● ]} • J4 = GOTO(J0, float) = closure({[T float ●]} = {[Tfloat ● ]} • 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●]}

  21. What I should have also asked . • Leftmost derivations, parse trees • Ambiguous grammars

More Related