Professional Documents
Culture Documents
CHAPTER IV
• Syntax-Directed Translation
1. Grammar symbols are associated with attributes to associate information with the
programming language constructs that they represent.
2. Values of these attributes are evaluated by the semantic rules associated with the
production rules.
– Syntax-Directed Definitions
– Translation Schemes
– Syntax-Directed Definitions:
– We associate a production rule with a set of semantic actions, and we do not say
when they will be evaluated.
– Translation Schemes:
Wollega university
Nooli
CHAPTER 4 Compiler Design
• Syntax-Directed Definitions
– This set of attributes for a grammar symbol is partitioned into two subsets called
• synthesized and
2. This dependency graph determines the evaluation order of these semantic rules.
3. Evaluation of a semantic rule defines the value of an attribute. But a semantic rule may
also have some side effects such as printing a value.
1. A parse tree showing the values of attributes at each node is called an annotated
parse tree.
2. The process of computing the attributes values at the nodes is called annotating (or
decorating) of the parse tree.
3. Of course, the order of these computations depends on the dependency graph induced
by the semantic rules.
• Syntax-Directed Definition
b=f(c1,c2,…,cn)
Wollega university
Nooli
CHAPTER 4 Compiler Design
OR
b is an inherited attribute one of the grammar symbols in α (on the right side of
the production), and c1,c2,…,cn are attributes of the grammar symbols in the production ( A→α ).
L → E return print(E.val)
2. The token digit has a synthesized attribute lexval (it is assumed that it is evaluated by the
lexical analyzer).
• S-Attributed Definitions
Wollega university
Nooli
CHAPTER 4 Compiler Design
3. We would like to evaluate the semantic rules during parsing (i.e. in a single pass, we will
parse and we will also evaluate semantic rules during the parsing).
S-Attributed Definitions
In a given an SDD, it is very hard to tell whether there exist any parse trees whose
dependency graphs have cycles. In practice, translations can be implemented using classes of
SDD's that guarantee an valuation order, since they do not permit dependency graphs with
cycles. Moreover, the two classes introduced in this section can be implemented efficiently in
connection with top-down or bot tom-up parsing.
• We put the values of the synthesized attributes of the grammar symbols into a parallel
stack.
– When an entry of the parser stack holds a grammar symbol X (terminal or non-
terminal), the corresponding entry in the parallel stack will hold the synthesized
attribute(s) of the symbol X.
stack parallel-stack
Wollega university
Nooli
CHAPTER 4 Compiler Design
top
top
L-Attributed Definitions
The second class of SDD's is called L-attributed definitions. The idea behind this class is
that, between the attributes associated with a production body,dependency-graph edges
can go from left to right, but not from right to left(hence "L-attributed"). More precisely,
each attribute must be either. Synthesized, or . Inherited, but with the rules limited as
follows.
Suppose that there is a production A -+ X1X2 - - Xn, and that there is an inherited
attribute Xi.a computed by a rule associated with this production. Then the rule may use
only:
(c) Inherited or synthesized attributes associated with this occurrence of Xi itself, but only
in such a way that there are no cycles in a dependency graph formed by the attributes of
this Xi
Wollega university
Nooli