Professional Documents
Culture Documents
Unit-4 SDD AND RUN TIME ENVIRONMENT
Unit-4 SDD AND RUN TIME ENVIRONMENT
SEMANTIC ANALYSIS
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.
o We associate Attributes to the grammar symbols representing the language
constructs.
o Values for attributes are computed by Semantic Rules associated with
grammar productions.
Example:
PRODUCTION SEMANTIC RULES
T->FT’ T’.inh=F.val
T.val=T’.syn
T’->*FT1’ T1’.inh=T’.inh*F.val
T’.syn=T1’.syn
T’->Ɛ T’.syn=T’.inh
F->id F.val=id.lexval
Let us use this SDD to compute
4*5.
DEPENDENCY GRAPHS
S-ATTRIBUTED DEFINITIONS
L-attributed definition:
Definition: A SDD its L-attributed if each inherited attribute of Xi in the RHS of A X1
::Xn depends only on
1. attributes of X1;X2; : : : ;Xi1 (symbols to the left of Xi in the RHS)
2. inherited attributes of A.
SDDs are useful for is construction of syntax trees. A syntax tree is a condensed form of
parse tree.
mkLeaf(num, val)-If the node is a leaf, an additional field holds the lexical value
for the leaf .
mkLeaf(id, entry)-creates an identifier node labeled with id and a pointer to a
symbol table is given by „entry‟
mkNode(op, left,right) -If the node is an interior node, there are as many fields
as the node has children in the syntax tree.
Example: The S-attributed definition in figure below constructs syntax trees for a simple
expression grammar involving only the binary operators + and -. As usual, these
operators are at the same precedence level and are jointly left associative. All
nonterminals have one synthesized attribute node, which represents a node of the syntax
tree.
A compiler must check that the source program follows both the syntactic and semantic
conventions of the source language.
This checking is called as static checking.
Examples of static checks are:
1. Type checks.
2. Flow-of-control checks
3. Uniqueness checks
4. Name-related checks
A type checker verifies that the type of a construct matches that expected by its context.
The language contains two data types namely: integer and char and the third basic
type is type_error , used to signal errors.
For example,
Similarly the semantic rules for arrays and pointers are given below:
Example:
1. Static Allocation:
The size of data objects is known at compile time. The names of these objects
are bound to storage at compile time only.
The binding of name with the amount of storage allocated do not change at
run time. Hence, the name of this allocation is called static allocation.
In static allocation, the compiler can determine the amount of storage required
by each data object.
A language has first-class functions if functions can be declared within any scope passed
as arguments to other functions returned as results of functions. In a language with first
class functions and static scope, a function value is generally represented by a closure. a
pair consisting of a pointer to function code a pointer to an activation record. Passing
functions as arguments is very useful in structuring of systems using upcalls
An example:
main()
{ int x =4;
int f(int y) {
return
x*y;
}
int g (int →int h){
int x = 7;
return h(3) + x;
}
Call-by-Reference
Also known as call-by-address or call-by-location. The caller passes to the called
procedure the l-value of the parameter.
If the parameter is an expression, then the expression is evaluated in a new
location, and the address of the new location is passed.
Parameters in Fortran are passed by reference an old implementation bug in
Fortran
func(a,b) { a = b};
call func(3,4); print(3);
Copy-Restore
A hybrid between call-by-value and call-by reference. The actual parameters are
evaluated and their r-values are passed as in call- by-value. In addition, l values are
determined before the call. When control returns, the current r-values of the formal
parameters are copied back into the l-values of the actual parameters.
Call-by-Name
The actual parameters literally substituted for the formals. This is like a macro expansion
or in-line expansion Call-by-name is not used in practice. However, the conceptually
related technique of in-line expansion is commonly used. In-lining may be one of the
most effective optimization transformations if they are guided by
execution profiles.
The simplest and easiest to implement data structure for symbol table is a linear
list of records.
Use single array or collection of several arrays for this purpose to store name and
their associated information. Names are added to end of array. End of array
always marks by a point known as space.
When we insert any name in this list then searching is done in whole array from
„space‟ to beginning of array. If word is not found in array then we create an entry
at „space‟ and increment „space‟ by one or value of data type.
In figure (a) represent the simple list and (b) represent self-organizing list in which Id1 is
related to Id2 and Id3 is related to Id1.
3. Hash Table
A hash table, or a hash map, is a data structure that associates keys with values
„Open hashing‟ is a key that is applied to hash table. In hashing –open, there is a property
that no limit on number of entries that can be made in table.
Using hash function any name can be search in O(1) time. However, the rare
worst-case lookup time can be as bad as O(n). A good hash function is essential
for good hash table performance.
4. Search Tree
Another approach to organize symbol table is that we add two link fields i.e. left
and right child, we use these field as binary search tree. All names are created as child of
root node that always follows the property of binary tree. For inserting any name it
always follow binary search tree insert algorithm.
Example : Create list, search tree and hash table for given program for given program
int a,b,c;
int sum (int x, int y)
{
a = x+y
return (a)