CD Unit3
CD Unit3
Page 84
UNIT-III
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
In the analysis –synthesis model of a compiler, the front-end translates a source program into an
intermediate representation from which the back-end generates target code, in many compilers the
source code is translated into a language which is intermediate in complexity between a HLL and
machine code .the usual intermediate code introduces symbols to stand for various temporary
quantities.
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:
Page 85
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:
The parse tree itself is a useful intermediate-language representation for a source program,
especially in optimizing compilers where the intermediate code needs to extensively restructure.
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.
Page 86
Exmples:
* d
a +
b c
If---then---else
= :=
a b a + -
c d 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
Page 87
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
Page 88
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
Page 89
• 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
Page 90
– 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.
Page 91
•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).
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 }
Page 92
S ->if E then S1 { if (E.type=boolean then S.type=S1.type
else S.type=type-error }
else S.type=type-error }
E->E1( E2) {
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
else if (s=array(s1,s2) and t=array(t1,t2)) then return (sequiv(s1,t1) and sequiv(s2,t2)) else if (s = s1 x
Page 93
Names for Type Expressions:
•In some programming languages, we give a name to a type expression, and we use that name as a
type expression afterwards.
type link = ↑cell; ? p,q,r,s have same types ? var p,q : link;
–Get equivalent type expression for a type name (then use structural equivalence), or
A formalist called as syntax directed definition is used fort specifying translations for
programming language constructs.
A syntax directed definition is a generalization of a context free grammar in which each
grammar symbol has associated set of attributes and each and each productions is associated
with a set of semantic rules
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.
• Semantic rules set up dependencies between attributes which can be represented by a dependency
graph.
Page 94
• 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→;
Page 95
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;
Page 96
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
Page 97
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.
Page 98
Dependency Graphs:
Page 99
Postfix Translation Schemes
The postfix SDT implements the desk calculator SDD with one change: the action for the first
production prints the value. As the grammar is LR, and the SDD is S-attributed.
L →E n {print(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 → 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 }
Page 100