You are on page 1of 3

Practical # 07

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

Code:
#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(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;j<n;j++)
a[j]=a[j+2];
d++;
continue;
}
if((isalpha(a[i-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++;
}
}
getch();
}
Output:

Result: The above program was successfully written

and executed

You might also like