Syntax Directed Translation (Compatibility Mode) PDF
Syntax Directed Translation (Compatibility Mode) PDF
Outline
Syntax Directed Definitions
Evaluation Orders of SDD’s
Applications of Syntax Directed Translation
Syntax Directed Translation Schemes
1
4/27/2019
2
4/27/2019
3
4/27/2019
4
4/27/2019
5
4/27/2019
6
4/27/2019
Synthesized Attributes
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
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.
7
4/27/2019
8
4/27/2019
9
4/27/2019
Dependency graph
10
4/27/2019
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.
11
4/27/2019
•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)
S-Attributed definitions
An SDD is S-attributed if every attribute is synthesized
We can have a post-order traversal of parse-tree to
evaluate attributes in S-attributed definitions
postorder(N) {
for (each child C of N, from the left) postorder(C);
evaluate the attributes associated with node N;
}
12
4/27/2019
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
Either inherited or synthesized attributes associated with the
occurrences of symbols X1,X2,…,Xi-1 located to the left of Xi
Inherited or synthesized attributes associated with this occurrence
of Xi itself, but in such a way that there is no cycle in the graph
13
4/27/2019
14
4/27/2019
15
4/27/2019
Syntax tree
Example
16
4/27/2019
+ *
*
d
a -
b c
17
4/27/2019
To entry for i
i 10
18
4/27/2019
Example:
+
t1 = b – c
+ * t2 = a * t1
t3 = a + t2
* t4 = t1 * d
d
t5 = t3 + t4
a -
b c
19
4/27/2019
T1 := -B
T2 := C + D
T3 := T1*T2
A :=T3
20
4/27/2019
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
21
4/27/2019
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)
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)
22
4/27/2019
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 2 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.
Input is x=a+b*c
O/P:- t1=b*c
t2=a+t1
x=t2
23
4/27/2019
24
4/27/2019
T -> T1 * F
T * F
T.Val F.val
Example T
T.val
L -> E n {print(stack[top-1].val);
top=top-1; }
E -> E1 + T {stack[top-2].val=stack[top-2].val+stack.val;
top=top-2;}
E -> T
T -> T1 * F {stack[top-2].val = stack[top-2].val +
stack[top].val;
top=top-2; }
T -> F
F -> (E) {stack[top-2].val=stack[top-1].val
top=top-2;}
F -> digit
25
4/27/2019
26
4/27/2019
27