Implement Macro in C Program Example
Implementing macro processor in C :-
Included :- source file,input file,expanded file
Source code:-
1: #include<stdio.h>
2: #include<conio.h>
3: #include<string.h>
4: void GETLINE();
5: void PROCESSLINE();
6: void DEFINE();
7: void EXPAND();
8: FILE *expanded;
9: FILE *input;
10: char label[10],opcode[10],operand[25];
11: char line[20];
12: int namcount=0,defcount=0;
13: int EXPANDING;
14: int curr;
15: struct namtab
16: {
17: char name[10];
18: int start,end;
19: }mynamtab[15];
20: struct deftab
21: {
22: char macroline[25];
23: }mydeftab[25];
24: struct argtab
25: {
26: char arg[3][9];
27: }myargtab;
28: ///MACRO MAIN
29: int main()
30: {
31: EXPANDING=0;
32: input =fopen("input.txt","r"); //INPUT FILE open to read
33: expanded=fopen("expanded.txt","w");
34: GETLINE();
35: while(strcmp(opcode,"END")!=0)
36: {
37: PROCESSLINE();
38: GETLINE();
39: }
40: getch();
41: return 1;
42: }
43: //GETLINE
44: void GETLINE()
45: { char word1[10],word2[10],word3[10],buff[10];
46: int count=0,i,j=0;
47: if(EXPANDING)strcpy(line,mydeftab[curr++].macroline);
48: else fgets(line,20,input);
49: opcode[0]='\0';label[0]='\0';operand[0]='\0';word1[0]='\0';word2[0]='\0';word3[0]='\0';
50: for(i=0;line[i]!='\0';i++)
51: {
52: if(line[i]!=' ')
53: buff[j++]=line[i];
54: else
55: {
56: buff[j]='\0';
57: strcpy(word3,word2);
58: strcpy(word2,word1);
59: strcpy(word1,buff);
60: j=0;count++;
61: }
62: }
63: buff[j-1]='\0';
64: strcpy(word3,word2);
65: strcpy(word2,word1);
66: strcpy(word1,buff);
67: switch(count)
68: {
69: case 0:strcpy(opcode,word1);break;
70: case 1:{strcpy(opcode,word2);strcpy(operand,word1);}break;
71: case 2:{strcpy(label,word3);strcpy(opcode,word2);strcpy(operand,word1);}break;
72: }
73: }
74: ////////pROCESSLINE
75: void PROCESSLINE()
76: {
77: int i;
78: for( i=0;i<namcount;i++)
79: {printf("\n%s \n %s",opcode,mynamtab[i].name);
80: if(!strcmp(opcode,mynamtab[i].name))
81: {
82: printf("found %s",opcode);
83: EXPAND();
84: GETLINE();
85: }
86: }
87: if(strcmp(opcode,"MACRO")==0)
88: {printf("macro");
89: DEFINE();
90: }
91: else fprintf(expanded,"%s",line);
92: }
93: void DEFINE()
94: {
95: int LEVEL,i=0,j=0,k=0;
96: char param[5][9];
97: char s[3];
98: strcpy(s,"123");
99: strcpy(mynamtab[namcount].name,label);
100: mynamtab[namcount].start=defcount;
101: strcpy(mydeftab[defcount].macroline,line);
102: while(operand[i]!='\0')
103: {
104: if(operand[i]!=',')
105: param[j][k++]=operand[i];
106: else
107: {
108: param[j++][k]='\0';
109: k=0;
110: }
111: i++;
112: }
113: param[j][k]='\0';
114: LEVEL=1;
115: while(LEVEL>0)
116: {
117: GETLINE();
118: if(operand[0]!='\0')
119: for(i=0;i<3;i++)
120: if(!strcmp(operand,param[i]))
121: {
122: operand[0]='?';
123: operand[1]=s[i];
124: }
125: if(!strcmp(opcode,"MACRO"))
126: LEVEL++;
127: else if(!strcmp(opcode,"MEND"))
128: LEVEL--;
129: strcpy(mydeftab[defcount].macroline,label);
130: strcat(mydeftab[defcount].macroline," ");
131: strcat(mydeftab[defcount].macroline,opcode);
132: strcat(mydeftab[defcount].macroline," ");
133: strcat(mydeftab[defcount++].macroline,operand);
134: }
135: mynamtab[namcount++].end=defcount;
136: }
137: void EXPAND()
138: {
139: int i,end=0,j=0,k=0;
140: EXPANDING=1;
141: int arg=0;
142: for(i=0;i<namcount;i++)
143: {
144: if(!strcmp(opcode,mynamtab[i].name))
145: {
146: curr=mynamtab[i].start;
147: end=mynamtab[i].end;
148: printf("%d %d",curr,end);
149: while(operand[i]!='\0')
150: {
151: if(operand[i]!=',')
152: myargtab.arg[j][k++]=operand[i];
153: else
154: {
155: myargtab.arg[j++][k]='\n';
156: k=0;
157: }
158: i++;
159: }
160: myargtab.arg[j][k]='\n';
161: }
162: }
163: while(curr<end-1)
164: {
165: GETLINE();
166: if(operand[0]=='?')
167: strcpy(operand,myargtab.arg[operand[1]-'0'-1]);
168: fprintf(expanded,"%s %s %s",label,opcode,operand);
169: }
170: EXPANDING=0;
171: }
An Example for input code:-
START 1000
DFR
LABEL MACRO P,Q
LDA P
VDS Q
CVD bbb
MEND
LABEL V,A
STA
END
Example of Expanded Code:-
START 1000
DFR
LDA V
VDS A
CVD bbSTA
Comments
Post a Comment