Islamic University Gaza Engineering Faculty Department of Computer Engineering ECOM 5060: Compiler Design Discussion Chapter 5 Syntax Directed Translation Eng. Eman R. Habib May, 2014
2 Computer Architecture Discussion Exercise 1: Give an SDD to differentiate expressions such as x (3 x + x x) involving the operators + and, the variable x, and constants. Assume that no simplification occurs, so that, for example, 3 x will be translated into 3 1 + 0 x E E 1 + T E.t = E 1.t + T.t E.d = E 1.d + T.d E E 1 - T E.t = E 1.t - T.t E.d = E 1.d - T.d E T E.t = T 1.t E.d = T 1.d T T 1 F T.t = T 1.t F.t T.d = T 1.t F.d + F.t T 1.d T T 1 / F T.t = T 1.t / F.t T.d = ( F.t T 1.d - T 1.t F.d ) / F.t F.t T F T.t = F.t T.d = F.d F (E) F.t = ( E.t ) F.d = ( E.d ) F digit F.t = digit.value F.d = 0 F id F.t = id.lexem F.d = 1 Exercise 2: Below is a grammar for expressions involving operator + and integer or floating-point operands. Floating-point numbers are distinguished by having a decimal point. E E + T T T num.num num a) Give an SDD to determine the type of each T and expression E. b) Extend your SDD of (a) to translate expressions into postfix notation. Use the unary operator inttofloat to turn an integer into an equivalent float. a) E E 1 + T E T T num.num T num if (E 1.type = int & T.type = int) then E.type = int else E.type = float E.type = T.type T.type = float T.type = int
3 Computer Architecture Discussion b) E E 1 + T E T T num 1.num 2 T num if (E 1.type = int & T.type = int) then E.type = int E.t = E 1.t T.t + else if E 1.type = int then E.type = float E.t = inttofloat(e 1.t) T.t + else if T.type = int then E.type = float E.t = E 1.t inttofloat (T.t) + else E.type = float E.t = E 1.t T.t + E.type = T.type E.t = T.t T.type = float T.t = num 1.value. num 2.value T.type = int T.t = num.value Exercise 3: Rewrite the underlying grammar in the syntax-directed definition of Example 5.10 so that type information can be propagated using synthesized attributes alone. D int A D float B A A, id A id B B, id B id addtype(id.entry, int) addtype(id.entry, int) addtype(id.entry, float) addtype(id.entry, float)
4 Computer Architecture Discussion Exercise 4: Suppose declarations are generated by the following grammar. D id L L, id L :T T int float Construt a translation scheme to enter the type of each identifier into the symbol table. D id L L, id L 1 L :T T int T float addtype(id.entry, L.type) L.type = L 1.type addtype(id.entry, L 1.type) L.type = T.type T.type = int T.type = float Exercise 5: Turn the L-attributed SDD of the following figure into an SDT. S L {B.ps = L.s} B {S.ht = B.ht} L ϵ {L.s = 10} B {B 1.ps = B.ps} B 1 {M.i = B.ps} M {B 2.ps = M.s} B 2 {B.ht = max(b 1.ht, B 2.ht)} B { B 1.ps = B.ps} B1 sub {N.i = B.ps} N { B 2.ps = N.s} B 2 {B.ht = disp(b 1.ht, B 2.ht)} B text {B.ht = text.h X B.ps} M ϵ {M.s = M.i} N ϵ {N.s = shrink(n.i)}
5 Computer Architecture Discussion Exercise 6: In this problem you are asked to develop a syntax-directed translation scheme to evaluate binary strings. First you are asked to develop the CFG and then the corresponding attribute grammar illustrating its operation via an example. a) Develop a context-free grammar (CFG) that describes all the binary strings (over the alphabet {0,1}) whose decimal values are multiples of 4. Assume that there can be leading 0s digits but any input sequence that corresponds to the decimal value zero is not in the language, i.e. 0000 but 00100 is in the language. b) For the CFG developed above define the attributes for the non-terminal symbols and terminal symbols of the grammar and develop semantic rules that can be used to compute as the value of an attribute at the start symbol, the numeric decimal value of the input string. For instance for the string 00100 the decimal values of an attribute you need to defined and that is associated with the start symbol should be the value 4. c) Using the attribute grammar developed in b) show the annotated parse tree for the input 11100 illustrating the dependences between the evaluation of the attributes in this particular parse tree. S List 100 R List.pos = 3 S.value = (List.value + 4) * 2 R.Bits R R 1 0 R.Bits = R 1.Bits + 1 R Ԑ R.Bits = 0 List List 1 Bit List 1.pos = List.pos + 1 Bit.pos = List.pos List.value = List 1.value + Bit.value List Bit Bit.pos = List.pos List.value = Bit.value Bit 0 Bit.value = 0 Bit 1 Bit.value = 2 Bit.pos *2 0 =28 R Bits =0 Ԑ
6 Computer Architecture Discussion Exercise 7: Consider the following syntax-directed definition over the grammar defined by G = ({S, A, Sign}, S, {,, -, +, n }, P) with P the set of production and the corresponding semantic rules depicted below. There is a special terminal symbol n that is lexically matched by any string of one numeric digit and whose value is the numeric value of its decimal representation. For the non-terminal symbols in G we have defined two attributes, sign and value. The nonterminal A has these two attributes whereas S only has the value attribute and Sign only has the sign attribute. S A Sign S.val = A.val; A.sign = Sign.sign; print(a.val); Sign + Sign.sign = 1 Sign - Sign.sign = 0 A n A.val = value(n) A A1, n A1.sign = A.sign; if(a.sign = 1) then A.val = max (A1.val,value(n)); else A.val = min (A1.val,value(n)); For this Syntax-Directed Definition answer to the following questions: a) Explain the overall operation of this syntax-directed definition and indicate (with a brief explanation) which of the attributes are either synthesized or inherited. b) Give an attributed parse tree for the source string 5,2,3- and evaluate the attributes in the attributed parse tree depicting the order in which the at tributes need to be evaluated (if more than one order is possible indicate one.) c) Suggest a modified grammar and actions exclusively using synthesized attributes. Explain its basic operation. a) This syntax-directed definition computes the maximum or minimum of a sequence of integers depending on the suffix - or + respectively. As to the attributes, clearly the sign attribute for the Sign non-terminal is synthesized. The val attribute is also synthesized but the sign attribute for A is inherited as it flows from right-to-left (thus from top to bottom in the parse tree) for a production of A. b)
7 Computer Architecture Discussion c) The idea is to decouple at the top the two situations, computing a minimum or maximum based on the value of the Sign non-terminal. To accomplish this we have two very similar non-terminal symbols B and C as shown below, which replace the A terminal symbol. Note also the presence of two productions for S for the two computation functions, min and max and the absence of the non-terminal Sign. All attributes are now synthesized as shown in the figure below. S B + S.val = B.val; print(b.val) S C - S.val = C.val; print(c.val) B n B.val = value(n) B B 1, n B.val = max(b1.val,value(n)) C n C.val = value(n) C C 1, n C.val = min(c1.val,value(n))
8 Computer Architecture Discussion Exercise 8: Let synthesized attribute val give the value of the binary number generated by S in the following grammar. For example, on input 101.101, S.val = 5.625. S L.L L L LB B B 0 1 S L 1.L 2 S L L L 1 B L B B 0 B.val = 0 B 1 B.val = 1 S.val = L 1.val + (L 2.val/2^L 2.bit) S.val = L.val L.val = L 1.val 2 + B.val L.bit = L.bit+1 L.val = B.val L.bit = 1 باالعادة بعملها مناقشة واخر كومبايلر مناقشة اخر هي انو بما... فلسطين ترجعلنا و حقنا يرجعلنا هللا انو اختم فبدي 66 ال نكبتنا ذكرى أيار 15 بكرة انو وبما... أرضه على يرجع الجئ وكل... فلسطين كل موعدنا والعودة تجمعنا وفلسطين #انتماء #نكبة_فلسطين #nakbba_66 انو زكركو بدي اإلداري االعتقال إلسقاط اإلدرايين األسرى إلضراب 21 ال اليوم هو اليوم انو وبما... ندعيلهم انو نعملو بنقدر شي واقل مطالبهم ويحققو ينتصرو لحتى معهم نتضامن بيستنو األسرى #مي_وملح Best Wishes