Mod 1
Mod 1
M (AP, CSE)
IES College of Engineering
MODULE 4
Syntax directed translation:
Syntax directed definitions, Bottom- up evaluation of S-
attributed definitions, L- attributed definitions, Top-
down translation, Bottom-up evaluation of inherited
attributes.
Type Checking:
Type systems, Specification of a simple type checker.
1
Prepared by EBIN P.M (AP, CSE)
IES College of Engineering
2. Type checking : The process of verifying and enforcing the constraints of types is called type checking.
This may occur either at compile-time (a static check) or run-time (` dynamic check).
Static type checking is a primary task of the semantic analysis carried out by a compiler.
If type rules are enforced strongly (that is, generally allowing only those automatic type conversions
which do not lose information), the process is called strongly typed, if not, weakly typed.
3. Uniqueness checking : Whether a variable name is unique or not, in the its scope.
4. Type coercion : If some kind of mixing of types is allowed. Done in languages which are not
strongly typed. This can be done dynamically as well as statically.
5. Name Checks : Check whether any variable has a name which is not allowed. Ex. Name is same
as an identifier( Ex. int in java).
2
Prepared by EBIN P.M (AP, CSE)
IES College of Engineering
A parser has its own limitations in catching program errors related to semantics, something
that is deeper than syntax analysis.
Typical features of semantic analysis cannot be modeled using context free grammar
formalism.
If one tries to incorporate those features in the definition of a language then that language
doesn't remain context free anymore.
These are a couple of examples which tell us that typically what a compiler has to do
beyond syntax analysis.
An identifier x can be declared in two separate functions in the program, once of the type
int and then of the type char. Hence the same identifier will have to be bound to these two
different properties in the two different contexts.
Semantic Errors
We have mentioned some of the semantics errors that the semantic analyzer is expected to
recognize:
Type mismatch
Undeclared variable
Reserved identifier misuse.
Multiple declaration of variable in a scope.
Accessing an out of scope variable.
Actual and formal parameter mismatch.
Syntax Directed Translation
The Principle of Syntax Directed Translation states that the meaning of an input sentence is related
to its syntactic structure, i.e., to its Parse-Tree. By Syntax Directed Translations we indicate those
formalisms for specifying translations for programming language constructs guided by context-
free grammars.
– We associate Attributes to the grammar symbols representing the language constructs.
– Values for attributes are computed by Semantic Rules associated with grammar
productions.
Evaluation of Semantic Rules may:
– Generate Code;
– Insert information into the Symbol Table;
3
Prepared by EBIN P.M (AP, CSE)
IES College of Engineering
ATTRIBUTE GRAMMAR
Attributes are properties associated with grammar symbols. Attributes can be numbers,
strings, memory locations, data types, etc.
Attribute grammar is a special form of context-free grammar where some additional
information (attributes) are appended to one or more of its non-terminals in order to
provide context-sensitive information.
The right part of the CFG contains the semantic rules that specify how the grammar should
be interpreted. Here, the values of non-terminals E and T are added together and the result
is copied to the non-terminal E.
Semantic attributes may be assigned to their values from their domain at the time of
parsing and evaluated at the time of assignment or conditions.
4
Prepared by EBIN P.M (AP, CSE)
IES College of Engineering
Based on the way the attributes get their values, they can be broadly divided into two
categories : synthesized attributes and inherited attributes
ATTRIBUTES
Synthesized Inherited
1. Synthesized Attributes: These are those attributes which get their values from their
children nodes i.e. value of synthesized attribute at node is computed from the values of
attributes at children nodes in parse tree.
To illustrate, assume the following production:
EXAMPLE: S -> ABC
S.a= A.a,B.a,C.a
If S is taking values from its child nodes (A, B, C), then it is said to be a synthesized attribute, as
the values of ABC are synthesized to S.
Computation of Synthesized Attributes
Write the SDD using appropriate semantic rules for each production in given grammar.
The annotated parse tree is generated and attribute values are computed in bottom up
manner.
The value obtained at root node is the final output.
Consider the following grammar:
S --> E
E --> E1 +
T E --> T
T --> T1 *
F T --> F
F --> digit
5
Prepared by EBIN P.M (AP, CSE)
IES College of Engineering
Let us assume an input string 4 * 5 + 6 for computing synthesized attributes. The annotated parse
tree for the input string is
S
For computation of attributes we start from leftmost bottom node. The rule F –> digit is
used to reduce digit to F and the value of digit is obtained from lexical analyzer which
becomes value of F i.e. from semantic action F.val = digit.lexval.
Hence, F.val = 4 and since T is parent node of F so, we get T.val = 4 from semantic action
T.val = F.val.
Then, for T –> T1 * F production, the corresponding semantic action is T.val = T1.val *
F.val . Hence, T.val = 4 * 5 = 20
Similarly, combination of E1.val + T.val becomes E.val i.e. E.val = E1.val + T.val = 26.
Then, the production S –> E is applied to reduce E.val = 26 and semantic action associated
with it prints the result E.val . Hence, the output will be 26.
EXAMPLE:
B can get values from A, C and D. C can take values from A, B, and D. Likewise, D can
take values from A, B, and C.
D --> T L
T --> int
T --> float
T --> double
L --> L1, id
L --> id
7
Prepared by EBIN P.M (AP, CSE)
IES College of Engineering
Let us assume an input string int a, c for computing inherited attributes. The annotated parse
tree for the input string is
The value of L nodes is obtained from T.type (sibling) which is basically lexical value
obtained as int, float or double.
Then L node gives type of identifiers a and c. The computation of type is done in top
down manner or preorder traversal.
Using function Enter_type the type of identifiers a and c is inserted in symbol table at
corresponding id.entry.
8
Prepared by EBIN P.M (AP, CSE)
IES College of Engineering
The interdependencies among the attributes of the various nodes of a parse-tree can be
depicted by a directed graph called a dependency graph.
There is a node for each attribute;
If attribute b depends on an attribute c there is a link from the node for c to
the node for b (b ← c).
Dependency Rule: If an attribute b depends from an attribute c, then we need to find the
semantic rule for c first and then the semantic rule for b.
9
Prepared by EBIN P.M (AP, CSE)
IES College of Engineering
2. Evaluation order
A dependency graph characterizes the possible order in which we can calculate the
attributes at various nodes of the parse tree.
If there is an edge from node M to N, then the attribute corresponding to M first be
evaluated before evaluating N.
Thus the allowable orders of evaluation are N1,N2…..,Nk such that if there is an edge
from Ni toNj then i<j
Such an ordering embeds a directed graph into a linear order, and is called a topological
sort of the graph.
If there is any cycle in the graph ,then there is no topologicalsort.ie, there is no way to
evaluate SDD on this parse tree.
TYPES OF SDT’S
1. S –attributed definition
2. L –attributed definition
S-attributed definition
10
Prepared by EBIN P.M (AP, CSE)
IES College of Engineering
L –attributed definition
L stands for one parse from left to right.
Ie, If an SDT uses both synthesized attributes and inherited attributes with a restriction
that inherited attribute can inherit values from parent and left siblings only, it is called as
L-attributed SDT.
EXAMPLE:
A BCD {B.a=A.a, C.a=B.a}
C.a=D.a This is not possible
Attributes in L-attributed SDTs are evaluated by depth-first and left-to-right parsing
manner.
Semantic actions are placed anywhere in RHS.
A{ }BC
B{ }C
BC{ }
Note: (Also write SDD for declaration statement as example)
If an attribute is S attributed, it is also L attributed.
Evaluation of L-attributed SDD
11
Prepared by EBIN P.M (AP, CSE)
IES College of Engineering
Evaluate the expression 3*5+4n using the above SDD both in bottom up and top
down approach
Solution: Bottom up evaluation for this expression is shown below
In both case first we need to draw the parse tree.
12
Prepared by EBIN P.M (AP, CSE)
IES College of Engineering
13
Prepared by EBIN P.M (AP, CSE)
IES College of Engineering
Workout problems
Traversal
begin
visit(m)
14
Prepared by EBIN P.M (AP, CSE)
IES College of Engineering
end
15
Prepared by EBIN P.M (AP, CSE)
IES College of Engineering
16
Prepared by EBIN P.M (AP, CSE)
IES College of Engineering
Example :- SDD and code fragment using S attributed definition for the input 3*5+4n is as
follows:
17
Prepared by EBIN P.M (AP, CSE)
IES College of Engineering
Do left
recursion for this grammar
18
Prepared by EBIN P.M (AP, CSE)
IES College of Engineering
Type Checking
The main aim of the compiler is to translate the source program to a form that can be
executed on a target machine.
For this purpose the compiler need to
1. Check that the source program follows the syntax and semantics of the concerned
language.
2. Check the flow of data between variables.
What is type?
19
Prepared by EBIN P.M (AP, CSE)
IES College of Engineering
Type expressions
20
Prepared by EBIN P.M (AP, CSE)
IES College of Engineering
21
Prepared by EBIN P.M (AP, CSE)
IES College of Engineering
A simple language
22
Prepared by EBIN P.M (AP, CSE)
IES College of Engineering
type error}
error}
TT1T2 {T.type=T1.typeT2.type}
**********
24