forked from SarthakShah001/Compiler2K23
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdriver.c
More file actions
589 lines (530 loc) · 17.3 KB
/
driver.c
File metadata and controls
589 lines (530 loc) · 17.3 KB
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
/*
***** Group No. - 9 *****
Name : Sarthak Shah ID : 2020A7PS0092P
Name : Siddharth Khandelwal ID : 2020A7PS0098P
Name : Archaj Jain ID : 2020A7PS0072P
Name : BhanuPratap Singh Rathore ID : 2020A7PS1675P
Name : Rishi Rakesh Shrivastava ID : 2020A7PS0108P
*/
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <time.h>
#include "removeComments.h"
#include "lexer.h"
#include "parser.h"
#include "ast.h"
#include "symbol_table.h"
#include "intermediateCodeGen.h"
#include "codegen.h"
#define BUFF_SIZE 5000
#define buff_size 200
void printMenu()
{
printf("\t Select the option you want to see\n ");
printf("\n------------------------------------------------------\n");
printf("\n 0 -> for exit\n");
printf("\n 1 -> Print tokens generated by lexer\n");
printf("\n 2 -> Print the Parse Tree\n");
printf("\n 3 -> Print the AST Tree\n");
printf("\n 4 -> Print the amount of memory allocated to parseTree and AST nodes\n");
printf("\n 5 -> Print the SYMBOL Table\n");
printf("\n 6 -> Print the Activation Record size\n");
printf("\n 7 -> Print type expression and width of Array variable\n");
printf("\n 8 -> Print the Errors and Total Compiling time\n");
printf("\n 9 -> Print the Assembly Code Generated\n\n");
}
void displayInfo()
{
printf("\n ------------------------------------------------------\n");
// printf("\033[32m");
// printf("\n\t FIRST and FOLLOW sets automated\n");
// printf("\t Both lexical and syntax analysis module developed\n");
// printf("\t Modules work with all testcases\n");
// printf("\t Parse tree is constructed for all testcases\n");
// printf("\033[0m");
// Display info to be updated
printf("\033[32m");
printf("\n LEVEL 4: More than 16 type of Semantic errors handled ");
printf("\n AST and Symbol Table Module is completely working");
printf("\n Type Checking and Semantic Modules handling all type of errors correctly");
printf("\n Intermediate codegen and Codegen done partially");
// printf("Handled Static and Dynamic arrays in code generation");
printf("\033[0m");
printf("\n\n");
printf("\n ------------------------------------------------------\n\n");
}
int count_nodes(parseTreeNode node)
{
if (node == NULL)
{
return 0;
}
return count_nodes(node->sibling) + count_nodes(node->child) + 1;
}
int count_tree_size(parseTreeNode node)
{
if (node == NULL)
{
return 0;
}
return count_tree_size(node->sibling) + count_tree_size(node->child) + sizeof(*node);
}
void print_parseTree_main(parseTreeNode root)
{
printf(" LEXEME ");
printf(" LINE_NO ");
printf(" TOKEN-TYPE ");
printf(" VALUE if NUM/RNUM ");
printf(" PARENT ");
printf(" IS A LEAF NODE ");
printf(" NON-TERMINAL ");
printf("\n\n");
printParseTree(root);
// freeParseTree(root) ;
// printParseTree(root) ;
root = NULL;
return;
}
void print_AST_main(parseTreeNode ast_root)
{
printf(" LEXEME ");
printf(" LINE_NO ");
printf(" TOKEN-TYPE ");
printf(" VALUE if NUM/RNUM ");
printf(" PARENT ");
printf(" IS A LEAF NODE ");
printf(" NON-TERMINAL ");
printf("\n\n");
ast_root = ast_root->syn_node;
if (ast_root->child == NULL)
{
// printf("YES NULL\n");
}
else
{
// printf("NO NULL %s\n", ast_strings[ast_root->child->ast_name]);
}
printf("ast_root=%s\n", nonterminal_str[ast_root->s->nt]);
printAST(ast_root);
}
void print_symbol_table_main()
{
printf("no_of_modules=%d\n\n", no_of_modules);
printf("VARIABLE NAME ");
printf(" SCOPE/MODULE NAME ");
printf(" SCOPE(LINE NO) ");
printf(" VARIABLE TYPE ");
printf("IS_ARRAY ");
printf(" IS_DYNAMIC ");
printf("RANGE_IF_ARR ");
printf(" WIDTH ");
printf(" OFFSET ");
printf(" NESTING ");
printf("\n\n");
for (int i = 0; i < no_of_modules; i++)
{
if (global_symbol_table[i]->table == NULL)
{
// table is not present
// printf("\n\nSymbol table not present for module <<%s>>\n\n", global_symbol_table[i]->mod_name);
}
else
print_symbol_module(global_symbol_table[i]);
}
return;
}
void print_symbol_table_array_main()
{
printf("no_of_modules = %d\n\n", no_of_modules);
printf("SCOPE/MODULE NAME ");
printf(" SCOPE(LINE NO) ");
printf(" NAME OF VARIABLE ");
printf(" IS_DYNAMIC ");
printf(" RANGE_IF_ARR ");
printf(" ELEMENT TYPE ");
printf("\n\n");
for (int i = 0; i < no_of_modules; i++)
{
if (global_symbol_table[i]->table == NULL)
{
// table is not present
// printf("\n\nSymbol table not present for module <<%s>>\n\n", global_symbol_table[i]->mod_name);
}
else
print_symbol_module_array(global_symbol_table[i]);
}
return;
}
void print_activation_record_main()
{
printf("\n\nModule name ");
printf(" Width ");
printf("\n\n");
for (int i = 0; i < no_of_modules; i++)
{
if (global_symbol_table[i]->table != NULL)
{
// table is present
printf("%-20s", global_symbol_table[i]->mod_name);
printf(" %d ", global_symbol_table[i]->width);
printf("\n");
}
}
return;
}
int main(int argc, char *argv[])
{
displayInfo();
global_symbol_table_init();
if (argc < 3)
{
printf("very few arguments\n");
exit(0);
}
FILE *fp = NULL;
fp = fopen(argv[1], "r");
if (fp == NULL)
{
printf("error in opening file\n");
exit(0);
}
FILE *asmfile = NULL;
asmfile = fopen(argv[2], "w");
parseTreeNode root;
parseTreeNode ast_root;
int t[2] = {0, 0};
while (true)
{
fp = fopen(argv[1], "r");
printMenu();
printf("Please Enter the Choice\n");
int x;
scanf("%d", &x);
printf("\n");
switch (x)
{
case 0:
{
// Exit Program
printf("\nTERMINATING THE PROGRAM\n");
fclose(fp);
exit(0);
}
case 1:
{
// char buffer[BUFF_SIZE], cleanFile[BUFF_SIZE];
// fread(buffer, sizeof(char), BUFF_SIZE, fp);
// char *testCaseFile = buffer;
// removeComments(testCaseFile, cleanFile);
// puts(cleanFile);
// Print Lexeme Tokens
print_tokens(fp, buff_size);
break;
}
case 2:
{
root = startParser(fp, buff_size);
if (error_count > 0)
{
printf("\033[31m");
printf("\nNo. of syntax errors = %d Can't print AST first resolve syntactic errors\n", error_count);
printf("\033[0m");
break;
}
else
{
printf("\033[32m");
printf("\nCode is syntactically correct\n");
printf("\033[0m");
}
// Print ParseTree
print_parseTree_main(root);
// freeParseTree(root) ;
// printParseTree(root) ;
break;
}
case 3:
{
root = startParser(fp, buff_size);
if (error_count > 0)
{
printf("\033[31m");
printf("\nNo. of syntax errors = %d\n", error_count);
printf("Can't print AST first resolve syntactic errors\n");
printf("\033[0m");
break;
}
else
{
printf("\033[32m");
printf("\nCode is syntactically correct\n");
printf("\033[0m");
}
printf("\n ------------------------------------------------------\n");
printf("\033[32m");
printf("\n\n Inorder Traversal of AST \n\n");
printf("\033[0m");
printf("\n ------------------------------------------------------\n\n");
ast_root = generate_ast(root);
// Print Abstract Syntax Tree
print_AST_main(ast_root);
// freeParseTree(root) ;
break;
}
case 4:
{
// Print Memory allocated to ParseTree and AST nodes
root = startParser(fp, buff_size);
int no_parseTreeNodes = count_nodes(root);
int size_parseTree = count_tree_size(root);
if (error_count > 0)
{
printf("\033[31m");
printf("\nNo. of syntax errors = %d\nCan't print AST first resolve syntactic errors\n", error_count);
printf("\033[0m");
break;
}
else
{
printf("\033[32m");
printf("\nCode is syntactically correct\n");
printf("\033[0m");
}
ast_root = generate_ast(root);
ast_root = ast_root->syn_node;
int no_AstNodes = count_nodes(ast_root);
int size_AST = count_tree_size(ast_root);
printf("\n");
printf("Parse Tree Nodes = %d\n", no_parseTreeNodes);
printf("Parse Tree Size = %d bytes\n", size_parseTree);
printf("AST Nodes = %d\n", no_AstNodes);
printf("AST Size = %d bytes\n", size_AST);
double compression_percentage = ((double)(size_parseTree - size_AST) / (double)(size_parseTree)) * 100;
printf("Compression Percentage = %lf\n", compression_percentage);
break;
}
case 5:
{
root = startParser(fp, buff_size);
no_of_errors = 0;
if (error_count > 0)
{
printf("\033[31m");
printf("\nNo. of syntax errors = %d\n", error_count);
printf("Code is syntactically incorrect\n");
printf("\033[0m");
break;
}
else
{
printf("\033[32m");
printf("\nCode is syntactically correct\n");
printf("\033[0m");
}
ast_root = generate_ast(root);
ast_root = ast_root->syn_node;
generate_symbol_table(ast_root, NULL, 0, 0, NULL, t);
pass2(ast_root, 0, NULL, 0);
if (no_of_errors > 0)
{
printf("\033[31m");
printf("\nTotal no of semantic errors = %d\n", no_of_errors);
printf("\033[0m");
}
else
{
printf("\033[32m");
printf("Code Compiles Successfully ...... \n\n");
printf("\033[0m");
}
print_symbol_table_main();
break;
}
case 6:
{
printf("\nActivation record to be printed\n");
root = startParser(fp, buff_size);
no_of_errors = 0;
if (error_count > 0)
{
printf("\033[31m");
printf("\nNo. of syntax errors = %d\n", error_count);
printf("Code is syntactically incorrect\n");
printf("\033[0m");
break;
}
else
{
printf("\033[32m");
printf("\nCode is syntactically correct\n");
printf("\033[0m");
}
ast_root = generate_ast(root);
ast_root = ast_root->syn_node;
generate_symbol_table(ast_root, NULL, 0, 0, NULL, t);
pass2(ast_root, 0, NULL, 0);
if (no_of_errors > 0)
{
printf("\033[31m");
printf("\nTotal no of semantic errors = %d\n", no_of_errors);
printf("\033[0m");
}
else
{
printf("\033[32m");
printf("Code Compiles Successfully ...... \n\n");
printf("\033[0m");
}
print_activation_record_main();
break;
}
case 7:
{
// Print array variables
root = startParser(fp, buff_size);
no_of_errors = 0;
if (error_count > 0)
{
printf("\033[31m");
printf("\nNo. of syntax errors = %d\n", error_count);
printf("Code is syntactically incorrect\n");
printf("\033[0m");
break;
}
else
{
printf("\033[32m");
printf("\nCode is syntactically correct\n");
printf("\033[0m");
}
ast_root = generate_ast(root);
ast_root = ast_root->syn_node;
generate_symbol_table(ast_root, NULL, 0, 0, NULL, t);
pass2(ast_root, 0, NULL, 0);
if (no_of_errors > 0)
{
printf("\033[31m");
printf("\nTotal no of semantic errors = %d\n", no_of_errors);
printf("\033[0m");
}
else
{
printf("\033[32m");
printf("Code Compiles Successfully ...... \n\n");
printf("\033[0m");
}
print_symbol_table_array_main();
break;
}
case 8:
{
// Error reporting and total compiling time
clock_t start_time, end_time;
double total_CPU_time, total_CPU_time_in_seconds;
start_time = clock();
// Parser Code here
root = startParser(fp, buff_size);
if (error_count > 0)
{
printf("\033[31m");
printf("\nNo. of syntax errors = %d\n", error_count);
printf("Code is syntactically incorrect\n");
printf("\033[0m");
end_time = clock();
break;
}
else
{
printf("\033[32m");
printf("\nCode is syntactically correct\n");
printf("\033[0m");
}
// AST and Smbol Table Code here
no_of_errors = 0;
ast_root = generate_ast(root);
ast_root = ast_root->syn_node;
generate_symbol_table(ast_root, NULL, 0, 0, NULL, t);
pass2(ast_root, 0, NULL, 0);
if (no_of_errors > 0)
{
printf("\033[31m");
printf("\nTotal no of semantic errors = %d\n", no_of_errors);
printf("\033[0m");
}
else
{
printf("\033[32m");
printf("Code Compiles Successfully ...... \n\n");
printf("\033[0m");
}
end_time = clock();
total_CPU_time = (double)(end_time - start_time);
total_CPU_time_in_seconds = total_CPU_time / CLOCKS_PER_SEC;
printf("\nTOTAL CPU CLOCK TICKS := %lf \n", total_CPU_time);
printf("TOTAL CPU TIME IN SECONDS := %lf \n", total_CPU_time_in_seconds);
break;
}
case 9:
{
root = startParser(fp, buff_size);
asmfile = fopen(argv[2], "w");
if (error_count > 0)
{
printf("\033[31m");
printf("\nNo. of syntax errors = %d\n", error_count);
printf("Code is syntactically incorrect\n");
printf("\033[0m");
break;
}
else
{
printf("\033[32m");
printf("\nCode is syntactically correct\n");
printf("\033[0m");
}
// AST and Symbol Table Code here
ast_root = generate_ast(root);
ast_root = ast_root->syn_node;
generate_symbol_table(ast_root, NULL, 0, 0, NULL, t);
pass2(ast_root, 0, NULL, 0);
// Intermediate codegen and Cdegen here
qNode start = (qNode)malloc(sizeof(struct QUADRUPLE));
if (no_of_errors > 0)
{
printf("\033[31m");
printf("\nNo of semantic errors = %d\n", no_of_errors);
printf("\nCode is semantically incorrect\n");
printf("\033[0m");
break;
}
else
{
printf("\033[32m");
printf("Code Compiles Successfully ...... \n\n");
printf("\033[0m");
}
// if (ast_root == NULL)
// {
// printf("ast_null\n");
// }
// print_symbol_table_main();
generateIR(start, ast_root, NULL, 0);
// printf("operator arg1 arg2 result\n");
// printQuadruple(start->next);
// print_symbol_table_main();
// printf("HIIIII\n");
createCode(start->next, asmfile);
break;
}
default:
{
printf("\nWrong choice\n");
break;
}
}
printf("\n\n");
}
return 0;
}