Three Address Code Report

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 11

A

Report
On

Submitted in partial fulfillment of the


requirements for the award of the degree
of

Three Address Code


Bachelor of Technology-CSE
SESSION: 2023-24
COMPILER DESIGN

Submitted by
NAME: Astha Malviya
ROLL NO: 202110101110093
Group: 64

Submitted To
Ms. NEETA
(Asst. Prof.)

FACULTY OF COMPUTER SCIENCE AND ENGINEERING


SHRI RAMSWAROOP MEMORIAL UNIVERSITY,
LUCKNOW-DEVA ROAD, U.P., INDIA
April, 2024

INTRODUCTION:

Three address code is a type of intermediate code which is easy to


generate and can be easily converted to machine code. It makes
use of at most three addresses and one operator to represent an
expression and the value computed at each instruction is stored
in temporary variable generated by compiler. The compiler
decides the order of operation given by three address code.

Example :
x + y * z t1 = y * z t2 = x +t1 t1 and t2
are compiler-generated temporary names Statements in this
language are of the form: x:=y opz
where x, y and z are names, constants or compiler-generated
temporary variables, and ‘op’ stands for any operator .
 Three Address Code is a linearized representation of a syntax
trees or a DAG .
T1 = b – c
T2 = a * t1
T3= a +t2
T4 = t1 * d
T5 = t3 +t4
LITERATURE REVIEW:
Three Address Code is Used in Compiler
Applications
1. Optimization: Three address code is often used as an
intermediate representation of code during optimization
phases of the compilation process. The three address
code allows the compiler to analyze the code and
perform optimizations that can improve the performance
of the generated code.
2. Code generation: Three address code can also be used
as an intermediate representation of code during the
code generation phase of the compilation process. The
three address code allows the compiler to generate code
that is specific to the target platform, while also ensuring
that the generated code is correct and efficient.
3. Debugging: Three address code can be helpful in
debugging the code generated by the compiler. Since
three address code is a low-level language, it is often
easier to read and understand than the final generated
code. Developers can use the three address code to
trace the execution of the program and identify errors or
issues that may be present.
4. Language translation: Three address code can also be
used to translate code from one programming language
to another. By translating code to a common
intermediate representation, it becomes easier to
translate the code to multiple target languages.
General Representation
a = b op c
Where a, b or c represents operands like names,
constants or compiler generated temporaries
and op represents the operator.

EXAMPLE-

1,Convert the expression a * – (b + c) into three address code.

2.Write three address code for following code


for(i = 1; i<=10; i++)
{
a[i] = x * 5;
}
Implementation of Three Address
Code
There are 3 representations of three
address code namely
1. Quadruple
2. Triples
3. Indirect Triples
1.Quadruple – It is a structure which
consists of 4 fields namely op, arg1,
arg2 and result. op denotes the
operator and arg1 and arg2 denotes the
two operands and result is used to store
the result of the expression.

Advantage –
Easy to rearrange code for global

optimization.
 One can quickly access value of

temporary variables using symbol


table.
Disadvantage –
 Contain lot of temporaries.

 Temporary variable creation increases

time and space complexity.

EXAMPLE-
Consider expression a = b * – c + b * – c. The three address code
is:
t1 = u-c
t2 = b * t1
t3 = u-c
t4 = b * t3
t5 = t2 + t4
a = t5
2. Triples – This representation doesn’t
make use of extra temporary variable to
represent a single operation instead
when a reference to another triple’s
value is needed, a pointer to that triple
is used. So, it consist of only three fields
namely op, arg1 and arg2.
Disadvantage –
 Temporaries are implicit and

difficult to rearrange code.


 It is difficult to optimize because

optimization involves moving


intermediate code. When a triple is
moved, any other triple referring to
it must be updated also. With help
of pointer one can directly access
symbol table entry.
Example –
Consider expression a = b * – c + b * –
c
3. Indirect Triples – This representation
makes use of pointer to the listing of all
references to computations which is made
separately and stored. Its similar in utility
as compared to quadruple representation
but requires less space than it. Temporaries
are implicit and easier to rearrange code.
Example –
Consider expression a = b * – c + b * – c
CONCLUSION -
 There are several techniques that can be
utilized in code generation in compiler
design; among
these techniques are peephole enhancement,
parse tree, simple code generator and three
location
code. Their structural review with syntax
revealed the peculiar strategy and individual trait
that
serve as determinant factor for specific
application and circumstance for execution.
 Three address code is a type of intermediate code which
is easy to generate and can be easily converted to
machine code. It makes use of at most three addresses
and one operator to represent an expression and the
value computed at each instruction is stored in
temporary variable generated by compiler.

You might also like