Assignment 3 :- Macro Processor
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char fnm[20];
char **pnt;
char **apt;
FILE *fpr,*fpw;
int mntcnt=0,kptcnt=0,mdtcnt=0,line=0;
struct mnttab
{
char name[20];
int pp;
int kp;
int kptptr;
int mdtptr;
}mnt[8];
struct kpttab
{
char name[20];
char value[20];
}kpt[32];
struct mdttab
{
char t1[20];
char t2[20];
char t3[20];
char t4[20];
}mdt[100];
int mnt_search(char*);
int pnt_search(char*);
void header(char*);
void disp();
void disp_pnt();
void MDP();
int kpt_search(int,char*);
void MCP(char*,int);
void disp_apt(int);
int main(int argc,char* argv[])
{
char ln[100],mn[20],mp[60];
int i=-1;
if(argc==2)
strcpy(fnm,argv[1]);
else
{
printf("\nFile name not given\n");
return;
}
fpr=fopen(fnm,"r");
fpw=fopen("macro.txt","a");
if(fpr==NULL)
{
printf("\nFile does not exists\n");
return;
}
while(!feof(fpr))
{
fgets(ln,100,fpr);
if(feof(fpr))
break;
if(ln[0]=='\n')
continue;
sscanf(ln,"%s %s",mn,mp);
i=mnt_search(mn);
if(i!=-1)
MCP(mp,i);
else if(strncasecmp(mn,"macro",5)==0)
{
fgets(ln,100,fpr);
header(ln);
}
else
fputs(ln,fpw);
}
disp();
}
int mnt_search(char *t)
{
int i;
for(i=0;i<mntcnt;i++)
if(strcmp(t,mnt[i].name)==0)
return i;
return -1;
}
int pnt_search(char *t)
{
int i;
for(i=0;i<mnt[mntcnt].pp+mnt[mntcnt].kp;i++)
if(strcmp(t,pnt[i])==0)
return i;
return -1;
}
int kpt_search(int i,char *t)
{
int j,k;
for(j=mnt[i].kptptr;j<=mnt[i].kp;j++)
{
k=0;
while(t[k]==kpt[j].name[k+1])
{
if(t[k]=='\0')
break;
k++;
}
if(t[k]=='\0')
return j;
}
return -1;
}
void header(char *ln)
{
char t[5];
int i,j=0,pp=0,kp=0;
pnt=(char**)malloc(10*sizeof(char*));
for(i=0;ln[i]!='\0';i++)
{
if(ln[i]==' ')
{
t[j]='\0';
if(mnt_search(t)==-1)
{
strcpy(mnt[mntcnt].name,t);
mnt[mntcnt].kptptr=kptcnt;
mnt[mntcnt].mdtptr=mdtcnt;
}
else
{
printf("\nline=%d Duplicate macro definition",line);
exit(1);
}
j=0;
}
else if(ln[i]==',')
{
t[j]='\0';
pnt[pp]=(char*)malloc(strlen(t)*sizeof(char));
strcpy(pnt[pp],t);
j=0;
pp++;
}
else if(ln[i]=='=')
{
t[j]='\0';
pnt[pp]=(char*)malloc(strlen(t)*sizeof(char));
strcpy(pnt[pp],t);
strcpy(kpt[kptcnt].name,t);
kp++;
pp++;
j=0;
i=i+1;
while(ln[i]!=',' && ln[i]!='\n')
{
t[j]=ln[i];
j++;
i++;
}
t[j]='\0';
strcpy(kpt[kptcnt].value,t);
kptcnt++;
j=0;
}
else
{
t[j]=ln[i];
j++;
}
}
mnt[mntcnt].pp=pp-kp;
mnt[mntcnt].kp=kp;
disp_pnt();
MDP();
mntcnt++;
}
void MDP()
{
char ln[50];
int i,n;
while(1)
{
fgets(ln,50,fpr);
if(strncasecmp(ln,"mend",4)==0)
{
strcpy(mdt[mdtcnt].t1,ln);
mdtcnt++;
return;
}
n=sscanf(ln,"%s %s %s %s",mdt[mdtcnt].t1,mdt[mdtcnt].t2,mdt[mdtcnt].t3,mdt[mdtcnt].t4);
if(mdt[mdtcnt].t1[0]=='&')
{
i=pnt_search(mdt[mdtcnt].t1);
sprintf(mdt[mdtcnt].t1,"%d",i);
}
if(mdt[mdtcnt].t2[0]=='&')
{
i=pnt_search(mdt[mdtcnt].t2);
sprintf(mdt[mdtcnt].t2,"%d",i);
}
if(mdt[mdtcnt].t3[0]=='&')
{
i=pnt_search(mdt[mdtcnt].t3);
sprintf(mdt[mdtcnt].t3,"%d",i);
}
if(mdt[mdtcnt].t4[0]=='&')
{
i=pnt_search(mdt[mdtcnt].t4);
sprintf(mdt[mdtcnt].t4,"%d",i);
}
mdtcnt++;
}
free(pnt);
}
void MCP(char *mp,int i)
{
char t1[20],t2[20],t3[20],t4[20];
int p,j=0,n,k=0,l;
char t[20];
p=mnt[i].pp+mnt[i].kp;
apt=(char**)malloc(p*sizeof(char*));
for(j=mnt[i].pp,k=mnt[i].kptptr;j<p;j++,k++)
{ apt[j]=(char*)malloc(strlen(kpt[k].value)*sizeof(char));
strcpy(apt[j],kpt[k].value);}
p=0,k=0,j=0;
while(1)
{
if(mp[j]==',' || mp[j]=='\0')
{t[k]='\0';
apt[p]=(char*)malloc(strlen(t)*sizeof(char));
strcpy(apt[p],t);
p++;k=0;if(mp[j]=='\0')break;}
else if(mp[j]=='=')
{
t[k]='\0';
n=kpt_search(i,t);
if(n!=-1)
{
j++;
k=0;
while(mp[j]!=',' && mp[j]!='\0')
{
t[k]=mp[j];
k++;j++;
}
t[k]='\0';
apt[n-mnt[i].kptptr+mnt[i].pp]=(char*)malloc(strlen(t)*sizeof(char));
strcpy(apt[n-mnt[i].kptptr+mnt[i].pp],t);
k=0;if(mp[j]=='\0')break;
}
}
else
{t[k]=mp[j];k++;}
j++;
}
disp_apt(i);
j=mnt[i].mdtptr;
printf("\n\nExpansion (%s)\n",mnt[i].name);
while(1)
{
if(strncasecmp(mdt[j].t1,"mend",4)==0)
break;
strcpy(t1,mdt[j].t1);
strcpy(t2,mdt[j].t2);
strcpy(t3,mdt[j].t3);
strcpy(t4,mdt[j].t4);
if(t1[0]>='0' && t1[0]<='9')
strcpy(t1,apt[atoi(t1)]);
if(t2[0]>='0' && t2[0]<='9')
strcpy(t2,apt[atoi(t2)]);
if(t3[0]>='0' && t3[0]<='9')
strcpy(t3,apt[atoi(t3)]);
if(t4[0]>='0' && t4[0]<='9')
strcpy(t4,apt[atoi(t4)]);
printf("%s %s %s %s\n",t1,t2,t3,t4);
fprintf(fpw,"%s %s %s %s\n",t1,t2,t3,t4);
j++;
}
free(apt);
}
void disp()
{
int i,j=0;
printf("\n\nMacro Name Table(MNT) count=%d",mntcnt);
printf("\nname\tpp\tkp\tkptptr\tmdtptr");
for(i=0;i<mntcnt;i++)
{
printf("\n%s\t%d\t%d\t%d\t%d",mnt[i].name,mnt[i].pp,mnt[i].kp,mnt[i].kptptr,mnt[i].mdtptr);
}
printf("\n\nKeyword Parameter Table(KPT) count=%d",kptcnt);
printf("\nname\tvalue");
for(i=0;i<kptcnt;i++)
{
printf("\n%s\t%s",kpt[i].name,kpt[i].value);
}
printf("\n");
printf("\n\nMacro Definiton Table(MDT) %d",mdtcnt);
for(i=0;i<mdtcnt;i++)
{
printf("\n%s %s %s %s",mdt[i].t1,mdt[i].t2,mdt[i].t3,mdt[i].t4);
}
printf("\n");
}
void disp_pnt()
{
int i;
printf("\n\nParameter Name Table(PNT) (%s)",mnt[mntcnt].name);
for(i=0;i<mnt[mntcnt].pp+mnt[mntcnt].kp;i++)
printf("\n%s",pnt[i]);
}
void disp_apt(int i)
{
int p;
p=mnt[i].pp+mnt[i].kp;
printf("\n\nActual Parameter Table(APT) (%s)",mnt[i].name);
for(i=0;i<p;i++)
printf("\n%s",apt[i]);
}
macro.asm
MACRO
COPY &ONE,&TWQ,®=AREG
MOVER ®,&ONE
MOVEM ®,&TWO
MEND
MACRO
CHANGE &FIRST,&SECOND,®=AREG,&OP=ADD
MOVER ®,&FIRST
&OP ®,&SECOND
MOVEM ®,&FIRST
MEND
0 Comments
If anyone has Doubts or suggestions please let me know