Macro Processor Pass Two in C Language
Implementation of Macro Processor Pass Two. Following cases to be considered
a)Macro without any parameters
b)Macro with Positional Parameters
c)Macro with Key word parameters
d)Macro with positional and keyword parameters
Macro definition and Call Macro Expansion,Design of Macro Processor Definition and expansion processing.Algorithms along with Data structures Nested Macro calls,Call within a call and definition within a definition.
Macro Processor Pass Two Code
#include #include #include #include FILE *fp; FILE *fp1; FILE *fp2; typedef struct pntab { char par[10]; }ptab; typedef struct MNT { char name[10]; int nop; int nok; int mdtp; int kpdtp; int noe; }MT; MT mnt[1]; ptab pntab[100]; /* typedef struct ssntab { char name[50]; }ssnt; typedef struct sstab { int val; }sst; sst sstab[50]; ssnt ssntab[50]; */ typedef struct evntab { char name[50]; }evnt; evnt evntab[50]; typedef struct evtab { int val; }evt; evt evtab[50]; typedef struct mdt { char entry[100]; }MDT; MDT mdt[100]; typedef struct aptab { char value[10]; }at; at aptab[100]; typedef struct kpdtab { char name[10]; char value[10]; }ktab; ktab kpdtab[10]; void disp_aptab(int len) { printf("\nAPTAB:\n"); int i; for(i=0;i<=len;++i) { printf("%d.%s\n",i+1,aptab[i].value); } } void disp_mdt(int len) { printf("MDT:\n"); int i=1; while(i<=len) { printf("%d.%s\n",i,mdt[i-1].entry); i++; } } void disp_pntab(int len) { int i; for(i=0;i<=len;++i) { printf("%d.%s\n",i+1,pntab[i].par); } } int search_pntab(char str[],int len) { int i; for(i=0;i<=len;++i) { if(strcmp(str,pntab[i].par)==0) { return i; } } return -1; } void disp_mnt() { printf("\tName\tPP\tKP\tMDTP\tKPDTP\n"); printf("\t%s\t%d\t%d\t%d\t%d\n",mnt[0].name,mnt[0].nop,mnt[0].nok,mnt[0].mdtp,mnt[0].kpdtp); } void main() { clrscr(); char str[100]; int i=0,ptr=-1,j; int nop=0,nok=0,noe=0; fp=fopen("mac.txt","r"); if(fp==NULL) { printf("File not found\n"); getch(); return; } fgets(str,100,fp); if(strcmp("MACRO\n",str)==0) printf("MAcro detected\nExecuting Pass1\n"); fgets(str,100,fp); //Creating PNTAB and MNT j=0; while(str[i]!='\t') { mnt[0].name[j]=str[i]; i++;j++; } int kpdtab_ptr=0; while(str[i]!='\n') { pntab[ptr].par[j]=NULL; j=0; ptr++;i++; if(str[i]=='&') { i++; ++nop; while(str[i]!=','&&str[i]!='\n') { if(str[i]=='=') { nop--; nok++; i++; strcpy(kpdtab[kpdtab_ptr].name,pntab[ptr].par); if(str[i]!=',') { int k=0; while(str[i]!=','&&str[i]!='\n') { kpdtab[kpdtab_ptr].value[k]=str[i]; i++; k++; } } } else { pntab[ptr].par[j]=str[i]; j++;i++; } } } } printf("\nPNTAB is\n"); disp_pntab(ptr); mnt[0].nop=nop; mnt[0].nok=nok; mnt[0].mdtp=0; mnt[0].kpdtp=0; char label[10],mneu[10],op1[10],op2[10]; char intercode[100],buff[10]; int x; int mdtp=0; while(!feof(fp)) { fgets(str,100,fp); i=0; if(str[0]=='\t') { strcpy(label,NULL); i++; } else { j=0; while(str[i]!='\t'&&str[i]!='\n') { label[j]=str[i]; i++;j++; } label[j]=NULL; if(strcmp(label,"MEND")==0) { strcpy(intercode,"\tMEND\n"); strcat(intercode,NULL); strcpy(mdt[mdtp].entry,intercode); mdtp++; break; } i++; } j=0; while(str[i]!='\t') { mneu[j]=str[i]; i++;j++; } mneu[j]=NULL; j=0;i+=2; while(str[i]!=',') { op1[j]=str[i]; i++;j++; } op1[j]=NULL; j=0;i+=2; while(str[i]!='\n') { op2[j]=str[i]; i++;j++; } op2[j]=NULL; if(strcmp(mneu,"LCL")==0) { noe++; } strcpy(intercode,NULL); if(strcmp(label,NULL)==0) { strcat(intercode,"\t"); } strcat(intercode,mneu); strcat(intercode,"\t"); x=search_pntab(op1,ptr); if(x!=-1) { strcat(intercode,"(P,"); itoa(x,buff,10); strcat(intercode,buff); strcat(intercode,"),"); } if(op2[0]=='=') { strcat(intercode,op2); } else { x=search_pntab(op2,ptr); if(x!=-1) { strcat(intercode,"(P,"); itoa(x,buff,10); strcat(intercode,buff); strcat(intercode,")\n\0"); } } end: strcat(intercode,NULL); strcpy(mdt[mdtp].entry,intercode); mdtp++; // getch(); } printf("\nMNT:\n"); disp_mnt(); disp_mdt(mdtp); fp2=fopen("mca.txt","r");//other file is mcal fgets(str,100,fp2); i=0; while(str[i]!=' ') i++; strcpy(buff,NULL); int k=0; int aptab_ptr=0; while(str[i]!='\n') { j=0; i++; while(str[i]!=',' && str[i]!='\n') { if(str[i]=='=') { buff[j]=NULL; x=search_pntab(buff,ptr); strcpy(buff,NULL); j=0; i++; while(str[i]!=',' && str[i]!='\n') { buff[j]=str[i]; i++;j++; } buff[j]=NULL; strcpy(aptab[x].value,buff); goto yo; } buff[j]=str[i]; i++;j++; } buff[j]=NULL; strcpy(aptab[aptab_ptr++].value,buff); yo: } aptab_ptr+=nok; i=0; int y; while(i<aptab_ptr) <br=""> { if(strcmp(aptab[i].value,NULL)==0) { y=i-nop; strcpy(aptab[i].value,kpdtab[y].value); } i++; } i=0; j=0; disp_aptab(ptr); getch(); printf("Expanded MAcro is:\n"); strcpy(intercode,NULL); while(i<mdtp-1) <br=""> { strcpy(intercode,NULL); j=0; while(mdt[i].entry[j]!='\n') { if(mdt[i].entry[j]=='(') { if(mdt[i].entry[j+1]=='P') { x=(int)mdt[i].entry[j+3]-48; //itoa(aptab[x].value,buff,10); strcat(intercode,aptab[x].value); j=j+4; } } else { x=strlen(intercode); intercode[x]=mdt[i].entry[j]; intercode[x+1]=NULL; } j++; } intercode[j]=NULL; printf("%s \n",intercode); i++; //mdt[i].entry } /*while(!feof(fp)) { fgets(str,100,fp); printf(str); } */ getch(); }
Input File
MACRO
INCR &MEM_VAL,&INCR_VAL,®
MOVER ®,&MEM_VAL
ADD ®,&INCR_VAL
MOVEM ®,&MEM_VAL
MEND
Leave a Reply