Unit 4 CD
Unit 4 CD
Unit 4 CD
SEMANTIC ANALYSIS
Semantic Analysis computes additional information related to the meaning of the
program once the syntactic structure is known.
In typed languages as C, semantic analysis involves adding information to the symbol
table and performing type checking.
The information to be computed is beyond the capabilities of standard
parsing techniques, therefore it is not regarded as syntax.
As for Lexical and Syntax analysis, also for Semantic Analysis we need both a
Representation Formalism and an Implementation Mechanism.
As representation formalism this lecture illustrates what are called Syntax Directed
Translations.
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.
oWe associate Attributes to the grammar symbols representing the language
constructs.
oValues for attributes are computed by Semantic Rules associated with
grammar productions.
Evaluation of Semantic Rules may:
o Generate Code;
o Insert information into the Symbol Table;
o Perform Semantic Check;
1
CS6660 – Compiler Design – Unit 4
The process of computing the attribute values at the nodes is called annotating or
decorating the parse tree.
Form of a SDD
Synthesized Attributes
3
CS6660 – Compiler Design – Unit 4
Inherited Attributes
An inherited attribute is one whose values id defined in terms of attributes at the parent
and/or siblings of that node.
Dependency Graph
If this production is used in the parse tree then there will be three nodes A.a,X.x,Y.y in
the dependency graph with an edge to A.a from X.x since A.a depends on X.x and an edge
to A.a from Y.y since A.a depends on Y.y.
Example:
E E1+E2 E.val:=E1.val+E2.val
Evaluation orders
Parse tree
methods
Rule based methods
5
CS6660 – Compiler Design – Unit 4
Oblivious
methods
Parse Tree Methods: At compile time, these methods will obtain an evaluation order
from the constructed dependency graph. These methods will fail to obtain the evaluation
order it dependency graph has a cycle.
Rule Based Methods: At compiler-construction time, the semantic rules associated
with productions are analyzed either by a hand or some specialized tool.
Oblivious Methods: An evaluation order is chosen without considering the semantic rules.
Rule based methods and oblivious methods need not explicitly construct the
dependency graph at compile time, so they can be more efficient in their use of compile
time and space.
A SDD is said to be circular if the dependency graph for some parse tree generated by its
grammar has a cycle.
SDDs are useful for is construction of syntax trees. A syntax tree is a condensed form of
parse tree.
Syntax trees are useful for representing programming language constructs like
expressions and statements.
They help compiler design by decoupling parsing from translation.
Each node of a syntax tree represents a construct; the children of the node represent
the meaningful components of the construct.
e.g. a syntax-tree node representing an expression E1 + E2 has label + and two children
representing the sub expressions E1 and E2
Each node is implemented by objects with suitable number of fields; each object will
have an op field that is the label of the node with additional fields as follows:
If the node is a leaf, an additional field holds the lexical value for the leaf.
This is created by function Leaf(op, val)
If the node is an interior node, there are as many field as the node has children in the syntax
tree.
This is created by function Node(op, c1,c2,c3…,ck)
6
CS6660 – Compiler Design – Unit 4
If the rules are evaluated during a post order traversal of the parse tree, or with
reductions during a bottom-up parse, then the sequence of steps shown below ends
with p5 pointing to the root of the constructed syntax tree.
With a grammar designed for top-down parsing, the same syntax trees are constructed,
using the same sequence of steps, even though the structure of the parse trees differs
significantly from that of syntax trees.
The L-attributed definition below performs the same translation as the S-attributed
definition shown before.
7
CS6660 – Compiler Design – Unit 4
TYPE CHECKING
A compiler must check that the source program follows both syntactic and
semantic conventions of the source language.
This checking, called static checking, detects and reports programming errors.
A type checker verifies that the type of a construct matches that expected by
its context. For example : arithmetic operator mod in Pascal requires integer operands,
so a type checker verifies that the operands of mod have type integer.
Type information gathered by a type checker may be needed when code is generated.
TYPE SYSTEMS
The design of a type checker for a language is based on information about the syntactic
constructs in the language, the notion of types, and the rules for assigning types to
language constructs.
For example : “ if both operands of the arithmetic operators of +,- and * are of type integer,
then the result is of type integer ”
Type Expressions
The type of a language construct will be denoted by a “type expression.”
A type expression is either a basic type or is formed by applying an operator called a
type constructor to other type expressions.
8
CS6660 – Compiler Design – Unit 4
The sets of basic types and constructors depend on the language to be checked.
1. Basic types such as boolean, char, integer, real are type expressions.
A special basic type, type_error , will signal an error during type checking; void
denoting “the absence of a value” allows statements to be checked.
2. Since type expressions may be named, a type name is a type expression.
Records : The difference between a record and a product is that the fields of a record have
names. The record type constructor will be applied to a tuple formed from field names
and field types.
For example:
type row = record
address: integer;
lexeme: array[1..15] of
char end;
var table: array[1...101] of row;
declares the type name row representing the type expression record((address X integer) X
(lexeme X array(1..15,char))) and the variable table to be an array of records of this type.
4. Type expressions may contain variables whose values are type expressions.
9
CS6660 – Compiler Design – Unit 4
x pointer
Type systems
A type system is a collection of rules for assigning type expressions to the various parts of a
program.
A type checker implements a type system. It is specified in a syntax-directed manner.
Different type systems may be used by different compilers or processors of the
same language.
Static and Dynamic Checking of Types
Checkingdone by a compiler is said to be static, while checking done when the target
program runs is termed dynamic.
Any check can be done dynamically, if the target code carries the type of an
element along with the value of that element.
Error Recovery
Since type checking has the potential for catching errors in program, it is desirable
for type checker to recover from errors, so it can check the rest of the input.
Error handling has to be designed into the type system right from the start; the
type checking rules must be prepared to cope with errors.
10
CS6660 – Compiler Design – Unit 4
Translation scheme:
P→D;E
D→D;D { addtype (id.entry , T.type)}
D → id : T
T → char { T.type : = char }
T → integer { T.type : = integer }
T → ↑ T1 { T.type : = pointer(T1.type) }
T → array [ num ] of T1 { T.type : = array ( 1… num.val , T1.type) }
In the above language,
→ There are two basic types : char and integer ;
→ type_error is used to signal errors;
→ the prefix operator ↑ builds a pointer type. Example , ↑ integer leads to the type
expression pointer ( integer ).
Type checking of expressions
In the following rules, the attribute type forE gives the type expression assigned to the
expression generated by E.
1. E → literal { E.type : = char }
E → num { E.type : = integer }
Here, constants represented by the tokens literal and num have type char and integer.
2. E → id { E.type : = lookup ( id.entry ) }
lookup ( e ) is used to fetch the type saved in the symbol table entry pointed to by e.
3. E → E1 mod E2 { E.type : = if E1. type = integer and
E2. type = integer then
integer else type_error }
The expression formed by applying the mod operator to two subexpressions of type integer
has type integer; otherwise, its type is type_error.
4. E → E1 [ E2 ] { E.type : = if E2.type = integer and
E1.type = array(s,t) then t
else type_error }
11
CS6660 – Compiler Design – Unit 4
In an array reference E1 [ E2 ] , the index expression E 2 must have type integer. The result
is the element type t obtained from the type array(s,t) of E1.
5. E → E1 ↑ { E.type : = if E1.type = pointer (t) then t
else type_error }
The postfix operator ↑ yields the object pointed to by its operand. The type of E ↑ is the type
t of the object pointed to by the pointer E.
Type checking of statements
Statements do not have values; hence the basic type void can be assigned to them. If an error
is detected within a statement, then type_error is assigned.
Translation scheme for checking the type of statements:
1. Assignment statement:
S → id : = E { S.type : = if id.type = E.type then void else
type_error }
2. Conditional statement:
S → if E then S1 { S.type : = if E.type = boolean then S1.type
else type_error }
3. While statement:
S → while E do S1 { S.type : = if E.type = boolean then S1.type
else type_error }
4. Sequence of statements:
S → S1 ; S2 { S.type : = if S1.type = void and S1.type = void
then void
else type_error }
Type checking of functions
The rule for checking the type of a function application is :
E → E1 ( E2) { E.type : = if E2.type = s and
E1.type = s → t then
t else type_error }
RUN-TIME ENVIRONMENT
SOURCE LANGUAGE ISSUES
Procedures:
A procedure definition is a declaration that associates an identifier with a statement. The
identifier is the procedure name, and the statement is the procedure body.
procedure readarray;
var i : integer;
begin
for i : = 1 to 9 do read(a[i])
end;
12
CS6660 – Compiler Design – Unit 4
When a procedure name appears within an executable statement, the procedure is said to
be called at that point.
Activation trees:
An activation tree is used to depict the way control enters and leaves activations. In an
activation tree,
1. Each node represents an activation of a procedure.
2. The root represents the activation of the main program.
3. The node for a is the parent of the node for b if and only if control flows
from activation a to b.
4. The node for a is to the left of the node for b if and only if the lifetime of a occurs
before the lifetime of b.
Control stack:
A control stack is used to keep track of live procedure activations. The idea is
to push the node for activation onto the control stack as the activation begins and to
pop the node when the activation ends.
The contents of the control stack are related to paths to the root of the activation tree.
When node n is at the top of control stack, the stack contains the nodes along the path
from n to the root.
Binding of names:
Even if each name is declared once in a program, the same name may denote different
data objects at run time. “Data object” corresponds to a storage location that holds values.
The term environment refers to a function that maps a name to a storage location. The
term state refers to a function that maps a storage location to the value held there.
environment state
13
CS6660 – Compiler Design – Unit 4
When an environment associates storage location s with a name x, we say that x is bound
to s. This association is referred to as a binding of x.
STORAGE ORGANIZATION
The executing target program runs in its own logical address space in which each
program value has a location.
The management and organization of this logical address space is shared between
the complier, operating system and target machine. The operating system maps the logical
address into physical addresses, which are usually spread throughout memory.
CODE
STATIC DATA
STACK
FREE MEMORY
HEAP
14
CS6660 – Compiler Design – Unit 4
15
CS6660 – Compiler Design – Unit 4
STATIC ALLOCATION
In static allocation, names are bound to storage as the program is compiled, so there is
no need for a run-time support package.
Since the bindings do not change at run-time, everytime a procedure is activated,
its names are bound to the same storage locations.
Therefore values of local names are retained across activations of a procedure. That is,
when control returns to a procedure the values of the locals are the same as they were
when control left the last time.
From the type of a name, the compiler decides the amount of storage for the name and
decides where the activation records go. At compile time, we can fill in the addresses
at which the target code can find the data it operates on.
STACK ALLOCATION
All compilers for languages that use procedures, functions or methods as units of user-
defined actions manage at least part of their run-time memory as a stack.
Each time a procedure is called , space for its local variables is pushed onto a stack,
and when the procedure terminates, that space is popped off the stack.
Calling sequences:
Procedures called are implemented in what is called as calling sequence, which
consists of code that allocates an activation record on the stack and enters information
into its fields.
A return sequence is similar to code to restore the state of machine so the calling
procedure can continue its execution after the call.
The code in calling sequence is often divided between the calling procedure (caller)
and the procedure it calls (callee).
When designing calling sequences and the layout of activation records, the
following principles are helpful:
Values communicated between caller and callee are generally placed at the
beginning of the callee‟s activation record, so they are as close as possible to the caller‟s
activation record.
Fixed length items are generally placed in the middle. Such items typically
include the control link, the access link, and the machine status fields.
Items whose size may not be known early enough are placed at the end of the
16
CS6660 – Compiler Design – Unit 4
activation record. The most common example is dynamically sized array, where
the value of one of the callee‟s parameters determines the length of the array.
We must locate the top-of-stack pointer judiciously. A common approach is to have it
point to the end of fixed-length fields in the activation record. Fixed- length data
can then be accessed by fixed offsets, known to the intermediate- code generator,
relative to the top-of-stack pointer.
The calling sequence and its division between caller and callee are as follows.
The caller evaluates the actual parameters.
The caller stores a return address and the old value of top_sp into the callee‟s
activation record. The caller then increments the top_sp to the respective
positions.
The callee saves the register values and other status information.
The callee initializes its local data and begins execution.
A suitable, corresponding return sequence is:
The callee places the return value next to the parameters.
Using the information in the machine-status field, the callee restores top_sp and
other registers, and then branches to the return address that the caller placed in the
status field.
Although top_sp has been decremented, the caller knows where the return value is,
relative to the current value of top_sp; the caller therefore may use that value.
17
CS6660 – Compiler Design – Unit 4
activation
control link
record for p
pointer to A
pointer to B
pointer to C
array A
arrays of p
array B
array C
Procedure p has three local arrays, whose sizes cannot be determined at compile time.
The storage for these arrays is not part of the activation record for p.
Access to the data is through two pointers, top and top-sp. Here the top marks the actual
top of stack; it points the position at which the next activation record will begin.
The second top-sp is used to find local, fixed-length fields of the top activation record.
The code to reposition top and top-sp can be generated at compile time, in terms of sizes
that will become known at run time.
HEAP ALLOCATION
Stack allocation strategy cannot be used if either of the following is possible:
1. The values of local names must be retained when activation ends.
2. A called activation outlives the caller.
18
CS6660 – Compiler Design – Unit 4
Heap allocation parcels out pieces of contiguous storage, as needed for activation
records or other objects.
Pieces may be deallocated in any order, so over the time the heap will consist of
alternate areas that are free and in use.
s Retained activation
s record for r
r q ( 1 , 9) control link
r
control link
q(1,9)
control link
The record for an activation of procedure r is retained when the activation ends.
Therefore, the record for the new activation q(1 , 9) cannot follow that for s physically.
If the retained activation record for r is deallocated, there will be free space in the
heap between the activation records for s and q.
19
CS6660 – Compiler Design – Unit 4
PARAMETER PASSING
The communication medium among procedures is known as parameter passing.
The values of the variables from a calling procedure are transferred to the called procedure by
some mechanism. Before moving ahead, first go through some basic terminologies pertaining to
the values in a program.
r-value: The value of an expression is called its r-value. The value contained in a single
variable also becomes an r-value if it appears on the right-hand side of the assignment operator.
r-values can always be assigned to some other variable.
l-value: The location of memory (address) where an expression is stored is known as the l-
value of that expression. It always appears at the left hand side of an assignment operator.
For example:
day = 1;
week = day * 7;
month = 1;
year = month * 12;
From this example, we understand that constant values like 1, 7, 12, and variables like
day, week, month and year, all have r-values. Only variables have l-values as they also
represent the memory location assigned to them.
For example:
7 = x + y; is an l-value error, as the constant 7 does not represent any memory location.
Formal Parameters
Variables that take the information passed by the caller procedure are called
formal parameters. These variables are declared in the definition of the called function.
Actual Parameters
Variables whose values or addresses are being passed to the called procedure are called
actual parameters. These variables are specified in the function call as arguments.
Example:
fun_one()
{
int actual_parameter = 10;
call fun_two(int actual_parameter);
20
CS6660 – Compiler Design – Unit 4
}
fun_two(int formal_parameter)
{
print formal_parameter;
}
Formal parameters hold the information of the actual parameter, depending upon
the parameter passing technique used. It may be a value or an address.
Pass by Value
In pass by value mechanism, the calling procedure passes the r-value of
actual parameters and the compiler puts that into the called procedure‟s activation record.
Formal parameters then hold the values passed by the calling procedure. If the values held by
the formal parameters are changed, it should have no impact on the actual parameters.
Pass by Reference
In pass by reference mechanism, the l-value of the actual parameter is copied to
the activation record of the called procedure. This way, the called procedure now has the address
(memory location) of the actual parameter and the formal parameter refers to the same memory
location. Therefore, if the value pointed by the formal parameter is changed, the impact should
be seen on the actual parameter as they should also point to the same value.
Pass by Copy-restore
This parameter passing mechanism works similar to „pass-by-reference‟ except that the
changes to actual parameters are made when the called procedure ends. Upon function call, the
values of actual parameters are copied in the activation record of the called procedure. Formal
parameters if manipulated have no real-time effect on actual parameters (as l-values are passed),
but when the called procedure ends, the l-values of formal parameters are copied to the l-values
of actual parameters.
Example:
int y;
calling_procedure()
{
y = 10;
copy_restore(y); //l-value of y is passed
printf y; //prints 99
}
copy_restore(int x)
21
CS6660 – Compiler Design – Unit 4
{
x = 99; // y still has value 10 (unaffected)
y = 0; // y is now 0
}
When this function ends, the l-value of formal parameter x is copied to the actual
parameter y. Even if the value of y is changed before the procedure ends, the l-value of x is
copied to the l-value of y making it behave like call by reference.
Pass by Name
Languages like Algol provide a new kind of parameter passing mechanism that works
like preprocessor in C language. In pass by name mechanism, the name of the procedure being
called is replaced by its actual body. Pass-by-name textually substitutes the
argument expressions in a procedure call for the corresponding parameters in the body of the
procedure so that it can now work on actual parameters, much like pass-by-reference.
SYMBOL TABLES
Symbol table is an important data structure created and maintained by compilers in order
to store information about the occurrence of various entities such as variable names, function
names, objects, classes, interfaces, etc. Symbol table is used by both the analysis and
the synthesis parts of a compiler.
A symbol table may serve the following purposes depending upon the language in hand:
A symbol table is simply a table which can be either linear or a hash table. It maintains
an entry for each name in the following format:
For example, if a symbol table has to store information about the following variable declaration:
22
CS6660 – Compiler Design – Unit 4
Implementation
If a compiler is to handle a small amount of data, then the symbol table can be
implemented as an unordered list, which is easy to code, but it is only suitable for small tables
only. A symbol table can be implemented in one of the following ways:
Operations
A symbol table, either linear or hash, should provide the following operations.
insert()
This operation is more frequently used by analysis phase, i.e., the first half of the
compiler where tokens are identified and names are stored in the table. This operation is used to
add information in the symbol table about unique names occurring in the source code.
The format or structure in which the names are stored depends upon the compiler in hand.
An attribute for a symbol in the source code is the information associated with
that symbol. This information contains the value, state, scope, and type about the
symbol. The insert() function takes the symbol and its attributes as arguments and stores the
information in the symbol table.
For example:
int a;
23
CS6660 – Compiler Design – Unit 4
insert(a, int);
lookup()
lookup() operation is used to search a name in the symbol table to determine:
lookup(symbol)
This method returns 0 (zero) if the symbol does not exist in the symbol table. If
the symbol exists in the symbol table, it returns its attributes stored in the table.
Scope Management
A compiler maintains two types of symbol tables: a global symbol table which can be
accessed by all the procedures andscope symbol tables that are created for each scope in the
program.
To determine the scope of a name, symbol tables are arranged in hierarchical structure as
shown in the example below:
...
int value=10;
void pro_one()
{
int one_1;
int one_2;
{ \
int one_3; |_ inner scope 1
int one_4; |
} /
24
CS6660 – Compiler Design – Unit 4
int one_5;
{ \
int one_6; |_ inner scope 2
int one_7; |
} /
}
void pro_two()
{
int two_1;
int two_2;
{ \
int two_3; |_ inner scope 3
int two_4; |
} /
int two_5;
}
...
25
CS6660 – Compiler Design – Unit 4
The global symbol table contains names for one global variable (int value) and two procedure
names, which should be available to all the child nodes shown above. The names mentioned in the
pro_one symbol table (and all its child tables) are not available for pro_two symbols and its child tables.
This symbol table data structure hierarchy is stored in the semantic analyzer and whenever a
name needs to be searched in a symbol table, it is searched using the following algorithm:
first a symbol will be searched in the current scope, i.e. current symbol table.
if a name is found, then search is completed, else it will be searched in the parent symbol table until,
either the name is found or global symbol table has been searched for the name.
26