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
Production
E E1 + T
E E1 - T
ET
T T1 ∗ F
T T1 / F
TF
F (E)
F digit
F id
Semantic Rules
E.t = E1.t || ‘+’ || T.t
E.d = E1.d || ‘+’ || T.d
E.t = E1.t || ‘-’ || T.t
E.d = E1.d || ‘-’ || T.d
E.t = T1.t
E.d = T1.d
T.t = T1.t || ‘∗’ || F.t
T.d = T1.t || ‘∗’ || F.d || ‘+’ F.t || ‘∗’ || T1.d
T.t = T1.t || ‘/’ || F.t
T.d = ‘(‘ || F.t || ‘∗’ || T1.d || ‘-’ T1.t || ‘∗’ || F.d || ‘)’ || ‘/’ || F.t ‘∗’ || F.t
T.t = F.t
T.d = F.d
F.t = ‘(‘ || E.t || ‘)’
F.d = ‘(‘ || E.d || ‘)’
F.t = digit.value
F.d = 0
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.
EE+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)
Production
E E1 + T
ET
T num.num
T num
Semantic Rules
if (E1.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)
Production
E E1 + T
ET
T num1.num2
T num
Semantic Rules
if (E1.type = int & T.type = int) then
E.type = int
E.t = E1.t || T.t || ‘+’
else if E1.type = int then
E.type = float
E.t = intToFloat(E1.t) || T.t || ‘+’
else if T.type = int then
E.type = float
E.t = E1.t || intToFloat (T.t) || ‘+’
else
E.type = float
E.t = E1.t || T.t || ‘+’
E.type = T.type
E.t = T.t
T.type = float
T.t = num1.value || ‘.’ || num2.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.
Production
D int A
D float B
A A , id
A id
B B , id
B id
Semantic Rules
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.
Production
D id L
L , id L1
L :T
T int
T float
Semantic Rules
addType(id.entry, L.type)
L.type = L1.type
addType(id.entry, L1.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 {B1.ps = B.ps} B1 {M.i = B.ps} M {B2.ps = M.s} B2 {B.ht = max(B1.ht, B2.ht)}
B { B1.ps = B.ps} B1 sub {N.i = B.ps} N { B2.ps = N.s} B2 {B.ht = disp(B1.ht, B2.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.
Production
S → List 100
List → List1 Bit
List → Bit
Bit → 0
Bit → 1
Semantic Rules
List.pos = 3
S.value = List.value + 4
List1.pos = List.pos + 1
Bit.pos = List.pos
List.value = List1.value + Bit.value
Bit.pos = List.pos
List.value = Bit.value
Bit.value = 0
Bit.value = 2Bit.pos
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
S
B
B
C
C
→B+
→C→n
→ B1 , n
→n
→ C1 , n
||
||
||
||
||
||
S.val = B.val; print(B.val)
S.val = C.val; print(C.val)
B.val = value(n)
B.val = max(B1.val,value(n))
C.val = value(n)
C.val = min(C1.val,value(n))
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
B0|1
Semantic Rules
)S.val = L1.val + (L2.val/2^L2.bit
S.val = L.val
L.val = L1.val ∗ 2 + B.val
L.bit = L.bit+1
L.val = B.val
L.bit = 1
B.val = 0
B.val = 1
Production
S L1.L2
SL
L L1 B
بما انو هً اخر مناقشة كومباٌلر واخر مناقشة بعملها باالعادة
وبما انو بكرة 51أٌار ذكرى نكبتنا ال 66فبدي اختم انو هللا ٌرجعلنا حقنا و ترجعلنا فلسطٌن ...
كل فلسطٌن ...وكل الجئ ٌرجع على أرضه ...
وفلسطٌن تجمعنا والعودة موعدنا
#انتماء
#نكبة_فلسطين
#nakbba_66
وبما انو الٌوم هو الٌوم ال 15إلضراب األسرى اإلدراٌٌن إلسقاط االعتقال اإلداري بدي زكركو انو
األسرى بٌستنو نتضامن معهم لحتى ٌنتصرو وٌحققو مطالبهم واقل شً بنقدر نعملو انو ندعٌلهم ...
#مً_وملح
Best Wishes
LB
B0
B1
8
© Copyright 2025 Paperzz