You are on page 1of 34

Syntax-Directed Translation

Acknowledgement
• Alfred V Aho, Monica S. Lam, Ravi Sethi,
Jeffrey D Ullman- “Compilers- Principles,
Techniques and Tools”

Dr. Girish Kumar Patnaik 2


Syntax-Directed Translation
• Grammar symbols are associated with attributes to associate
information with the programming language constructs that they
represent.
• Values of these attributes are evaluated by the semantic rules
associated with the production rules.
• Evaluation of these semantic rules:
– may generate intermediate codes
– may put information into the symbol table
– may perform type checking
– may issue error messages
– may perform some other activities
– in fact, they may perform almost any activities.
• An attribute may hold almost any thing.
– a string, a number, a memory location, a complex record.

Dr. Girish Kumar Patnaik 3


Syntax-Directed Translation
• When we associate semantic rules with productions, we use two
notations:
– Syntax-Directed Definitions
– Translation Schemes
• Syntax-Directed Definitions:
– give high-level specifications for translations
– hide many implementation details such as order of evaluation of semantic actions.
– We associate a production rule with a set of semantic actions, and we do not say when they will be
evaluated.

• Translation Schemes:
– indicate the order of evaluation of semantic actions associated with a production rule.
– In other words, translation schemes give a little bit information about implementation details.

Dr. Girish Kumar Patnaik 4


Syntax-Directed Definitions
• A syntax-directed definition is a generalization of a context-free grammar
in which:
– Each grammar symbol is associated with a set of attributes.
– This set of attributes for a grammar symbol is partitioned into two subsets called synthesized and inherited
attributes of that grammar symbol.
– Each production rule is associated with a set of semantic rules.
• Semantic rules set up dependencies between attributes which can be
represented by a dependency graph.
• This dependency graph determines the evaluation order of these semantic
rules.
• 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.

Dr. Girish Kumar Patnaik 5


Inherited and Synthesized Attributes
• A synthesized attribute for a non-terminal A at a parse-tree node N
is defined by a semantic rule associated with the production at N.
Note that the production must have A as its head.
• A synthesized attribute at node N is defined only in terms of
attribute values at the children of N and at N itself.
• An inherited attribute for a non-terminal B at a parse-tree node N is
defined by a semantic rule associated with the production at the
parent of N. Note that the production must have B as a symbol in
its body.
• An inherited attribute at node N is defined only in terms of
attribute values at N's parent , N itself, and N's siblings.

Dr. Girish Kumar Patnaik 6


Inherited and Synthesized Attributes
• If attribute of left-hand side is computed from
attributes in the right-hand side, attribute is
synthesized: bottom-up propagation
• If attribute of symbol on right-hand is computed
from attributes of left-hand side, or from
attributes of other symbols on right-hand side,
attribute is inherited: top-down propagation of
information

Dr. Girish Kumar Patnaik 7


Inherited and Synthesized Attributes
• We do not allow an inherited attribute at node N to be defined in
terms of attribute values at the children of node N
• We do allow a synthesized attribute at node N to be defined in
terms of inherited attribute values at node N itself
• Terminals can have synthesized attributes, but not inherited
attributes.
• Attributes for terminals have lexical values that are supplied by the
lexical analyzer;
• There are no semantic rules in the SDD itself for computing the
value of an attribute for a terminal.

Dr. Girish Kumar Patnaik 8


Inherited and Synthesized Attributes
• In the Example below, each of the non-terminals has a single
synthesized attribute, called val.
• The terminal digit has a synthesized attribute lexval, which is
an integer value returned by the lexical analyzer.

Dr. Girish Kumar Patnaik 9


Inherited and Synthesized Attributes
• An SDD that involves only synthesized
attributes is called S-attributed
• In an S-attributed SDD, each rule computes
an attribute for the non-terminal at the head
of a production from attributes taken from
the body of the production.
Dr. Girish Kumar Patnaik 10
Synthesized Versus Inherited Attributes

Production Semantic Rule inherited

DTL L.in := T.type


T  int T.type := ‘integer’
… …
L  id … := L.in synthesized

Dr. Girish Kumar Patnaik 11


Acyclic Dependency Graphs for Parse Trees

A.a
AXY A.a := f(X.x, Y.y)
X.x Y.y

A.a
X.x := f(A.a, Y.y)
X.x Y.y

A.a
Direction of
value dependence Y.y := f(A.a, X.x)
X.x Y.y
Dr. Girish Kumar Patnaik 12
Dependency Graphs with Cycles?
• Edges in the dependency graph determine the
evaluation order for attribute values
• Dependency graphs cannot be cyclic
A.a A.a := f(X.x)
X.x := f(Y.y)
X.x Y.y Y.y := f(A.a)
Error: cyclic dependence

Dr. Girish Kumar Patnaik 13


Annotated Parse Tree
• A parse tree showing the values of attributes at
each node is called an annotated parse tree.
• The process of computing the attributes values at
the nodes is called annotating (or decorating) of
the parse tree.
• Of course, the order of these computations
depends on the dependency graph induced by
the semantic rules.
Dr. Girish Kumar Patnaik 14
Example Attribute Grammar with
Synthesized+Inherited Attributes
Production Semantic Rule
DTL L.in := T.type
T  int T.type := ‘integer’
T  real T.type := ‘real’
L  L1 , id L1.in := L.in; addtype(id.entry, L.in)
L  id addtype(id.entry, L.in)

