-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMacroexpansion.c
104 lines (100 loc) · 1.9 KB
/
Macroexpansion.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void macro_replace(FILE *,FILE *);
int main(int argc,char *argv[])
{
int i;
FILE *fsrc,*fdest;
if(argc!=3)
{
printf("Improper number of arguments\n");
exit(0);
}
fsrc=fopen(argv[1],"r");
fdest=fopen(argv[2],"w");
macro_replace(fsrc,fdest);
printf("macro identifier is replaced with token sequence\n");
printf("Open the output file\n");
}
void macro_replace(FILE *input,FILE *output)
{
int i=0,j=0,k=0;
char c,m[5],buf[7],t[4],temp[4],x[4];
int len,len1,len2;
char s[7]="#define";
while((c=fgetc(input))!=EOF)
{
if(c==s[0])
{
fputc(c,output);
buf[0]=c;
for(i=1;i<=6;i++)
{
c=fgetc(input);
fputc(c,output);
if(c==s[i])
{
buf[i]=c;
}
else
fputc(c,output);
}
len=strlen(buf);
buf[len+1]='\0';//found that #define is availble in program
//printf("\n");
c=fgetc(input);
if(c==' ')
fputc(c,output);
do
{
fputc(c,output);
m[j]=c;//to store char carried from while
j++;
c=fgetc(input);
m[j]=c;
j++;
fputc(c,output);
m[j]=c;
}while((c=fgetc(input))!=' ');
m[j]='\0';
//printf(" m is %s\n ",m);
len1=strlen(m); //macro identifier is identified
printf("\n");
do
{ fputc(c,output);
t[k]=c;//to store char carried from while
k++;
c=fgetc(input);
t[k]=c;//to store next char
k++;
fputc(c,output);
t[k]=c;
}while((c=fgetc(input))!='\n');
fputc('\n',output);
t[k]='\0';
//printf(" t is %s\n ",t);
//printf("\n");//token sequence for macro identifer
while(c!=EOF)
{
for(i=0;i<len1;i++)
{
c=fgetc(input);
if(c==m[i])
{
temp[i]=t[i];
fputc(temp[i],output); //replacing macro identifier with it tokensequence.
//printf("%c",temp[i]);
}
else
fputc(c,output);
}
}
temp[i]='\0';
}//if
else
{
fputc(c,output);
}
}
}