-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdriver.c
441 lines (422 loc) · 17.3 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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
/*
Group Number : 2
1 Dhruv Rawat 2019B3A70537P thedhruvrawat
2 Chirag Gupta 2019B3A70555P Chirag5128
3 Swastik Mantry 2019B1A71019P Swastik-Mantry
4 Shreyas Sheeranali 2019B3A70387P ShreyasSR
5 Vaibhav Prabhu 2019B3A70593P prabhuvaibhav
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "Set.h"
#include "colorCodes.h"
#include "lexer.h"
#include "parser.h"
#include "ast.h"
#include "symbolTable.h"
#include "codegen.h"
/**
* @brief Removes the comments from the input file and outputs the same to console
*
* @param testCaseFile
*/
void removeComments(char* testCaseFile)
{
FILE *input, *output;
input = fopen(testCaseFile, "r");
// output = fopen(output, "w");
if (input == NULL) {
printf(RED BOLD "Error in opening Test Case File\n" RESET);
exit(1);
}
// if(output == NULL) printf(RED BOLD "Error in opening Output File\n" RESET);
int state = 0;
char ch;
while ((ch = fgetc(input)) != EOF) {
switch (state) {
case 0: {
if (ch == '*') {
state = 1;
} else {
printf("%c", ch);
state = 0;
}
break;
}
case 1: {
if (ch == '*') {
state = 2;
} else {
printf("*%c", ch);
state = 0;
}
break;
}
case 2: {
if (ch == '*') {
state = 3;
} else if (ch == '\n' || ch == '\b' || ch == '\t') {
printf("%c", ch);
state = 2;
} else {
state = 2;
}
break;
}
case 3: {
if (ch == '*') {
state = 0;
} else if (ch == '\n' || ch == '\b' || ch == '\t') {
state = 2;
printf("%c", ch);
} else {
state = 2;
}
break;
}
}
}
fclose(input);
printf(GREEN BOLD "\nFile: %s cleaned of comments\n" RESET, testCaseFile);
return;
}
/**
* @brief Takes input file and prints the token list in a formatted manned by invoking the lexer
*
* @param userCode
*/
void printTokenList(char* userCode)
{
FILE* lexerIP = fopen(userCode, "r");
if (lexerIP == NULL) {
printf(RED BOLD "File Not Found.\n" RESET);
exit(1);
}
setupLexer(lexerIP);
TOKEN* newToken;
int pos = 0;
printf(UNDERLINE "%-10s%-25s%-10s\n" RESET, "Line No.", "Lexeme", "Token Type");
while ((newToken = getNextToken())->tok != EOF_SYMBOL) {
// Not checking for lexeme type as requirement is to print lexeme as it is
printf("%-10d%-25s%-10s\n", newToken->linenum, newToken->lexeme, *(token_types + (newToken->tok)));
}
cleanLexer();
fclose(lexerIP);
return;
}
/**
* @brief Given the input file, invokes the parser and lexer to parse the code and outputs the parse tree to file
*
* @param sourceCode
* @param parseTreeOutput
*/
ParseTree* parseInput(char* sourceCode)
{
ParseTree* pt = parserMain(sourceCode);
cleanParser();
cleanLexer();
return pt;
}
int main(int argc, char* argv[])
{
printf("\e[1;1H\e[2J");
if (argc != 3) {
printf(YELLOW BOLD "Usage: ./compiler <testcase.txt> <outputFile.asm>\n" RESET);
return 1;
}
char* userCode = argv[1];
char* fileOutput = argv[2];
setBufferSize(512); // Sets BufferSize to the CLI argument
printf("---------------------- COMPILER DETAILS GROUP 2 ----------------------\n");
printf("Team Members: Dhruv, Vaibhav, Shreyas, Swastik, Chirag\n");
printf("----------------------------------------------------------------------\n");
printf(GREEN BOLD "Level 4: 16 or more ERRORs could be implemented\n\t> Symbol table module works\n\t> Abstract Syntax Tree (AST) module works\n\t> Type Checking module works\n\t> Semantic rules module works\n\t> Handled static and dynamic arrays in Type Checking module\n\t> Handled static arrays in Code Generation\n\t> Handled Integer, Real and Boolean data types in Code Generation\n" RESET);
int choice = 0;
char userInput[80];
while (true) {
printf(UNDERLINE "Please choose an option:\n" RESET);
printf(BOLD "[0] Exit\n");
printf("[1] Lexer: Print the token list generated by the lexer\n");
printf("[2] Parser: Parse to verify the syntactic correctness of the input source code and to produce parse tree\n");
printf("[3] AST: Print the Abstract Syntax Tree\n");
printf("[4] Memory: Display the amount of allocated memory and number of nodes to Parse Tree and AST\n");
printf("[5] Symbol Table: Print the Symbol Table\n");
printf("[6] Activation record size: Print the size of activation record for each module\n");
printf("[7] Static and Dynamic Arrays: Print the type expressions and width of array variables\n");
printf("[8] Error Reporting: Compile to verify the syntactic and semantic correctness of the input source code\n");
printf("[9] Code Generation: Generate assembly code for the input source code\n" RESET);
printf("Enter your choice: ");
fgets(userInput, sizeof(userInput), stdin);
if (sscanf(userInput, "%d", &choice) != 1) {
choice = 5;
}
switch (choice) {
case 0: {
exit(1);
break;
}
case 1: {
printf("\e[1;1H\e[2J");
printf("-------------------------------------------------------------------------------------\n");
printTokenList(userCode);
if (!LEXER_ERROR) {
printf(GREEN BOLD "Input source code is lexically correct\n" RESET);
}
printf(GREEN BOLD "Token Stream Over\n" RESET);
printf("-------------------------------------------------------------------------------------\n");
cleanLexer();
break;
}
case 2: {
printf("\e[1;1H\e[2J");
printf("-------------------------------------------------------------------------------------\n");
ParseTree* pt = parserMain(userCode);
if (!LEXER_ERROR) {
printf(GREEN BOLD "Input source code is lexically correct\n" RESET);
}
if (!PARSER_ERROR) {
printf(GREEN BOLD "Input source code is syntactically correct\n" RESET);
}
printParseTree(pt);
cleanLexer();
cleanParser();
printf("-------------------------------------------------------------------------------------\n");
break;
}
case 3: {
printf("\e[1;1H\e[2J");
printf("-------------------------------------------------------------------------------------\n");
ParseTree* pt = parserMain(userCode);
if (!LEXER_ERROR) {
printf(GREEN BOLD "Input source code is lexically correct\n" RESET);
}
if (!PARSER_ERROR) {
printf(GREEN BOLD "Input source code is syntactically correct\n" RESET);
}
if (PARSER_ERROR || LEXER_ERROR) {
printf(RED BOLD "Cannot generate AST due to lexical/syntactical errors.\n" RESET);
cleanLexer();
cleanParser();
break;
}
ASTCreator(pt);
prettyPrintAST();
cleanLexer();
cleanParser();
printf("-------------------------------------------------------------------------------------\n");
break;
}
case 4: {
printf("\e[1;1H\e[2J");
printf("-------------------------------------------------------------------------------------\n");
ParseTree* pt = parserMain(userCode);
if (!LEXER_ERROR) {
printf(GREEN BOLD "Input source code is lexically correct\n" RESET);
}
if (!PARSER_ERROR) {
printf(GREEN BOLD "Input source code is syntactically correct\n" RESET);
}
printf("-------------------------------------------------------------------------------------\n");
if (PARSER_ERROR || LEXER_ERROR) {
printf(RED BOLD "Cannot generate AST due to lexical/syntactical errors.\n" RESET);
cleanLexer();
cleanParser();
break;
}
ASTCreator(pt);
int ASTMemSize = tree->size * sizeof(ASTNode);
int ParseTreeMemSize = pt->sz * sizeof(ParseTreeNode);
printf("%-15s Number of Nodes - %d\t", "Parse Tree:", pt->sz);
printf("Allocated Memory - %d bytes\n", ParseTreeMemSize);
printf("%-15s Number of Nodes - %d\t", "AST:", tree->size);
printf("Allocated Memory - %d bytes\n", ASTMemSize);
printf("Compression Ratio (Count): %.2lf%%\n", (1 - ((double) tree->size) / pt->sz) * 100);
printf("Compression Ratio (Memory): %.2lf%%\n", (1 - ((double) ASTMemSize) / ParseTreeMemSize) * 100);
printf("-------------------------------------------------------------------------------------\n");
cleanLexer();
cleanParser();
break;
}
case 5: {
printf("\e[1;1H\e[2J");
printf("-------------------------------------------------------------------------------------\n");
ParseTree* pt = parserMain(userCode);
if (!LEXER_ERROR) {
printf(GREEN BOLD "Input source code is lexically correct\n" RESET);
}
if (!PARSER_ERROR) {
printf(GREEN BOLD "Input source code is syntactically correct\n" RESET);
}
if (PARSER_ERROR || LEXER_ERROR) {
printf(RED BOLD "Cannot generate AST due to lexical/syntactical errors.\n" RESET);
cleanLexer();
cleanParser();
break;
}
ASTCreator(pt);
generateSymbolTable(tree);
printSymbolTable("symbolTable.txt");
printf("-------------------------------------------------------------------------------------\n");
cleanLexer();
cleanParser();
break;
}
case 6: {
printf("\e[1;1H\e[2J");
printf("-------------------------------------------------------------------------------------\n");
ParseTree* pt = parserMain(userCode);
if (!LEXER_ERROR) {
printf(GREEN BOLD "Input source code is lexically correct\n" RESET);
}
if (!PARSER_ERROR) {
printf(GREEN BOLD "Input source code is syntactically correct\n" RESET);
}
if (PARSER_ERROR || LEXER_ERROR) {
printf(RED BOLD "Cannot generate AST due to lexical/syntactical errors.\n" RESET);
cleanLexer();
cleanParser();
break;
}
ASTCreator(pt);
printf("-------------------------------------------------------------------------------------\n");
printf(UNDERLINE BOLD "Semantic Analysis\n" RESET);
generateSymbolTable(tree);
if (!SEMANTIC_ERROR) {
printf(GREEN BOLD "The code is semantically correct.\n" RESET);
}
printf("-------------------------------------------------------------------------------------\n");
if (SEMANTIC_ERROR) {
printf(RED BOLD "Cannot generate activation records due to semantic errors.\n" RESET);
cleanLexer();
cleanParser();
break;
}
printf(UNDERLINE BOLD "Printing the activation records: \n" RESET);
for (int i = 0; i < HASH_TABLE_SIZE; ++i) {
GlobalRecord* moduleRecord = symbolTable->global[i];
while (moduleRecord != NULL) {
printf("%-25s%d\n", moduleRecord->name, moduleRecord->activationRecordSize);
moduleRecord = moduleRecord->next;
}
}
printf("-------------------------------------------------------------------------------------\n");
cleanLexer();
cleanParser();
break;
}
case 7: {
printf("\e[1;1H\e[2J");
printf("-------------------------------------------------------------------------------------\n");
ParseTree* pt = parserMain(userCode);
if (!LEXER_ERROR) {
printf(GREEN BOLD "Input source code is lexically correct\n" RESET);
}
if (!PARSER_ERROR) {
printf(GREEN BOLD "Input source code is syntactically correct\n" RESET);
}
if (PARSER_ERROR || LEXER_ERROR) {
printf(RED BOLD "Cannot generate AST due to lexical/syntactical errors.\n" RESET);
cleanLexer();
cleanParser();
break;
}
ASTCreator(pt);
printf("-------------------------------------------------------------------------------------\n");
printf(UNDERLINE BOLD "Semantic Analysis\n" RESET);
generateSymbolTable(tree);
if (!SEMANTIC_ERROR) {
printf(GREEN BOLD "The code is semantically correct.\n" RESET);
}
printf("-------------------------------------------------------------------------------------\n");
printf("Printing the type expressions of array variables\n");
printSymbolTableArray();
printf("-------------------------------------------------------------------------------------\n");
cleanLexer();
cleanParser();
break;
}
case 8: {
printf("\e[1;1H\e[2J");
clock_t start_time, end_time;
double total_CPU_time, total_CPU_time_in_seconds;
printf("-------------------------------------------------------------------------------------\n");
start_time = clock();
ParseTree* pt = parserMain(userCode);
if (!LEXER_ERROR) {
printf(GREEN BOLD "Input source code is lexically correct\n" RESET);
}
if (!PARSER_ERROR) {
printf(GREEN BOLD "Input source code is syntactically correct\n" RESET);
}
if (PARSER_ERROR || LEXER_ERROR) {
cleanLexer();
cleanParser();
break;
}
ASTCreator(pt);
printf("-------------------------------------------------------------------------------------\n");
printf(UNDERLINE BOLD "Semantic Analysis\n" RESET);
generateSymbolTable(tree);
if (!SEMANTIC_ERROR) {
printf(GREEN BOLD "The code is semantically correct.\n" RESET);
}
printf("-------------------------------------------------------------------------------------\n");
if (SEMANTIC_ERROR) {
printf(RED BOLD "Cannot generate code due to semantic errors.\n" RESET);
}
end_time = clock();
total_CPU_time = (double)(end_time - start_time);
total_CPU_time_in_seconds = total_CPU_time / CLOCKS_PER_SEC;
printf(YELLOW BOLD "TOTAL CPU TIME: %f\tTOTAL CPU TIME IN SECONDS: %f sec\n" RESET, total_CPU_time, total_CPU_time_in_seconds);
cleanLexer();
cleanParser();
break;
}
case 9: {
printf("\e[1;1H\e[2J");
printf("-------------------------------------------------------------------------------------\n");
ParseTree* pt = parserMain(userCode);
if (!LEXER_ERROR) {
printf(GREEN BOLD "Input source code is lexically correct\n" RESET);
}
if (!PARSER_ERROR) {
printf(GREEN BOLD "Input source code is syntactically correct\n" RESET);
}
if (PARSER_ERROR || LEXER_ERROR) {
cleanLexer();
cleanParser();
break;
}
ASTCreator(pt);
printf("-------------------------------------------------------------------------------------\n");
printf(UNDERLINE BOLD "Semantic Analysis\n" RESET);
generateSymbolTable(tree);
if (!SEMANTIC_ERROR) {
printf(GREEN BOLD "The code is semantically correct.\n" RESET);
}
printf("-------------------------------------------------------------------------------------\n");
if (SEMANTIC_ERROR) {
printf(RED BOLD "Cannot generate code due to semantic errors.\n" RESET);
cleanLexer();
cleanParser();
break;
}
createQuadrupleTable();
printQuadrupleTable();
printf(GREEN BOLD "Intermediate Code generated and printed to quadrupleTable.txt\n" RESET);
codeGenerator(quadTable, fileOutput);
printf(GREEN BOLD "Assembly Code generated and printed to code.asm\n" RESET);
cleanLexer();
cleanParser();
break;
}
default: {
printf(RED BOLD "Invalid choice! Try again... \n" RESET);
break;
}
}
}
exit(0);
}