Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
64 views

Pass 2 of Macro Processor

The document describes the program for the second pass of a macro processor. It defines various data structures like nametab, deftab, argtab to store symbol table information and macro definitions. It reads the input file, identifies macro calls and definitions. It then performs macro expansion by replacing macro calls with their definitions after substituting arguments. It prints the expanded output.

Uploaded by

sarasanthya
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views

Pass 2 of Macro Processor

The document describes the program for the second pass of a macro processor. It defines various data structures like nametab, deftab, argtab to store symbol table information and macro definitions. It reads the input file, identifies macro calls and definitions. It then performs macro expansion by replacing macro calls with their definitions after substituting arguments. It prints the expanded output.

Uploaded by

sarasanthya
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 11

PROGRAM FOR PASS-2 OF MACRO PROCESSOR.

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include "pass2_macro_s.c"
struct namtab
{
int start,end;
char name[15];
};
struct deftab
{
char sen[35];
};
struct argtab
{
char argnam[15];
int index;
int flag2;
};
struct final
{
char arr[35];
};
struct deftab d[35],d1[35];
struct argtab a[25],ta[25];
struct namtab n[25];
int i=0,j=0,k=0,j5=0,no;
main()
{
char fname[20],str[40],str1[40],*ptr,*ptr1,buf1[20],str2[35];
int gg,l=0,hh,i3,m2,flag1=0,m1,m4,t,ff,m5,count3=0,ll,count4;
struct final output[100],temp[20],temp2[20];
FILE *fp;
printf("\nEnter the name of the file:");
scanf("%s",fname);
pass1(fname);
fp=fopen(fname,"r");
if(fp==0)
{
printf("\nFile does not exsist!!");
}
else
{ l=0;
while(fgets(str,40,fp)!=NULL)
{
for(gg=0;gg<i;gg++)
{
if((strstr(str,n[gg].name))&&(!strstr(str,"MACRO")))
{
ll=0;count4=0;
while(ll<strlen(str))
{
if(str[ll]==',')
{
count4++;
ll++;
}
else
ll++;
}
if(count4==(no-1))
{

strcpy(str1,".");
strcat(str1,str);
str1[strlen(str1)-1]='\0';
strcpy(output[l].arr,str1);
l++;hh=0;
for(hh=0;hh<j;hh++)
{
if(strstr(d[hh].sen,"?"))
{
i3=0;
strcpy(str2,d[hh].sen);
ptr=strtok(str2," ");
while(ptr!=NULL)
{
strcpy(temp[i3].arr,ptr);
i3++;
ptr=strtok(NULL," ");
}
strcpy(str1,temp[0].arr);
strcat(str1," ");
m4=0;
while(m4<k)
{
char ss[2];
ss[0]=temp[1].arr[1];
ss[1]='\0';
if((atoi(ss)==a[m4].index)&&(count3==a[m4].flag2))
{
strcat(str1,a[m4].argnam);
break;
}
else
m4++;
}
str1[strlen(str1)]='\0';
strcpy(output[l].arr,str1);
l++;
}
else
{
if((!(strstr(d[hh].sen,"MACRO")))&&(!(strstr(d[hh].sen,"MEND"))))
{
strcpy(output[l].arr,d[hh].sen);
l++;
}
}
}
}
else
{
printf("\nThe number of parameters do not match!!!");
count3--;
}
count3++;
}
else
{
t=0;
while(t<j5)
{
if(strncmp(d1[t].sen,str,strlen(d1[t].sen)-1)==0)
break;
else
t++;
}
if(t>=j5)

{
strcpy(output[l].arr,str);
output[l].arr[strlen(str)-1]='\0';
l++;
}
}
}
}
printf("\n\nTHE EXPANDED PROGRAM.");
for(ff=0;ff<l;ff++)
printf("\n%s",output[ff].arr);
}
getch();
}

INPUT FILE
COPY START 1000
RDBUFF MACRO &P,&Q,&R
CLEAR A
CLEAR S
CLEAR X
+LDT #4096
TD &P
JEQ *-3
RD &P
STCH &Q
JLT *-19
LDA &R
COMP #0
STX &VAL
MEND
RDBUFF MACRO &P1,&Q1,&R1
LDA B
MEND
FIRST STL RETADR
RDBUFF F1,,L1
CLEAR B
RDBUFF ,BUFF2,L2
RDBUFF F3,BUFF3,
RDBUFF F4,BUFF4,L4
RDBUFF F5,BUFF5
JLT *-29
END

OUTPUT
Enter the name of the file:sara21.txt
The parameter is not found!!!
line:STX &VAL
The macro is duplicated!!!
NAME TAB.
Name of the macro:RDBUFF
Start of definition:0
End of Definition:13
DEFINITION TAB.
RDBUFF MACRO &P,&Q,&R

