0% found this document useful (0 votes)
26 views

Unit - 3

The document discusses intermediate code generation in compiler design. It states that intermediate code is used to translate source code into machine code in a way that is independent of the target machine. There are two types of intermediate code representations: high-level which is close to source code, and low-level which is close to the target machine. A common low-level representation is three-address code, which breaks expressions into sub-expressions using temporary variables. Syntax directed definitions associate semantic rules with grammar productions to compute attribute values for an annotated parse tree.

Uploaded by

slcascs2022
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Unit - 3

The document discusses intermediate code generation in compiler design. It states that intermediate code is used to translate source code into machine code in a way that is independent of the target machine. There are two types of intermediate code representations: high-level which is close to source code, and low-level which is close to the target machine. A common low-level representation is three-address code, which breaks expressions into sub-expressions using temporary variables. Syntax directed definitions associate semantic rules with grammar productions to compute attribute values for an annotated parse tree.

Uploaded by

slcascs2022
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

COMPILER DESIGN UNIT - 3

Intermediate Code Generation in Compiler Design


In the analysis-synthesis model of a compiler, the front end of a compiler
translates a source program into an independent intermediate code, then the back
end of the compiler uses this intermediate code to generate the target code (which
can be understood by the machine).
Intermediate code is used to translate the source code into the machine code.
Intermediate code lies between the high-level language and the machine language.

 If the compiler directly translates source code into the machine code without
generating intermediate code then a full native compiler is required for each
new machine.
 The intermediate code keeps the analysis portion same for all the compilers
that's why it doesn't need a full compiler for every unique machine.
 Intermediate code generator receives input from its predecessor phase and
semantic analyzer phase. It takes input in the form of an annotated syntax
tree.
 Using the intermediate code, the second phase of the compiler synthesis
phase is changed according to the target machine.

Department of Computer Science,


Sri Lakshmi College of Arts & Science,
Kallakurichi
Page | 1
COMPILER DESIGN UNIT - 3

Intermediate representation
Intermediate code can be represented in two ways:
1. High Level intermediate code
2. Low Level intermediate code
High Level intermediate code: High level intermediate code can be represented as
source code. To enhance performance of source code, we can easily apply code
modification. But to optimize the target machine, it is less preferred.
Low Level intermediate code: Low level intermediate code is close to the target
machine, which makes it suitable for register and memory allocation etc. it is used
for machine-dependent optimizations.

Department of Computer Science,


Sri Lakshmi College of Arts & Science,
Kallakurichi
Page | 2
COMPILER DESIGN UNIT - 3

Three-Address Code
Intermediate code generator receives input from its predecessor phase,
semantic analyzer, in the form of an annotated syntax tree. That syntax tree then
can be converted into a linear representation, e.g., postfix notation. Intermediate
code tends to be machine independent code. Therefore, code generator assumes to
have unlimited number of memory storage (register) to generate code.
Example:
a = b + c * d;
The intermediate code generator will try to divide this expression into sub-
expressions and then generate the corresponding code.
r1 = c * d;
r2 = b + r1;
a = r2
r being used as registers in the target program.

Department of Computer Science,


Sri Lakshmi College of Arts & Science,
Kallakurichi
Page | 3
COMPILER DESIGN UNIT - 3

A three-address code has at most three address locations to calculate the


expression. A three-address code can be represented in two forms:
 quadruples and
 triples
Quadruples
Each instruction in quadruples presentation is divided into four fields:
 operator
 arg1
 arg2 and
 result
The above example is represented below in quadruples format:

Department of Computer Science,


Sri Lakshmi College of Arts & Science,
Kallakurichi
Page | 4
COMPILER DESIGN UNIT - 3

Triples
Each instruction in triples presentation has three fields:
 op
 arg1 and
 arg2
The results of respective sub-expressions are denoted by the position of expression.
Triples represent similarity with DAG and syntax tree. They are equivalent to
DAG while representing expressions.

Triples face the problem of code immovability while optimization, as the


results are positional and changing the order or position of an expression may
cause problems.
Indirect Triples: This representation is an enhancement over triples representation.
It uses pointers instead of position to store results. This enables the optimizers to
freely re-position the sub-expression to produce an optimized code.

Department of Computer Science,


Sri Lakshmi College of Arts & Science,
Kallakurichi
Page | 5
COMPILER DESIGN UNIT - 3

Syntax Directed Definition


Syntax Directed Definition (SDD) is a kind of abstract specification. It is
generalization of context free grammar in which each grammar production X –> a
is associated with it a set of production rules of the form s = f(b1, b2, ……bk)
where s is the attribute obtained from function f. The attribute can be a string,
number, type or a memory location. Semantic rules are fragments of code which
are embedded usually at the end of production and enclosed in curly braces ({ }).
Example:
E --> E1 + T { E.val = E1.val + T.val}
Annotated Parse Tree – The parse tree containing the values of attributes at each
node for given input string is called annotated or decorated parse tree.
Features
 High level specification
 Hides implementation details
 Explicit order of evaluation is not specified
Types of attributes – There are two types of attributes:
1. Synthesized Attributes – These are those attributes which derive 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.
Example:
E --> E1 + T { E.val = E1.val + T.val}
In this, E.val derive its values from E1.val and T.val

Department of Computer Science,


Sri Lakshmi College of Arts & Science,
Kallakurichi
Page | 6
COMPILER DESIGN UNIT - 3

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.
Example: Consider the following grammar
S --> E
E --> E1 + T
E --> T
T --> T1 * F
T --> F
F --> digit
The SDD for the above grammar can be written as follow

Let us assume an input string 4 * 5 + 6 for computing synthesized attributes. The


annotated parse tree for the input string is

Department of Computer Science,


Sri Lakshmi College of Arts & Science,
Kallakurichi
Page | 7
COMPILER DESIGN UNIT - 3

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.
Department of Computer Science,
Sri Lakshmi College of Arts & Science,
Kallakurichi
Page | 8
COMPILER DESIGN UNIT - 3

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.
2. Inherited Attributes – These are the attributes which derive their values from
their parent or sibling nodes i.e. value of inherited attributes are computed by value
of parent or sibling nodes.
Example:
A --> BCD { C.in = A.in, C.type = B.type }
Computation of Inherited Attributes –
Construct the SDD using semantic actions.
The annotated parse tree is generated and attribute values are computed in top
down manner.
Example: Consider the following grammar
S --> T L
T --> int
T --> float
T --> double
L --> L1, id
L --> id
The SDD for the above grammar can be written as follow

Department of Computer Science,


Sri Lakshmi College of Arts & Science,
Kallakurichi
Page | 9
COMPILER DESIGN UNIT - 3

Let us assume an input string int a, c for computing inherited attributes. The
annotated parse tree for the input string is

Department of Computer Science,


Sri Lakshmi College of Arts & Science,
Kallakurichi
Page | 10
COMPILER DESIGN UNIT - 3

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.
Syntax tree
A syntax tree depicts the natural hierarchical structure of a source program.
A dag (Directed Acyclic Graph) gives the same information but in a more compact
way because common sub expressions are identified. A syntax tree and dag
(Directed Acyclic Graph) for the assignment statement a: = b * - c + b * - c are as
follows

Department of Computer Science,


Sri Lakshmi College of Arts & Science,
Kallakurichi
Page | 11
COMPILER DESIGN UNIT - 3

Syntax tree for the expression a + (a + d )*(b - c):

Department of Computer Science,


Sri Lakshmi College of Arts & Science,
Kallakurichi
Page | 12

You might also like