-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdriver.c
More file actions
117 lines (113 loc) · 4.07 KB
/
Copy pathdriver.c
File metadata and controls
117 lines (113 loc) · 4.07 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
/*
Group No: 26
Authors:
Nithin Benny Myppan - 2016A7PS0014P
Adhitya Mamallan - 2016A7PS0028P
Swarup N - 2016A7PS0080P
Naveen Unnikrishnan - 2016A7PS0111P
*/
#include "lexer.h"
#include "parser.h"
#include "ast.h"
#include "symbolTable.h"
#include "semanticAnalyzer.h"
#include <time.h>
int main(int argc, char* argv[]){
printf("Level 2: Symbol Table, AST, Semantic Rules and Type Checking work\n\n");
if(argc!=3){
printf("Please re-enter command as ./compiler testcase.txt\n");
return 0;
}
Grammar *g = loadGrammar("grammar.txt");
printf("> Grammar loaded.\n");
FirstAndFollow* ff = getFirstAndFollowSets(g);
printf("> First and follow sets computed.\n");
ParsingTable pt = makeNewParseTable();
createParseTable(g, ff, pt);
printf("> Parse table created.\n\n");
int loop = 1, errors;
ParseTree tree = NULL;
AST ast = NULL;
STTree st = NULL;
while(loop){
printf("Enter the task to perform.\n\n");
printf("0 - Quit\n");
printf("1 - Print the token list generated by the lexer\n");
printf("2 - Parse to verify the syntactic correctness of the input code and print the parse tree appropriately\n");
printf("3 - Print the Abstract Syntax Tree in appropriate format\n");
printf("4 - Display the amount of allocated memory and number of nodes in the parse tree and the abstract syntax tree\n");
printf("5 - Print the Symbol Table\n");
printf("6 - Print the list of global variables, with size and offset\n");
printf("7 - Print the total memory requirement of each function\n");
printf("8 - Print the type expressions and width of global records\n");
int op;
scanf("%d", &op);
switch(op){
case 0:
loop = 0;
break;
case 1:
printf("\n>> Printing token list.\n********************************\n\n");
printTokenList(argv[1]);
break;
case 2:
tree = parseInputSourceCode(argv[1],pt,ff,&errors);
printParseTree(tree);
break;
case 3:
if(tree==NULL){
printf("\nParse tree hasn't been made yet, please run 2 first.\n\n");
break;
}
if(errors>0){
printf("\nSource code is syntactically wrong, can't make AST.\n\n");
}
if(ast==NULL) ast = makeAST(tree);
displayAST(ast);
break;
case 4:
if(tree==NULL || ast==NULL){
printf("\nAST hasn't been made yet, please run 3 first.\n\n");
break;
}
compressionRatio(tree, ast);
printf("\n");
break;
case 5:
if(ast==NULL){
printf("\nAST hasn't been made yet, please run 3 first.\n\n");
break;
}
if(st==NULL){
printf("\n");
st = makeSymbolTables(ast);
}
displaySTTree(st);
break;
case 6:
if(st==NULL){
printf("\nSymbol Table hasn't been made yet, please run 5 first.\n\n");
break;
}
printGlobalVars(st);
break;
case 7:
if(st==NULL){
printf("\nSymbol Table hasn't been made yet, please run 5 first.\n\n");
break;
}
printFnMemories(st);
break;
case 8:
if(st==NULL){
printf("\nSymbol Table hasn't been made yet, please run 5 first.\n\n");
break;
}
printTypeExpressionGlobalRecord(st);
break;
default:
printf("Invalid argument, please try again.\n\n");
}
}
return 0;
}