-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdriver.c
338 lines (278 loc) · 8.96 KB
/
driver.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/*
Batch no. 82
AUTHORS :
Pranjal Gupta (2013B4A7470P)
Tanaya Jha (2013B3A7304P)
*/
/*
Driver module, to run modules separately on the source code.
*/
#include <stdio.h>
#include "lexerDef.h"
#include "parserDef.h"
#include "lexer.h"
#include "parser.h"
#include "ast.h"
#include "scope.h"
#include "semantics.h"
#include "codegen.h"
int main(int argc, char ** argv) {
printf("\n");
printf("LEVEL 4 : AST / Symbol table / Type checking / Semantic analysis / ASM code generation works. \n\n");
printf("ASSUMPTIONS : \n");
printf("(a). In code generation, only integer-type variables are used and there is only the main function. \n");
printf("(b). Type checking has been included in the semantics module itself. \n");
printf("(c). The same data structure is used for both Parse Tree and AST. AST is generated by structural changes to the Parse tree in one traversal through the structure. \n");
printf("\n");
if(argc < 3) {
printf("Too few arguments. \n");
}
else {
grammar * gr = NULL;
firstAndFollowSets * ff = NULL;
parseList * tableHead = NULL;
treeNode * head = NULL;
symbolScope * sHead = NULL;
int option, countPT = 0, countAST = 0, ASTconstruct = 0, node_size;
int synErrors = 0, scpErrors = 0, semErrors = 0, semChecks = 0;
printf("\n----------------------------------------------------------------------------\n");
printf("\nPress option for the defined task.\n");
printf("%sDriver performs all the operations only once, if there are changes in the source code, rerun. %s\n\n", BOLDRED, RESET);
printf("\n1.\tPrint the token list generated by the lexer.\n");
printf("\n2.\tParse the source code to verify the syntactical correctness and print the parse tree. (Displays syntactic errors, if any)\n");
printf("\n3.\tPrint the Abstract Syntax Tree \n");
printf("\n4.\tDisplay the sizes and compression percentage of the AST\n");
printf("\n5.\tPrint the symbol table. (Displays scope resolution errors, if any)\n");
printf("\n6.\tParse the source code for type checking and semantic correctness. (Displays semantic errors, if any)\n\t(To displays all the syntactic and semantic errors, call directly)\n");
printf("\n7.\tGenerate assembly code.\n");
printf("\n\n");
scanf("%d", &option);
switch(option) {
case 1 : // tokens print
setUpStream(argv[1]);
printf("\nPrinting the tokens generated by lexical analyzer...\n");
token * got;
got = getToken();
for(int i=0; got->id != 57 ;i++) {
printf("%s\t%s\t%d\n", got->lxm, got->val, got->lno);
got = getToken();
}
break;
case 2 : // syntactical errors reporting and parse tree formation
if(gr == NULL) {
gr = readGrammarFromFile("grammar.txt");
}
if(ff == NULL) {
ff = computeFirstAndFollowSets(gr);
}
if(tableHead == NULL) {
tableHead = createParseTable(gr, ff);
}
if(head == NULL) {
head = parseInputSourceCode(gr, argv[1], tableHead, &synErrors);
if(!countPT) {
countPT = 0;
countNodes(head, &countPT);
}
}
else {
printf("Parse tree is already created.\n");
}
if(!ASTconstruct)
;// printParseTreeOrig(head);
else {
printf("Cannot print parse tree after AST construction since the same structure is altered.\n");
}
break;
case 3 : // construct AST and count AST nodes
if(gr == NULL) {
gr = readGrammarFromFile("grammar.txt");
}
if(ff == NULL) {
ff = computeFirstAndFollowSets(gr);
}
if(tableHead == NULL) {
tableHead = createParseTable(gr, ff);
}
if(head == NULL) {
head = parseInputSourceCode(gr, argv[1], tableHead, &synErrors);
if(!countPT) {
countPT = 0;
countNodes(head, &countPT);
}
}
if(!ASTconstruct) {
constructAST(head);
ASTconstruct = 1;
}
printf("The printing format of the AST is the same as that of the parse table. \n");
printf("In-order traversal : first child -> parent ->rest of the childs \n");
printf("-----------------------------------------------------------------------------------------------------------------------\n");
printf("LEXEME\t\tLINE\t\tTERMINAL\tVALUE\t\tPARENT\t\t\tLEAF\t\tNON TERMINAL\n");
printf("-----------------------------------------------------------------------------------------------------------------------\n");
printAST(head);
if(!countAST) {
countAST = 0;
countNodes(head, &countAST);
}
break;
case 4 : // compression calculations
if(gr == NULL) {
gr = readGrammarFromFile("grammar.txt");
}
if(ff == NULL) {
ff = computeFirstAndFollowSets(gr);
}
if(tableHead == NULL) {
tableHead = createParseTable(gr, ff);
}
if(head == NULL) {
head = parseInputSourceCode(gr, argv[1], tableHead, &synErrors);
if(!countPT) {
countPT = 0;
countNodes(head, &countPT);
}
}
if(!ASTconstruct) {
constructAST(head);
ASTconstruct = 1;
}
if(!countAST) {
countAST = 0;
countNodes(head, &countAST);
}
node_size = sizeof(treeNode);
printf("PARSE TREE : \t\t Number of nodes = %d, \tAllocated memory = %d bytes\n", countPT, countPT*node_size);
printf("ABSTRACT SYNTAX TREE : \t Number of nodes = %d, \tAllocated memory = %d bytes\n", countAST, countAST*node_size);
printf("\nCompression percentage : %f \n" , ((double)countPT-(double)countAST)*100/(double)countPT );
break;
case 5 : // scope
if(gr == NULL) {
gr = readGrammarFromFile("grammar.txt");
}
if(ff == NULL) {
ff = computeFirstAndFollowSets(gr);
}
if(tableHead == NULL) {
tableHead = createParseTable(gr, ff);
}
if(head == NULL) {
head = parseInputSourceCode(gr, argv[1], tableHead, &synErrors);
if(!countPT) {
countPT = 0;
countNodes(head, &countPT);
}
}
if(!ASTconstruct) {
constructAST(head);
ASTconstruct = 1;
}
if(synErrors) {
printf("There are errors in the parse table. Cannot construct Symbol table.\n");
}
else {
if(sHead == NULL) {
sHead = initScopeStructure(head, &scpErrors);
if(!scpErrors)
printf("%s\nThe code has no scope resolution errors.%s\n\n", BOLDGREEN, RESET);
}
printf("\n\n");
printf("IDENTIFIER \t\t USAGE \t\t TYPE \t\t LINE NO. \t\t SCOPE \t\t SCOPE LEVEL \t\t WIDTH \t\t OFFSET\n");
printf("-------------------------------------------------------------------------------------------------------------------------------------------------\n\n");
printScopeStructure(sHead);
}
break;
case 6 : // semantic correctness
if(gr == NULL) {
gr = readGrammarFromFile("grammar.txt");
}
if(ff == NULL) {
ff = computeFirstAndFollowSets(gr);
}
if(tableHead == NULL) {
tableHead = createParseTable(gr, ff);
}
if(head == NULL) {
head = parseInputSourceCode(gr, argv[1], tableHead, &synErrors);
if(!countPT) {
countPT = 0;
countNodes(head, &countPT);
}
}
if(!ASTconstruct) {
constructAST(head);
ASTconstruct = 1;
}
if(sHead == NULL) {
if(synErrors) {
printf("There are errors in the parse table. Cannot construct Symbol table.\n");
}
else
sHead = initScopeStructure(head, &scpErrors);
}
else
printf("\nScoping errors are listed only once at the time of creation of the scope structure.\n\n");
if(synErrors) {
printf("There are errors in the parse table. Cannot construct check semantics.\n" );
}
else {
checkSemantics(head, sHead, &semErrors);
semChecks = 1;
if(!semErrors)
printf("%s\nThe code has no semantic errors.%s\n\n", BOLDGREEN, RESET);
}
break;
case 7 : // code generation
if(gr == NULL) {
gr = readGrammarFromFile("grammar.txt");
}
if(ff == NULL) {
ff = computeFirstAndFollowSets(gr);
}
if(tableHead == NULL) {
tableHead = createParseTable(gr, ff);
}
if(head == NULL) {
head = parseInputSourceCode(gr, argv[1], tableHead, &synErrors);
if(!countPT) {
countPT = 0;
countNodes(head, &countPT);
}
}
if(!ASTconstruct) {
constructAST(head);
ASTconstruct = 1;
}
if(sHead == NULL) {
if(synErrors) {
printf("There are errors in the parse table. Cannot construct Symbol table.\n");
}
else
sHead = initScopeStructure(head, &scpErrors);
}
if(!semChecks) {
if(synErrors) {
printf("There are errors in the parse table. Cannot construct check semantics.\n" );
}
else {
checkSemantics(head, sHead, &semErrors);
if(!semErrors)
printf("%s\nThe code has no semantic errors.%s\n\n", BOLDGREEN, RESET);
}
}
if(synErrors || scpErrors || semErrors) {
printf("There are errors in the source code. Cannot generate ASM\n" );
}
else {
FILE * fp = fopen(argv[2], "w+");
codeGenInit(head, sHead, fp);
fclose(fp);
}
break;
default :
break;
}
}
printf("\n\n");
return 0;
}