0% found this document useful (0 votes)
13 views4 pages

SPCC Exp5

System programs and compiler construction experiments

Uploaded by

sanjanabhosle27
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)
13 views4 pages

SPCC Exp5

System programs and compiler construction experiments

Uploaded by

sanjanabhosle27
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/ 4

EXPERIMENT NO.

Aim of the Experiment: - Implementation of Intermediate code generation phase of


compiler.

Lab Outcome: - Identify and Validate tokens for given high level language and
Implement synthesis phase of compiler

Date of Performance: - ___________________

Date of Submission: - __________________

Implementation Understanding Punctuality & Discipline Total Marks


(05) (05) (05) (15)

________________________________

Practical Incharge
EXPERIMENT NO-5

Aim: Implementation of Intermediate code generation phase of compiler.

Theory:

Three ways of intermediate representation:

Syntax tree

Postfix notation

Three address code

The semantic rules for generating three-address code from common programming
language constructs are similar to those for constructing syntax trees or for
generating postfix notation.

Graphical Representations:

1.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 subexpressions are identified. A syntax tree and dag for the
assignment statement a : = b * - c + b * - c are as follows:

2.Postfix notation:

Postfix notation is a linearized representation of a syntax tree; it is a list of the nodes


of the tree in which a node appears immediately after its children. The postfix
notation for the syntax tree given above is a b c uminus * b c uminus * + assign

3. Three-Address Code:

Three-address code is a sequence of statements of the general form x : = y op z where


x, y and z are names, constants, or compiler-generated temporaries; op stands for any
operator, such as a fixed- or floating-point arithmetic operator, or a logical operator
on Boolean valued data. Thus a source language expression like x+ y*z might be
translated into a sequence

t1 : = y * z

t2 : = x + t1

where t1 and t2 are compiler-generated temporary names.

The reason for the term “three-address code” is that each statement usually
contains three addresses, two for the operands and one for the result.

Advantages of three-address code:


The unraveling of complicated arithmetic expressions and of nested flow-of-
control statements makes three-address code desirable for target code generation
and optimization.

The use of names for the intermediate values computed by a program allows three
address code to be easily rearranged – unlike postfix notation.

Three-address code is a liberalized representation of a syntax tree or a dag in which


explicit names correspond to the interior nodes of the graph. The syntax tree and dag
are represented by the three-address code sequences. Variable names can appear
directly in three address statements.

Types of Three-Address Statements:

The common three-address statements are:

Assignment statements of the form x : = y op z, where op is a binary arithmetic or


logical operation.
Assignment instructions of the form x : = op y, where op is a unary operation.
Essential unary operations include unary minus, logical negation, shift operators,
and conversion operators that, for example, convert a fixed-point number to a floating
point number.
Copy statements of the form x : = y where the value of y is assigned to x. The
unconditional jump goto L. The three-address statement with label L is the next to be
executed.
Conditional jumps such as if x relop y goto L. This instruction applies a relational
operator (<, =, >=, etc. ) to x and y, and executes the statement with label L next if x
stands in relation relop to y. If not, the three-address statement following if x relop y
goto L is executed next, as in the usual sequence.
param x and call p, n for procedure calls and return y, where y representing a returned
value is optional.

For example,

param x1

param x2

.......

paramxn

callp,n

generated as part of a call of the procedure p(x1, x2, …. ,xn ).

Indexed assignments of the form x : = y[i] and x[i] : = y.

Address and pointer assignments of the form x: = &y , x : = *y, and *x : = y.


Program:

import java.util.Scanner;
public class Main {
public static void main(String[] args) {
String a[] = new String[50];
String op[] = {"+", "-", "*", "/", "%", "^"};
String n[] = {"t0", "t1", "t2", "t3", "t4", "t5"};
String exp;
int count = 0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the arithmetic Expression: ");
exp = sc.nextLine();
a = exp.split(" ");
System.out.println("Op\targument1\targument2\tresult obtained");
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < op.length; j++) {
if (a[i].equals(op[j])) {
if (count == 0) {
System.out.println(a[i] + "\t" + a[i - 1] + "\t\t" + a[i + 1] + "\t");
} else {
System.out.println(a[i] + "\t" + n[count - 1] + "\t\t" + a[i + 1] + "\t\t" + n[count
- 1]);
}
count++;
}
}
}
}
}

Output:

Conclusion: Hence, we have successfully implemented intermediate code generation of


compiler.

You might also like