Sunday 25 September 2011


                                           SINGLE PASS MACRO PROCESSOR

AIM:
To write a C program to implement the macroprocesso.
ALGORITHM:
Step 1: Start the program.
Step 2: Get the statement from input file.
Step 3: From the line read, check if the opcode is the directive “MACRO” if so then the number of macro must be incremented.
Step 4: Repeat step-1 and 2 until end of the file is encountered.
Step 5: Open number of files in write mode. These files will later hold the body of macro.
Step 6: If the “MACRO” enter macro name in operand field.
Step 7: Write all remaining lines directly.
Step 8: Stop the program.

SOURCE CODE:
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct macrodef
{
char label[10],opcode[10],operand[10];
}mdef[20];
struct input
{
char label[10],opcode[10],operand[10];
}ip[20];
struct output
{
char label[10],opcode[10],operand[10];
}op[20];
char mname[10];
char arg[10][10];
void main()
{
int i=0,j=0,k,n,o=1,q,w,t;
int x,y,p,l,flag;
char temp[10][10];
clrscr();
printf("\nEnter the macro name:");
scanf("%s",mname);
printf("\nEnter the no. of arguments:");
scanf("%d",&n);
printf("\nEnter the arguments:");
for(k=1;k<=n;k++)
{
scanf("%s",arg[k]);
}
printf("\nEnter the macro definition");
do
{
i++;
printf("\nEnter the \nLabel\tOpcode\tOperand\n");
scanf("%s\t%s\t%s",mdef[i].label,mdef[i].opcode,mdef[i].operand);
}
while(strcmp(mdef[i].opcode,"MEND")!=0);
printf("\nEnter the input program\n");
do
{
j++;
printf("\nEnter the \nLabel\tOpcode\tOperand\n");
scanf("%s\t%s\t%s",ip[j].label,ip[j].opcode,ip[j].operand);
}
while(strcmp(ip[j].opcode,"END")!=0);
for(k=1;k<=j;k++)
{
if(strcmp(ip[k].opcode,mname)!=0)
{
strcpy(op[o].label,ip[k].label);
strcpy(op[o].opcode,ip[k].opcode);
strcpy(op[o].operand,ip[k].operand);
o++;
}
else
{
x=0;
l=strlen(ip[k].operand);
y=1;
while(x<1)
{
p=0;
while((ip[k].operand[x]!=',')&&(ip[k].operand[x]!='\0'))
{
temp[y][p]=ip[k].operand[x];
p++;
x++;
}
temp[y][p]='\0';
y++;
x++;
}
for(w=1;w<i;w++)
{
flag=0;
for(t=1;t<=n;t++)
{
if(strcmp(mdef[w].operand,arg[t])==0)
{
flag=1;
break;
}
}
if(flag==1)
{
strcpy(op[o].label,mdef[w].label);
strcpy(op[o].opcode,mdef[w].opcode);
strcpy(op[o].operand,temp[t]);
}
o++;
}
}
}
printf("Macro Definition\n");
printf("%s\tMacro",mname);
for(q=1;q<n;q++)
{
printf("&%s",arg[q]);
}
printf("&%s\n",arg[n]);
for(q=1;q<i;q++)
{
printf("%s\t%s\t%s\n",mdef[q].label,mdef[q].opcode,mdef[q].operand);
}
printf("Input program\n");
for(q=1;q<=j;q++)
{
printf("%s\t%s\t%s\n",ip[q].label,ip[q].opcode,ip[q].operand);
}
printf("Output Program\n");
for(q=1;q<o;q++)
{
printf("%s\t%s\t%s\n",op[q].label,op[q].opcode,op[q].operand);
}
getch();
}


OUTPUT:


Enter the macro name:ADD

Enter the no. of arguments:1

Enter the arguments:X

Enter the macro definition
Enter the
Label   Opcode  Operand
-       LDA     X

Enter the
Label   Opcode  Operand
-       MEND    -

Enter the input program

Enter the
Label   Opcode  Operand
-       ADD     A

Enter the
Label   Opcode  Operand
-       END     -
Macro Definition
ADD     Macro&X
-       LDA     X
Input program
-       ADD     A
-       END     -
Output Program
-       LDA     A
-       END     -
Result
Thus above program executed and output verified.

0 comments:

Post a Comment