-
Notifications
You must be signed in to change notification settings - Fork 0
/
driver.c
executable file
·267 lines (227 loc) · 8.89 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
//Batch Number : 67
//Dhairya Sangoi - 2012B4A7640P
//Gautam Singhania - 2012B4A7495P
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <math.h>
#include "trieDef.h"
#include "trie.h"
#include "rulesDef.h"
#include "rules.h"
#include "parseTreeDef.h"
#include "parseTree.h"
#include "helperDef.h"
#include "stackDef.h"
#include "stack.h"
#include "helper.h"
#include "parser.h"
#include "lexerDef.h"
#include "lexer.h"
#include "dynamicArrayDef.h"
#include "dynamicArray.h"
#include "quadrupleDef.h"
#include "quadruple.h"
#include "scopeHashTableDef.h"
#include "recordsHashTableDef.h"
#include "allFunctionsHashTableDef.h"
#include "allFunctionsHashTable.h"
#include "scopeHashTable.h"
#include "recordsHashTable.h"
#include "registerDef.h"
#include "semanticAnalyzerDef.h"
#include "semanticAnalyzer.h"
#include "enum.h"
int main(int argc, char *argv[])
{
if (argc != 3){
printf("Usage: %s <testCase.txt> <code.asm>\n",argv[0]);
exit(0);
}
char *filename = "rule.txt";
char *enumfilename = "enum.h";
trie *terminals, *nonterminals;
token *allTokens[130];
ruleq **allRules = (ruleq **)malloc(sizeof(ruleq)*100);
ruleq **newRules = (ruleq **)malloc(sizeof(ruleq)*100);
int term,nonterm,rule;
populateDataStructures(filename, &terminals, &nonterminals, allTokens, allRules, newRules, &term, &nonterm, &rule,enumfilename);
int firstSetRules[rule+1][nonterm+term];
int firstSetterminals[nonterm+term][nonterm+term];
int followSet[nonterm+term][nonterm+term];
int parseTable[nonterm+term][nonterm+term];
createFirstSet(term, nonterm, rule, firstSetRules, firstSetterminals, allTokens, allRules, terminals);
createFollowSet(term, nonterm, rule, followSet, firstSetRules, firstSetterminals, allTokens, allRules, terminals, nonterminals);
createParseTable(term, nonterm, rule, parseTable, followSet, firstSetRules, firstSetterminals, allTokens, terminals);
FILE *inputfile = fopen(argv[1],"r");
tokenlist *tklist = caller_function(inputfile,30,terminals);
parseTree *pt;
recordsHashTable *rht;
allFunctionsHashTable *afht;
scopeHashTable *globalScope;
rht = createRecordsHashTable(5,7);
afht = createAllFunctionsHashTable(5,7);
globalScope = createScopeHashTable(5,7);
quadruple *q = createQuadruple(20);
int parsenodecount, astnodecount, sizenode;
float compression = 0;
parsenodecount = 0;
astnodecount = 0;
sizenode = sizeof(parseTreeNode);
int p[2];
pipe(p);
int success = parseInputSourceCode(&pt, terminals, nonterminals, allTokens, allRules, term, nonterm, parseTable,tklist, firstSetterminals,followSet,p[1]);
close(p[1]);
char *compilationmessage = (char *)malloc(sizeof(char)*1000000);
char *temp = compilationmessage;
int len;
while ( (len = read(p[0], temp, 1) ) > 0){
temp+=len;
}
*temp = '\0';
close(p[0]);
int p1[2];
pipe(p1);
int success2 = 0;
char *semanticmessage = NULL;
FILE *fp1 ;
char *parsetree;
parsetree = (char *)malloc(sizeof(char) * 100);
FILE *asmfile;
int isopen = 0;
int hasOtherFunctions = 0;
if (success == 1){
fp1 = fopen("parsetree.txt","w+");
dfsParseTree(pt->head,fp1);
dfsp(pt->head, &parsenodecount);
makeAST(pt->head,newRules,NULL);
dfsp(pt->head, &astnodecount);
if (pt->head->child->index != EPS){
hasOtherFunctions = 1;
}
asmfile = fopen(argv[2],"w");
isopen = 1;
success2 = makeScopeTables(pt->head,rht,afht,globalScope,q,p1[1],asmfile); //change the last parameter
close(p1[1]);
semanticmessage = (char *)malloc(sizeof(char)*100000);
char *temp2 = semanticmessage;
int len2;
while ( (len2 = read(p1[0], temp2, 1) ) > 0){
temp2+=len2;
}
*temp2 = '\0';
close(p1[0]);
compression =( ((float)(parsenodecount * sizenode) - (astnodecount * sizenode))/(parsenodecount * sizenode) ) * 100;
}
//return 0;
/*
printf("\nRecord Hash Table:\n");
printRecordHashTable(rht);
printf("\nAllFunctions Hash Table:\n");
printAllFunctionsHashTable(afht);
printf("\nGlobal Scope Hash Table:\n");
printScopeHashTable(globalScope);
printf("\n\n");
return 0;
int count = 0;
if (success){
FILE *parsingtree = fopen(argv[2],"w");
dfsParseTree(pt->head,parsingtree, &count);
fclose(parsingtree);
}
*/
printf("(a) FIRST and FOLLOW set automated.\n(b) Both lexical and syntax analysis modules implemented.\n(c) Modules working for all test cases.\n");
while (1){
printf("\n**************************************************************************\n");
printf("\nPress option for the defined task:\n");
printf("1: For printing the token list\n");
printf("2: For parsing to verify the syntactic correctness of the input source code\n");
printf("3: For printing the abstract syntax tree\n");
printf("4: For displaying the amount of allocated memory and number of nodes\n");
printf("5: For printing the Symbol Table\n");
printf("6: For compiling to verify the syntactic and semantic correctness\n");
printf("7: For producing assembly code\n");
printf(" Any other to exit the program\n\nChoice: ");
char choice;
scanf(" %c",&choice);
printf("\n**************************************************************************\n");
switch(choice){
case '1':
printTokenList(tklist,allTokens);
break;
case '2':
if (success){
fseek(fp1,0,SEEK_SET);
while(fgets(parsetree,100,fp1)!=NULL){
fputs(parsetree,stdout);
}
//printf("%s",parsetree);
}
else {
printf("Didn't generate parse tree since compilation failed\n");
}
break;
case '3':
if (success){
dfsParseTree(pt->head,stdout);
}
else {
printf("Didn't generate abstract syntax tree since compilation failed\n");
}
break;
case '4':
if (success){
printf("Parse tree\tNumber of nodes = %d\tAllocated Memory = %d Bytes\n", parsenodecount, sizenode*(parsenodecount));
printf("AST \tNumber of nodes = %d\tAllocated Memory = %d Bytes\n", astnodecount, sizenode*(astnodecount));
printf("Compression Percentage = %f%%", compression);
}
else {
printf("Didn't generate parse tree and abstract syntax tree since compilation failed\n");
}
break;
case '5':
if (success == 1 && success2 == 1){
printf("Symbol Table Entries: \n");
printScopeHashTable(globalScope,"_global",rht);
printAllFunctionsHashTable(afht,rht);
}
else {
printf("The code being not correct (syntactically or semantically), couldn't print symbol table.\n");
}
break;
case '6':
if (success == 0){
printf("Syntactical Errors:\n");
printf("%s\n",compilationmessage);
}
else {
if (success2 == 0){
printf("Semantic Errors:\n");
printf("%s\n",semanticmessage);
}
else {
printf("No syntactical and semantic errors. Compilation Successfull.\n");
}
}
break;
case '7':
if (success2 == 1 && hasOtherFunctions == 0){
printf("Generated <%s> file.\n", argv[2]);
if (isopen == 1){
fclose(asmfile);
isopen = 0;
}
}
else {
printf("Couldn't generate file <%s> since code had syntactic or semantic mistakes or more than one fuctions.\n", argv[2]);
}
break;
default:
printf("Exiting Compiler Project. It was a nice learning experience :)\n");
remove("parsetree.txt");
//delete parsetree.txt
return 0;
}
}
return 0;
}