Syntax Directed
Translation
Part I
Syntax Directed Translation
Syntax = form, Semantics = meaning
• Technique used to build semantic information for
large structures,
– based on its syntax
• In other words… Translation of languages guided by
the context-free grammars
The Essence of Syntax-Directed Translation
• The semantics (i.e., meaning) of the various constructs in the
language is viewed as attributes of the corresponding grammar
symbols.
• Example:
sequence of characters 495
– grammar symbol TOK_INT
– meaning ≡ integer 495
– is an attribute of TOK_INT.
• Attributes are associated with Terminal as well as Nonterminal
symbols.
• An attribute may hold almost any thing
– a string, a number, a memory location, a complex record.
The Essence of Syntax-Directed Translation
• 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.
The Essence of Syntax-Directed Translation
Production Semantic Actions
E Æ E1 + T [Link] = [Link] + [Link]
EÆT [Link] = [Link]
T Æ T1 * F [Link] = [Link] * [Link]
TÆF [Link] = [Link]
F Æ num [Link] = value(num)
FÆ(E) [Link] = [Link]
Rule = compute the value of the attribute ‘val’ at the parent
by adding together the value of the attributes at two of the
children
Syntax-Directed Definitions and Translation Schemes
• Two notations to associate semantic rules with productions
• 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.
– More readable.
• 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.
– More efficient.
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.
Syntax-Directed Definition -- Example
Production Semantic Rules
L→En [Link] = [Link]
E → E1 + T [Link] = [Link] + [Link]
E→T [Link] = [Link]
T → T1 * F [Link] = [Link] * [Link]
T→F [Link] = [Link]
F→(E) [Link] = [Link]
F → digit [Link] = [Link]
• Symbols E, T, and F are associated with a
synthesized attribute val.
• The token digit has a synthesized attribute lexval (it is
assumed that it is evaluated by the lexical analyzer).
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.
The production must have A as its head.
OR
The value of a synthesized attribute for a node is
computed using only information associated with the
node and the node’s children (or the lexical analyzer
for leaf nodes).
A
Example: Production Semantic Rules
AÆBCD A.a := B.b + C.e BC D
Example Problems for Synthesized
• Expression grammar – given a valid
expression (ex: 1 * 2 + 3), determine the
associated value while parsing.
• Grid – Given a starting location of 0,0 and a
sequence of north, south, east, west moves
(ex: NESNNE), find the final position on a unit
grid.
Synthesized Attributes – Expression Grammar
Production Semantic Actions
E Æ E1 + T [Link] = [Link] + [Link]
EÆT [Link] = [Link]
T Æ T1 * F [Link] = [Link] * [Link]
TÆF [Link] = [Link]
F Æ num [Link] = value(num)
FÆ(E) [Link] = [Link]
Synthesized Attributes –Annotating the parse tree
Production Semantic Actions
E
Val =
E Æ E1 + T [Link] = [Link] + [Link]
E + T
EÆT [Link] = [Link] Val = Val =
T Æ T1 * F [Link] = [Link] * [Link]
T F
TÆF [Link] = [Link] Val = Val =
F Æ num [Link] = value(num) TVal = * F Num
Val =
FÆ(E) [Link] = [Link] =4
F Num
Val =
=3
Input: 2 * 3 + 4 Num
=2
Synthesized Attributes –Annotating the parse tree
Production Semantic Actions
E
Val =10
E Æ E1 + T [Link] = [Link] + [Link]
E + T
EÆT [Link] = [Link] Val =6 Val =4
T Æ T1 * F [Link] = [Link] * [Link]
T F
TÆF [Link] = [Link] Val =6 Val =4
F Æ num [Link] = value(num) TVal =2 * F Num
Val = 3
FÆ(E) [Link] = [Link] =4
F Num
Val = 2
=3
Input: 2 * 3 + 4 Num
=2
Synthesized Attributes –Annotating the parse tree
Production Semantic Actions
E Val =
E Æ E1 + T [Link] = [Link] + [Link]
E + T Val =
EÆT [Link] = [Link] Val =
T Æ T1 * F [Link] = [Link] * [Link]
T T * FVal =
TÆF [Link] = [Link] Val = Val =
F Æ num [Link] = value(num)
F Num
F
FÆ(E) [Link] = [Link] Val = Val = =4
Num Num
Input: 2 + 4 * 3 =2 =3
Synthesized Attributes –Annotating the parse tree
Production Semantic Actions
E Val =14
E Æ E1 + T [Link] = [Link] + [Link]
E + T Val =12
EÆT [Link] = [Link] Val =2
T Æ T1 * F [Link] = [Link] * [Link]
T T * FVal =4
TÆF [Link] = [Link] Val =2 Val =3
F Æ num [Link] = value(num)
F Num
F
FÆ(E) [Link] = [Link] Val =2 Val =3 =4
Num Num
Input: 2 + 4 * 3 =2 =3
Grid Example
• Given a starting location of 0,0 and a sequence of
north, south, east, west moves (ex: NEENNW), find
the final position on a unit grid.
start
final
Synthesized Attributes – Grid Positions
Production Semantic Actions
seq Æ seq1 instr seq.x = seq1.x + [Link]
seq.y = seq1.y + [Link]
seq Æ BEGIN seq.x = 0, seq.y = 0
instr Æ NORTH [Link] = 0, [Link] = 1
instr Æ SOUTH [Link] = 0, [Link] = -1
instr Æ EAST [Link] = 1, [Link] = 0
instr Æ WEST [Link] = -1, [Link] = 0
Synthesized Attributes –Annotating the parse tree
Production Semantic Actions x=-1
seq Æ seq1 instr seq.x = seq1.x + [Link] y=-1 seq
seq.y = seq1.y + [Link] x=-1 dx=0
y=0 dy=-1
seq Æ BEGIN seq.x = 0, seq.y = 0 seq instr
instr Æ NORTH [Link] = 0, [Link] = 1
dx=0
instr Æ SOUTH [Link] = 0, [Link] = -1
x=-1
y=1 dy=-1 S
instr Æ EAST [Link] = 1, [Link] = 0
seq instr
instr Æ WEST [Link] = -1, [Link] = 0 x=0 dx=-1
instr S
y=1 dy=0
seq
Input: BEGIN N W S S x=0 dx=0
W
y=0 dy=1
seq instr
BEGIN N
Inherited Attributes
if an attribute is not synthesized, it is inherited.
• An inherited attribute for a nonterminal B at a
parse tree node N is defined by a semantic rule
associated with the production at the parent of N.
• The production must have B as a symbol in its
body.
• Inherited attribute at node N is defined only in
terms of attribute values at N’s parent, N itself and N’s
siblings.
Production Semantic Rules
Example:
AÆBCD B.b := A.a + C.b
Inherited Attributes – Determining types
Productions Semantic Actions
Decl Æ Type List [Link] = [Link]
Type Æ int [Link] = INT
Type Æ real [Link] = REAL
List Æ List1, id [Link] = [Link],
addtype([Link])
List Æ id addtype([Link],[Link])
Inherited Attributes – Example
Productions Semantic Actions
Decl
Decl Æ Type List [Link] = [Link]
type=INT in=INT
Type Æ int [Link] = INT Type List
Type Æ real [Link] = REAL
List Æ List1, id [Link] = [Link], int List , id
in=INT
addtype([Link])
=c
List Æ id addtype([Link],[Link])
List , id
in=INT =b
Input: int a,b,c id
=a
Syntax-Directed Definitions
• 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.
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.
Annotated Parse Tree -- Example
Input: 5+3*4 L
[Link]=17 n
[Link]=5 + [Link]=12
[Link]=5 [Link]=3 * [Link]=4
[Link]=5 [Link]=3 [Link]=4
[Link]=5 [Link]=3
Dependency Graph
Input: 5+3*4 L
[Link]=17
[Link]=5 [Link]=12
[Link]=5 [Link]=3 [Link]=4
[Link]=5 [Link]=3 [Link]=4
[Link]=5 [Link]=3
Syntax-Directed Definition (SDD)
• In a syntax-directed definition, each production A→α is
associated with a set of semantic rules of the form:
b=f(c1,c2,…,cn) where f is a function,
and b can be one of the followings:
Î b is a synthesized attribute of A and c1,c2,…,cn are
attributes of the grammar symbols in the production ( A→α ).
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→α ).
Attribute Grammar
• So, a semantic rule b=f(c1,c2,…,cn) indicates that the
attribute b depends on attributes c1,c2,…,cn.
• In a syntax-directed definition, a semantic rule may
just evaluate a value of an attribute or it may have
some side effects such as printing values.
• An attribute grammar is a syntax-directed definition
in which the functions in the semantic rules cannot
have side effects (they can only evaluate values of
attributes).
Syntax-Directed Definition – Example2
Production Semantic Rules
E → E1 + T [Link]=newtemp(), [Link] = [Link] || [Link] || add
[Link],[Link],[Link]
E→T [Link] = [Link], [Link]=[Link]
T → T1 * F [Link]=newtemp(), [Link] = [Link] || [Link] || mult
[Link],[Link],[Link]
T→F [Link] = [Link], [Link]=[Link]
F→(E) [Link] = [Link], [Link]=[Link]
F → id [Link] = [Link], [Link]=“”
• Symbols E, T, and F are associated with synthesized attributes
loc and code.
• The token id has a synthesized attribute name (it is assumed
that it is evaluated by the lexical analyzer).
• It is assumed that || is the string concatenation operator.
Syntax-Directed Definition – Inherited Attributes
Production Semantic Rules
D→TL [Link] = [Link]
T → int [Link] = integer
T → real [Link] = real
L → L1 id [Link] = [Link], addtype([Link],[Link])
L → id addtype([Link],[Link])
• Symbol T is associated with a synthesized attribute
type.
• Symbol L is associated with an inherited attribute in.
A Dependency Graph – Inherited Attributes