SDT 2025
SDT 2025
1
22/04/25
2
22/04/25
3
22/04/25
4
22/04/25
Synthesized Attributes
E T
T F
T F
11
Another example
S->xxw { print(10)}
/y { print(20) }
W-> Sz { printf(30)
Steps:
1. Build a parse tree for the input string
2. Traverse the tree, wherever we find a reduction, fire
the semantic rule associated with grammar.
12
5
22/04/25
O/P: 20 30 10 30 10
13
14
6
22/04/25
https://gateoverflow.in/270377/compiler-sdt-top-down-parsing
15
Inherited Attributes
Value at node in parse tree is defined in terms of attributes
at parent and/or siblings of that node.
Convenient for expressing dependence of programming
language construct
AàXY { X.i =A.s, à i is inherited attribute ?? Why
X.inh = Y.in} à inh is inherited attribute?? Why
Evaluation order:- cannot be evaluated by simple traversal
Order of computation of inherited attributes of children are
important.
Inherited attributes of children can depend from both left and right
siblings.
Evaluated in depth first left to right manner( like preporder)
16
7
22/04/25
The following syntax-directed definition generates binary strings where (one of) the attributes gives
the number represented by the string. The non-terminals are B and D, the terminals are 0 and 1. The
symbol B stands for binary expansion and the symbol D stands for digit.
18
8
22/04/25
T L
L1 , id3
19
20
9
22/04/25
• Dependency Graphs are the most general technique used to evaluate syntax
directed definitions with both synthesized and inherited attributes.
21
22
10
22/04/25
Dependency graph
1. Create 3 nodes
2. Add edges from E1.val to
E.val and
3. From E2.val towards E.val
23
Evaluation Order
• The evaluation order of semantic rules depends from a Topological
Sort derived from the dependency graph.
• Topological Sort: Any ordering m1,m2, . . . ,mk such that if mi à mj is
a link in the dependency graph then mi < mj.
• Any topological sort of a dependency graph gives a valid order to
evaluate the semantic rules.
•The graph shown to the left has many valid topological sorts,
including:5, 7, 3, 11, 8, 2, 9, 10 (visual left-to-right, top-to-bottom)
•3, 5, 7, 8, 11, 2, 9, 10 (smallest-numbered available vertex first)
•5, 7, 3, 8, 11, 10, 9, 2 (fewest edges first)
•7, 5, 11, 3, 10, 8, 9, 2 (largest-numbered available vertex first)
•5, 7, 11, 2, 3, 8, 9, 10 (attempting top-to-bottom, left-to-right)
•3, 7, 8, 5, 11, 10, 2, 9 (arbitrary)
24
11
22/04/25
25
26
12
22/04/25
27
• Alternative Approach.
Design the syntax directed definition in such a way that attributes can be
evaluated with a fixed order avoiding to build the dependency graph
(method followed by many compilers).
36
13
22/04/25
– They form a class that is less general than the class of non-circular
definitions.
37
S-Attributed definitions
An SDD is S-attributed if every attribute is synthesized
We can have a bottom –up/ post-order traversal of
parse-tree to evaluate attributes in S-attributed
definitions
A -> XYZ {A.S = X.S + Y.S}
postorder(N) {
for (each child C of N, from the left) postorder(C);
evaluate the attributes associated with node N;
}
Semantic actions are Placed in RHS
38
14
22/04/25
L-Attributed definitions
A SDD is L-Attributed if the edges in dependency graph goes from Left to Right but
not from Right to Left.
More precisely, each attribute must be either
Synthesized
Inherited, but if there is a production A->X1X2…Xn and there is an inherited
attribute Xi.a computed by a rule associated with this production, then the rule
may only use:
Inherited attributes associated with the head A
39
Example
1. Find whether it is s/l attributed?
Example 2.
P1: S -> MN {S.val= M.val + N.val}
P2: M -> PQ {M.val = P.val * Q.val and P.val =Q.val}
40
15
22/04/25
Translation schemes
Translation Schemes are more implementation oriented than syntax directed
definitions since they indicate the order in which semantic rules and attributes are to
be evaluated.
41
• The parser keeps the values of the synthesized attributes in its stack.
• Whenever a reduction A -> α is made, the attribute for A is computed
from the attributes of α which appear on the stack.
42
16
22/04/25
43
- Top-1
3 Top-2
Print( E.val)
E.val = T.val
T.val = F.val
F.val = E.val
F.val = digit.lexval
Before reduction :-
Ntop= top-r+1. ( r=3)
= 3-3+1 = =1
After reduction
Top=ntop
44
17
22/04/25
Reduction at T*F
Rule is
Value[ntop]=val[top]*val[top-2]
Also r=3
Ntop= top-r+1
= 3-3+1
=1
45
PROCESS
Semantic Actions are treated as terminal symbols: Annotated parse-trees contain
semantic actions as children of the node standing for the corresponding
production.
Semantic Actions are enclosed between braces {} and are inserted within the
right-hand side of productions.
Translation Schemes are useful to evaluate L-Attributed definitions at
parsing time (even if they are a general mechanism).
46
18
22/04/25
2. A synthesized attribute for the non terminal on the left-hand side can only be computed when
all the attributes it references have been computed: The action is usually put at the end of the
production.
47
Translation Schemes
A Semantic definition scheme to map infix to postfix
E -à T R
R -à addop T R { print (addop) }. | epsilon.
T à num. { print (num) }
48
19
22/04/25
50
20
22/04/25
51
21
22/04/25
53
2 problems solved
impleme Synthesized Inherited
ntation
Bottom up While parsing in Cannot do it easily
parser bottom up manner Procedure:-
- Convert l-attributed ( inherited) to s-attributed
(1) - Introduce new non-terminal and move all rules to
the right.
- Adding non-terminal will allocate space on value
stack. (4)
Top Down Why? Left recursion Procedure:-
parser Remove left recursion; USE L-ATTRIBUTED DEFINITIONS AND PLACE
Convert s-attributed RULES IN PRODUCTION
definition to l-
attributed definition (2)
(3)
54
22
22/04/25
Problem:-
Left recursion???
Left part of tree will be built before right parse tree
Evaluate all attributes on the left side of the tree and take
them to RHS
General rule
Remove Left Recursion
Convert s-attribute rules to l-attributes since they evaluate
from left hand side.
55
23
22/04/25
57
9–5+2
58
24
22/04/25
59
60
25
22/04/25
61
General Algorithm
62
26
22/04/25
3 problems solved
impleme Synthesized Inherited
ntation
Bottom up While parsing in Cannot do it easily
parser bottom up manner Procedure:-
- Convert l-attributed ( inherited) to s-attributed
(1) - Introduce new non-terminal and move all rules to
the right.
- Adding non-terminal will allocate space on value
stack. (4)
Top Down Why? Left recursion Procedure:-
parser Remove left recursion; USE L-ATTRIBUTED DEFINITIONS AND PLACE
Convert s-attributed RULES IN PRODUCTION
definition to l-
attributed definition (2)
(3)
63
64
27
22/04/25
66
28
22/04/25
67
68
29
22/04/25
69
30
22/04/25
72
31
22/04/25
Markers can also be used to simulate rules what are not copy rules
Stack1 Value
M1 -->E M1
Top. --> A A
a a
73
74
32
22/04/25
2 cases:
1.Reduce by M
2.Reduce by non-Marker symbol:
1.We have to compute A.s note that
A.i is already computed and lives at position on stack just below
the position into which we insert A itself.
For eg:
- Compute inherited attribute associated with
X3.i. ( j=3)
- This shall be in M3
- Therefore it shall be calculated when M-
epsilon is reduced and M is pushed
- Refer to the drawn figure
75
33
22/04/25
77
34
22/04/25
79
80
35
22/04/25
Syntax tree
81
Example
800
300
Syntax tree for
a-4+c 100
200
82
36
22/04/25
+ *
*
d
a -
b c
83
84
37
22/04/25
85
To entry for i
i 10
86
38
22/04/25
87
Example:
+
t1 = b – c
+ * t2 = a * t1
t3 = a + t2
* t4 = t1 * d
d
t5 = t3 + t4
a -
b c
88
39
22/04/25
T1 := -B
T2 := C + D
T3 := T1*T2
A :=T3
89
90
40
22/04/25
Example
do i = i+1; while (a[i] < v);
L: t1 = i + 1 100: t1 = i + 1
i = t1 101: i = t1
t2 = i * 8 102: t2 = i * 8
t3 = a[t2] 103: t3 = a[t2]
if t3 < v goto L 104: if t3 < v goto 100
91
goto
92
41
22/04/25
93
Triples
To avoid entering temporary names into symbol table, we
allow a statement computing temporary value to represent
that value.
A structure of this sort with only three fields OP, ARG1 and
ARG2 where ARG1 and ARG2, the arguments of OP are
either pointers to symbol table or pointers to structure
itself.
OP ARG1 ARG2
(0) UMINUS B -
(1) + C D
(2) * (0) (1)
(3) := A (2)
94
42
22/04/25
Indirect Triples
Listing pointers to triples rather than listing triples
themselves
STATEMENT
(0) (14)
(1) (15) OP ARG1 ARG2
(2) (16)
(14) UMINUS B -
(3) (17)
(15) + C D
(16) * (14) (15)
(17) := A (16)
95
Comparison of representations
Using Quadruple:
On producing object code, each datum, temporary will be assigned memory
Quadruples tend to clutter up symbol table with temp names.
Quadruples make location for each temporary that can be immediately accessed via
symbol table
In case of triples we have no idea unless we scan code, how many temporaries
are active simultaneously, or how many words must be allocated for
temporaries, therefore assignment of locations to temporaries is deferred to
code generation.
Code motion: in quadruples, if we move statement computing A, statement
using A require no change ( used in optimization)
In triples moving statement that defines temporary value requires to change
all pointers to that statement in ARG1 and Arg2 arrays.
No problem in indirect triples, move statement requires reordering of
STATEMENT list. Can also save space as compared to quadruples if same temp
is used more than once.
96
43
22/04/25
Input is x=a+b*c
O/P:- t1=b*c
t2=a+t1
x=t2
97
S->id=E { gen(id.name=E.place);}
E:->E1+T { E.place=newTemp;
gen(E.place=E1.place+T.place)}
/T { E.place=T.place}
T-> T1*F {T.place=newTemp;
gen(T.place=T1.place*F.place);}
/F { T.place=F.place}
F->id { F.place=id.name;}
S Input is x=a+b*c
O/P:- t1=b*c
id = E t2=a+t1
x=t2
E + T
T T * F
F F
id
id id
98
44
22/04/25
Type Checking
Problem: Verify that a type of a construct matches
that expected by its context.
Examples:
mod requires integer operands (PASCAL)
(dereferencing) – applied to a pointer
a[i] – indexing applied to an array
f(a1, a2, …, an) – function applied to correct
arguments
Information gathered by a type checker:
Needed during code generation.
99
Type Systems
A collection of rules for assigning type
expressions to the various parts of a program.
Based on: Syntactic constructs, notion of a
type.
Example: If both operators of “+”, “-”, “*” are
of type integer then so is the result.
Type Checker: An implementation of a type
system.
n Syntax Directed.
100
45
22/04/25
101
102
46
22/04/25
Type Checking
- Carried out in sematic phase
- Type checking information added with sematic rules
- Basic type checking is performed
- Extended to type checking of complex attributes
103
/ E1==E2 {
{ if ((E1.type==E2.type) && (E1.type == int/boolean))
then E.type=boolean
else error }}
104
47
22/04/25
105
106
48