-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdriver.c
132 lines (122 loc) · 3.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
// GROUP 39
// AKANKSHYA MISHRA 2016A7PS0026P
// NARAPAREDDY BHAVANA 2016A7PS0034P
// KARABEE BATTA 2016A7PS0052P
// AASTHA KATARIA 2016A7PS0062P
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "lexer.h"
#include "key.h"
#include "parser.h"
#include "grammar.h"
#include "semantic.h"
#include "codegen.h"
extern parse_table T;
extern int semantic_correct;
// extern int parse_correct;
// extern int ParseNodes;
// extern int ASTNodes;
int main(int argc, char *argv[]){
int option;
printf("LEVEL 4: Symbol table/ AST/ Semantic Rules modules work.\n\n");
populate_transition_table();
populateKeyWordTable();
populateGrammar();
populateStrTable();
ComputeFirstAndFollowSets();
createParseTable();
while(1){
printf("Please enter the option:\n");
scanf("%d", &option);
if(option==0){
break;
}else if(option==1){
//Invoke only lexer
FILE *fp = fopen(argv[1], "r");
tokenInfo *ti;
do{
ti = getNextToken(fp);
if(ti->tokenName==TK_NUM) printf("Line %d: Token %s, Lexeme %d\n", ti->line_no, TerminalString(ti->tokenName), ti->u.value_of_int);
else if(ti->tokenName==TK_RNUM) printf("Line %d: Token %s, Lexeme %.2f\n", ti->line_no, TerminalString(ti->tokenName), ti->u.value_of_real);
else printf("Line %d: Token %s, Lexeme %s\n", ti->line_no, TerminalString(ti->tokenName), ti->u.lexeme);
// free(ti);
}while(ti->tokenName!=EOS);
fclose(fp);
printf("\n");
}else if(option==2){
treeNodeIt* t = parseInputSourceCode(argv[1]);
printParseTree(t);
}else if(option==3){
printf("Traversal order of AST: Post-order\n");
treeNodeIt* t = parseInputSourceCode(argv[1]);
ASTNodeIt* ast = makeAbstractSyntaxTree(t);
printAST(ast);
}else if(option==4){
treeNodeIt* t = parseInputSourceCode(argv[1]);
size_t p = printParseTreeNodes(t);
printf("Parse tree Number of nodes = %d\t\tAllocated Memory = %lu Bytes\n", ParseNodes, p);
ASTNodeIt* ast = makeAbstractSyntaxTree(t);
size_t a = printASTNodes(ast);
printf("AST Number of nodes = %d\t\tAllocated Memory =%lu Bytes\n", ASTNodes, a);
printf("Compression percentage = %f\n", (((float)p-a)/(float)p)*100 );
}
else if(option==5 || option==6 || option==7 || option==8){
treeNodeIt* t = parseInputSourceCode(argv[1]);
ASTNodeIt* ast = makeAbstractSyntaxTree(t);
ASTNodeIt * ch = ast->node->u.n->children->node->u.n->children;
populateGlobalTable(ast);
SymbolTable = create_HTEle();
while(ch!=NULL){
populateSymbolTable(ch);
ch=ch->next;
}
populateSymbolTable(ast->node->u.n->children->next);
if(option==5){
printSymbolTable();
}else if(option==6){
printGlobalvar();
}else if(option==7){
printMemActRec();
}else{
printGlobalTable_recDef();
}
}else if(option==9){
clock_t start_time, end_time;
double total_CPU_time, total_CPU_time_in_seconds;
start_time = clock();
treeNodeIt* t = parseInputSourceCode(argv[1]);
if(parse_correct){
ASTNodeIt *ast = semanticAnalyzer(t);
}
if(!semantic_correct){
printf("Code compiles successfully...\n");
}
else printf("\n");
end_time = clock();
total_CPU_time = (double) (end_time - start_time);
total_CPU_time_in_seconds = total_CPU_time / CLOCKS_PER_SEC;
// Print both total_CPU_time and total_CPU_time_in_seconds
printf("Total CPU time: %f\nTotal CPU time in seconds: %f\n", total_CPU_time, total_CPU_time_in_seconds);
}else if(option==10){
int i;
printf("Please enter whether code is free of syntactic, semantic or type mismatch errors(0 for no, 1 for yes)\n");
printf("To check, use option=9 first\n");
treeNodeIt* t = parseInputSourceCode(argv[1]);
ASTNodeIt *ast;
if(parse_correct){
ast = semanticAnalyzer(t);
}
if(!semantic_correct){
printf("Code compiles successfully...\n");
continue;
}
else printf("\n");
generateCode(ast, argv[2]);
}
else{
printf("Enter correct option\n");
}
}
return 0;
}