TUTORIAL 3. Date: 6th March Wednesday ---------- 1. We want to examine the issues that arise when three address code is generated for a procedure call. To this end examine the following example carefully. var a: array [1..10] of integer; x: real; i:integer; procedure P(var x: integer; y:real) begin x := x + y end begin --- a[a[i]] = 6; P(a[i], a[i]+1); --- end. The code that we intend to generate for this program is: proc P t1 = *x // x is a var parameter, has to be dereferenced t2 = int_to_real(t1) t3 = t2 +_real y t4 = real_to_int(t3) *x = t4 return proc main --- t5 = i * 4 // code for a[a[i]] = 6, begin t6 = addr(a) - 4 t7 = t6[t5] t8 = addr(a) - 4 t8[t7] = 6 t9 = i * 4 // code for l-value of a[i] t10 = addr(a) - 4 // being passed to a var parameter t11 = t9 + t10 t12 = i * 4 // code for r-value of a[i] + 1 t13 = addr(a) - 4 // being passed to a value paramete t14 = t12[t13] t15 = t14 + 1 param t11 param t15 call p --- return Here is the grammar that you have to deal with: program -> D begin S end D -> varD; procD varD -> var id : type | varD ; varD type -> basictype | array range of basictype basictype -> integer | real procD -> proc id (var id, id); varD; begin S end | procD; procD range -> [integer .. integer] * S -> S ; S | ass | proccall * ass -> lexp := rexp * lexp -> id | id[rexp] * rexp -> id | rexp + rexp | id[rexp] * proccall -> call id (lexp, rexp) Write L-attributed definitions for the productions marked with a * to generate intermediate code. Assume that during processing of the decalration part of the grammar, the symboltable was populated with whatever information is required. 2. Consider the grammar shown below in which P and S are the only non-terminals: P -> S dot S -> ass S -> S ; S S -> while cond do S S -> break The effect of the break statement is to jump to the statement following all the enclosing whiles. Write a syntax directed translation scheme which will compute an attribute called jumpto of each break, indicating the target of the break statement. Correspondingly there should be an attribute called label for each of the terminals ass, break, while and dot, standing for the destination of jumps due to breaks. This attribute may be computed only at relevant places. As an example, the sentence w1 c1 d1 w2 c2 d2 b1 ; b2 ; w3 c3 d3 b3 ; w4 c4 d4 b4 dot should result in the following attribute values: b1.jumpto = b2.jumpto = 1, w3.label = 1, b3.jumpto = 2, w4.label = 2, b4.jumpto = 3 and dot.label = 3 You may use as many attributes as you want, but clearly describe their meaning. Also, for the example sentence shown above, draw the parse tree and indicate how different attribute value is generated and transmitted.