-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdriver.c
122 lines (115 loc) · 3.09 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
/*
* COMPILER PROJECT- ERPLAG COMPILER
* CSE - D 2019
* Sahil Garg, Jenit Jain, Amrit Goyal
*/
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include "lexerDef.h"
#include "lexer.h"
#include "parserDef.h"
#include "parser.h"
#include "symbolDef.h"
#include "symbolTable.h"
#define LIMIT 25
void printSep()
{
printf("\n***************************************************************************************\n");
}
int main(int argc, char * argv[])
{
//Initialise the hash table for keywords
hashTable table = initializeHashTable(31);
FILE *fp = fopen("keywords.txt", "r");
int type, option;
char input[21];
while(~fscanf(fp, "%d %s", &type, input))
{
addKeyword(table, type, input);
}
fclose(fp);
// Initialise the Grammer
Grammar G = (Grammar)malloc(RULECNT*sizeof(gHead));
G = createGrammar();
findFirst(G);
findFollow(G);
createParseTable(G);
while(1)
{
//Display the menu
printf("\nERPLAG COMPILER MENU OPTIONS:\n");
printf("1: For producing clean code by removal of comments.\n");
printf("2: For printing the token list generated by the lexer.\n");
printf("3: For printing the Symbol table.\n");
printf("4: For parsing to verify the syntactic correctness of the input source code\n");
printf("5: For creating the parsetree and printing it(inorder traversal)\n");
printf("6: To exit.\n");
int option;
scanf("%d",&option);
if(option == 1)
{
removeComments(argv[1],"cleancode.txt");
printSep();
printf("cleancode.txt created\n");
printSep();
}
else if(option == 2)
{
printSep();
int error_exist = populateLexemeTable(argv[1], table, 1);
if (error_exist == 1)
{
printf("Recheck your code for Lexical errors\n");
}
printSep();
}
else if (option == 3)
{
removeComments(argv[1], "cleancode.txt");
parseTree Tree = parseInputSourceCode("cleancode.txt", table, G, 0);
tokenInfo *T = getFirstToken();
hashTable2 tableId = initializeHashTable2(hash_capacity_2);
hashTable2 tableFunc = initializeHashTable2(hash_capacity_2);
totalScopeList* scopeTable = (totalScopeList *)malloc(LIMIT * sizeof(totalScopeList));
int i;
for(i = 0; i < LIMIT; ++i)
{
scopeTable[i].scope_start = 0;
scopeTable[i].scope_end = 0;
}
createIDTable(T, scopeTable, tableId, tableFunc, 0);
T = getFirstToken();
secondRun(T, tableId, tableFunc, 0);
printf("Symbol Table sucessfully created.\n");
printf("%-4s %-10s\t%-20s %-10s %-15s %-6s %-6s %-6s\n", "SNO", "Var-Name", "Datatype", "Func-Name", "Start - End", "Level", "Size", "Offset");
printVariables(tableId, scopeTable);
}
else if(option == 4)
{
removeComments(argv[1], "cleancode.txt");
printSep();
parseTree Tree = parseInputSourceCode("cleancode.txt", table, G, 0);
printSep();
}
else if (option == 5)
{
removeComments(argv[1], "cleancode.txt");
printSep();
// parseTree Tree = parseInputSourceCode("clean.txt", table, G, 0);
printParseTree("cleancode.txt", table, G);
printf("\n");
printSep();
}
else if (option == 6)
{
break;
}
else
{
printf("\nInvalid Option\n");
}
}
return(0);
}