ECE 468/573 Midterm 1 September 28, 2012 Nme:! Purdue emil:! Plese sign the following: I ffirm tht the nswers given on this test re mine nd mine lone. I did not receive help from ny person or mteril (other thn those explicitly llowed). X Prt Points Score 1 10 2 15 3 15 4 25 5 35 6 20 Totl 120
Prt 1: Short nswers (10 points) Hlf credit tken off for not providing justifiction. 1) You hve just come up with new kind of compiler optimiztion tht cn improve the run-time of code by fctor of 10, but is very slow. For code tht normlly tkes 10 seconds to run, the compiler tkes 1000 seconds to perform the optimiztion, but the optimized code runs in 1 second. Would you rther implement this optimiztion in stndrd compiler (tht compiles code before it runs) or in just-in-time compiler? Justify your nswer (4 points). Becuse this optimiztion tkes long time, it is hrd to justify performing it in JIT compiler, s the code would hve to run for very long time to mke up for the compiltion time. Hence, we would prefer to do this in stndrd compiler. 2) Suppose this optimiztion requires seeing the high-level structure of your code (e.g., the loops in the code). Should this optimiztion tke plce in the frontend of your compiler? Or the bck-end? Justify your nswer (3 points). Becuse the bckend strts with n intermedite representtion nd produces ssembly, it never gets to see the high-level structure of the code (e.g., loops re converted into brnches). Hence, the optimiztion needs to tke plce in the front-end, where the code structure is still vilble. 3) You hve just been employed by Intel to work on their compiler technology nd to give it more powerful optimiztions thn compilers for ARM. Should you be working on front-end optimiztions? Or bck-end? Justify your nswer (3 points.) Becuse the optimiztions you re doing re rchitecture specific, this needs to be done in the bck-end, which cn be specilized for prticulr rchitectures. Note, lso, tht ny optimiztions done in the front-end would likely help ARM s well, since they re not rchitecture-specific: probbly not something Intel would wnt to do!
Prt 2: Regulr expressions, finite utomt nd scnners (15 points) 1) Give n rgument for why we cnnot use regulr expressions to define progrmming lnguges like Micro (hint: think bout the wy blocks of code cn nest inside ech other). (6 points): Lnguges like Micro hve nested blocks tht strt with BEGIN nd end with END (or { nd }, etc.) Prsing the lnguge requires mtching n equl number of BEGINs with ENDs, which we cnnot do with regulr expression. 2) Consider the following NFA. Fill in the trnsition tble below with its corresponding DFA using the subset construction. (8 points): One point per row 2 c 4 6 1 b c 3 5 b 7 Stte Finl? b c 1 no 2 3 err 2 no err err 4 3 no 2, 3, 5 err 4 4 no 6 no no 2, 3, 5 no 2, 3, 5, 6 7 4 6 yes err err err 2, 3, 5, 6 yes 2, 3, 5, 6 7 4 7 yes err err err 3) List which sttes should be merged when you reduce the bove DFA (1 point): We cn combine sttes 6 nd 7: they re both finl nd they hve the sme behvior on every input. Note tht we cnnot combine 2, 3, 5 nd 2, 3, 5, 6 becuse one is finl nd one is not.
Prt 3: Grmmrs (15 points) Let G be the grmmr: S! AC$ A! xab A! B! y C! BCz C! Using this grmmr, nswer the following questions. 1) Drw the prse tree for the string xyyz$ (3 points) Not given here (but everyone got this right) 2) Describe the kinds of strings this grmmr cn generte. (3 points) x i y i y k z k (This question did not get counted) 3) Cn the lnguge of this grmmr be cptured by regulr expression? If so, give the regulr expression. If not, give short rgument why not. (4 points) No, becuse you need to be ble to count the numbers of x s nd z s to get the right number of y s, which regex cnnot do. (-2 points for no explntion) 4) Give grmmr tht cptures the following lnguge, which hs some number of s or b s, followed by the sme number of c s or d s (5 points) ( b) i (c d) i Mny possible nswers. Here s one: S! M M! AMC A! b C! c d
Prt 4: LL prsers (25 points) Answer the questions in this prt using the sme grmmr from Prt 3. 1) Give the First sets for ech non-terminl in the grmmr (8 pts) 2 points per set First(S) = {$, x, y} First(A) = {x, λ} First(B) = {y} First(C) = {y, λ} 2) Give the Follow sets for ech non-terminl in the grmmr (8 pts) 2 points per set Follow(S) = {} Follow(A) = {y, $} Follow(B) = {y, z, $} Follow(C) = {z, $} 3) Fill in the following prse tble (8 pts) 0.5 points per box x y z $ S 1 1 1 A 2 3 B 4 C 5 6 6 4) Is this grmmr LL(1) or not? Why or why not? (1 pt) Yes, becuse there re no conflicts in the prse tble.
Prt 5: LR(0) Prsers (35 points) Use the following grmmr for the next two questions: 1) Fill in the missing informtion for the for the following CFSM (including edge lbels) (14 points) 1 point per configurtion, 2 points for edge lbels Stte 0 S AA$ A xb A y 1.S! AA$ 2.A! xb 3.A! y 4.B! Az Stte 1 S A A$ A xb A y Stte 2 S AA $ Stte 3 S AA$ Stte 4 A y Stte 7 A xb Stte 5 A x B B Az A xb A y Stte 6 B A z Stte 8 B Az 2) List the reduce sttes in the bove CFSM, nd the shift sttes, ssuming stte 3 is n ccept stte. (4 points) Shift sttes: 0, 1, 2, 5, 6 Reduce sttes: 4, 7, 8 (3 ok) 3) Is this n LR(0) grmmr? Why or why not? (2 points) Yes. No S/R conflicts
4) Show the ctions tken by the prser s it prses xyzy$. Actions should either be of the form Shift X or Reduce R (goto X) If the prser gets to stte 3, it ccepts. You cn use the following tble to help show your work (you my not need ll of the rows) (15 points) -3 points per mistke (prtil credit given for crrying through errors) Prse stck Remining input Action 0 x y z y $ Shift 5 0 5 y z y $ Shift 4 0 5 4 z y $ Reduce 3 (goto 6) 0 5 6 z y $ Shift 8 0 5 6 8 y $ Reduce 4 (goto 7) 0 5 7 y $ Reduce 2 (goto 1) 0 1 y $ Shift 4 0 1 4 $ Reduce 3 (goto 2) 0 1 2 $ Shift 3 0 1 2 3 Accept
Prt 6: LR(1) Prsers (ECE 573 only) (20 points): Consider the following grmmr: 1) Fill in the missing informtion from this prtil LR(1) mchine (15 points) 1 point per configurtion Stte 0 S A$, {} A xyb, {$} A xb, {$} Stte 1 A x yb, {$} A x B, {$} B ya, {$} B y, {$} y x Stte 3 A xy B, {$} B y A, {$} B y, {$} B ya, {$} B y, {$} A xyb, {$} A xb, {$} y 1.S! A$ 2.A! xyb 3.A! xb 4.B! ya 5.B! y A B A Stte 4 S A $, {} $ Stte 4 S A$, {} Stte 4 A xyb, {$} B Stte 2 A xb, {$} x Stte 6 B y A, {$} B y, {$} A xyb, {$} A xb, {$} A Stte 5 B ya, {$} 2) Show wht the ction tble entries for Stte 3 would be (i.e., give the row for Stte 3). For reduce ctions, give wht rule would be reduced (4 points) x! Shift y! Shift $! Reduce(5) 3) Is this grmmrlr(0)? Why or why not? (1 point) No. Without lookhed, there would be shift/reduce conflicts in sttes 3 nd 6.