Professional Documents
Culture Documents
CH 4 - Semantic Analysis
CH 4 - Semantic Analysis
1
Syntax Directed Translation
• Parsing an input to do nothing about it is
useless.
• Various actions can be performed while
doing parsing.
• These actions are done by semantic
actions associated to the different rules of
the grammar.
2
Syntax directed definitions
• SDT=Grammar + Semantic Rules
• A syntax directed definition is a
generalization of the CFG in which each
grammar symbol has an associated set of
attributes (synthesized and inherited).
• An attribute can represent anything we
choose ( a string, a number, a type, a
memory location, etc.)
3
• Ex: SDT for evaluation of expressions
/F {T.value= F.value}
4
Eg: For the input 2+3*4, show
its evaluation
5
• Ex: SDT for Infix to postfix conversion
expressions
E->E+T {print(“+”);}
/T { }
T->T*F {print(“*”);}
/F {}
F->num {print(num.lvalue);}
6
EX: 2 + 3*4
7
• Ex: Given the SDT below:
S->xxW {print(“1”);}
/y {print(“1”);}
W->Sz {print(“3”);}
/F {T.value= F.value}
/4 {F.value= 4;}
9
W=4-2-4*2
Ex: SDT for evaluation of expressions
/F {T.value= F.value}
W=2#3&5#6&4
10
Synthesized Vs Inherited Attributes
• The value of a synthesized attribute is
computed from the values of attributes at
the children of that node in the parse tree.
13
Binary to decimal conversion
Eg:
11 : 3 101:5
1011 10110
11*2+0=22
1*2 +0=2
2*2 +1=5
5*2 +1=11
14
10111
11*2+1=23
15
EX: SDT that converts binary to
decimal
N->L {N.dval=L.dval;}
/B {L.dval=B.dval;}
B-> 0 {B.dval=0;}
/1 {B.dval=1;}
Eg: w=1011 16
Number with binary point to
decimal
1101.011 = 13 + 3 = 13.375
23
17
Example 1
Syntax rules Semantic rules
N L1. L2 N.dval = L1.dval + L2.dval / (2L2.count)
L B L.dval = B.dval
L.count = 1
B 0 B.dval = 0; B.count=1
B 1 B. dval = 1;B.count=1
How many attributes are there? Which are synthesized? Which are inherited?
Exercise: Draw the decorated parse tree for input 1011.01 18
Example 1 (cont’d)
• In the above example, everything is
calculated from leaves to root
all attributes are synthesized.
19
Example 2
Syntax rules Semantic rules
N L1 . L2 N.v = L1.v + L2.v
L1.s = 0
L2.s = -L2.l
L1 L2 B L1.l = L2.l + 1
L2.s = L1.s + 1
B.s = L1.s
L1.v = L2.v + B.v
L B L.v = B.v
L.l = 1
B.s = L.s
B 0 B.v = 0
B 1 B.v = l * 2B.s
21
Example 3
Note: num.value is the attribute of num that gives its value.
E T E.v := T.v
T F T.v := F.v
23
Evaluation order
• The attributes should be evaluated in a
given order because they depend on one
another.
• The dependency of the attributes is
represented by a dependency graph.
• b(j) -----D()----> a (i) if and only if there
exists a semantic action such as
a (i) := f (... b (j) ...)
24
Dependency Graph
• Algorithm for the construction of the dependency graph
25
Dependency Graph (cont’d)
• Draw the dependency graphs for Example
3 and Example 4
26
Evaluation order
• Several methods have been proposed for evaluating
semantic rules:
30
• Summary of S-attributed SDT
1)Uses only synthesized attributes
2)Attributes are evaluated in Bottom Up
parsing
31
L-Attributed grammars
(L-attributed SDT)
• It is difficult to execute the tasks of the compiler
just by synthesized attributes.
• The L-attributed class of grammars allow a
limited kind of inherited attributes.
Definition: A grammar is L-Attributed if and only if for
each rule X0 X1 X2 ... Xj ... XN, all inherited attributes of
Xj depend only on:
– Attributes of X1, ... Xj-1
– Inherited attributes of X0
• Of course all S-attributed grammars are L-attributed.
32
L-Attributed grammars (cont’d)
Example:
A LM { L.h = f1 (A.h)
M.h = f2 (L.s)
A.s = f3 (M.s) }
Does this production contradict the rules?
No
33
L-Attributed grammars (cont’d)
Example:
A QR { R.h = f4 (A.h)
Q.h = f5 (R.s)
A.s = f6 (Q.s) }
Does this production contradict the rules?
Yes, since Q.h depends on R.s
The grammar containing this production
is not L-Attributed.
34
• Summary of L(left)-attributed SDT
• Uses both inherited and synthesized
attributes
• Each inherited attribute is restricted to
inherit either from parent or left sibling
only.
35
Consider the following SDTs
1)A->LM {L.i=f(A.i) ; A.s=f(M.s);}
2) A->QR {R.i=f(A.i);Q.i=f(R.i);A.s=f(Q.s);}
3)A->BC {B.s=A.s}
36