Chapter 4 - 6
Chapter 4 - 6
4092)
Lecture 4: Semantic Analysis
2
A Step-Back
CHAPTER TWO
•Strings
•Regular
expressions
•Tokens
•Transition
diagrams
•Finite Automata 3
A Step-Back
CHAPTER THREE
•Grammars
•Derivations
•Parse-trees
•Top-down parsing (LL)
•Bottom-up paring (LR,
SLR,LALR)
4
We Need Some Tools
5
Semantic Analysis
8
Syntax Directed Translation
Syntax Directed Translation
Refers to a method of compiler implementation where
the source
language translation is completely driven by the
parser.
It is the translation of languages guided by context
free grammars.
The parsing process and parse trees are used to
direct semantic analysis and the intermediate code
translation of the source program
This can be a separate phase of a compiler or we can
augment the CFG with information to control the
semantic analysis and translation process. Such
9
Continued…
Conceptually with both the syntax directed
translation and translation scheme we:
Parse the input token stream
Build the parse tree
Traverse the tree to evaluate the semantic
rules at the parse tree nodes.
Input string parse tree
dependency graph evaluation
order for semantic rules
Syntax directed translation: set of
semantic actions performed for each
production. Enclosed by { }
10
Syntax Directed Translation
Describes output to generate for each input
construct.
CFG in which program fragment(output
11
Syntax Directed Definitions
Syntax directed definition: semantic rule
in which each variable adds extra
variable. And each production adds
semantic rule
are a generalization of context-free grammars
in which:
Grammar symbols have an associated set of
Attributes;
Productions are associated with Semantic Rules
for computing
Such formalism
the values generates Annotated
of attributes.
Parse-Trees where each node of the tree is
a record with a field for each attribute 12
Continued…
Syntax directed translation (SDT)
Grammar +semantic rules= SDT
Informal notation
Steps to construct
Example annotated parse tree
EE+T| T {E.val=E.val+T.val}
Step1: generate parse tree
Step2: parse the tree left to
{E.val=T.val} right and top to bottom
TT*F|F {T.val=T.val *F.val}
Step3: whenever there is a
{T.val=F.val} reduction go to the production
Fnum {F.val=num.val}and carry out the action
Input 2+3*4
13
Syntax Directed Definitions
Attributed CFG Example
Attributed grammar that calculates the
value of an expression,
Syntax Semantic
Rules Rules
E→E + T E1.v= E2.v +
E→T T.v
T→T * F E.v = T.v
T→F T1.v= T2.v *
F→id F.v
F→(E) T.v = F.v
F.v = 14
Annotated Parse Tree Example
15
Attributes
Attributes: The properties of an entity are
called the attributes.
The attributes of the production rules of the
grammars.
There are two kinds of attributes namely:
19
Types of Attributes
Synthesized Attributes
Synthesized attributes: If the attributes of
the parent depend on the attributes of the
children then such attributes are called
synthesized attributes.
22
Synthesized Attributes
Production Semantic Rules
L→En { print (E.val)}
E → E1 + T { E.val := E1.val + T.val}
E→T { E.val := T.val }
T → T1 * F { T.val := T1.val * F.val}
T→F { T.val := F.val }
F → (E) { F.val := E.val }
F → digit {F.val := digit.lexval}
The first production L → E n, prints the value of
expression generated by E. L is the start symbol of the
grammar.
The second production returns the val of expression
E1and the value of the type T to the value of expression
E.
Similarly all the remaining productions are assigned 23
Synthesized Attributes…
Parse tree for the input string 3 * 5 + 4 n, constructed using the grammar and rules
24
Inherited Attributes…
25
Inherited Attributes…
D→T L { L.in := T.type }
→ int {T.type := integer }
→ real {T.type := real }
→ L1 , id {L1.in := L.in ; addtype (id.entry, L.in)
→ id {addtype (id.entry, L.in)}
Input string is: real id1,id2
type is a synthesized attribute
in is an inherited attribute
26
Syntax directed translation and
attributes
A syntax-directed definition ( SDD) is a context-
free grammar together with attributes and rules.
Attributes are associated with grammar symbols
a grammar symbol.
The syntax directed translation can be of two
types:
1.Synthesized Translation.
2. Inherited Translation.
27
S-attributed Grammar
Definition
An S-Attributed Definition is a
Syntax Directed Definition that
uses only synthesized attributes.
Evaluation Order
Semantic rules in an S-Attributed
Definition can be evaluated by a bottom-
up, or PostOrder, traversal of the parse-
tree
31
S-attributed Grammar
S-attributed Grammar Example
Syntax Semantic
Rules Rules
L → En print(E.val)
E → E1 + E.val = E1.val +
T T.val E.val =
E → T T.val
T → T1* F T.val = T1.val
T → F
33
S-attributed Grammar
Exercises
Give
the annotated parse tree of
(3+4)*(5+6)n from the following
grammar
Syntax Semantic
Rules Rules
L → En
print(E.val)
E → E1 +
E.val = E1.val +
T T.val E.val =
E → T
T.val
T → T1 *
T.val = T1.val
F F.val
T → F
T.val = F.val
F → (E)
F.val = E.val 34
F →
F.val
L-attributed Grammars
Definition
An L-Attributed Definition is a
Syntax Directed Definition that
uses both synthesized and Inherited
attributes.
It
is always possible to rewrite a
syntax directed definition to use only
synthesized attributes,
Evaluation Order.
Inheritedattributes cannot be evaluated
by a simple PreOrder traversal of the 35
L-attributed Grammars
39
Graph
The graph that shows the flow of
information which helps in computation of
various attribute values in a particular parse
tree.
Implementing a SDD consists primarily in
finding an order for the evaluation of
attributes
The attributes should be evaluated in a
given order because they depend on one 40
Inter-dependency of
Attributes
A Dependency Graph shows the
interdependencies among the attributes of
the various nodes of a parse-tree
Dependency Graphs are the most general
technique used to evaluate SDD with both
synthesized and inherited attributes.
T(j) ----D()---> E(i) if and only if there exists a
semantic action
such as E(i) := f (... T (j) ...) 41
Graph
Algorithm for the construction of the
dependency graph
for each node n in the parse tree do
foreach 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 the node for ci to the 42
node for b;
Dependency
Graph
43
Examp
le
The dependency graph for
the parse-tree of real id1,
id2, id3
Evaluation
Order
The evaluation order of semantic rules
depends from a Topological Sort derived
from the dependency graph
A topological sort
of a directed acyclic graph is any ordering m1,
m2, . . ., mk of the nodes of the graph such
that edges go from nodes earlier in the
ordering to later nodes.
i.e., if mi→mk is an edge from mi to mk then mi
appears before mk in the ordering
Any topological sort of a dependency
graph gives a valid order to evaluate the 45
Evaluating
Semantic Rules
20 Parse Tree methods
3
At compile time evaluation order obtained from the
topological sort of dependency graph.
Fails if dependency graph has a cycle
Rule Based Methods
Semantic rules analyzed by hand or specialized tools
at compiler
construction time
Order of evaluation of attributes associated with a
production is pre-
determined at compiler construction time
For this method, the dependency graph need not be
constructed
Oblivious Methods
Evaluation order is chosen without considering the
semantic rules. 46
47
Strongly Non-Circular Syntax Directed
Definitions
L-Attributed Definitions
48
Evaluation of S-Attributed Definitions
53
Introduct
ion
21
0
54
Introduct
ion
21
0
uniqueness
Flow of Control
57
Examples of Dynamic
Checks
Array Out of
Bound int [] a =
new int [10]; for (int
i=0; i<20; ++i)
a[i] = i;
Division by
zero
float a=50;
for (int i=0;
i<5; ++i)
a= a/i;
58
Type Checking
Introduction
Basic types
are atomic types that have no internal
structure as far as the programmer is
concerned
They include types like integer, real, boolean,
character , and enumerated types
Constructed types
include arrays, records, sets, and structures
constructed from the basic types and/or other
61
Type
Checker
63
Type Expressions
cont‟d
A type constructor applied to a type
expression is a type expression.
Constructors include:
Arrays
If I in an index set and T is a type expression,
then array (I, T)
is a type expression
Example: array[1..10] of int == array(10,int);
Records
The difference between products and records
is that, records
have names for record fields. 64
Type System
Type System:
Collection of rules for assigning type expressions to
the various
part of a program
Type Systems are specified using syntax directed
definitions
A type checker implements a type systems
Sound type system : is one where any program that
passes the static type checker cannot contain run-time
type errors. Such languages are said to be strongly typed
languages.
65
Specification of a Type
System
The syntax directed definition for
associating a type
to an Identifier is:
67
Specification of a Type
System
70
Name type
ptr = *integer
Equivalence var
A : ptr;
B : ptr;
C : *integer;
D, E : *integer;
In some language, types can be given
names
Do the variables A, B, C, D, E have the
same type?
The answer varies from implementation to
implementation
Name Equivalence:
We have name equivalence between two type
expressions if and only if they are identical
Structural equivalence
Names are replaced by type expressions they define,
so two types are structurally equivalent if they
represent two structurally equivalent type
expressions when all names have been substituted 71
Type
Conversion
Coercion is implicit type conversion done by
the compiler
Explicit type conversion is done by the
programmer
Example. What‟s the type of “x + y” if:
1. x is of type real;
2. y is of type int;
3. Different machine instructions are used for
operations on reals and integers.
Depending on the language, specific
conversion rules must be adopted by the
compiler to convert the type of one of the
operand of +
72
The type checker in a compiler can insert these
Type Coercion in
Expressions
73
CHAPTER SIX
INTERMEDIATE CODE GENERATION
75
Intermediate Code
Generation
Syntax trees,
Postfix
notations, and
Three-address
code
77
Syntax Tree
E + E
a b
a b
78
Postfix Notation
79
Three-Address
code
The three address code is a sequence of
statements of the form:
X := Y op Z
where: X, Y, and Z are names, constants or
compiler-generated
temporaries
op is an operator such as integer or floating
point arithmetic operator or logical operator on
Boolean data
Important Notes:
No built-up arithmetic operator is permitted
Only one operator at the right side of the
assignment is
possible, i.e., x + y + z is not possible
It has been given the name three-address 80
Types of Three-Address Statements
Statement Format Comments
Assignment (binary operation) X := Y op Z Arithmetic and logical operators used
Assignment (unary operation) X := op Y Unary -, not, conversion operators used
Copy statement X := Y
Unconditional jump goto L
Conditional jump If X relop y goto L
Function call param X1 The parameters are specified by param
param X2 The procedure p is called by indicating the
… number of parameters n
param Xn
call p, n
82
generation
100