Syntax Directed Translation
Syntax Directed Translation
T.val=15 F.val= 4
F.val= 3 digit.lexval=5
digit.lexval=3
S-attributed definition:
A syntax-directed definition that uses synthesized
attributes exclusively
inherited attributes
Inherited attributes are values that are computed at a node N in a parse
tree from attribute values of the parent of N, the siblings of N, and N
itself
T.type=real L.in=real
L.in=real , id2
id1
inherited attributes
Example 2: SDD based on a grammar suitable for top down parsing
T.val=15
F.val=3 T’.inh=3
T’.syn=15
digit.lexval=5 Є
A.a
X.x Y.y
Dependency Graphs
real id1,id2,id3 D
T.type=real entry
L.in=real 6
4 5
real 7
L.in
, id3 3 entry
8 entry
9
L.in
, id2 2 entry
10
entry
id1
1 entry
Dependency Graphs
T.val 9
F.val 3 T’.inh
5 8
T’.syn
1
digit.lexval F.val 4 T ’.inh
* 6 1
T 1’.syn 7
2
digit.lexval Є
Dependency Graphs
• Numbers 1 through 10 represent the nodes of the
dependency graph
• Nodes 1, 2, and 3 represent the attribute entry associated
with each of the leaves labeled id
• Nodes 6, 8, and 10 are the dummy attributes that represent
the application of the function addType to a type and one of
these entry values
• Node 4 represents the attribute T.type, and is actually where
attribute evaluation begins and passed to nodes 5, 7, and 9
Evaluation Order
of Dependency Graph
• Evaluation Order of semantic rules is obtained by a
topological sort of a directed acyclic graph
• In the topological sort, the dependent attributes c1, c2,……..,ck
in a semantic rule b:= f(c1, c2,……..,ck) are available at a node
before f is evaluated
• Evaluation of the semantic rules in this order yields the
translation of the input string
Evaluation Order
of Dependency Graph
• From this topological sort, we obtain the following
program.(ai for the attribute associated with the
node numbered i in the dependency graph.)
– a4=real;
– a5=a4;
– Addtype (id3.entry, a5);
– a7=a5;
– Addtype (id2.entry, a7);
– a9=a7;
– Addtype (id1.entry, a9);
Methods for evaluating semantic rules
• Parse tree methods
I. obtain an evaluation order from a topological sort of the dependency
graph
II. fail to find an order if the dependency graph for the particular parse
tree has a cycle
• Rule based methods
I. semantic rules associated with productions are analyzed, either by
hand, or by specialized tool
II. the order is predetermined at compiler construction time
• Oblivious methods
I. evaluation order is chosen without considering the semantic rules
II. order of evaluation is forced by the parsing method, independent of
the semantic rules
III. restricts the class of syntax directed definitions that can be
Notes: implemented
Rule-based and oblivious methods need not explicitly construct the dependency graph at
compiler time, so they can be more efficient in their use of compiler time and space
L-attributed Definition
A syntax-directed definition is L-attributed if each inherited
attribute of Xj,1<=j<=n, on the right side of A X1X2…Xn,
depends only on
– 1)The attributes of the symbols X1,X2,…Xj-1 to the left of Xj in the
production and
– 2)The inherited attributes of A.
Note: 1) L-attributes can always be evaluated in depth-first order
2) “L” means “left”, because L-attribute information appears to
flow from left to right
3)Every S-attributed definition is L-attributed, because the
restrictions in the definition apply only to inherited attributes.
Dependency Graphs
real id1,id2,id3 D
1 entry
T.type=real L.in=real
5 6
4
real 7
L.in
, id3 3 entry
8 entry
9
L.in
, id2 2 entry
10
entry
id1
1 entry
Dependency Graphs
1
T.val 9
F.val 3 T’.inh
5 8
T’.syn
1
digit.lexval F.val 4 T ’.inh
* 6 1
T 1’.syn 7
2
digit.lexval Є
Next
Code Optimization