Write a C program for implementing Macro Processor

By | 03.12.2016

C program for Macro Processor


Write a C program for Macro Processor. Here’s a Simple Program implementation of Macro processor in C Programming Language.

We know that Macro is a single line abbreviation for a group of statements. We should define that sentence – cluster before using them in actual program.

Macro processor will analyse the program and on encountering  macro variable, it will replace that ‘Macro invocation’ to corresponding Macro definition.

It should also take care the number  and position of arguments used in Macro invocation.


ALGORITHM:


  • STEP 1: Start the program execution.
  • STEP 2: Macro instructions are included in a separate file.
  • STEP 3: The instructions with ‘macro’,’mend’,’call’ on them should not be printed in the output.
  • STEP 4: Print all other instructions such as start,load,store,add,sub etc with their values.
  • STEP 5: Stop the program execution.

Below is the source code for C program for Macro Processor which is successfully compiled and run on Windows System to produce desired output as shown below :


SOURCE CODE : :


#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
void main()
{ char n1,n,c1,i;
char fn[10][10],ilab[20],iopd[20],m[20][3],oper[20],opd[20];
FILE *fp1,*fp2,*p[5];

n=0;
fp1=fopen("C:\\Users\\acer\\Documents\\macin.txt","r");
while(!feof(fp1))
{
fscanf(fp1,"%s%s%s",ilab,iopd,oper);
if(strcmp(iopd,"macro")==0)
n++;
}
printf("no.of macros=%d\n",n);
n1=n;
printf("enter the text filename \n");
for(i=0;i<n;i++)
{
scanf("%s",fn[i]);
p[i]=fopen(fn[i],"w");
}
n=0;
rewind(fp1);
while(!feof(fp1))
{
fscanf(fp1,"%s%s%s",ilab,iopd,oper);
if(strcmp(iopd,"macro")==0)
{
    strcpy(m[n],oper);
fscanf(fp1,"%s%s%s",ilab,iopd,oper);
while(strcmp(iopd,"mend")!=0)
{
fprintf(p[n],"%s%s%s\n",ilab,iopd,oper);
fscanf(fp1,"%s%s%s",ilab,iopd,oper);
}
fclose(p[n]);
n++;
}}
for(i=0;i<n1;i++)
p[i]=fopen(fn[i],"r");
fp2=fopen("C:\\Users\\acer\\Documents\\macin2.txt","w");
rewind(fp1);
fscanf(fp1,"%s%s%s",ilab,iopd,oper);
while(!feof(fp1))
{ if(strcmp(iopd,"call")==0)
{
for(i=0;i<n1;i++)
{ if(strcmp(m[i],oper)==0)
{
rewind(p[i]);
fscanf(p[i],"%s%s%s",ilab,iopd,oper);
while(!feof(p[i]))
{
fprintf(fp2,"%s%s%s",ilab,iopd,oper);
c1=1;
fscanf(p[i],"%s%s%s",ilab,iopd,oper);
}
break;
}}} if(c1!=1)
fprintf(fp2,"%s%s%s\n",ilab,iopd,oper);
c1=0;
fscanf(fp1,"%s%s%s",ilab,iopd,oper);
}
fprintf(fp2,"%s%s%s\n",ilab,iopd,oper);
}

OUTPUT : :


Input:
macin.txt

** macro m1
** move a,b
** mend ---
** macro m2
** lda b
** mend ---
** start 1000
** lda a
** call m1
** call m2
** add a,b

Output:

No. of Macros =2
Enter the Text file Names
Mac1.dat
Mac2.dat
outm.txt
** macro m1
** move a,b
** mend---
** macro m2
** lda b
** mend ---
** start 1000
** lda a
** move a,b callm1
** lda b callm2
** add a,b
mac1.dat
** move a,b
mac2.dat
** lda b

If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach upto you in the short interval.


Thanks for reading the post.

3.8 4 votes
Article Rating
Category: Advance Programs C Programming Tags:

About Tunde A

My name is Tunde Ajetomobi, a Tech Enthusiast and Growth Hacker. I enjoy creating helpful content that solves problem across different topics. Codezclub is my way of helping young aspiring programmers and students to hone their skills and find solutions on fundamental programming languages.

Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments