–4–
Semantic Analysis
( Abstract Syntax Trees)
Objectives
•
1.
To Understand
The concept of Static Semantic Analysis
2.
Syntax Directed Definitions & Construction of Syntax
Trees
3.
Bottom up Evaluation of S-attributed Definitions
4.
L-attributed Definitions
5.
Top Down Translation
6.
Bottom up Evaluation of Inherited Attributes
www.Bookspar.com | Website for Students |
VTU - Notes - Question Papers
Where are we now ?
characters
Scanner
tokens
Parser
Source
Parse
Tree
Semantic
Actions
www.Bookspar.com | Website for Students |
VTU - Notes - Question Papers
Abstract
Syntax Tree
( IR )
•
•
Semantic Actions – Why?
While Parsing checked the syntactical correctness
of the program;
There is a level of correctness that is not
captured by a grammar
1.
2.
3.
4.
Has a variable been declared?
Are types consistent in an expression?
In the assignment x=y, is y assignable to x?
Does a method call have the right number and
types of parameters?
5. In a selector p.q, is q a method or field of class p?
6. Is variable x guaranteed to be initialized before it is
used? etc. etc. etc.
www.Bookspar.com | Website for Students |
VTU - Notes - Question Papers
•
•
Semantic Actions – What?
While Parsing checked the syntactical
correctness of the program;
Semantic Actions include:
1. Symbol table handling
•
•
•
Maintaining information about declared names
Maintaining information about types
Maintaining scopes
2. Checking context conditions
•
•
Scoping rules
Type checking
3. Invocation of code generation routines
www.Bookspar.com | Website for Students |
VTU - Notes - Question Papers
Semantic Actions – when ?
• Key Idea: At significant points during parsing
perform some semantic action
– Typically when a production is reduced (LR) or
– at a convenient point in the parse (LL)
• Semantic actions that can be performed during
Parsing
– Build (and return) a representation of the parsed
chunk(large) of the input (compiler)
– Perform some sort of computation and return result
(interpreter)
• Also Known as Static Semantic Analysis
www.Bookspar.com | Website for Students |
VTU - Notes - Question Papers
•
Static Semantic Analysis – How ?
Done by a technique called Syntax Directed Definition
– A generalization (or Extension ) of a context free grammar
in which each grammar symbol has an associated set of
attributes
1. Associate information with a programming language
construct by attaching attributes to the grammar symbols
representing the constructs.
2. Values for attributes are computed by “ semantic rules”
associated with the grammar production.
– A node in grammar is a record with fields for holding
information,
www.Bookspar.com | Website for Students |
VTU - Notes - Question Papers
1. Attributes in Syntax Directed Definition
• Each nonterminal ( an intermediate node in the parse
tree) has associated properties or attributes
• Example attributes could be:
– Name
– Memory Location
– Type,
– Font-size, etc
• For each separate instance of the grammar symbol,
there will be separate instance of the attribute.
• A node in the parse tree for the grammar symbol will
be a record holding these attributes as fields
• Value of the attribute is defined by Semantic Rule
associated with the production used at that node
www.Bookspar.com | Website for Students |
VTU - Notes - Question Papers
E2 .val
1.1 Types of Attributes
+
E1 .val
num .val
1. Synthesized Attributes
– An attribute of a grammar symbol is synthesized if it is
computed from the attributes of its children in the
parse tree corresponding to the rightmost derivation
– Example: E --> E + num | num
– E2.val = E1.val + num.val ( val of E2 computed based
on the VAL of E1 and num)
– Used Extensively in LR parsing practice
– A syntax directed Definition that uses Synthesized
Attributes is known as “S – attributed Definition” (S
– attributed Definitions are based on LR Grammar)
www.Bookspar.com | Website for Students |
VTU - Notes - Question Papers
D.type
1.2 Types of Attributes
2. Inherited Attributes
T.type
L.type
– An attribute of a grammar symbol is inherited, if it is
computed from the attributes of its parents and
siblings
L.type
– D → T {$2.type:=$1.type} L
– L → id , {$3.type:=$1.type} L
,
L.type
– Typical for LL parsing (top down).id
– Convenient for expressing the dependence of a
programming language construct on the context in
which it appears
– More natural to use with Syntax Directed Definition
(Although it is possible to write a syntax directed
Definition using only synthesized attributes)
www.Bookspar.com | Website for Students |
VTU - Notes - Question Papers
2. Semantic Rules in Syntax Directed Defn
• Define the value of an attribute associated with a
grammar symbol
• Associated with the grammar rule used at that node
Production
Semantic Rule
Notes:
Associates an integer
valued synthesized
E → E1 + T E.Val = E1.val + T.val
attribute called val with
E→T
E.val = T.val
each nonterminal E,T, & F
T → T1 *F T.Val = T1.val x F.val The token digit has a
synthesized attribute lexval
T→F
T.val = F.val
whose value will be
F→(E)
F.val = E.val
supplied by the Lexical
www.Bookspar.com | Website for Students |
F → digit
F.Val = digit.lexval
VTU - Notes - Question Papers Analyzer
L→En
Print (E: Val)
2. Semantic Rules in Syntax Directed Defn
• Define the value of an attribute associated with a
grammar symbol
• Associated with the grammar rule used at that node
Production
Semantic Rule
Notes:
Associates an integer
valued synthesized
E → E1 + T E.Val = E1.val + T.val
attribute called val with
E→T
E.val = T.val
each nonterminal E,T, & F
T → T1 *F T.Val = T1.val x F.val The token digit has a
synthesized attribute lexval
T→F
T.val = F.val
whose value will be
F→(E)
F.val = E.val
supplied by the Lexical
www.Bookspar.com | Website for Students |
F → digit
F.Val = digit.lexval
VTU - Notes - Question Papers Analyzer
L→En
Print (E: Val)
S-attributed definition Example
• The Parse Tree for 3* 5+ 4 Using Production rules
L
below is:
E
Production
E
L→En
E → E1 + T
T→F
F→(E)
F → digit
F
T
E→T
T → T1 *F
+
T
F
Digit
*
T
F
Digit
Digit
S-attributed definition Example
(Contd.)
• The annotated parse tree with attribute grammer
and synthesized attributes is:
Semantic Rule
Print (E: Val)
E.ValE= 19
E.Val = E1.val +
T.val
E.val = T.val
E.ValE= 15
T.Val = T1.val x F.val
T.val = F.val
F.val = E.val
F.Val = digit.lexval
L
T.ValT = 4
+
F.ValF= 4
T.ValT = 15
T.ValT = 3
F.ValF = 3
digit.lexVal
Digit = 3
*
F.ValF= 5
digit.lexVal
Digit = 4
digit.lexVal
Digit = 5
Attribute grammar Example
• Consider the following CFG for signed binary
numbers:
– (1) Number →Sign List ,
– (3) List → List Bit | Bit ,
(2) Sign → + | –
(4) Bit → 0 | 1
Parse tree for string “ 1” is:
Parse tree for string “ 101” is:
1.
2.
3.
4.
1. Number → Sign List
2. → Sign List Bit
3. → Sign List 1
4. → Sign List Bit 1
5. → Sign List 0 1
6. → Sign Bit 0 1
7. → Sign 1 0 1
Number → Sign List
→ Sign Bit
→ Sign 1
→–1
Attribute Grammar Example (Contd.)
Attribute Grammar for the CFG of signed binary
numbers is :
Attribute Grammar Example (Contd.)
www.Bookspar.com | Website for Students |
VTU - Notes - Question Papers
Attribute Grammar Example (Contd.)
Val = – 5
Pos = 0
Val = 5
Neg = True
Pos = 1
Val = 4
Pos = 2
Val = 4
Pos = 0
Val = 1
Pos = 1
Val = 0
Pos = 2
Val = 4
www.Bookspar.com | Website for Students |
VTU - Notes - Question Papers
Attribute Grammar Example (
Contd.)
Val = – 5
Pos = 0
Val = 5
Neg = True
Pos = 1
Val = 4
Pos = 2
Val = 4
Pos = 2
Val = 4
Pos = 0
Val = 1
Pos = 1
Val = 0
• The example on the
side Shows the
decorated parse tree
with attribute
dependencies shown
with arrows.
• If the parse tree is
taken out,
• What is left is a
DEPENDENCY GRAPH
Dependency Graph
• A directed graph that depicts the
interdependencies among the inherited and
synthesized attributes at the nodes in a parse tree
• Example:
– Consider the grammar production E → E1 + E2 and the
Corresponding semantic rule E.val : = E1.val+ E2.val
– The parse tree would be:
Val
– The Dependency Graph would
●
E
be shown separately as
E1 ● +
Val
www.Bookspar.com | Website for Students |
VTU - Notes - Question Papers
E2 ●
Val
20
Dependency Graph
• A directed graph that depicts the
interdependencies among the inherited and
synthesized attributes at the nodes in a parse
tree
• The order in which the semantic rules are
evaluated is decided by the earlier node to the
later node.
• The dependency graph is constructed
topologically;
– Grammar is used to construct the parse tree from
the input
www.Bookspar.com | Website for Students |
VTU - Notes - Question Papers
Algorithm For constructing
Dependency graph
for each node 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
www.Bookspar.com | Website for Students |
VTU - Notes - Question Papers
22
Abstract Syntax Tree (AST)
• A condensed form of a parse tree useful in
representing language constructs of a
programming language
• An intermediate representation that allows
translation to be decoupled from parsing
• For Example: The production: S → if B then S1 else
S2
Similarly, 3 * 5 + 4 +
If-then-else
will have
a syntax tree as:
will result in
4
*
B
S1
S2
3
www.Bookspar.com | Website for Students |
VTU - Notes - Question Papers
5
ASTtree
versus
• While a parse
keeps Parse
track of Tree
all productions
used to build the parse tree, an AST is a condensed
form of this with just the information needed in
later stages of compilation or interpretation.
www.Bookspar.com | Website for Students |
VTU - Notes - Question Papers
•
•
•
•
•
AST
Construction
for
Expressions
Similar to the translation of expression into
postfix form
Sub-trees are constructed for the sub-expressions
by creating a node for each operator & operand
The children of the Operator node are the nodes
representing the sub-expressions constituting the
operands of the operator
In a node for operator, ( often called the Label of
the node) One field identifies the operator & the
rest contain pointer to the nodes of operands
Each node in the syntax tree can be represented
as a record with several fields
www.Bookspar.com | Website for Students |
VTU - Notes - Question Papers
•
Constructing
AST
(Contd.)
Following functions are used to create the nodes of
AST. ( Each function returns pointer to the newly
created node)
1. mknode
(op, an
left,
right)node with label op and two fields
Creates
operator
containing pointers left and right.
Creates
an identifier node with label id and a field
2. mkleaf
(id, entry)
containing entry , a pointer to the symbol table entry.
Creates
number node with label num and a field
3. mkleaf
(num,a entry)
containing val,www.Bookspar.com
the value| Website
of forthe
number
Students |
VTU - Notes - Question Papers
Constructing
AST
–
An
Example
+ | |
Sentence a – 4 + c
- |
|
id |
num|
id |
to entry for a
4
to entry for c
p1 := mkleaf (id, entry a)
p2 := mkleaf (num, 4)
The tree is built bottom-up using
p3 := mknode (‘-’, p1, p2)
the function call sequence →
p4:= mkleaf (id, entry c)
p5 := mknode (‘+’, p3, p4 )
www.Bookspar.com | Website for Students |
VTU - Notes - Question Papers
A SDD for Constructing AST
PRODUCTION
SEMANTIC RULE
E → E1 + T
E.nptr := mknode ( ‘+’ E1.nptr, T.nptr )
E → E1 – T
E.nptr := mknode ( ‘–’ E1.nptr, T.nptr )
E→T
E.nptr := T.nptr )
T→(E)
T.nptr := E.nptr )
T → id
T.nptr := mkleaf ( id, id, entry )
T → num
T.nptr := mkleaf ( num, num, val)
NOTE:
• Uses underlying productions of the grammar to
schedule calls to the functions mknode and
mkleaf
www.Bookspar.com | Website for Students |
• The synthesized
attribute nptr for E and T keeps
VTU - Notes - Question Papers
A SDD for Constructing AST
E. Nptr - |
E. Nptr +
|
E. nptr
T. nptr
id .nptr
id |
to entry for a
+ | |
T. nptr
num .nptr
num|
T. nptr
id .nptr
id |
4
to entry for c
PRODUCTION
SEMANTIC RULE
E → E1 + T
E.nptr := mknode ( ‘+’ E1.nptr, T.nptr )
E → E1 – T
E.nptr := mknode ( ‘–’ E1.nptr, T.nptr )
E→T
E.nptr := T.nptr )
T→(E)
T.nptr := E.nptr )
T → id
T.nptr := mkleaf ( id, id, entry )
T → num
T.nptr := mkleaf ( num, num, val)
Directed A-cyclic graph (DAG)
•
Identifies common sub-expressions
•
Like syntax tree,
– it has an interior node representing an operator
and two children as operands; left & right.
– The only difference is that the node in a DAG
representing a common sub-expression has more
than one parent in the syntax tree.
•
The common sub-expression would be
represented as a duplicated sub-tree
•
Can also be used to represent a set of
expressions
www.Bookspar.com | Website for Students |
VTU - Notes - Question Papers
DAG Usage – An Example
• Consider the expression : a:= b*-c + b*-c
The corresponding
DAG would be:
• The corresponding Syntax Tree would
be:assign
assign
a
+
a
*
*
uminus
b
b uminus
c
t1 := - c;
c
t2 := b * t1;
t3 := -c;
t4 := b* t3;
t5 := t2 + t4;
a := t5;
www.Bookspar.com | Website for Students |
VTU - Notes - Question Papers
+
*
b uminus
c
t1 := - c;
t2 := b * t1;
t3 := t2 + t2;
a := t3;
•
•
Translators for SDD
A translator for an arbitrary SDD can be difficult
to build
However:
1. Large classes of syntax directed Definitions exist for
which it s easier to build a translator
2. SDD’s with synthesized attributes ( A.k.a. S–
attributed definitions) can be evaluated using
Bottom-up EvaluationTechnique
3. SDD’s with Inherited attributes ( A.k.a. L–Attributed
definitions ) are evaluated using top – down
method known as “depth–first order”
www.Bookspar.com | Website for Students |
VTU - Notes - Question Papers
Bottom-Up Evaluation of SDD’s
•
A translator for an S-attributed definition can
often be implemented with the help of an
LR-Parser generator.
•
From the S-attributed definition, the parser
generator can construct a translator that can
evaluate attributes as it parses the input.
•
One can have extra fields in the parser stack
to hold the values of the synthesized
attributes value.
www.Bookspar.com | Website for Students |
VTU - Notes - Question Papers
Thank you
www.Bookspar.com | Website for Students |
VTU - Notes - Question Papers
1.
Syntax Directed Definition
Summary
SDD is a generalization of the Context Free
Grammar.
2. Each symbol can have 2 kinds of attributes
i. synthetic
&
inherited
3. The value of an attribute is defined by the
semantic rule associated with the production
4. Synthetics computed from the children at the
node and inherited from the siblings and parents
of the node.
5. Semantic rules
set
up
the
dependency
of
the
Dependency Graph
35
attributes that will be represented as……….
a graphContd.
www.Bookspar.com | Website for Students |
VTU - Notes - Question Papers
6.
Syntax Directed Definition
Summary
The Dependency Graph deciding the order of
evaluation of the semantic rules..
7. The evaluation defines the values of the
attribute at the nodes in the parse tree
8. A parse tree showing the attributes at each node
is called an Annotated Parse Tree and the
process of evaluation is called decorating the
tree.
9. An SDD with only synthesized attributes
exclusively is called S-attributed definition and
can always be implemented bottom up
36
www.Bookspar.com | Website for Students |
VTU - Notes - Question Papers
Syntax Directed Definition –
Another example
• In this example, an inherited attribute distributes
type information to the various identifiers in the
declaration: Semantic Rule
Notes
Production
D→TL
L.in:= T.type
T → int
T.Type := integer
T → real
T.Type := real
L → L1, id
L → id
The nonterminal T has a
synthesized attribute type
whose value is determined
by the type declaration
Associated with the
productions for L call
procedure addtype to add
the type of each identifier to
its entry in the symbol table
L1.in := L.in
addtype (id.entry, L.in)
www.Bookspar.comL.in)
| Website for Students |
addtype (id.entry,
VTU - Notes - Question Papers
37
Syntax Directed Definition – Another
example
• The corresponding Decorated Parser tree for the
sentence
• real id1, id2, id3
D
Production
Semantic Rule
D→TL
L.in:= T.type
T → int
T.Type := integer
T → real
T.Type := real
L → L1, id
L → id
T.type = real
L.in = real
realL.in = real
L.in = real
L1.in := L.in
Addtype (id.entry, L.in)
Id
Addtype (id.entry,
L.in)
www.Bookspar.com | Website for Students | 1
VTU - Notes - Question Papers
,
,
Id3
Id2
38
Dependency Graph Example
Dependency Graph for the construct: real id1,id2,id3
D
Semantic Rule
L.in:= T.type
T.Type := integer
T.Type := real
L1.in := L.in
addtype (id.entry, L.in)
addtype (id.entry, L.in)
4 type
T.type = real
real
in 9
L.in = real
in 7
L.in = real
,
In 10
in 5
L.in = real
in 8
,
In 6
id3
3 entry
id2 2 entry
1,2,3 entry nodes
id1 1 entry
4,5,6,7,9 are internal
www.Bookspar.com | Website for Students |
VTU - Notes - Question Papers
8,10 are link nodes
39
Input
a * b+ cn
* b+ cn
* b+ cn
* b+ cn
b+ cn
+ cn
+ cn
+ cn
+ cn
cn
n
n
n
n
Desk Calculator SDD with LR
Stack
Val parserProduction Rule
a
F
T
T*
T*b
T*F
T
E
E+
E+c
E+F
E+T
E
En
a
F digit
a
a
TF
a
a,b
a,b
F digit
ab
TT*F
ab
ET
ab ,
ab , c
ab , c
F digit
ab , c
TF
ab+c
EE+T
www.Bookspar.com | Website for Students |
VTU ab+c
- Notes - Question Papers
Input
a*b+c
L→En
E → E1 + T
E→T
T → T1 *F
T→F
F→(E)
F → digit
40
L – Attributed Definitions
• A syntax directed definition where each inherited
attribute of Xi for 1 <= i <= n, on the right side of
AX1, X2, Xn, depends only on
– The attribute symbols X1, X2, Xi-1 to the left of Xi in
the production &
– The inherited attributes of A
• Note : Every S-attribute definition is L-attributed
since the restrictions 1 and 2 above applies only to
inherited attributes.
• The order of evaluation of the attributes is the
order in which the nodes of the parse tree are
“created” by the parsing method ( Depth-first
41
order )
www.Bookspar.com | Website for Students |
VTU - Notes - Question Papers
DEPTH – FIRST ORDER
• Depth first evaluation order for attributes
• procedure dfvisit (n : node );
• begin
– for each child m of n, from left to right
– do begin
• evaluate inherited attributes of m;
• dfvisit ( m );
– end;
– evaluate synthesized attributes of n;
• end
www.Bookspar.com | Website for Students |
VTU - Notes - Question Papers
42
Parse Tree
for 9 – 5 + 2
GRAMMAR
ETR
R addtop T {print ( addtop.lexeme) } | R |
T num { print ( num,val ) }
E
T
9
print(‘9’)
R
-
T
5
print(‘-’)
print(‘5’)
www.Bookspar.com | Website for Students |
VTU - Notes - Question Papers
R
T
+
2
print((‘2’)
print(‘2’)
43
R
Example 9
• Production
E E1 + T
E E1 - T
ET
T 0 |1 |…..|9
- 5 + 2
• Semantic rule
E. t E1 .t || T.t ||’+’
E. t E1 .t || T.t ||’-’
E. t T.t
T ‘0’ |’1’ |…..|’9’
E.t =95-2+
E.t = 95-
E.t= 9
T.t =5
T.t = 2
T.t= 9
9
-
5 www.Bookspar.com | Website
+ for Students |
VTU - Notes - Question Papers
2
44
Problem
Consider the problem of translating decimal numbers
between 0 to 99 into their English words / phrases.
Number
word / phrase
0
zero
1
One
10
Ten
11…..
Eleven
19
Nineteen
20
Twenty
30
Thirty
31
Thirty
one
www.Bookspar.com | Website for Students |
VTU - Notes - Question Papers
45
Grammar
1.
N := D | D P
2.
P := D
D := 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
PARSE TREE
ANNOTATED
PARSE TREE
N
N .trans
P
.trans
D
.in
P
D.val
D
D 1.val
6
6
8
8
3.
www.Bookspar.com | Website for Students |
VTU - Notes - Question Papers
46
Grammar
1. N := D | D P
2. P := D
3. D := 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
Production
Semantic rule
N := D
N.trans := spell (D.val)
N := D P
P.in := D.val
N.trans := P.trans
P := D
P.trans := if D1.val = 0 then decade (P.in)
else if P.in <= 1 then spell (10*p.in + D1.val)
else decade (P.in) || spell (D1.val)
D := 0
D.val := 0
spell ( 1 ) = One
….
----www.Bookspar.com | Website for Students |
VTU - Notes - Question Papers
decade (1) = Ten
……..
decade ( 9 ) = ninety
47
© Copyright 2026 Paperzz