UNIT-3 Part-A:Semantic Analysis 1. Intermediate Code Forms
UNIT-3 Part-A:Semantic Analysis 1. Intermediate Code Forms
asia
UNIT-3
Part-A:Semantic analysis
An intermediate code form of source program is an internal form of a program created by the
compiler while translating the program created by the compiler while translating the program
from a high –level language to assembly code(or)object code(machine code).an intermediate
source form represents a more attractive form of target code than does assembly. An optimizing
Compiler performs optimizations on the intermediate source form and produces an object module.
Analysis + syntheses=translation
We assume that the source program has already been parsed and statically checked.. the various
intermediate code forms are:
a) Polish notation
b) Abstract syntax trees(or)syntax trees
c) Quadruples
d) Triples three address code
e) Indirect triples
f) Abstract machine code(or)pseudocopde
a. postfix notation:
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
The ordinary (infix) way of writing the sum of a and b is with the operator in the middle: a+b.
the postfix (or postfix polish)notation for the same expression places the operator at the right
end, as ab+.
In general, if e1 and e2 are any postfix expressions, and Ø to the values denoted by e1 and e2 is
indicated in postfix notation nby e1e2Ø.no parentheses are needed in postfix notation because
the position and priority (number of arguments) of the operators permits only one way to decode
a postfix expression.
Example:
Postfix notation can be generalized to k-ary operators for any k>=1.if k-ary operator Ø is
applied to postfix expression e1,e2,……….ek, then the result is denoted by e1e2…….ek Ø. if
we know the priority of each operator then we can uniquely decipher any postfix expression by
scanning it from either end.
Example:
The right hand * says that there are two arguments to its left. since the next –to-rightmost symbol
is c, simple operand, we know c must be the second operand of *.continuing to the left, we
encounter the operator +.we know the sub expression ending in + makes up the first operand of
*.continuing in this way ,we deduce that ab+c* is “parsed” as (((a,b)+),c)*.
b. syntax tree:
A parse tree, however, often contains redundant information which can be eliminated,
Thus producing a more economical representation of the source program. One such variant of a
parse tree is what is called an (abstract) syntax tree, a tree in which each leaf represents an
operand and each interior node an operator.
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
Exmples:
* d
a +
b c
If---then---else
= := :=
a b a + b -
c d c d
Three-Address Code:
• In three-address code, there is at most one operator on the right side of aninstruction; that is, no
built-up arithmetic expressions are permitted.
x+y*z �t1 = y * z
t2 = x + t1
• Example
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
Problems:
Write the 3-address code for the following expression
1. if(x + y * z > x * y +z)
a=0;
2. (2 + a * (b – c / d)) / e
3. A :=b * -c + b * -c
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
The multiplication i * 8 is appropriate for an array of elements that each take 8 units of space.
C. quadruples:
• Three-address instructions can be implemented as objects or as record with fields for the
operator and operands.
• Three such representations
– Quadruple, triples, and indirect triples
• A quadruple (or quad) has four fields: op, arg1, arg2, and result.
Example
D. Triples
• A triple has only three fields: op, arg1, and arg2
• Using triples, we refer to the result of an operation x op y by its position, rather by
an explicit temporary name.
Example
d. Triples:
• A triple has only three fields: op, arg1, and arg2
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
• Using triples, we refer to the result of an operation x op y by its position, rather by an explicit
temporary name.
Example
Fig: Representations of a = b * - c + b * - c
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
– All assignments in SSA are to variables with distinct names; hence the term static single-
assignment.
2. Type Checking:
•A compiler has to do semantic checks in addition to syntactic checks.
•Semantic Checks
•A type system is a collection of rules for assigning type expressions to the parts of a program.
•A sound type system eliminates run-time type checking for type errors.
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
•A programming language is strongly-typed, if every program its compiler accepts will execute
without type errors.
In practice, some of type checking operations is done at run-time (so, most of the programming
languages are not strongly yped).
–Ex: int x[100]; … x[i] most of the compilers cannot guarantee that i will be between 0 and 99
Type Expression:
•The type of a language construct is denoted by a type expression.
–A basic type
•void: no type
–A type name
•arrays: If T is a type expression, then array (I,T)is a type expression where I denotes index
range. Ex: array (0..99,int)
•products: If T1and T2 are type expressions, then their Cartesian product T1 x T2 is a type
expression. Ex: int x int
•pointers: If T is a type expression, then pointer (T) is a type expression. Ex: pointer (int)
•functions: We may treat functions in a programming language as mapping from a domain type
D to a range type R. So, the type of a function can be denoted by the type expression D→R
where D are R type expressions. Ex: int→int represents the type of a function which takes an int
value as parameter, and its return type is also int.
else S.type=type-error }
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
else S.type=type-error }
else S.type=type-error }
else E.type=type-error }
f: double x char->int
•As long as type expressions are built from basic types (no type names), we may use structural
equivalence between two type expressions
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
•In some programming languages, we give a name to a type expression, and we use that name as
a type expression afterwards.
–Get equivalent type expression for a type name (then use structural equivalence), or
SDD is a generalization of CFG in which each grammar productions X->α is associated with it a
set of semantic rules of the form
a: = f(b1,b2…..bk)
– This set of attributes for a grammar symbol is partitioned into two subsets called synthesized
and inherited attributes of that grammar symbol.
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
• This dependency graph determines the evaluation order of these semantic rules.
• Evaluation of a semantic rule defines the value of an attribute. But a semantic rule may also
have some side effects such as printing a value.
An attribute is said to be synthesized attribute if its value at a parse tree node is determined from
attribute values at the children of the node
An inherited attribute is one whose value at parse tree node is determined in terms of attributes at
the parent and | or siblings of that node.
The attribute can be string, a number, a type, a, memory location or anything else.
The parse tree showing the value of attributes at each node is called an annotated parse
tree.
The process of computing the attribute values at the node is called annotating or decorating the
parse tree.Terminals can have synthesized attributes, but not inherited attributes.
• A parse tree showing the values of attributes at each node is called an Annotated parse tree.
• The process of computing the attributes values at the nodes is called annotating (or
decorating) of the parse tree.
• Of course, the order of these computations depends on the dependency graph induced by the
semantic rules.
Ex1:1) Synthesized Attributes :
Ex: Consider the CFG :
S→ EN
E→ E+T
E→E-T
E→ T
T→ T*F
T→T/F
T→F
F→ (E)
F→digit
N→;
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
Solution: The syntax directed definition can be written for the above grammar by using semantic
actions for each production.
S →EN S.val=E.val
E →E1+T E.val =E1.val + T.val
E →E1-T E.val = E1.val – T.val
E →T E.val =T.val
T →T*F T.val = T.val * F.val
T →T|F T.val =T.val | F.val
F → (E) F.val =E.val
T →F T.val =F.val
F →digit F.val =digit.lexval
N →; can be ignored by lexical Analyzer as; I
is terminating symbol
For the Non-terminals E,T and F the values can be obtained using the attribute “Val”.
In S→EN, symbol S is the start symbol. This rule is to print the final answer of expressed.
1. Write the SDD using the appropriate semantic actions for corresponding production rule of the
given Grammar.
2. The annotated parse tree is generated and attribute values are computed. The Computation is
done in bottom up manner.
PROBLEM 1:
Consider the string 5*6+7; Construct Syntax tree, parse tree and annotated tree.
Solution:
The corresponding annotated parse tree is shown below for the string 5*6+7;
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
Syntax tree:
Advantages: SDDs are more readable and hence useful for specifications
Ex2:
PROBLEM : Consider the grammar that is used for Simple desk calculator. Obtain
the Semantic action and also the annotated parse tree for the string
3*5+4n.
L→En
E→E1+T
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
E→T
T→T1*F
T→F
F→ (E)
F→digit
Solution :
L→En L.val=E.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 corresponding annotated parse tree U shown below, for the string 3*5+4n.
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
Dependency Graphs:
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
PART-B
Symbol tables
Symbol table:
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
Unordered List
Simplest to implement
Linked list can grow dynamically – alleviates problem of a fixed size array
Insertion is fast O(1), but lookup is slow for large tables – O(n) on average
Ordered List
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
Storage Allocation:
Compiler must do the storage allocation and provide access to variables and data
Memory management
Stack allocation
Heap management
Garbage collection
Storage Organization:
Operating system will later map it to physical addresses, decide how touse cache
memory, etc.
Program code
Other static data storage, including global constants and compilergenerated data
Stack to support call/return policy for procedures
Heap to store data that can outlive a call to a procedure
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
Stack allocation is a valid allocation for procedures since procedure calls are nest
Example:
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
Activation records
Procedure calls and returns are usually managed by a run-time stack called the control
stack.
Each live activation has an activation record (sometimes called a frame)
The root of activation tree is at the bottom of the stack
The current execution path specifies the content of the stack with the last
Activation has record in the top of the stack.
A General Activation Record
Activation Record
Temporary values
Local data
A saved machine status
An “access link”
A control link
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
Values communicated between caller and callee are generally placed at the beginning of
callee‟s activation record
Fixed-length items: are generally placed at the middle
Items whose size may not be known early enough: are placed at the end of activation
record
We must locate the top-of-stack pointer judiciously: a common approach is to have it
point to the end of fixed length fields
ML:
ML is a functional language
Variables are defined, and have their unchangeable values initialized, by a statementof
the form:
val (name) = (expression)
Functions are defined using the syntax:
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
Memory Manager:
Locality in Programs:
The conventional wisdom is that programs spend 90% of their time executing 10% of the code:
jntuworldupdates.org Specworld.in