SIMULATION OF ANSI-C MACROPROCESSOR
PROGRAM CODE:-
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<conio.h>
struct deftab
{
char macroname[25];
char parameters[10][10];
int nop;
char value[150];
}s[10];
int nob=-1,il=-1,i=0,n=0,k=0,ival=-1,iop,x1=0;
char ch2[3];
char ret(int n)
{
char ch;
switch(n)
{
case 1:ch='1';break;
case 2:ch='2';break;
case 3:ch='3';break;
case 4:ch='4';break;
case 5:ch='5';break;
case 6:ch='6';break;
case 7:ch='7';break;
default:ch='?';break;
}
return ch;
}
int searchval(char *str)
{
int x1;
for(x1=0;x1<=nob;x1++)
{
if(strcmp(str,s[x1].macroname)==0)
return x1;
}
return -1;
}
int matchpara(int ob,char *str)
{
int i=0;
for(i=0;i<=s[ob].nop;i++)
{
if(strcmp(s[ob].parameters[i],str)==0)
return i+1;
}
return 0;
}
int main()
{
FILE *fp,*op,*op1;
int para=0;
int tol,n3=-1;
char line[100][250],exp[250],ch,word[100][25]={"\0"},newword[100][25]={"\0"},newword2[10][25]={"\0"},mac[100],newword3[100][25]={"\0"};
char neww[100][25]={"\0"},neww2[100][25]={"\0"};
fp=fopen("cinput.txt","r");
op=fopen("cmacro.txt","w");
op1=fopen("macroop.txt","w");
if(fp==NULL)
{
printf("\n File not found");
exit(0);
}
while((fgets(line[++il],100,fp))&&!feof(fp))
{
i=0;
strcpy(exp,line[il]);
ch=exp[i];
if(ch=='#')
{
//printf("%s",line[il]);
k=-1;
word[++n][++k]=ch;
i++;
ch=exp[i];
while(isalpha(ch))
{
word[n][++k]=ch;
i++;
ch=exp[i];
}
//printf("\n check--%s",word[n]);
if(strcmp("#define",word[n])!=0)
{ i=strlen(exp);
continue;}
i++;
ch=exp[i];
nob++;n++;k=-1;
while(isalpha(ch))
{
word[n][++k]=ch;
i++;
ch=exp[i];
}
strcpy(s[nob].macroname,word[n]);
// printf("macro name= %s//",s[nob].macroname);
if(ch=='{')
{
ival=-1;
s[nob].nop=-1;
i++;ch=exp[i];
strcpy(s[nob].value,"\0");
while(ch!='}')
{
s[nob].value[++ival]=ch;
i++;ch=exp[i];
}
}
else if(ch!='(' && ch!='{')
{
i++;
ch=exp[i];
ival=-1;
s[nob].value[++ival]=ch;
if(toascii(ch)==34)
{
i++;
ch=exp[i];
s[nob].value[++ival]=ch;
i++;ch=exp[i];
while(toascii(ch)!=34)
{
s[nob].value[++ival]=ch;
i++;
ch=exp[i];
}
s[nob].value[++ival]=ch;
}
else
{
i++;ch=exp[i];
while(isalpha(ch)||isdigit(ch)||ch=='.'||toascii(ch)==44)
{
s[nob].value[++ival]=ch;
i++;
ch=exp[i];
}
}
s[nob].value[++ival]='\0';
s[nob].nop=-1;
// printf("\n value=%s//\n",s[nob].value);
}//close if
else if(ch=='(')
{
s[nob].nop=-1;
again:
s[nob].nop++;
iop=-1;
i++;
ch=exp[i];
while(isalpha(ch)||isdigit(ch))
{
s[nob].parameters[s[nob].nop][++iop]=ch;
i++;
ch=exp[i];
}
s[nob].parameters[s[nob].nop][++iop]='\0';
// printf("\n para [%d]=%s",s[nob].nop,s[nob].parameters[s[nob].nop]);
if(ch!=')')
goto again;
//now ch=')' end of para list
i+=2;
ch=exp[i];
ival=-1;
//test
if(exp[i-1]=='('){
while(ch!=')' &&i<strlen(exp))
{
if(isalpha(ch))
{
n++;
k=-1;
while(isalpha(ch)||isdigit(ch))
{
word[n][++k]=ch;
//printf("\n (%s)",word[n]);
i++;
ch=exp[i];
}
word[n][++k]='\0';
//printf("\n word[%d]=%s",n,word[n]);
if(matchpara(nob,word[n]))
{
int retn=matchpara(nob,word[n]);
s[nob].value[++ival]='$';
s[nob].value[++ival]=ret(retn);
}
else if(searchval(word[n])!=-1&&ch!='(')
{
// printf("\n value found==%s",s[searchval(word[n])].value);
strcat(s[nob].value,s[searchval(word[n])].value);
ival+=strlen(s[searchval(word[n])].value);
}
else
{
strcat(s[nob].value,word[n]);
ival+=strlen(word[n]);
}
if(ch!=')')
s[nob].value[++ival]=ch;
}
i++;
ch=exp[i];
}
}
else
{
// printf("\n in else only");
while(ch!='}' &&i<strlen(exp))
{
n++;
k=-1;
while(isalpha(ch)||isdigit(ch))
{
word[n][++k]=ch;
//printf("\n (%s)",word[n]);
i++;
ch=exp[i];
}
word[n][++k]='\0';
//printf("\n word[%d]=%s",n,word[n]);
if(matchpara(nob,word[n]))
{
int retn=matchpara(nob,word[n]);
s[nob].value[++ival]='$';
s[nob].value[++ival]=ret(retn);
}
else if(searchval(word[n])!=-1&&ch!='(')
{
// printf("\n value found==%s",s[searchval(word[n])].value);
strcat(s[nob].value,s[searchval(word[n])].value);
ival+=strlen(s[searchval(word[n])].value);
}
//last
else
{
strcat(s[nob].value,word[n]);
ival+=strlen(word[n]);
}
//last
if(ch!='}')
s[nob].value[++ival]=ch;
//}
i++;
ch=exp[i];
}
}
s[nob].value[++ival]='\0';
}
//printf("\n");
i++;
}
}
for(i=0;i<=nob;i++)
{
printf("\n %s \t%s\t",s[i].macroname,s[i].value);
if(s[i].nop!=-1)
{
printf("||parameters(");
for(x1=0;x1<=s[i].nop;x1++)
printf("%s,",s[i].parameters[x1]);
printf(")");
}
}
fclose(fp);
fp=fopen("cinput.txt","r");
il=-1;n=-1;
printf("\n");
while((fgets(line[++il],100,fp))&&!feof(fp))
{
i=0;
int nn=-1;
strcpy(exp,line[il]);
for(x1=0;x1<100;x1++)
mac[x1]='\0';
//printf("%s",exp);
ch=exp[i];
if(ch=='#'&&exp[i+1]!='d')
{fprintf(op,"%s",exp);
i=strlen(exp);}
if(ch=='#' &&exp[i+1]=='d')
{//fprintf(op,"%s",exp);
i=strlen(exp);}
while(i<strlen(exp))
{
if(toascii(ch)==34)
{
k=-1;
newword[++n][++k]=ch;
i++;
ch=exp[i];
while(toascii(ch)!=34)
{
newword[n][++k]=ch;
i++;
ch=exp[i];
}
newword[n][++k]=ch;
i++;
ch=exp[i];
// printf("\n quoted=%s &ch=%c",newword[n],ch);
fprintf(op,"%s",newword[n]);
}
if(isalpha(ch))
{
k=-1;
newword[++n][++k]=ch;
i++;
ch=exp[i];
while(isalpha(ch)||isdigit(ch))
{
newword[n][++k]=ch;
i++;
ch=exp[i];
}
//printf("\nnewword[n]= %s",newword[n]);
if(searchval(newword[n])!=-1)
{
// printf("\n macro found=%s",newword[n]);
strcpy(mac,s[searchval(newword[n])].value);
// printf("\n value=%s",mac);
if(s[searchval(newword[n])].nop!=-1)
{
loop2:
k=-1;
i++;ch=exp[i];
newword2[++nn][++k]=ch;
i++;ch=exp[i];
while(isalpha(ch))
{
newword2[nn][++k]=ch;
i++;
ch=exp[i];
}
// printf("\nparameters[%d]- %s",nn,newword2[nn]);
if(ch!=')')
goto loop2;
for(x1=0;x1<=strlen(mac);x1++)
{
ch=mac[x1];
if(mac[x1]!='$')
fprintf(op,"%c",ch);
else
{
x1++;
int index;
ch2[0]=mac[x1];
ch2[1]='\0';
index=strtol(ch2,NULL,10);
fprintf(op,"%s",newword2[index-1]);
}
}
}
//
else
fprintf(op,"%s",mac);
}
else
fprintf(op,"%s",newword[n]);
//printf("\n check==%s",newword[n]);}
}
fprintf(op,"%c",ch);
i++;
ch=exp[i];
}
}
fclose(fp);
fclose(op);
op=fopen("cmacro.txt","r");
//start
il=-1;n=-1;
printf("\n");
while((fgets(line[++il],100,fp))&&!feof(fp))
{
i=0;
int nn=-1;
strcpy(exp,line[il]);
for(x1=0;x1<100;x1++)
mac[x1]='\0';
//printf("%s",exp);
ch=exp[i];
if(ch=='#')
{
fprintf(op1,"%s",exp);i=strlen(exp);
}
while(i<strlen(exp))
{
if(isalpha(ch))
{
k=-1;
neww[++n][++k]=ch;
i++;
ch=exp[i];
while(isalpha(ch))
{
neww[n][++k]=ch;
i++;
ch=exp[i];
}
//printf("\n newword[%d]=%s",n,neww[n]);
if(searchval(neww[n])!=-1&&s[searchval(neww[n])].nop==-1)
{
if(s[searchval(neww[n])].nop!=-1)
fprintf(op1,"%s",s[searchval(neww[n])].value);
}
//lasttry!!
else if(searchval(neww[n])!=-1&&s[searchval(neww[n])].nop>=0)
{
for(x1=0;x1<100;x1++)
mac[x1]='\0';
strcpy(mac,s[searchval(neww[n])].value);
//printf("\n value=%s",mac);
if(s[searchval(neww[n])].nop!=-1)
{
loop3:
k=-1;
i++;ch=exp[i];
neww2[++nn][++k]=ch;
i++;ch=exp[i];
while(isalpha(ch))
{
neww[nn][++k]=ch;
i++;
ch=exp[i];
}
// printf("\nparameterss[%d]- %s",nn,neww2[nn]);
if(ch!=')')
goto loop3;
for(x1=0;x1<=strlen(mac);x1++)
{
ch=mac[x1];
if(mac[x1]!='$')
fprintf(op1,"%c",ch);
else
{
x1++;
int index;
ch2[0]=mac[x1];
ch2[1]='\0';
index=strtol(ch2,NULL,10);
printf("\n %d %s",index,neww2[index-1]);
fprintf(op1,"%s",neww2[index-1]);
}
}
}
//
}
else
fprintf(op1,"%s",neww[n]);
fprintf(op1,"%c",ch);
}
else
fprintf(op1,"%c",ch);
i++;
ch=exp[i];
}
// printf("\n ch=%c",ch);
if(i<strlen(exp))
fprintf(op1,"%c\n",exp[i]);
if(ch==';'||ch==')')
fprintf(op1,"%c\n",ch);
}
//
fclose(op);
fclose(op1);
getch();
}
SAMPLE INPUT-FILE:-
#include<stdio.h>
#include<conio.h>
#define pi 3.147
#define str "Area calculated of"
#define prd(x,y)(x*y)
#define sum(q,w)(q+w)
#define absdiff(a,b)(a>b?a-b:b-a)
#define printend{printf("\nthe end!!")}
int main()
{
int l,b,summ;
float rad;
summ=sum(l,b);
printf("\n Enter the radius ");
scanf("%f",&rad);
printf("\n The value of pi is",pi);
printf("\n Enter length and breadth of rectangle");
scanf("%d%d",&l,&b);
printf("\n %s circle=%f", str, pi*rad*rad);
printf("\n %s of rectangle=%f",str,prd(l,b));
printf("\n Absolute difference=%d",absdiff(l,b));
printend;
getch();
}
PROGRAM CODE:-
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<conio.h>
struct deftab
{
char macroname[25];
char parameters[10][10];
int nop;
char value[150];
}s[10];
int nob=-1,il=-1,i=0,n=0,k=0,ival=-1,iop,x1=0;
char ch2[3];
char ret(int n)
{
char ch;
switch(n)
{
case 1:ch='1';break;
case 2:ch='2';break;
case 3:ch='3';break;
case 4:ch='4';break;
case 5:ch='5';break;
case 6:ch='6';break;
case 7:ch='7';break;
default:ch='?';break;
}
return ch;
}
int searchval(char *str)
{
int x1;
for(x1=0;x1<=nob;x1++)
{
if(strcmp(str,s[x1].macroname)==0)
return x1;
}
return -1;
}
int matchpara(int ob,char *str)
{
int i=0;
for(i=0;i<=s[ob].nop;i++)
{
if(strcmp(s[ob].parameters[i],str)==0)
return i+1;
}
return 0;
}
int main()
{
FILE *fp,*op,*op1;
int para=0;
int tol,n3=-1;
char line[100][250],exp[250],ch,word[100][25]={"\0"},newword[100][25]={"\0"},newword2[10][25]={"\0"},mac[100],newword3[100][25]={"\0"};
char neww[100][25]={"\0"},neww2[100][25]={"\0"};
fp=fopen("cinput.txt","r");
op=fopen("cmacro.txt","w");
op1=fopen("macroop.txt","w");
if(fp==NULL)
{
printf("\n File not found");
exit(0);
}
while((fgets(line[++il],100,fp))&&!feof(fp))
{
i=0;
strcpy(exp,line[il]);
ch=exp[i];
if(ch=='#')
{
//printf("%s",line[il]);
k=-1;
word[++n][++k]=ch;
i++;
ch=exp[i];
while(isalpha(ch))
{
word[n][++k]=ch;
i++;
ch=exp[i];
}
//printf("\n check--%s",word[n]);
if(strcmp("#define",word[n])!=0)
{ i=strlen(exp);
continue;}
i++;
ch=exp[i];
nob++;n++;k=-1;
while(isalpha(ch))
{
word[n][++k]=ch;
i++;
ch=exp[i];
}
strcpy(s[nob].macroname,word[n]);
// printf("macro name= %s//",s[nob].macroname);
if(ch=='{')
{
ival=-1;
s[nob].nop=-1;
i++;ch=exp[i];
strcpy(s[nob].value,"\0");
while(ch!='}')
{
s[nob].value[++ival]=ch;
i++;ch=exp[i];
}
}
else if(ch!='(' && ch!='{')
{
i++;
ch=exp[i];
ival=-1;
s[nob].value[++ival]=ch;
if(toascii(ch)==34)
{
i++;
ch=exp[i];
s[nob].value[++ival]=ch;
i++;ch=exp[i];
while(toascii(ch)!=34)
{
s[nob].value[++ival]=ch;
i++;
ch=exp[i];
}
s[nob].value[++ival]=ch;
}
else
{
i++;ch=exp[i];
while(isalpha(ch)||isdigit(ch)||ch=='.'||toascii(ch)==44)
{
s[nob].value[++ival]=ch;
i++;
ch=exp[i];
}
}
s[nob].value[++ival]='\0';
s[nob].nop=-1;
// printf("\n value=%s//\n",s[nob].value);
}//close if
else if(ch=='(')
{
s[nob].nop=-1;
again:
s[nob].nop++;
iop=-1;
i++;
ch=exp[i];
while(isalpha(ch)||isdigit(ch))
{
s[nob].parameters[s[nob].nop][++iop]=ch;
i++;
ch=exp[i];
}
s[nob].parameters[s[nob].nop][++iop]='\0';
// printf("\n para [%d]=%s",s[nob].nop,s[nob].parameters[s[nob].nop]);
if(ch!=')')
goto again;
//now ch=')' end of para list
i+=2;
ch=exp[i];
ival=-1;
//test
if(exp[i-1]=='('){
while(ch!=')' &&i<strlen(exp))
{
if(isalpha(ch))
{
n++;
k=-1;
while(isalpha(ch)||isdigit(ch))
{
word[n][++k]=ch;
//printf("\n (%s)",word[n]);
i++;
ch=exp[i];
}
word[n][++k]='\0';
//printf("\n word[%d]=%s",n,word[n]);
if(matchpara(nob,word[n]))
{
int retn=matchpara(nob,word[n]);
s[nob].value[++ival]='$';
s[nob].value[++ival]=ret(retn);
}
else if(searchval(word[n])!=-1&&ch!='(')
{
// printf("\n value found==%s",s[searchval(word[n])].value);
strcat(s[nob].value,s[searchval(word[n])].value);
ival+=strlen(s[searchval(word[n])].value);
}
else
{
strcat(s[nob].value,word[n]);
ival+=strlen(word[n]);
}
if(ch!=')')
s[nob].value[++ival]=ch;
}
i++;
ch=exp[i];
}
}
else
{
// printf("\n in else only");
while(ch!='}' &&i<strlen(exp))
{
n++;
k=-1;
while(isalpha(ch)||isdigit(ch))
{
word[n][++k]=ch;
//printf("\n (%s)",word[n]);
i++;
ch=exp[i];
}
word[n][++k]='\0';
//printf("\n word[%d]=%s",n,word[n]);
if(matchpara(nob,word[n]))
{
int retn=matchpara(nob,word[n]);
s[nob].value[++ival]='$';
s[nob].value[++ival]=ret(retn);
}
else if(searchval(word[n])!=-1&&ch!='(')
{
// printf("\n value found==%s",s[searchval(word[n])].value);
strcat(s[nob].value,s[searchval(word[n])].value);
ival+=strlen(s[searchval(word[n])].value);
}
//last
else
{
strcat(s[nob].value,word[n]);
ival+=strlen(word[n]);
}
//last
if(ch!='}')
s[nob].value[++ival]=ch;
//}
i++;
ch=exp[i];
}
}
s[nob].value[++ival]='\0';
}
//printf("\n");
i++;
}
}
for(i=0;i<=nob;i++)
{
printf("\n %s \t%s\t",s[i].macroname,s[i].value);
if(s[i].nop!=-1)
{
printf("||parameters(");
for(x1=0;x1<=s[i].nop;x1++)
printf("%s,",s[i].parameters[x1]);
printf(")");
}
}
fclose(fp);
fp=fopen("cinput.txt","r");
il=-1;n=-1;
printf("\n");
while((fgets(line[++il],100,fp))&&!feof(fp))
{
i=0;
int nn=-1;
strcpy(exp,line[il]);
for(x1=0;x1<100;x1++)
mac[x1]='\0';
//printf("%s",exp);
ch=exp[i];
if(ch=='#'&&exp[i+1]!='d')
{fprintf(op,"%s",exp);
i=strlen(exp);}
if(ch=='#' &&exp[i+1]=='d')
{//fprintf(op,"%s",exp);
i=strlen(exp);}
while(i<strlen(exp))
{
if(toascii(ch)==34)
{
k=-1;
newword[++n][++k]=ch;
i++;
ch=exp[i];
while(toascii(ch)!=34)
{
newword[n][++k]=ch;
i++;
ch=exp[i];
}
newword[n][++k]=ch;
i++;
ch=exp[i];
// printf("\n quoted=%s &ch=%c",newword[n],ch);
fprintf(op,"%s",newword[n]);
}
if(isalpha(ch))
{
k=-1;
newword[++n][++k]=ch;
i++;
ch=exp[i];
while(isalpha(ch)||isdigit(ch))
{
newword[n][++k]=ch;
i++;
ch=exp[i];
}
//printf("\nnewword[n]= %s",newword[n]);
if(searchval(newword[n])!=-1)
{
// printf("\n macro found=%s",newword[n]);
strcpy(mac,s[searchval(newword[n])].value);
// printf("\n value=%s",mac);
if(s[searchval(newword[n])].nop!=-1)
{
loop2:
k=-1;
i++;ch=exp[i];
newword2[++nn][++k]=ch;
i++;ch=exp[i];
while(isalpha(ch))
{
newword2[nn][++k]=ch;
i++;
ch=exp[i];
}
// printf("\nparameters[%d]- %s",nn,newword2[nn]);
if(ch!=')')
goto loop2;
for(x1=0;x1<=strlen(mac);x1++)
{
ch=mac[x1];
if(mac[x1]!='$')
fprintf(op,"%c",ch);
else
{
x1++;
int index;
ch2[0]=mac[x1];
ch2[1]='\0';
index=strtol(ch2,NULL,10);
fprintf(op,"%s",newword2[index-1]);
}
}
}
//
else
fprintf(op,"%s",mac);
}
else
fprintf(op,"%s",newword[n]);
//printf("\n check==%s",newword[n]);}
}
fprintf(op,"%c",ch);
i++;
ch=exp[i];
}
}
fclose(fp);
fclose(op);
op=fopen("cmacro.txt","r");
//start
il=-1;n=-1;
printf("\n");
while((fgets(line[++il],100,fp))&&!feof(fp))
{
i=0;
int nn=-1;
strcpy(exp,line[il]);
for(x1=0;x1<100;x1++)
mac[x1]='\0';
//printf("%s",exp);
ch=exp[i];
if(ch=='#')
{
fprintf(op1,"%s",exp);i=strlen(exp);
}
while(i<strlen(exp))
{
if(isalpha(ch))
{
k=-1;
neww[++n][++k]=ch;
i++;
ch=exp[i];
while(isalpha(ch))
{
neww[n][++k]=ch;
i++;
ch=exp[i];
}
//printf("\n newword[%d]=%s",n,neww[n]);
if(searchval(neww[n])!=-1&&s[searchval(neww[n])].nop==-1)
{
if(s[searchval(neww[n])].nop!=-1)
fprintf(op1,"%s",s[searchval(neww[n])].value);
}
//lasttry!!
else if(searchval(neww[n])!=-1&&s[searchval(neww[n])].nop>=0)
{
for(x1=0;x1<100;x1++)
mac[x1]='\0';
strcpy(mac,s[searchval(neww[n])].value);
//printf("\n value=%s",mac);
if(s[searchval(neww[n])].nop!=-1)
{
loop3:
k=-1;
i++;ch=exp[i];
neww2[++nn][++k]=ch;
i++;ch=exp[i];
while(isalpha(ch))
{
neww[nn][++k]=ch;
i++;
ch=exp[i];
}
// printf("\nparameterss[%d]- %s",nn,neww2[nn]);
if(ch!=')')
goto loop3;
for(x1=0;x1<=strlen(mac);x1++)
{
ch=mac[x1];
if(mac[x1]!='$')
fprintf(op1,"%c",ch);
else
{
x1++;
int index;
ch2[0]=mac[x1];
ch2[1]='\0';
index=strtol(ch2,NULL,10);
printf("\n %d %s",index,neww2[index-1]);
fprintf(op1,"%s",neww2[index-1]);
}
}
}
//
}
else
fprintf(op1,"%s",neww[n]);
fprintf(op1,"%c",ch);
}
else
fprintf(op1,"%c",ch);
i++;
ch=exp[i];
}
// printf("\n ch=%c",ch);
if(i<strlen(exp))
fprintf(op1,"%c\n",exp[i]);
if(ch==';'||ch==')')
fprintf(op1,"%c\n",ch);
}
//
fclose(op);
fclose(op1);
getch();
}
SAMPLE INPUT-FILE:-
#include<stdio.h>
#include<conio.h>
#define pi 3.147
#define str "Area calculated of"
#define prd(x,y)(x*y)
#define sum(q,w)(q+w)
#define absdiff(a,b)(a>b?a-b:b-a)
#define printend{printf("\nthe end!!")}
int main()
{
int l,b,summ;
float rad;
summ=sum(l,b);
printf("\n Enter the radius ");
scanf("%f",&rad);
printf("\n The value of pi is",pi);
printf("\n Enter length and breadth of rectangle");
scanf("%d%d",&l,&b);
printf("\n %s circle=%f", str, pi*rad*rad);
printf("\n %s of rectangle=%f",str,prd(l,b));
printf("\n Absolute difference=%d",absdiff(l,b));
printend;
getch();
}
No comments:
Post a Comment