Departmet of Computer ciece Columbia Uiversity olutios to Fial COM W45 Programmig Laguages ad Traslators Moday, May 4, 2009 4:0-5:25pm, 309 Havemeyer Closed book, o aids. Do questios 5. Each questio is worth 20 poits. Questio 6 is optioal, extra credit, 0 poits.. Here is a fragmet of C code: struct studet { it id; char studet[30]; } studet; a) Explai the roles of the three uses of the idetifier studet. struct studet { // here studet is a structure tag it id; char studet[30]; // here studet is a structure member } studet; // here studet is a variable b) Are these three uses i the same scope? Explai. The three uses are i the same scope but i differet ame spaces. 2. Cosider the cotext-free grammar G: ab ba a) Describe L(G). how two parse trees for the setece abab i L(G). L(G) is the set of all strigs of a s ad b s with the same umber of a s as b s. a b a b b a a b
b) Costruct the LR() parsig actio ad goto tables for L(G). how the behavior of a LR() parser usig these tables o the iput abab. The sets of items for the augmeted grammar are: I0: ab ba I3: b a ab ba I: I2: a b ab ba I4: a b I5: b a I6: ab ab ba I9: ab I7: ba ab ba I8: ab The parsig actio ad goto tables costructed from these sets of items are: tate Actio Goto a b $ 0 s2/r (3) s3/r (3) r(3) accept 2 s2/r (3) s3/r (3) r(3) 4 3 s2/r (3) s3/r (3) r(3) 5 4 s6 5 s7 6 s2/r (3) s3/r (3) r(3) 8 7 s2/r (3) s3/r (3) r(3) 9 8 r() r() r() 9 r(2) r(2) r(2) Note the multiple shift-reduce coflicts that arise from the ambiguity i the grammar. O the iput abab, here is oe sequece of moves a LR() parser usig these tables ca make: 2
tack ymbols Iput Actio 0 abab$ shift 2 02 a bab$ shift 3 023 ab ab$ reduce by 0235 ab ab$ shift 7 02357 aba b$ reduce by 023579 aba b$ reduce by ba 024 a b$ shift 6 0246 ab $ reduce by 02468 ab $ reduce by ab 0 $ accept This sequece of moves correspods to the first parse tree above. There is aother sequece of moves correspodig to the secod parse tree. 3. Cosider the followig partial sytax-directed defiitio for traslatig ifstatemets ito three address code: Productio P assig if ( B ) ematic Rules.ext = ewlabel() P.code =.code label(.ext).code = assig.code B.true = ewlabel() B.false =.ext.ext =.ext.code = B.code label(b.true).code B B && B 2? B ot B? B true? B false? Here the fuctio ewlabel() creates a ew label each time it is called ad label(l) attaches label L to the ext three-address istructio to be geerated. a) Fill i the sematic rules for the B-productios (they represet boolea expressios) usig jumps to true ad false labels. ee ALU, p. 404. b) how how your DD traslates the if-statemet if (true && ot false) assig 3
ito three-address istructios by costructig a aotated parse tree for the if-statemet. P P.code = "goto L3 L3: goto L2 L2: assig.code L:".ext = "L" P.code = "goto L3 L3: goto L2 L2: assig.code" if ( B ) B.true = "L2".ext = "L" B.false = "L" B.code = "goto L3 L3: goto L2".code = "assig.code" B && B B.true = "L3" B.true = "L2" B.false = "L" B.false = "L" B.code = "goto L3" B.code = "goto L2" assig true ot B B.true = "L" B.false = "L2" B.code = "goto L2" false 4
4. Cosider the arithmetic expressio a * b + c / (d e) ad a register machie with istructios of the form LD reg, src T dst, reg OP reg, reg2, reg3 // the registers eed ot be distict a) Draw a sytax tree for the expressio ad label the odes with Ershov umbers. The sytax tree of this expressio is isomorphic to the oe o p. 568, ALU. b) Geerate machie code for the expressio o a machie with two registers miimizig the umber of spills. This expressio eeds oe spill o a two-register machie. ee ALU, p. 572, for a isomorphic code sequece. 5. Cosider the followig sequece of three-address code: x = 0 i = 0 L: t = i * 4 t2 = a[t] t3 = i * 4 t4 = b[t3] t5 = t2 * t4 x = x + t5 i = i + if i < goto L a) Draw a flow graph for this three-address code. 5
ENTRY () x = 0 (2) i = 0 B (3)L:t = i * 4 (4) t2 = a[t] (5) t3 = i * 4 (6) t4 = b[t3] (7) t5 = t2 * t4 (8) x = x + t5 (9) i = i + (0) if i< goto L B2 EXIT b) Optimize this code by elimiatig commo subexpressios, performig reductio i stregth o iductio variables, ad elimiatig all the iductio variables that you ca. tate what trasformatios you are usig at each optimizatio step. First, we ca elimiate the commo subexpressio i*4 i lies (3) ad (4) by usig t i place of t3 i lie (6) ad elimiatig lie (5). Next, t ad i are both iductio variables i block B2. We ca elimiate either oe of these iductio variables i the loop. We choose to elimiate t by replacig lie (9) by i = i + 4, addig the statemet t6 = 4 *, to the ed of block B, replacig the test i< i lie (0) by i<t6, ad usig i to idex the arrays. The resultig optimized flow graph is: 6
ENTRY x = 0 i = 0 t6 = 4 * B t2 = a[i] t4 = b[i] t5 = t2 * t4 x = x + t5 i = i + 4 if i<t6 goto L B2 EXIT 6. Optioal [extra credit, 0 poits]. Cosider agai the cotext-free grammar G from questio 2: ab ba a) How may parse trees are there for the setece ababab? There are 5 parse trees. b) Write a recurrece relatio for the umber of parse trees for the setece (ab). Let T() be the umber of parse trees for (ab). By symmetry T() is also the umber of parse trees for (ba). For the base cases, we have T(0) = T() = ice we ca write (ab) as a(ba) i b(ab) --i, we have the recurrece 7
) ( ) ( ) ( 0 i T i T T i Σ = = c) What is the solutio to your recurrece? The solutio is the Catala umbers, + = T 2 ) (. 8