-
Notifications
You must be signed in to change notification settings - Fork 2
/
grammar.c
160 lines (143 loc) · 3.42 KB
/
grammar.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
BATCH NO. 27
Mayank Agarwal (2014A7PS111P)
Karan Deep Batra(2014A7PS160P)
*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include "ntort.h"
#include "hashtable.h"
#include "grammar.h"
char buff[500];
node* makenode()
{
node* temp = (node*)malloc(sizeof(node));
temp->next = NULL;
temp->firstntort = NULL;
temp->lastntort = NULL;
return temp;
}
void populateGrammar(hashtable* table)
{
FILE* fp = fopen("newGrammar.txt","rb");
insert(table, "$");
for(int i=0; i<maxnonterminals; i++)
{
char lhsNT[100];
fscanf(fp,"%s",buff); //scanning LHS nonterminal
strcpy(lhsNT, buff);
if(!present(table, buff))
insert(table, buff);
int ind = present(table, buff);
fscanf(fp, "%s", buff); //scanning -->
fscanf(fp, "%s", buff); //first RHS non terminal or terminal
grammar[ind] = makenode();
node* curr = grammar[ind];
while(buff[0] != '$')
{
if(buff[0] == '|')
{
// insert $
curr->lastntort->next = makentortnode(buff[0]=='<' ? 1 : 0, present(table,"$"), "$");
curr->lastntort = curr->lastntort->next;
curr->lastntort->next = makentortnode(1, present(table, lhsNT), lhsNT);
curr->lastntort = curr->lastntort->next;
curr->next = makenode();
curr = curr->next;
}
else
{
if(!present(table, buff))
insert(table, buff);
int rhsind = present(table, buff);
if(curr->firstntort == NULL)
{
curr->firstntort = makentortnode(buff[0]=='<' ? 1 : 0, rhsind, buff);
curr->lastntort = curr->firstntort;
}
else
{
curr->lastntort->next = makentortnode(buff[0]=='<' ? 1 : 0, rhsind, buff);
curr->lastntort = curr->lastntort->next;
}
if(buff[0] == '<')
{
if(toppointers[rhsind].firstntort == NULL)
{
toppointers[rhsind].firstntort = curr->lastntort;
toppointers[rhsind].lastntort = curr->lastntort;
}
else
{
(toppointers[rhsind].lastntort)->down = curr->lastntort;
toppointers[rhsind].lastntort = (toppointers[rhsind].lastntort)->down;
}
}
}
fscanf(fp, "%s", buff);
}
// insert $
curr->lastntort->next = makentortnode(buff[0]=='<' ? 1 : 0, present(table,"$"), "$");
curr->lastntort = curr->lastntort->next;
curr->lastntort->next = makentortnode(1, present(table, lhsNT), lhsNT);
curr->lastntort = curr->lastntort->next;
}
// printhashtable(table);
fclose(fp);
return;
}
void printTopDownGrammar(hashtable* table) //printing grammar down pointers
{
FILE* fp = fopen("nonterminals.txt", "rb");
for(int i=0; i<maxnonterminals; i++)
{
fscanf(fp,"%s",buff);
int ind = present(table, buff);
printf("%s --> ", buff);
if(ind == 0)
{
printf("########ERROR######### %s\n", buff);
return;
}
node curr = toppointers[ind];
ntort* first = curr.firstntort;
while(first != NULL)
{
printf(" %s ",first->next->str);
first = first->down;
}
printf("\n");
}
return ;
}
void printGrammar(hashtable* table) //printing grammar
{
FILE* fp = fopen("nonterminals.txt", "rb");
for(int i=0; i<maxnonterminals; i++)
{
fscanf(fp,"%s",buff);
int ind = present(table, buff);
printf("%s --> ", buff);
if(ind == 0)
{
printf("########ERROR######### %s\n", buff);
return;
}
node* curr = grammar[ind];
while(curr != NULL)
{
ntort* first = curr->firstntort;
while(first != NULL)
{
printf(" %s ",first->str);
first = first->next;
}
printf("|");
curr = curr->next;
}
printf("\n");
}
return ;
}