CLEAR A
CLEAR S
CLEAR X
+LDT #4096
TD ?0
JEQ *-3
RD ?0
STCH ?1
JLT *-19
LDA ?2
COMP #0
.STX &VAL
MEND
ARGUMENTS TAB.
index:0 argument:F1
index:1 argument:NULL
index:2 argument:L1
index:0 argument:NULL
index:1 argument:BUFF
index:2 argument:L2
index:0 argument:F3
index:1 argument:BUFF3
index:2 argument:NULL
index:0 argument:F4
index:1 argument:BUFF4
index:2 argument:L4

The Expanded Program:


FIRST STL RETADR
.RDBUFF F1,,L1
CLEAR A
CLEAR S
CLEAR X
+LDT #4096
TD &F1
JEQ *-3
RD &F1
STCH &NULL
JLT *-19
LDA &L1
COMP #0
CLEAR B
.RDBUFF ,BUFF2,L2
CLEAR A
CLEAR S
CLEAR X
+LDT #4096
TD &NULL
JEQ *-3
RD &NULL
STCH &BUFF2
JLT *-19
LDA &L2
COMP #0
.RDBUFF F3,BUFF3,
CLEAR A
CLEAR S
CLEAR X
+LDT #4096
TD &F3
JEQ *-3
RD &F3
STCH &BUFF3

JLT *-19
LDA &NULL
COMP #0
RDBUFF F4,BUFF4,L4
CLEAR A
CLEAR S
CLEAR X
+LDT #4096
TD &F4
JEQ *-3
RD &F4
STCH &BUFF4
JLT *-19
LDA &L4
COMP #0
JLT *-29
END

