-
Notifications
You must be signed in to change notification settings - Fork 0
/
driver.c
171 lines (164 loc) · 7.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
/*
GROUP - 14
Created By:
1) Tarun Kumar (2016A7PS0005P)
2) Varun Gupta (2016A7PS0087P)
3) Avichal Jain (2016A7PS0046P)
4) Vaibhav Maheshwari (2016A7PS0081P)
*/
#include "typeChecker.h"
#include "symbol.h"
#include "icg.h"
#include "CodeGenerator.h"
#include <time.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char const *argv[]) {
printf("LEVEL 4: Symbol table/ AST/ Semantic Rules modules work.\n\n");
if (argc < 3) {
printf("Not enough arguments passed => Run again\n");
return 0;
}
int x;
symbolTable table = initializeSymbolTable();
while (true) {
hasErrors = false;
initialize();
printf("\nChoose option:\n");
printf("0: For exit \n");
printf("1: Printing Token List \n");
printf("2: Printing Parse Tree and Errors \n");
printf("3: Printing Abstract Syntax Tree \n");
printf("4: Allocated Memory and Number of Nodes\n");
printf("5: Print Symbol Table \n");
printf("6: Global Variables \n");
printf("7: Total Memory Requirement \n");
printf("8: Record Definitions \n");
printf("9: Calculating Time of Code\n");
printf("10: Generate Assembly Code\n");
scanf("%d", &x);
printf("\n");
switch (x) {
case 0: {
printf("\n*******************Exit from the loop************************\n\n");
exit(0);
}
case 1: {
printf("\n*******************Printing Token List************************\n\n");
FILE *fp2 = fopen(argv[1], "r");
printTokens(table, fp2, true);
fclose(fp2);
printf("\n");
break;
}
case 2: {
printf("\n*******************Printing Parse Tree and Errors************************\n\n");
ParseTree root = printParserLexer(table, "grammar.txt", argv[1], true, false);
break;
}
case 3: {
printf("\n*******************Printing Abstract Syntax Tree************************\n\n");
ParseTree root = printParserLexer(table, "grammar.txt", argv[1], false, false);
if (!hasErrors) {
createAST(root);
printf("Printing the Abstract Syntax Tree in Pre Order Traversal\n");
printf("Sample print: Parent Node -> All its children in the same line\n");
printf("\n");
printAST(root);
}
printf("\n");
break;
}
case 4: {
printf("\n*******************Allocated Memory and Number of Nodes************************\n\n");
initializeVariablesParser();
ParseTree root = printParserLexer(table, "grammar.txt", argv[1], false, true);
initializeVariableAST();
long int memoryParseTree = getMemory();
printf("Number of nodes in Parse Tree : %ld\n", getParseNodes());
printf("Allocated memory for Parse Tree : %ld\n", memoryParseTree);
createAST(root);
long int memoryAST = getMemoryAST();
printf("Number of nodes in Abstract Syntax Tree : %ld\n", getFreeNode());
printf("Allocated memory for Abstract Syntax Tree : %ld\n", memoryAST);
double percentage = ((memoryParseTree - memoryAST) * 100.0) / (memoryParseTree);
printf("Compression percentage : %.3lf%%\n", percentage);
break;
}
case 5: {
printf("\n*******************Print Symbol Table************************\n\n");
ParseTree root = printParserLexer(table, "grammar.txt", argv[1], false, true);
createAST(root);
symbolTable SymbolTable = buildSymbolTable(root, false);
printTable(root, SymbolTable);
break;
}
case 6: {
printf("\n*******************Global Variables************************\n\n");
ParseTree root = printParserLexer(table, "grammar.txt", argv[1], false, true);
createAST(root);
symbolTable SymbolTable = buildSymbolTable(root, false);
printGlobalVar(root, SymbolTable);
break;
}
case 7: {
printf("\n*******************Total Memory Requirement************************\n\n");
ParseTree root = printParserLexer(table, "grammar.txt", argv[1], false, true);
createAST(root);
symbolTable SymbolTable = buildSymbolTable(root, false);
printFunctionWidth(root, SymbolTable);
break;
}
case 8: {
printf("\n*******************Record Definitions************************\n\n");
ParseTree root = printParserLexer(table, "grammar.txt", argv[1], false, true);
createAST(root);
symbolTable SymbolTable = buildSymbolTable(root, false);
printRecordDefinitions(root, SymbolTable);
break;
}
case 9: {
printf("\n*******************Calculating Time of Code************************\n\n");
clock_t begin, end;
double totalTime, totalTime_in_sec;
begin = clock();
ParseTree root = printParserLexer(table, "grammar.txt", argv[1], true, true);
if (!hasErrors) {
createAST(root);
symbolTable SymbolTable = buildSymbolTable(root, true);
bool getStatus = getErrorStatus();
printf("The input source code is semantically %s.\n", !getStatus ? "correct" : "incorrect");
printf("Code compiles successfully..........\n");
if (!getStatus) {
int maxVariableName;
iCode code = getIntermediateCode(root, &maxVariableName);
generateAssemblyFile(code, maxVariableName, argv[2]);
}
}
end = clock();
totalTime = (double) (end - begin);
totalTime_in_sec = totalTime / CLOCKS_PER_SEC;
printf("totalTime : %lf , totalTimeInSeconds : %lf", totalTime, totalTime_in_sec);
printf("\n");
break;
}
case 10: {
printf("\n*******************Generate Assembly Code************************\n\n");
ParseTree root = printParserLexer(table, "grammar.txt", argv[1], true, true);
if (!hasErrors) {
createAST(root);
symbolTable SymbolTable = buildSymbolTable(root, true);
bool getStatus = getErrorStatus();
printf("Code compiles successfully..........\n");
if (!getStatus) {
int maxVariableName;
iCode code = getIntermediateCode(root, &maxVariableName);
generateAssemblyFile(code, maxVariableName, argv[2]);
}
}
break;
}
}
}
}