05+Syntax-Directed+Translation
05+Syntax-Directed+Translation
By Bishnu Gautam 1
Compiler Construction Semantic Analysis
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 synthesized and inherited attributes of that grammar
Semantic rules set up dependencies between attributes which can be
represented by a dependency graph.
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.
By Bishnu Gautam 4
Compiler Construction Semantic Analysis
Syntax-Directed Definitions
Given a production
A→α
then each semantic rule is of the form
b = f(c1,c2,…,ck)
where f is a function and ci are attributes of A and
α, and either
– b is a synthesized attribute of A
– b is an inherited attribute of one of the grammar
symbols in α
By Bishnu Gautam 5
Compiler Construction Semantic Analysis
Syntax-Directed Definition
Example
Production Semantic Rules
L → E return print(E.val)
E → E1 + T E.val = E1.val + T.val
E→T E.val = T.val Note: all attributes in
this example are of
T → T1 * F T.val = T1.val * F.val the synthesized type
T→F T.val = F.val
F→(E) F.val = E.val
F → digit F.val = digit.lexval
digit.lexval=5 digit.lexval=3
By Bishnu Gautam 7
Compiler Construction Semantic Analysis
Inherited Attributes
An inherited attribute at any node is defined based on the attributes at
the parent and/or siblings of the node.
By Bishnu Gautam 8
Compiler Construction Semantic Analysis
Inherited Attributes
Example
By Bishnu Gautam 9
Compiler Construction Semantic Analysis
Inherited Attributes
Parse Tree
The value of L.in at the three L-nodes
gives the type of identifiers id1, id2 &
Input : real id1 , id2 , id3
id3. These values are determine by
computing the value of attribute T.type
at the left child of the root and then
D evaluating L.in in top-down at the three
L-nodes in the right sub tree of the root
T.type = ‘real’ L.in = ‘real’
id1.entry
By Bishnu Gautam 10
Compiler Construction Semantic Analysis
Dependency Graph
In order to correctly evaluate attributes of syntax tree nodes, a
dependency graph is useful. A dependency graph is a directed graph
that contains attributes as nodes and dependencies across attributes as
edges.
Algorithm for dependency graph construction
for each node n in the parse tree do
for each attribute a of the grammar symbol at n do
Construct a node in the dependency graph for a
for each node n in the parse tree do
for each semantic rule b = f(c1, c2, …, ck) associated with the
production used at n do
for i = 1 to k do
construct an edge from ci to the node for b
By Bishnu Gautam 11
Compiler Construction Semantic Analysis
Dependency Graph
Grammar in page 9 For Parse Tree of Page 10
id1.entry
By Bishnu Gautam 12
Compiler Construction Semantic Analysis
S-Attributed Definitions
By Bishnu Gautam 13
Compiler Construction Semantic Analysis
Bison Example
%token DIGIT
%%
L : E ‘\n’ { printf(“%d\n”, $1); }
;
E : E ‘+’ T { $$ = $1 + $3; }
| T { $$ = $1; }
;
T : T ‘*’ F { $$ = $1 * $3; }
| F { $$ = $1; }
;
F : ‘(’ E ‘)’ { $$ = $2; }
| DIGIT { $$ = $1; } Synthesized attribute
; of parent node F
%%
By Bishnu Gautam 14
Compiler Construction Semantic Analysis
Definitions in Yacc
Stack val Input Action Semantic Rule
$ _ 3*5+4n$ shift
$3 3 *5+4n$ reduce F → digit $$ = $1
Input: 3*5+4n $ F 3 *5+4n$ reduce T → F $$ = $1
$T 3 *5+4n$ shift
$T* 3_ 5+4n$ shift
Grammar $T*5 3_5 +4n$ reduce F → digit $$ = $1
in Page 14 $T*F 3_5 +4n$ reduce T → T * F $$ = $1 * $3
$T 15 +4n$ reduce E → T $$ = $1
$E 15 +4n$ shift
$E+ 15 _ 4n$ shift
$E+4 15 _ 4 n$ reduce F → digit $$ = $1
$E+F 15 _ 4 n$ reduce T → F $$ = $1
$E+T 15 _ 4 n$ reduce E → E + T $$ = $1 + $3
$E 19 n$ shift
$En 19 _ $ reduce L → E n print $1
$L 19 $ accept
By Bishnu Gautam 16
Compiler Construction Semantic Analysis
L-Attributed Definitions
An L-attribute is an inherited attribute which can be evaluated in a left-to-
right fashion. L-attributed definitions can be evaluated using a depth-first
evaluation order. ( L –> attributes flow from left to right)
More precisely, a syntax-directed definition is L-attributed if each inherited
attribute of Xj on the right side of A → X1 X2 … 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
By Bishnu Gautam 17
Compiler Construction Semantic Analysis
By Bishnu Gautam 18
Compiler Construction Semantic Analysis
Translation Schemes
A translation scheme is a context-free grammar in which:
– attributes are associated with the grammar symbols and
– semantic actions enclosed between braces {} are inserted within
the right sides of productions.
Ex: A → { ... } X { ... } Y { ... }
Semantic Actions
In translation schemes, we use semantic action terminology instead
of semantic rule terminology used in syntax-directed definitions.
The position of the semantic action on the right side indicates when
that semantic action will be evaluated.
By Bishnu Gautam 19
Compiler Construction Semantic Analysis
A Translation Scheme
Example
A simple translation scheme that converts infix
expressions to the corresponding postfix expressions.
E→TR
R → + T { print(“+”) } R1
R→ε
T → id { print(id.name) }
a+b+c Î ab+c+
infix expression postfix expression
By Bishnu Gautam 20
Compiler Construction Semantic Analysis
A Translation Scheme
Example
E
T R
id {print(“a”)} + T {print(“+”)} R
id {print(“b”)} + T {print(“+”)} R
id {print(“c”)} ε
The depth first traversal of the parse tree (executing the semantic actions
in that order) will produce the postfix representation of the infix
expression.
By Bishnu Gautam 21
Compiler Construction Semantic Analysis
Top-Down Translation
By Bishnu Gautam 23
Compiler Construction Semantic Analysis
Translation Scheme
Evaluating attributes
Evaluation of string XYY
A.a = g(g(f(X.x), Y1.y), Y2.y)
A.a = f(X.x) Y1
Translation Scheme
Evaluating attributes
A
Evaluation of string XYY
X R1.i = f(X.x)
Translation Scheme
Evaluating attributes
Evaluation of string XYY
A.s = R1.s = g(g(f(X.x), Y1.y), Y2.y)
By Bishnu Gautam 28
Compiler Construction Semantic Analysis
E→TR E→TR
R → + T { print(“+”) } R1 R → + T M R1
R→ε R→ε
T → id { print(id.name) } T → id { print(id.name) }
M → ε { print(“+”) }
By Bishnu Gautam 29
Compiler Construction Semantic Analysis
With L-attributed definitions, the required value is always below in the stack.
By Bishnu Gautam 30
Compiler Construction Semantic Analysis
Exercise
1. For the following grammar:
E → E + E | E * E | (E) | id
Annotate the grammar with syntax directed definitions using
synthesized attributes. Remove left recursion from the grammar
and rewrite the attributes correspondingly.
3. Questions 5.1, 5.2, 5.4, 5.6, 5.8, 5.11, 5.12 & 5.14
By Bishnu Gautam 31
Compiler Construction Semantic Analysis
Assignments
1. Write a bison program that can evaluate prefix expressions with
arbitrary arity. This means, an expression is an operator followed
by any number of expressions (not just two).
By Bishnu Gautam 32