-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdriver.c
120 lines (111 loc) · 3.74 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
/* Group 3
Ashrya Agrawal 2018A7PS0210P
Kalit Naresh Inani 2018A7PS0207P
Prajwal Gupta 2018A7PS0231P */
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "readGrammar.h"
#include "tokenizer.h"
#include "parsetree.h"
#include "print.h"
int main(int argc, char *argv[])
{
if (argc > 2)
printf("Recieved more command line arguments than expected.\n\n");
else if (argc == 1)
printf("You have not passed any Source Code File\n\n");
clock_t begin = clock();
Node *arr = (Node *)malloc(sizeof(Node) * NUM_RULES);
char *grammarFile = "Grammar.txt";
readGrammar(grammarFile, arr);
printf("\n----------------------------------------------------READING GRAMMAR COMPLETED-------------------------------------------------\n\n");
Token *ts = NULL;
char *fileName = argv[1];
ts = tokeniseSourcecode(fileName, ts);
printf("\n-------------------------------------------------TOKENISING SOURCE CODE COMPLETED---------------------------------------------\n\n");
while (1)
{
printf("Option 0: exit\n");
printf("Option 1: Create parse tree\n");
printf("Option 2: Traverse the parse tree to construct typeExpressionTable. Also print the type errors while traversing the parse tree and accessing the typeExpressionTable\n");
printf("Option 3: Print parse tree in the specified format\n");
printf("Option 4: Print typeExpressionTable in the specified format.\n");
int choice;
scanf("%d", &choice);
switch (choice)
{
case 0:
{
exit(0);
}
case 1:
{
TreeNode *t = NULL;
t = createParseTree(t, ts, arr);
break;
}
case 2:
{
TreeNode *t = NULL;
t = createParseTree(t, ts, arr);
if (t == NULL)
{
printf("There is some syntax error in the program\n");
}
else
{
initializeTable();
shouldPrintErrorsOrNot(1);
printf("LineNo.\t\tStatement Type\t\tOperator\tLexeme1\t\t\tType1\t\tLexeme2\t\t\tType2\t\tDepth\t\tMessage\n\n");
traverseParseTree(t);
printf("\n-------------------------------------------------TRAVERSING PARSE TREE COMPLETED---------------------------------------------");
printf("\n----------------------------------------------------PRINTING TYPE ERRORS COMPLETED--------------------------------------------\n\n");
}
break;
}
case 3:
{
TreeNode *t = NULL;
t = createParseTree(t, ts, arr);
if (t == NULL)
{
printf("There is some syntax error in the program\n");
}
else
{
initializeTable();
shouldPrintErrorsOrNot(0);
traverseParseTree(t);
printParseTree(t);
}
break;
}
case 4:
{
TreeNode *t = NULL;
t = createParseTree(t, ts, arr);
if (t == NULL)
{
printf("There is some syntax error in the program\n");
}
else
{
initializeTable();
shouldPrintErrorsOrNot(0);
traverseParseTree(t);
printTypeExpressionTable(table, currentTableEntry);
}
break;
}
default:
{
printf("No such choice available.\n");
}
}
}
clock_t end = clock();
double time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
printf("TIME SPENT: %lf\n", time_spent);
return 0;
}