-
Notifications
You must be signed in to change notification settings - Fork 2
/
driver.c
234 lines (209 loc) · 5.83 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
/*
BATCH NO. 27
Mayank Agarwal (2014A7PS111P)
Karan Deep Batra(2014A7PS160P)
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "lexerDef.h"
#include "lexer.h"
#include "grammar.h"
#include "first.h"
#include "follow.h"
#include "parser.h"
#include "parserDef.h"
#include "mainsymboltable.h"
#include "makeAST.h"
#include "typeExtractor.h"
#include "semantics.h"
#include "codegen.h"
void printCleanFile()
{
FILE* fp = fopen("cleanFile.txt", "r");
fseek(fp,0,SEEK_END);
size_t inputsize = ftell(fp);
rewind(fp);
for(int i=0; i<inputsize; i++)
{
char ch = fgetc(fp);
printf("%c", ch);
}
return;
}
void printTokens()
{
if(tokenlist == NULL)
return;
tokeninfo* temp = tokenlist;
while(strcmp(temp->tokenname, "$") != 0)
{
if(strcmp(temp->tokenname, "ERROR_1") == 0)
{
printf("ERROR_1: Identifier at line %d is longer than the prescribed length\n", temp->linenumber);
}
else if(strcmp(temp->tokenname, "ERROR_2") == 0)
{
printf("ERROR_2: Unknown Symbol %s at line %d\n", temp->lexeme, temp->linenumber);
}
else if(strcmp(temp->tokenname, "ERROR_3") == 0)
{
printf("ERROR_3: Unknown pattern %s at line %d\n", temp->lexeme, temp->linenumber);
}
else{
printf("%s %s %d\n", temp->tokenname, temp->lexeme, temp->linenumber);
}
temp = temp->next;
}
return;
}
void printErrorTokens()
{
if(tokenlist == NULL)
return;
tokeninfo* temp = tokenlist;
while(strcmp(temp->tokenname, "$") != 0)
{
if(strcmp(temp->tokenname, "ERROR_1") == 0)
{
printf("ERROR_1: Identifier at line %d is longer than the prescribed length\n", temp->linenumber);
}
else if(strcmp(temp->tokenname, "ERROR_2") == 0)
{
printf("ERROR_2: Unknown Symbol %s at line %d\n", temp->lexeme, temp->linenumber);
}
else if(strcmp(temp->tokenname, "ERROR_3") == 0)
{
printf("ERROR_3: Unknown pattern %s at line %d\n", temp->lexeme, temp->linenumber);
}
temp = temp->next;
}
return;
}
int main(int argc, char* argv[])
{
printf("Level 4: Symbol Table/ AST/ Type Checking/ Semantic Rules/ Code Generation modules work.\n");
if(argc < 3)
{
printf("USAGE: ./compiler testcase.txt code.asm");
return 0;
}
FILE* fp = fopen(argv[1], "rb");
removeComments(fp);
fclose(fp);
fp = fopen(argv[1], "rb");
tokenlist = getAllTokens(fp);
fclose(fp);
hashtable* table = makehashtable();
populateGrammar(table);
// printhashtable(table);
// printGrammar(table);
// printTopDownGrammar(table);
populateFirstSets(table);
// printFirstSets(table);
populateFollowSets(table);
// printFollowSets(table);
makeParseTable(table);
// printParseTable(table);
// int syntaxcorrect = parseGrammar(table, tokenlist);
// printParseTree(root, "ROOT");
int control;
printf("------------------------------------------------------------\n");
printf("Press the appropriate option\n");
printf("1 : For printing the token list generated by the lexer.\n");
printf("2 : For parsing to verify the syntactic correctness of the input source code and to produce parse tree.\n");
printf("3 : For printing the inorder traversal of the Abstract Syntax Tree in appropriate format\n");
printf("4 : For displaying the amount of allocated memory and number of nodes to each of parse tree and abstract syntax tree.\n");
printf("5 : For printing the Symbol Table.\n");
printf("6 : For compiler to verify the syntactic and semantic correctness of the input source code.\n");
printf("7 : For producing assembly code (only when there is no syntactic, semantic or type mismatch errors).\n");
scanf("%d",&control);
if(control == 1)
{
printTokens();
}
if(control == 2)
{
int syntaxcorrect = parseGrammar(table, tokenlist, 0);
if(syntaxcorrect)
printParseTree(root, "ROOT");
else
printf("ERROR in making Parse Tree because parsing is not correct.\n");
}
if(control == 3)
{
int syntaxcorrect = parseGrammar(table, tokenlist, 0);
if(syntaxcorrect)
{
makeAST(root, "ROOT");
printAST(root, "ROOT");
}
else
printf("ERROR in making Abstract Syntax Tree because parsing is not correct.\n");
}
if(control == 4)
{
int syntaxcorrect = parseGrammar(table, tokenlist, 0);
if(syntaxcorrect)
{
int nodesPT = 0, memoryPT = 0;
parseTreeMemory(root, &nodesPT, &memoryPT);
printf("Parse Tree\tNumber of Nodes = %d\tAllocated Memory = %d\tBytes\n", nodesPT, memoryPT);
int nodesAST = 0, memoryAST = 0;
makeAST(root, "ROOT");
astMemory(root, &nodesAST, &memoryAST);
printf("AST\t\tNumber of Nodes = %d\tAllocated Memory = %d\tBytes\n", nodesAST, memoryAST);
printf("Compression Percentage = %f\n", ((memoryPT-(float)memoryAST)/memoryPT)*100);
}
else
printf("ERROR, parsing is not correct.\n");
}
if(control == 5)
{
int syntaxcorrect = parseGrammar(table, tokenlist, 0);
if(syntaxcorrect)
{
makeAST(root, "ROOT");
mainsymboltable* globaltable = makemainsymboltable();
populatemainsymboltable(root, NULL, globaltable, 0);
printmainsymboltable(globaltable);
}
}
if(control == 6)
{
int syntaxcorrect = parseGrammar(table, tokenlist, 0);
if(syntaxcorrect == 0)
{
printf("Syntax Errors :\n");
printErrorTokens();
parseGrammar(table, tokenlist, 1);
}
else
{
makeAST(root, "ROOT");
mainsymboltable* globaltable = makemainsymboltable();
populatemainsymboltable(root, NULL, globaltable, 1);
traverseAST_fortypechecking(root);
checkSemantics(root, globaltable);
if(semanticCorrect == 0)
printf("Code Compiled Successfully.....\n");
}
}
if(control == 7)
{
int syntaxcorrect = parseGrammar(table, tokenlist, 1);
if(syntaxcorrect)
{
FILE* asmFile = fopen(argv[2], "w+");
makeAST(root, "ROOT");
mainsymboltable* globaltable = makemainsymboltable();
populatemainsymboltable(root, NULL, globaltable, 0);
generate_code(asmFile, globaltable, root);
fclose(asmFile);
}
else
printf("ERROR, Syntax errors exist in the code.\n");
}
return 0;
}