2 Syntax Directed Transiation
2 Syntax Directed Transiation
2 Syntax Directed Transiation
In this phase, for each production CFG, we will give some seman- Translation schemes These schemes indicate the order in which
tic rule. semantic rules are to be evaluated. This is an input and output
mapping.
Syntax directed translation scheme
A CFG in which a program fragment called output action (seman-
tic action or semantic rule) is associated with each production is SyntAx directed definitionS
known as Syntax Directed Translation Scheme. A SDD is a generalization of a CFG in which each grammar sym-
These semantic rules are used to bol is associated with a set of attributes.
There are two types of set of attributes for a grammar symbol.
1. Generate intermediate code.
2. Put information into symbol table. 1. Synthesized attributes
3. Perform type checking. 2. Inherited attributes
4. Issues error messages. Each production rule is associated with a set of semantic rules.
6.28 | Unit 6 • Compiler Design
Semantic rules setup dependencies between attributes Example: An inherited attribute distributes type informa-
which can be represented by a dependency graph. tion to the various identifiers in a declaration.
The dependency graph determines the evaluation order For the grammar
of these semantic rules. D → TL
Evaluation of a semantic rule defines the value of an
T → int
attribute. But a semantic rule may also have some side
T → real
effects such as printing a value.
L → L1, id
Attribute grammar: An attribute grammar is a syntax L → id
directed definition in which the functions in semantic rules
‘cannot have side effects’. That is, The keyword int or real followed by a list of
identifiers.
Annotated parse tree: A parse tree showing the values of
In this T has synthesized attribute type: T.type. L has an
attributes at each node is called an annotated parse tree.
inherited attribute in L.in
The process of computing the attribute values at the
Rules associated with L call for procedure add type to the
nodes is called annotating (or decorating) of the parse tree.
type of each identifier to its entry in the symbol table.
In a SDD, each production A → ∝ is associated with a
set of semantic rules of the form:
b = f (c1, c2,… cn) where Production Semantic Rule
f : A function D → TL L.in = T.type
b can be one of the following: T → int T.type = integer
b is a ‘synthesized attribute’ of A and c1, c2,…cn are attrib-
utes of the grammar symbols in A → ∝. T → real T.type = real
The value of a ‘synthesized attribute’ at a node is com- L → L1, id addtype L1.in = L.in(id.entry, L.in)
puted from the value of attributes at the children of that
L → id addtype (id.entry, L.in)
node in the parse tree.
Example: The annotated parse tree for the sentence real id1, id2, id3 is
shown below:
Production Semantic Rule
D
expr → expr1 + term expr.t: = expr1.t||term.t||’+’
T⋅type = real L ⋅in = real
expr → expr1 – term expr.t: = expr1.t||term.t||’-‘
. . .
X⋅x Y⋅y
The Annotated parse tree for the expression 5 + 3 * 4 is
shown below: Example 2:
val
D E
E⋅val = 17 return
E2
E⋅val = 5 + T⋅val = 12 E1 +
val val
T⋅val = 5 T⋅val = 3 * F ⋅val = 4 Example 3: real p, q;
L⋅in = real
F⋅val = 5 F⋅val = 3 digit⋅lexval = 4
T ⋅type = real add type (q⋅real)
digit⋅lexval = 5 L1⋅in = real
digit⋅lexval = 3
id⋅entry = q
add type (P⋅real)
Example 1: Consider an example, which shows semantic
rules for Infix to posfix translation: id⋅entry = p
. . .
A → {S1} X1{S2}X2…{Sn}Xn
If we have both inherited and synthesized attributes then we
have to follow the following rules: After removing embedding semantic actions:
1. An inherited attribute for a symbol on the right side A → M1X1M2X2…MnXn
of a production must be computed in an action before M1 → ∈{S1}
that symbol.
M2 → ∈{S2}
2. An action must not refer to a synthesized attribute of
. . .
a symbol on the right side of the action.
3. A synthesized attribute for the non–terminal on the left Mn→ ∈ {Sn}
can only be computed after all attributes it references, For example,
have been computed.
E → TR
Note: In the implementation of L-attributed definitions dur- R → +T {print (‘+’)} R1
ing predictive parsing, instead of syntax directed transla- R→∈
tions, we will work with translation schemes. T → id {print (id.name)}
⇓ remove embedding semantic actions
Eliminating left recursion from E → TR
translation scheme R → +TMR1
Consider following grammar, which has left recursion R→∈
E → E + T {print (‘+’) ;} T → id {print (id.name)}
M → ∈ {print (‘+’)}
When transforming the grammar, treat the actions as if they
were terminal symbols. After eliminating recursion from
the above grammar. Translation with inherited attributes
E → TR Let us assume that every non-terminal A has an inherited
R → +T {print (‘+’);} R attribute A.i and every symbol X has a synthesized attribute
R→∈ X.s in our grammar.
For every production rule A → X1, X2 . . . Xn, introduce
Bottom-up Evaluation new marker non-terminals
M1, M2, . . . Mn and replace this production rule with A →
of Inherited Attributes M1X1M2X2 . . . MnXn
•• Using a bottom up translation scheme, we can implement The synthesized attribute of Xi will not be changed.
any L-attributed definition based on LL (1) grammar. The inherited attribute of Xi will be copied into the syn-
•• We can also implement some of L-attributed definitions thesized attribute of Mi by the new semantic action added at
based on LR (1) using bottom up translations scheme. the end of the new production rule
•• The semantic actions are evaluated during the reductions. Mi → ∈
•• During the bottom up evaluation of S-attributed defi-
nitions, we have a parallel stack to hold synthesized Now, the inherited attribute of Xi can be found in the
attributes. synthesized attribute of Mi.
Where are we going to hold inherited attributes? A → {B.i = f1(. .) B { c.i = f2(. .)} c {A.s = f3(. .)}
We will convert our grammar to an equivalent grammar to
⇓
guarantee the following:
•• All embedding semantic actions in our translation scheme A → {M1.i = f1(. .)} M1 {B.i = M1.s} B {M2.i = f2(. .)}M2
will be moved to the end of the production rules. {c.i = M2.S} c {A.s = f3 (. .)}
•• All inherited attributes will be copied into the synthesized M1 → ∈ {M1.s = M1.i}
attributes (may be new non-terminals). M2 → ∈ {M2.s = M2.i}
6.32 | Unit 6 • Compiler Design
exerciSeS
Practice Problems 1 3. Which of the following productions with transla-
Directions for questions 1 to 13: Select the correct alterna- tion rules converts binary number representation into
tive from the given choices. decimal.
1. The annotated tree for input ((a) + (b)), for the rules (A) Production Semantic Rule
given below is
B→0 B.trans = 0
Production Semantic Rule
B→1 B.trans = 1
E→E+T $ $ = mknode (‘+’, $1, $3)
B → B0 B1.trans = B2.trans*2
E → E-T $ $ = mknode (‘-’, $1, $3)
B → B1 B1.trans = B2.trans * 2 + 1
E→T $ $ = $1;
T → (E) $ $ = $2;
(B) Production Semantic Rule
T → id $ $ = mkleaf (id, $1)
B→0 B.trans = 0
T → num $ $ = mkleaf (num, $1)
B → B0 B1.trans = B2.trans*4
(A) E (B) E
id = a id = a A → LM L.i := l(A. i)
M.i := m(L.s)
(C) E (D) None of these A.s := f(M.s)
E + T A → QR R.i := r(A.i)
T id = b Q.i := q(R.s)
A.s := f(Q.s)
id = a
7. Consider the following annotated parse tree: If Input = begin east south west north, after evaluating
A A⋅num = y⋅num + z⋅num this sequence what will be the value of S.x and S.y?
(A) (1, 0) (B) (2, 0)
B⋅num = num B + C C⋅num = num (C) (-1, -1) (D) (0, 0)
11. What will be the values s.x, s.y if input is ‘begin west
num num south west’?
(A) (–2, –1)
Which of the following is true for the given annotated (B) (2, 1)
tree? (C) (2, 2)
(A) There is a specific order for evaluation of attribute (D) (3, 1)
on the parse tree.
(B) Any evaluation order that computes an attribute 12. Consider the following grammar:
‘A’ after all other attributes which ‘A’ depends on, S → E S.val = E.val
is acceptable.
(C) Both (A) and (B) E.num = 1
(D) None of these. E → E*T E1.val = 2 * E2.val + 2 * T.val
Common data for questions 8 and 9: Consider the fol- E2.num = E1.num + 1
lowing grammar and syntax directed translation. T.num = E1.num + 1
E → T E.val = T.val
E→E+T E1.val = E2.val + T.val
T.num = E.num + 1
E→T E.val = T.val
T → T + P T1.val = T2.val + P.val
T → T*P T1.val = T2.val * P.val *
P.num T2.num = T1.num + 1
P.num = T1.num + 1
T→P T.val = P.val * P.num
T → P T.val = P.val
P → (E) P.val = E.val
P.num = T.num + 1
P→0 P.num = 1
P → (E) P.val = E.val
P.val = 2
P→1 P.num = 2 E .num = P.num
P → i
P.val = 1 P.val = I | P.num
Which attributes are inherited and which are synthe-
8. What is E.val for string 1*0? sized in the above grammar?
(A) 8 (B) 6
(A) Num attribute is inherited attribute. Val attribute is
(C) 4 (D) 12
synthesized attribute.
9. What is the E.val for string 0 * 0 + 1?
(B) Num is synthesized attribute. Val is inherited at-
(A) 8 (B) 6
tribute.
(C) 4 (D) 12
(C) Num and val are inherited attributes.
10. Consider the following syntax directed definition: (D) Num and value are synthesized attributes.
Production Semantic Rule 13. Consider the grammar with the following translation
S→b S.x = 0
rules and E as the start symbol.
S.y = 0 E → E1@T {E.value = E1.value*T.value}
S → S1 I S.x = S1.x + I.dx
S.y = S1.y + I.dy
|T {E.value = T.value}
I → east I.dx = 1 T → T1 and F {T.value = T1.value + F.value}
I.dy = 0
|F {T.value = F.value}
I → north I.dx = 0
I.dy = 1 F → num {F.value = num.value}
I → west I.dx = -1
Compute E.value for the root of the parse tree for the
I.dy = 0
expression: 2 @ 3 and 5 @ 6 and 4
I → south I.dx = 0
I.dy = -1 (A) 200 (B) 180
(C) 160 (D) 40
6.34 | Unit 6 • Compiler Design
(C) It detects shift-reduce conflict, and resolves the (C) The maximum number of successors of a node
conflict in favor of a shift over a reduce action. in an AST and a CFG depends on the input pro-
(D) It detects shift-reduce conflict, and resolves the gram.
conflict in favor of a reduce over a shift action. (D) Each node in AST and CFG corresponds to at
(B) Assume the conflicts in Part (A) of this question most one statement in the input program.
are resolved and an LALR (1) parser is gener- 3. Consider the following Syntax Directed Translation
ated for parsing arithmetic expressions as per the Scheme (SDTS), with non-terminals {S, A} and ter-
given grammar. Consider an expression 3 × 2 minals {a, b}.[2016]
+ 1. What precedence and associativity proper- S → aA { print 1 }
ties does the generated parser realize?
S → a { print 2 }
(A) Equal precedence and left associativity; expres-
sion is evaluated to 7 A → Sb { print 3 }
(B) Equal precedence and right associativity; expres- Using the above SDTS, the output printed by a bot-
sion is evaluated to 9 tom-up parser, for the input aab is:
(C) Precedence of ‘×’ is higher than that of ‘+’, and (A) 1 3 2 (B) 2 2 3
both operators are left associative; expression is (C) 2 3 1 (D) syntax error
evaluated to 7 4. Which one of the following grammars is free from left
(D) Precedence of ‘+’ is higher than that of ‘×’, and recursion?[2016]
both operators are left associative; expression is (A) S → AB
evaluated to 9 A → Aa|b
2. In the context of abstract-syntax-tree (AST) and B → c
control-flow-graph (CFG), which one of the follow- (B) S → Ab|Bb|c
A → Bd|ε
ing is TRUE?[2015]
B → e
(A) In both AST and CFG, let node N2 be the suc- (C) S → Aa|B
cessor of node N1. In the input program, the code A → Bb|Sc|ε
corresponding to N2 is present after the code cor- B → d
responding to N1. (D) S → Aa|Bb|c
(B) For any input program, neither AST nor CFG A → Bd|ε
will contain a cycle. B → Ae|ε
Answer Keys
Exercises
Practice Problems 1
1. A 2. D 3. A 4. B 5. A 6. A 7. B 8. C 9. B 10. D
11. A 12. A 13. C
Practice Problems 2
1. A 2. D 3. C 4. C 5. A 6. C 7. D 8. B 9. C 10. C