NAME:SARANYA.M
REG NO:312211104092
PROGRAM FOR PASS-1 OF MACRO PROCESSOR.
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct namtab
{
int start,end;
char name[15];
};
struct deftab
{

char sen[35];
};
struct argtab
{
char argnam[15];
int index;
};
struct final
{
char arr[20];
};
main()
{
FILE *fp;
int i=0,i1,j=0,k=0,j1,m1,k1,m2,flag=0,ff,yy,jj,gg,count,count1,ll,pp,no,uu,count4;
char fname[20],str[40],*ptr,str1[40],buf[10],buf1[10];
struct final temp[25],temp1[25],temp2[25];
struct deftab d[35];
struct argtab a[25],ta[25];
struct namtab n[25];
printf("\nEnter the name of the file:");
scanf("%s",fname);
fp=fopen(fname,"r");
if(fp==0)
{
printf("\nFile does not exsist!!");
}
else
{
while(fgets(str,40,fp)!=NULL)
{
i1=0;
if(strstr(str,"MACRO"))
{
strcpy(d[j].sen,str);
d[j].sen[strlen(d[j].sen)-1]='\0';
j++;
ptr=strtok(str," ");
while(ptr!=NULL)
{
strcpy(temp[i1].arr,ptr);
i1++;
ptr=strtok(NULL," ");
}
for(uu=0;uu<i;uu++)
{
if(strcmp(temp[0].arr,n[uu].name)==0)
break;
}
if(uu>=i)
{
strcpy(n[i].name,temp[0].arr);
n[i].start=j-1;
j1=0;
ptr=strtok(temp[2].arr,",");
while(ptr!=NULL)
{
strcpy(ta[j1].argnam,ptr);
ta[j1].index=j1++;
ptr=strtok(NULL,",");
}
no=j1;
while(fgets(str,40,fp)!=NULL)
{
if(!strstr(str,"MEND"))
{
m1=0;flag=0;count4=0;

while(m1<=j1)
{
if(strstr(str,ta[m1].argnam))
{
k1=0;flag=1;
ptr=strtok(str," ");
while(ptr!=NULL)
{
strcpy(temp1[k1].arr,ptr);
k1++;
ptr=strtok(NULL," ");
}
strcpy(str1,temp1[0].arr);
strcat(str1," ");
snprintf(buf,8,"%d",ta[m1].index);
strcpy(buf1,"?");
strcat(buf1,buf);
strcat(str1,buf1);
strcat(str1,"\0");
strcpy(d[j].sen,str1);
j++;
}
else if((strstr(str,"&"))&&(!strstr(str,ta[m1].argnam)))
{
count4++;
m1++;
}
else
m1++;
}
if((flag!=1)&&(count4<=j1))
{
strcpy(d[j].sen,str);
d[j].sen[strlen(d[j].sen)-1]='\0';
j++;
}
else if(count4>j1)
{
printf("\nThe parameter is not found!!!");
printf("\nline:%s",str);
strcpy(buf1,".");
strcat(buf1,str);
strcpy(d[j].sen,buf1);
j++;
}
}
else
{
strcpy(d[j].sen,"MEND");
j++;
goto q;
}
}
q:n[i].end=j-1;
i++;
}
else
{
printf("\nThe macro is duplicated!!!");
j--;
}
}
for(ll=0;ll<i;ll++)
{
if(strstr(str,n[ll].name))
{

ptr=strtok(str," ");i1=0;
while(ptr!=NULL)
{
strcpy(temp[i1].arr,ptr);
i1++;
ptr=strtok(NULL," ");
}
gg=0;count1=0;
while(gg<strlen(temp[1].arr))
{
if(temp[1].arr[gg]==',')
{
count1++;
gg++;
}
else
gg++;
}
if(count1==(no-1))
{
gg=0;
if(temp[1].arr[0]==',')
{
strcpy(a[k].argnam,"NULL");
a[k].index=0;
k++;
count=1;
ptr=strtok(temp[1].arr,",");
while(ptr!=NULL)
{
strcpy(a[k].argnam,ptr);
a[k].argnam[strlen(a[k].argnam)-1]='\0';
a[k].index=count++;
k++;
ptr=strtok(NULL,",");
}
}
else if(temp[1].arr[strlen(temp[1].arr)-2]==',')
{
count=0;
ptr=strtok(temp[1].arr,",");
while(ptr!=NULL)
{
if(count!=(no-1))
{
strcpy(a[k].argnam,ptr);
a[k].index=count++;
k++;
}
else
{
strcpy(a[k].argnam,"NULL");
a[k].index=count++;
k++;
}
ptr=strtok(NULL,",");
}
}
else
{
count=0;pp=0;
while(temp[1].arr[gg]!=',')
{
a[k].argnam[pp]=temp[1].arr[gg];
gg++;pp++;
}
a[k].argnam[pp]='\0';

a[k].index=count++;
k++;
if(temp[1].arr[gg]==',')
{
if(temp[1].arr[gg+1]==',')
{
strcpy(a[k].argnam,"NULL");
a[k].index=count++;
k++;gg++;
}
else
{
pp=0;gg++;
while(temp[1].arr[gg]!=',')
{
a[k].argnam[pp]=temp[1].arr[gg];
gg++;pp++;
}
a[k].argnam[pp]='\0';
a[k].index=count++;
k++;
}
}
pp=0;gg++;
while(temp[1].arr[gg+1]!='\0')
{
a[k].argnam[pp]=temp[1].arr[gg];
gg++;pp++;
}
a[k].argnam[pp]='\0';
a[k].index=count++;
k++;
}
}
else {
//printf("\nThe number of parameters do not match!!!");
}
}
}
}
printf("\n\nNAME TAB.");
for(ff=0;ff<i;ff++)
printf("\nName of the macro:%s\nStart of definition:%d\nEnd of Definition:
%d",n[ff].name,n[ff].start,n[ff].end);
printf("\n\nDEFINITION TAB.");
for(yy=0;yy<j;yy++)
printf("\n%s",d[yy].sen);
printf("\n\nARGUMENTS TAB.");
for(jj=0;jj<k;jj++)
printf("\nindex:%d argument:%s",a[jj].index,a[jj].argnam);
}
getch();
}

INPUT FILE
COPY START 1000
RDBUFF MACRO &P,&Q,&R
CLEAR A
CLEAR S
CLEAR X
+LDT #4096
TD &P
JEQ *-3
RD &P

STCH &Q
JLT *-19
LDA &R
COMP #0
STX &VAL
MEND
RDBUFF MACRO &P1,&Q1,&R1
LDA B
MEND
FIRST STL RETADR
RDBUFF F1,,L1
CLEAR B
RDBUFF ,BUFF2,L2
RDBUFF F3,BUFF3,
RDBUFF F4,BUFF4,L4
RDBUFF F5,BUFF5
JLT *-29
END

OUTPUT
Enter the name of the file:sara21.txt
The parameter is not found!!!
line:STX &VAL
The macro is duplicated!!!
NAME TAB.
Name of the macro:RDBUFF
Start of definition:0
End of Definition:13
DEFINITION TAB.
RDBUFF MACRO &P,&Q,&R
CLEAR A
CLEAR S
CLEAR X
+LDT #4096
TD ?0
JEQ *-3
RD ?0
STCH ?1
JLT *-19
LDA ?2
COMP #0
.STX &VAL
MEND
ARGUMENTS TAB.
index:0 argument:F1
index:1 argument:NULL
index:2 argument:L1
index:0 argument:NULL
index:1 argument:BUFF
index:2 argument:L2
index:0 argument:F3
index:1 argument:BUFF3
index:2 argument:NULL
index:0 argument:F4
index:1 argument:BUFF4

index:2

argument:L4

You might also like