Synthesized: T.type, id.entry


Inherited: L.in

Dr. Girish Kumar Patnaik 15


Example Annotated Parse Tree

T.type = ‘real’ L.in = ‘real’

real L.in = ‘real’ , id3.entry

L.in = ‘real’ , id2.entry


id1.entry

Dr. Girish Kumar Patnaik 16


Example Annotated Parse Tree with
Dependency Graph
D

T.type = ‘real’ L.in = ‘real’

real L.in = ‘real’ , id3.entry

L.in = ‘real’ , id2.entry


id1.entry

Dr. Girish Kumar Patnaik 17


Annotated parse
tree for 3 * 5 + 4 n

Dr. Girish Kumar Patnaik 18


S-Attributed Definitions
• An SDD is S-attributed if every attribute is
synthesized
• We can evaluate its attributes in any bottom-up
order of the nodes of the parse tree
• Simple to evaluate the attributes by performing
a postorder traversal of the parse tree and
evaluating the attributes at a node
Dr. Girish Kumar Patnaik 19
L-Attributed Definitions
• Dependency-graph edges can go from left to right, but not from
right to left
• Each attribute must be either
– Synthesized, or
– Inherited, but with the rules limited as follows .
• Suppose that there is a production A → XIX2... Xn , and that there is an
inherited attribute Xi · a computed by a rule associated with this production.
Then the rule may use only:
a) Inherited attributes associated with the head A.
b) Either inherited or synthesized attributes associated with the occurrences of symbols
Xl , X2 , ••• ,Xi-l located to the left of Xi .
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

Dr. Girish Kumar Patnaik 20


L-Attributed Definitions
• L-Attributed

• Not L-Attributed

Dr. Girish Kumar Patnaik 21


Applications of Syntax-Directed
Translation
• Construction of Syntax Trees
• The Structure of a Type

Dr. Girish Kumar Patnaik 22


Construction of Syntax Trees
• A syntax-tree node representing an expression El+ E2
has label + and two children representing the sub
expressions El and E2 ·
• Implement the nodes of a syntax tree by objects
– If the node is a leaf, an additional field holds the lexical value
for the leaf
– If the node is an interior node, there are as many additional
fields as the node has children in the syntax tree.

Dr. Girish Kumar Patnaik 23


Syntax Tree
for a – 4 + c

Dr. Girish Kumar Patnaik 24


The Structure of a Type
• Inherited attributes are useful when the structure of the parse
tree differs from the abstract syntax of the input
• Int [2][3] can be read as “array of 2 arrays of 3 integers”
• The corresponding type expression array(2, array(3, integer) )
is represented by the tree

Dr. Girish Kumar Patnaik 25


• synthesized attribute t
• inherited attribute b

Dr. Girish Kumar Patnaik 26


Syntax-Directed Translation Schemes
• A syntax- directed translation scheme (SDT) is
a context free grammar with program
fragments embedded within production
bodies .
• The program fragments are called semantic
actions and can appear at any position within
a production body.
Dr. Girish Kumar Patnaik 27
Postfix Translation Schemes
• SDT's with all actions at the right ends of the
production bodies are called postfix SDT's.
• Implementation of the desk calculator

Dr. Girish Kumar Patnaik 28


Parser-Stack Implementation of Postfix
SDT's
• The attribute(s) of each grammar symbol can be put
on the stack in a place where they can be found
during the reduction.
• The best plan is to place the attributes along with the
grammar symbols (or the LR states that represent
these symbols) in records on the stack itself.

Dr. Girish Kumar Patnaik 29


Implementing the desk calculator on a
bottom-up parsing stack

Dr. Girish Kumar Patnaik 30


SDT's With Actions Inside Productions
• if we have a production B →X {a} Y, the action {a} is done
after we have recognized X (if X is a terminal) or all the
terminals derived from X (if X is a nonterminal)
• If the parse is bottom-up, then we perform action {a} as
soon as this occurrence of X appears on the top of the
parsing stack.
• If the parse is top-down, we perform {a} just before we
attempt to expand this occurrence of Y (if Y a nonterminal)
or check for Y on the input (if Y is a terminal)

Dr. Girish Kumar Patnaik 31


Eliminating Left Recursion From SDT's
• Before Elimination

• After Elimination

Dr. Girish Kumar Patnaik 32


SDT's for L-Attributed Definitions
• The rules for turning an L-attributed SDD into an SDT are as follows:
1. Embed the action that computes the inherited attributes for a
nonterminal A immediately before that occurrence of A in the
body of the production.
– If several inherited attributes for A depend on one another in an
acyclic fashion, order the evaluation of attributes so that those
needed first are computed first.
2. Place the actions that compute a synthesized attribute for the
head of a production at the end of the body of that production.

Dr. Girish Kumar Patnaik 33


SDT's for L-Attributed Definitions
• Production for While-Statement

• SDD for While-Statements

• SDT for While-Statements


Dr. Girish Kumar Patnaik 34

You might also like