Monday, April 6, 2009

source code

#include
#include
#include
#include
#include
struct tbl
{
char arg1[10],arg2[10],op[10],res[10];
}t[10];
char s[20][20];
int l;
int resol(int x,int j)
{
int k=0,n=strlen(s[x])-1;
t[j].op[k]=s[x][n-2];
t[j].arg1[k]=s[x][n-3];
t[j].arg2[k]=s[x][n-1];
itoa(l,t[j].res,10);
strcat(t[j].res,"t");
strrev(t[j].res);
l++;
cout<j++;
for(int i=strlen(s[x])-4;i>0;i=-2)
{
t[j].op[k]=s[x][i];
if(t[j].op[k]=='=')
{
t[j].op[k]='=';
itoa(l-1,t[j].arg1,10);
strcat(t[j].arg1,"t");
strrev(t[j].arg1);
strcpy(t[j].arg2," ");
t[j].res[k]=s[x][i-1];
cout<j++;
}
else
{
t[j].arg1[k]=s[x][l-1];
strcpy(t[j].arg2,t[j-1].res);
itoa(l,t[j].res,10);
strcat(t[j].res,"t");
strrev(t[j].res);
l++;
cout<j++;
}
}
return j;
}
void iff()
{
int i,j,k,n;
i=j=k=0;
char*s1="if()";
for(i=0;i<3;i++)
{
if(s1[i]!=s[0][i])
{
cout<<"format like 1 \n try again later!";
return;
}
}
/*if(s1[6]!=s[0][6])
{
cout<<"\n format like 2 \n try again!";
return;
}*/
char*s2="else";
if(strcmp(s2,s[2])==1)
{
cout<<"format like 3 \n try again!";
cout<return;
}
cout<<"\n\t pos \t op \t arg1 \t arg2 \t res";
t[j].op[k]=s[0][4];
t[j].arg1[k]=s[0][3];
t[j].arg2[k]=s[0][5];
strcpy(t[j].res,"t0");
cout<j++;
strcpy(t[j].op,"==");
strcpy(t[j].arg1,t[j-1].res);
strcpy(t[j].arg2,"FALSE");
int z=j+(int)((strlen(s[1])-1)/2+1);
itoa(z,t[j].res,10);
cout<j++;
l=1;
j=resol(1,j);
j=resol(3,j);
}
void main()
{
fflush(stdin);
clrscr();
int i,l;
i=0;
cout<<"\n front end of the compiler:\n-----------------\n";
cout<<"\n three address code generator for conditional 'if':\n";
cout<<"\n quadraples implementation \n";
cout<<"\n enter the statements: \n(for terminate enter ~ in a new line)\n";
do
{
gets(s[i]);
i++;
}
while(strcmp(s[i-l],"~"));
iff();
cout<<"\n end";
getch();
}

OUTPUT:

Front end of a compiler:
Three address code generator for conditional ‘if’:
quadruple implementations:

enter the statements:
for terminate enter ~ in a newline
if(aa=b+c;
else
d=e*f;
~

pos op arg1 arg2 res

0 < a b t0
1 == t0 FALSE 4
2 + b c t1
3 = t1 a
4 * e f t2
5 = t2 d
end