forked from f20180301/PPL_Assignment_2020
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdriver.c
114 lines (113 loc) · 3.91 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
/**
SHUBHAM AGARWAL -- 2018A7PS0301P
YASH RAJ SINGH -- 2018A7PS0214P
SAHIL KATTNA -- 2018A7PS0154P
AGRAWAL RAJAT RAMESH -- 2018A7PS0182P
**/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "readGrammar.c"
#include "tokeniseSourceCode.c"
#include "parseTree.c"
#include "typeExpressionTable.c"
int main(int argc, char *argv[])
{
//INTIALISATION PART
// 1. Read the grammar.
grammar *g = malloc(NON_TERMINALS * sizeof(grammar));
readGrammar("grammar.txt", g);
// 2. Tokenize the source code
if (argc == 2)
tokeniseSourcecode(argv[1], head);
else
{
printf("Please provide the sourcefile.txt as command line\n");
return 0;
}
// 3. Create the parseTree
//create the start node
posToken = head;
parseTree *p = malloc(sizeof(parseTree));
p->isTerm = 0;
p->Node.nonTerminal.expRule = NULL;
p->Node.nonTerminal.nt = s;
p->parent = p;
p->sibling = NULL;
p->firstChild = NULL;
p->depth = 0;
p->exp_type.tag = not_app;
// if (posToken != NULL)
// {
// printf("ERROR READING INPUT TESTCASE (IT OCCURS USUALLY IF AN UNKNOWN SYMBOLS APPEAR WHICH CANNOT DOEAS NOT MATCH ANY RULE)\n");
// return 0;
// }
int n;
// //4. Printing the parse tree
// printParseTree(p);
// traverse_parse_tree(p);
// printParseTree(p);
// printTypeExpressionsTable(table, "typeExpressionTable.txt");
do
{
printf("Option 0:To Exit\n");
printf("Option 1:Create Parse Tree\n");
printf("Option 2:Create and Traverse Parse Tree\n");
printf("Option 3:Create, Traverse Parse Tree and Print Parse Tree\n");
printf("Option 4:Create, Traverse Parse Tree, Print Parse Tree and Type Expression Table\n");
printf("Please Choose your option : ");
scanf("%d", &n);
switch (n)
{
case 0:
printf("EXITING!!\n");
break;
case 1:
createParseTree(p, g);
if (posToken != NULL)
{
printf("ERROR READING INPUT TESTCASE (IT OCCURS USUALLY IF AN UNKNOWN SYMBOLS APPEAR WHICH CANNOT DOEAS NOT MATCH ANY RULE)\n");
return 0;
}
printf("Parse Tree Created Successfully\n");
break;
case 2:
createParseTree(p, g);
if (posToken != NULL)
{
printf("ERROR READING INPUT TESTCASE (IT OCCURS USUALLY IF AN UNKNOWN SYMBOLS APPEAR WHICH CANNOT DOEAS NOT MATCH ANY RULE)\n");
return 0;
}
traverse_parse_tree(p);
printf("Errors if any are Printed.\nParse Tree Traversal Successfull.\n");
break;
case 3:
createParseTree(p, g);
if (posToken != NULL)
{
printf("ERROR READING INPUT TESTCASE (IT OCCURS USUALLY IF AN UNKNOWN SYMBOLS APPEAR WHICH CANNOT DOEAS NOT MATCH ANY RULE)\n");
return 0;
}
traverse_parse_tree(p);
printParseTree(p);
printf("Errors if any are Printed.\nParse Tree Printing in parseTree.txt and Traversal Successfull.\n");
break;
case 4:
createParseTree(p, g);
if (posToken != NULL)
{
printf("ERROR READING INPUT TESTCASE (IT OCCURS USUALLY IF AN UNKNOWN SYMBOLS APPEAR WHICH CANNOT DOEAS NOT MATCH ANY RULE)\n");
return 0;
}
traverse_parse_tree(p);
printParseTree(p);
printTypeExpressionsTable(table, "typeExpressionTable.txt");
printf("Errors if any are Printed.\nParse Tree Printing in parseTree.txt, Traversal performed and Type Expression Table Printing in typeExpressionTable.txt Successfull.\n");
break;
default:
printf("Sorry! Enter a number between 0-4\n");
break;
}
} while (n != 0);
return 0;
}