3.2
3.2
(KCS-502)
Unit-3
Syntax Directed Translation
Contents
• Syntax Directed Translations
• Syntax Directed Definitions
• Implementing Syntax Directed Definitions
– Dependency Graphs
– S-Attributed Definitions
– L-Attributed Definitions
• Translation Schemes
Strongly Non-Circular Syntax Directed
Definitions
• Strongly Non-Circular Syntax Directed Definitions. Formalisms
for which an attribute evaluation order can be fixed at compiler
construction time.
– They form a class that is less general than the class of non-circular
definitions.
– In the following we illustrate two kinds of strictly non-circular
definitions: S-Attributed and L-Attributed Definitions.
Evaluation of S-Attributed Definitions
• Synthesized Attributes can be evaluated by a bottom-up
parser as the input is being analyzed avoiding the
construction of a dependency graph.
• 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.
• Thus, a translator for an S-Attributed Definition can be simply
implemented by extending the stack of an LR-Parser.
L-Attributed Definitions
• L-Attributed Definitions contain both synthesized and
inherited attributes but do not need to build a dependency
graph to evaluate them.
• Definition. A syntax directed definition is L-Attributed if each
inherited attribute of Xj in a production A → X1 . . . Xj . . . Xn,
depends only on:
1. The attributes of the symbols to the left (this is what L in L-
Attributed stands for) of Xj , i.e., X1 X2 . . . Xj−1, and
2. The inherited attributes of A.
• Theorem. Inherited attributes in L-Attributed Definitions can
be computed by a PreOrder traversal of the parse-tree.
Evaluating L-Attributed Definitions
• L-Attributed Definitions are a class of syntax directed
definitions whose attributes can always be evaluated by
single traversal of the parse-tree.
• The following procedure evaluate L-Attributed Definitions by
mixing PostOrder (synthesized) and PreOrder (inherited)
traversal.
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.
• Definition. A Translation Scheme is a context-free grammar in
which
1. Attributes are associated with grammar symbols;
2. 2. Semantic Actions are enclosed between braces {} and are
inserted within the right-hand side of productions.
3. • Yacc uses Translation Schemes.
Contd…
• Translation Schemes deal with both synthesized and inherited
attributes.
• Semantic Actions are treated as terminal symbols: Annotated
parse-trees contain semantic actions as children of the node
standing for the corresponding production.
• Translation Schemes are useful to evaluate L-Attributed
definitions at parsing time (even if they are a general
mechanism).
– An L-Attributed Syntax-Directed Definition can be turned into a
Translation Scheme.
Translation Schemes: An Example
• Consider the Translation Scheme for the L-Attributed
Definition for “type declarations”:
Contd…
Design of Translation Schemes
• When designing a Translation Scheme we must be sure that
an attribute value is available when a semantic action is
executed.
• When the semantic action involves only synthesized
attributes: The action can be put at the end of the production.
• Rules for Implementing L-Attributed SDD’s. If we have an L-
Attibuted Syntax-Directed Definition we must enforce the
following restrictions:
1. An inherited attribute for a symbol in the right-hand side of
a production must be computed in an action before the
symbol;
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.
Compile-Time Evaluation of Translation
Schemes
• Attributes in a Translation Scheme following the above rules
can be computed at compile time similarly to the evaluation
of S-Attributed Definitions.
• Main Idea. Starting from a Translation Scheme (with
embedded actions) we introduce a transformation that makes
all the actions occur at the right ends of their productions.
– For each embedded semantic action we introduce a new Marker
(i.e., a non terminal, say M) with an empty production ( M → ξ);
– The semantic action is attached at the end of the production M → ξ.
Example
Contd…
Contd…
Intermediate Representation (IR)
• IR is a data structure used internally by a compiler or virtual machine
(VM) while translating a source program
• • Front end analyses a source program and creates an IR
• • Back end analyses the IR and generates target code
compilers
Intermediate Representation (IR)
• • Compilers may create a number of IRs during the translation process
• iv. Procedure calls and returns of the form "param 𝑥", "call
goto L
30