forked from batman-nair/LL-1-Parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.cpp
360 lines (304 loc) · 8.9 KB
/
parser.cpp
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#include <iostream>
#include <fstream>
#include <vector>
#include <set>
#include <map>
#include <stack>
using namespace std;
void find_first(vector< pair<char, string> > gram,
map< char, set<char> > &firsts,
char non_term);
void find_follow(vector< pair<char, string> > gram,
map< char, set<char> > &follows,
map< char, set<char> > firsts,
char non_term);
int main(int argc, char const *argv[])
{
if(argc != 3) {
cout<<"Arguments should be <grammar file> <input string>\n";
return 1;
}
// Arguments check
// cout<<argv[1]<<argv[2];
// Parsing the grammar file
fstream grammar_file;
grammar_file.open(argv[1], ios::in);
if(grammar_file.fail()) {
cout<<"Error in opening grammar file\n";
return 2;
}
cout<<"Grammar parsed from grammar file: \n";
vector< pair<char, string> > gram;
int count = 0;
while(!grammar_file.eof()) {
char buffer[20];
grammar_file.getline(buffer, 19);
char lhs = buffer[0];
string rhs = buffer+3;
pair <char, string> prod (lhs, rhs);
gram.push_back(prod);
cout<<count++<<". "<<gram.back().first<<" -> "<<gram.back().second<<"\n";
}
cout<<"\n";
// Gather all non terminals
set<char> non_terms;
for(auto i = gram.begin(); i != gram.end(); ++i) {
non_terms.insert(i->first);
}
cout<<"The non terminals in the grammar are: ";
for(auto i = non_terms.begin(); i != non_terms.end(); ++i) {
cout<<*i<<" ";
}
cout<<"\n";
// Gather all terminals
set<char> terms;
for(auto i = gram.begin(); i != gram.end(); ++i) {
for(auto ch = i->second.begin(); ch != i->second.end(); ++ch) {
if(!isupper(*ch)) {
terms.insert(*ch);
}
}
}
// Remove epsilon and add end character $
terms.erase('e');
terms.insert('$');
cout<<"The terminals in the grammar are: ";
for(auto i = terms.begin(); i != terms.end(); ++i) {
cout<<*i<<" ";
}
cout<<"\n\n";
// Start symbol is first non terminal production in grammar
char start_sym = gram.begin()->first;
map< char, set<char> > firsts;
for(auto non_term = non_terms.begin(); non_term != non_terms.end(); ++non_term) {
if(firsts[*non_term].empty()){
find_first(gram, firsts, *non_term);
}
}
cout<<"Firsts list: \n";
for(auto it = firsts.begin(); it != firsts.end(); ++it) {
cout<<it->first<<" : ";
for(auto firsts_it = it->second.begin(); firsts_it != it->second.end(); ++firsts_it) {
cout<<*firsts_it<<" ";
}
cout<<"\n";
}
cout<<"\n";
map< char, set<char> > follows;
// Find follow of start variable first
char start_var = gram.begin()->first;
follows[start_var].insert('$');
find_follow(gram, follows, firsts, start_var);
// Find follows for rest of variables
for(auto it = non_terms.begin(); it != non_terms.end(); ++it) {
if(follows[*it].empty()) {
find_follow(gram, follows, firsts, *it);
}
}
cout<<"Follows list: \n";
for(auto it = follows.begin(); it != follows.end(); ++it) {
cout<<it->first<<" : ";
for(auto follows_it = it->second.begin(); follows_it != it->second.end(); ++follows_it) {
cout<<*follows_it<<" ";
}
cout<<"\n";
}
cout<<"\n";
int parse_table[non_terms.size()][terms.size()];
fill(&parse_table[0][0], &parse_table[0][0] + sizeof(parse_table)/sizeof(parse_table[0][0]), -1);
for(auto prod = gram.begin(); prod != gram.end(); ++prod) {
string rhs = prod->second;
set<char> next_list;
bool finished = false;
for(auto ch = rhs.begin(); ch != rhs.end(); ++ch) {
if(!isupper(*ch)) {
if(*ch != 'e') {
next_list.insert(*ch);
finished = true;
break;
}
continue;
}
set<char> firsts_copy(firsts[*ch].begin(), firsts[*ch].end());
if(firsts_copy.find('e') == firsts_copy.end()) {
next_list.insert(firsts_copy.begin(), firsts_copy.end());
finished = true;
break;
}
firsts_copy.erase('e');
next_list.insert(firsts_copy.begin(), firsts_copy.end());
}
// If the whole rhs can be skipped through epsilon or reaching the end
// Add follow to next list
if(!finished) {
next_list.insert(follows[prod->first].begin(), follows[prod->first].end());
}
for(auto ch = next_list.begin(); ch != next_list.end(); ++ch) {
int row = distance(non_terms.begin(), non_terms.find(prod->first));
int col = distance(terms.begin(), terms.find(*ch));
int prod_num = distance(gram.begin(), prod);
if(parse_table[row][col] != -1) {
cout<<"Collision at ["<<row<<"]["<<col<<"] for production "<<prod_num<<"\n";
continue;
}
parse_table[row][col] = prod_num;
}
}
// Print parse table
cout<<"Parsing Table: \n";
cout<<" ";
for(auto i = terms.begin(); i != terms.end(); ++i) {
cout<<*i<<" ";
}
cout<<"\n";
for(auto row = non_terms.begin(); row != non_terms.end(); ++row) {
cout<<*row<<" ";
for(int col = 0; col < terms.size(); ++col) {
int row_num = distance(non_terms.begin(), row);
if(parse_table[row_num][col] == -1) {
cout<<"- ";
continue;
}
cout<<parse_table[row_num][col]<<" ";
}
cout<<"\n";
}
cout<<"\n";
string input_string(argv[2]);
input_string.push_back('$');
stack<char> st;
st.push('$');
st.push('S');
// Check if input string is valid
for(auto ch = input_string.begin(); ch != input_string.end(); ++ch) {
if(terms.find(*ch) == terms.end()) {
cout<<"Input string is invalid\n";
return 2;
}
}
// cout<<"Processing input string\n";
bool accepted = true;
while(!st.empty() && !input_string.empty()) {
// If stack top same as input string char remove it
if(input_string[0] == st.top()) {
st.pop();
input_string.erase(0, 1);
}
else if(!isupper(st.top())) {
cout<<"Unmatched terminal found\n";
accepted = false;
break;
}
else {
char stack_top = st.top();
int row = distance(non_terms.begin(), non_terms.find(stack_top));
int col = distance(terms.begin(), terms.find(input_string[0]));
int prod_num = parse_table[row][col];
if(prod_num == -1) {
cout<<"No production found in parse table\n";
accepted = false;
break;
}
st.pop();
string rhs = gram[prod_num].second;
if(rhs[0] == 'e') {
continue;
}
for(auto ch = rhs.rbegin(); ch != rhs.rend(); ++ch) {
st.push(*ch);
}
}
}
if(accepted) {
cout<<"Input string is accepted\n";
}
else {
cout<<"Input string is rejected\n";
}
return 0;
}
void find_first(vector< pair<char, string> > gram,
map< char, set<char> > &firsts,
char non_term) {
// cout<<"Finding firsts of "<<non_term<<"\n";
for(auto it = gram.begin(); it != gram.end(); ++it) {
// Find productions of the non terminal
if(it->first != non_term) {
continue;
}
// cout<<"Processing production "<<it->first<<"->"<<it->second<<"\n";
string rhs = it->second;
// Loop till a non terminal or no epsilon variable found
for(auto ch = rhs.begin(); ch != rhs.end(); ++ch) {
// If first char in production a non term, add it to firsts list
if(!isupper(*ch)) {
firsts[non_term].insert(*ch);
break;
}
else {
// If char in prod is non terminal and whose firsts has no yet been found out
// Find first for that non terminal
if(firsts[*ch].empty()) {
find_first(gram, firsts, *ch);
}
// If variable doesn't have epsilon, stop loop
if(firsts[*ch].find('e') == firsts[*ch].end()) {
firsts[non_term].insert(firsts[*ch].begin(), firsts[*ch].end());
break;
}
set<char> firsts_copy(firsts[*ch].begin(), firsts[*ch].end());
// Remove epsilon from firsts if not the last variable
if(ch + 1 != rhs.end()) {
firsts_copy.erase('e');
}
// Append firsts of that variable
firsts[non_term].insert(firsts_copy.begin(), firsts_copy.end());
}
}
}
}
void find_follow(vector< pair<char, string> > gram,
map< char, set<char> > &follows,
map< char, set<char> > firsts,
char non_term) {
// cout<<"Finding follow of "<<non_term<<"\n";
for(auto it = gram.begin(); it != gram.end(); ++it) {
// finished is true when finding follow from this production is complete
bool finished = true;
auto ch = it->second.begin();
// Skip variables till reqd non terminal
for(;ch != it->second.end() ; ++ch) {
if(*ch == non_term) {
finished = false;
break;
}
}
++ch;
for(;ch != it->second.end() && !finished; ++ch) {
// If non terminal, just append to follow
if(!isupper(*ch)) {
follows[non_term].insert(*ch);
finished = true;
break;
}
set<char> firsts_copy(firsts[*ch]);
// If char's firsts doesnt have epsilon follow search is over
if(firsts_copy.find('e') == firsts_copy.end()) {
follows[non_term].insert(firsts_copy.begin(), firsts_copy.end());
finished = true;
break;
}
// Else next char has to be checked after appending firsts to follow
firsts_copy.erase('e');
follows[non_term].insert(firsts_copy.begin(), firsts_copy.end());
}
// If end of production, follow same as follow of variable
if(ch == it->second.end() && !finished) {
// Find follow if it doesn't have
if(follows[it->first].empty()) {
find_follow(gram, follows, firsts, it->first);
}
follows[non_term].insert(follows[it->first].begin(), follows[it->first].end());
}
}
}