Program to generate intermediate code of any given expression.
Program:
#include<iostream.h>
#include<conio.h>
#include<ctype.h>
main()
{
int i,k,t,len,b,p,j,c=0,y,o;
char exp[45],temp[8][50],A[8],stack[8][8];
char op[10]={'+','*','-','/'};
char sym[11]={'0','1','2','3','4','5','6','7','8','9'};t=0;j=0;
cout<<"Enter an expression\n";
cin>>exp;
len=strlen(exp);
for(i=len;i>=1;i--)
{
if(isdigit(exp[i]))
{
while(isdigit(exp[i]))
{
temp[t][i]=exp[i];
i--;
} exp[i+1]='q';
cout<<"After reduction the new expression is ";
for(b=0;b<=i+1;b++)
{
cout<<exp[b];
}cout<<i<<endl;
}
else if(isalpha(exp[i]))
{
while(isalpha(exp[i]))
{
temp[t][i]=exp[i];
i--;
} exp[i+1]='q';
cout<<"After reduction the new expression is ";
for(b=0;b<=i+1;b++)
{
cout<<exp[b];
}cout<<i<<endl;
}
t++;
}
getch();
}
Output:
Program:
#include<iostream.h>
#include<conio.h>
#include<ctype.h>
main()
{
int i,k,t,len,b,p,j,c=0,y,o;
char exp[45],temp[8][50],A[8],stack[8][8];
char op[10]={'+','*','-','/'};
char sym[11]={'0','1','2','3','4','5','6','7','8','9'};t=0;j=0;
cout<<"Enter an expression\n";
cin>>exp;
len=strlen(exp);
for(i=len;i>=1;i--)
{
if(isdigit(exp[i]))
{
while(isdigit(exp[i]))
{
temp[t][i]=exp[i];
i--;
} exp[i+1]='q';
cout<<"After reduction the new expression is ";
for(b=0;b<=i+1;b++)
{
cout<<exp[b];
}cout<<i<<endl;
}
else if(isalpha(exp[i]))
{
while(isalpha(exp[i]))
{
temp[t][i]=exp[i];
i--;
} exp[i+1]='q';
cout<<"After reduction the new expression is ";
for(b=0;b<=i+1;b++)
{
cout<<exp[b];
}cout<<i<<endl;
}
t++;
}
getch();
}
Output:
0 comments:
Post a Comment