-
Notifications
You must be signed in to change notification settings - Fork 0
/
driver.c
123 lines (110 loc) · 2.87 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
/*
GROUP NUMBER: 45
GROUP MEMBERS:
1. SHAH NEEL KAUSHIK
2. MEHTA AASHAY PINKESH
3. RANADE SHUBHANKAR PRASAD
*/
#include "lexerDef.h"
#include "lexer.h"
#include "parser.h"
#include "stackTree.h"
#include "parserDef.h"
#include <time.h>
extern HashTable ht;
extern char *map_terminals_to_strings[TOTAL_NUM_TOKENS];
//Reads <filename> and prints all lexemes along with their respective line numbers
void printTokens(FILE *fp){
while(1){
tokenInfo token = getNextToken(fp);
if(token.type_of_token == NOT_KEYWORD){
break;
}
if(token.type_of_token == NOT_TOKEN || token.type_of_token == UNK_SYMB){
// printf("--------------->\n");
continue;
}
printf("Line %d: Token Name: %-15s Lexeme = %s\n", token.line_num, map_terminals_to_strings[token.type_of_token], token.value);
}
}
int main(int argc, char *argv[]){
//Initializes a hash-table ADT to store keywords
ht = initializeHashTable();
FILE *fp = fopen(argv[1], "r");
FILE *outfile = fopen(argv[2], "w");
if(argc!=3){
printf("Format: ./stage1exe [inputfile] [outputfile]\n");
return 0;
}
int option;
TreeNode root;
clock_t start_time, end_time;
double total_CPU_time, total_CPU_time_in_seconds;
printf("FIRST and FOLLOW sets automated.\nBoth lexical and syntax analysis modules implemented.\n\n");
while(1){
printf("Enter option: ");
scanf("%d", &option);
switch(option){
//Press 0 to exit the program
case 0:
fclose(fp);
fclose(outfile);
freeHashTable();
return 0;
//Press 1 to create a copy of the source code without any comments
case 1:
removeComments(argv[1], "no_comments.txt");
system("cat no_comments.txt");
break;
//Press 2 to print a list of all lexemes in the source file
case 2:
printTokens(fp);
if(feof(fp)){
printf("Rewinding1\n");
rewind(fp);
}
break;
case 3:
loadGrammar();
computeFirstSet();
computeFollowSet();
createParsingTable();
root = parse(fp);
printTree(root, &outfile);
freeParseTree(root);
freeGrammar();
break;
case 4:
start_time = clock();
loadGrammar();
computeFirstSet();
computeFollowSet();
createParsingTable();
root = parse(fp);
printTree(root, &outfile);
freeParseTree(root);
freeGrammar();
end_time = clock();
total_CPU_time = (double) (end_time - start_time);
total_CPU_time_in_seconds = total_CPU_time / CLOCKS_PER_SEC;
printf("time taken = %fs, ticks = %f\n", total_CPU_time_in_seconds, total_CPU_time);
break;
case 5:
loadGrammar();
computeFirstSet();
computeFollowSet();
createParsingTable();
root = parse(fp);
printTree(root, &outfile);
createAst(root);
AstNode astRoot = root->addr;
freeParseTree(root);
printAst(astRoot);
break;
//Default case to handle all other options
default:
printf("Not a valid option\n");
}
}
return 0;
}