0% found this document useful (0 votes)
42 views5 pages

Practical6 Front End

The document describes an algorithm to implement the front end of a compiler that produces three-address statements from expressions. It reads in an expression, converts it to postfix notation, and outputs equivalent three-address statements with temporary variables. The program includes functions for reading input, checking for operators and operands, and outputting three-address statements by replacing subexpressions with temporary variables.

Uploaded by

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

Practical6 Front End

The document describes an algorithm to implement the front end of a compiler that produces three-address statements from expressions. It reads in an expression, converts it to postfix notation, and outputs equivalent three-address statements with temporary variables. The program includes functions for reading input, checking for operators and operands, and outputting three-address statements by replacing subexpressions with temporary variables.

Uploaded by

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

Mehran University of Engineering and Technology, Jamshoro

Deparment of Computer Systems Engineering


09CSE
Subject: Compiler Construction
LAB#6

Object: Write a program to implement front end of compiler that produce three address statement
Algorithm:
1. start the program
2. declare the required variables
3. read the expression from keyboard or user and store it in to array
3. read the arrays elements up to delimiter and convert it into three address statement using for
loop , isalpha(), isdigit() and if else statement
5. three address statement is of the form c = a+b*d
e.g
An expression c= a+b*d then equivalent three address code statement is,
t0 = b*d
t1= a+t0
c = t1
6. print all three address statement
7. stop the program

#include <constream.h>
#include <string.h>
#include <ctype.h>
void main()
{
char a[100];
int i,j,n;
char d = 'a';
cout<<"enter the expression";
cin>>a;
n = strlen(a);

for (i = 0;i<n; i++)


{
if(a(i) == '!')
{
cout<<d<<a[i], a[i+1];
a[i]='I';
a[i+1]= d;
d++;
}

for (i=0;i<n;i++)
{
if((a[i-2]=='I')&&(a[i]=='*')&&(a[i+1]=='I'))

{
cout<<d<<a[i-2]<<a[i-1]<<a[i+1<<a]i+2];
a[i-2]='I';
a[i-1]=d;
for (j=0;j<n;j++)
a[j]=a[j+3];
d++;
continue;
}
if((a[i-2]=="I")&&(a[i]=='*')&&(isalpha(a[i+1])))

{
cout<<d<<a[i-2]<<a[i-1]<<a[i+1];
a[i-2]='I';
a[i-1]= d;
for(j=i;j<n;j++)
a[j]=a[j+2];
d++;
continue
}
if(isalpha(a[i-1])&&(a[i]=='*')&&(a[i+1]=='I'))
{
cout<<d <<a[i-1]<<a[i+1]<<a[i+2]);
a[i-1]='I';
a[i]=d;
for (j=i+1;j<n;j++)
a[j]=a[j+2];
d++;
continue;
}
}
for (i=0;i<n;i++)

{
if((a[i-2]=="I")&&(a[i]=='+')&&(a[i+1]=='I'))
{
cout<<d<<a[i-2]<<a[i-1]<<a[i+1]a[i+2];

a[i-2]='I';
a[i-1]=d;
for (j=i;j<n;j++)
a[j]=a[j+2];
d++;
continue;
}
if((isalpha[i-1]))&&(a[i]=='+')&&(a[i+1]=='I'))
{
cout<<d<<a[i-1]<<a[i+1]<<a[i+2];
a[i-1]='I';
a[i]=d;
for (j=i;j<n;j++)
a[j]=a[j+2];
d++;
continue;
}
if(isalpha(a[a-1]))&&(a[i]=='+')&&(isalpha(a[i+1])))
{
cout<<d<<a[i-1]<<a[i+1];
a[i-1]='I';
a[i]=d;
for (j=i;j<n;j++)
a[j]=a[j+1];
d++;
continue;

}
for i=0;i<n;i++)
{
if(a[i]=='=')

cout<<d<<a[i-1]<<a[i+1]<<a[i+2];
d++;
}
}
}

Output:

Result: The above program was successfully written and executed

You might also like