-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtop_down_parser.cpp
More file actions
370 lines (340 loc) · 11.3 KB
/
top_down_parser.cpp
File metadata and controls
370 lines (340 loc) · 11.3 KB
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
361
362
363
364
365
366
367
368
369
370
#include<bits/stdc++.h>
using namespace std;
unordered_map<string, vector<string>> prods;
unordered_map<string, set<string>> first;
unordered_map<string, set<string>> follow;
unordered_map<string, set<string>> rprod_lprod;
unordered_map<string , vector<pair<string,string>>> parsing_table;
unordered_map<string , vector<pair<string,string>>> parsing_table_lprod;
map<char,int> terminals;
char lambda = '$';
set<string> firstof(string leftprod){
if(first[leftprod].size() != 0) return first[leftprod];
for(auto x : prods[leftprod]){
//cout << "left prod: " << leftprod << " x: " << x << "\n";
if(x.size() == 1 && x[0] == lambda) first[leftprod].insert("$");
if(terminals.find(x[0]) == terminals.end() && x[0] != lambda){
//cout << x[0] << "<- non-terminal found" << "\n";
string c = "";c += x[0];
set<string> st = firstof(c);
for(auto as : st){
if(as == "$"){
for(int i = 1; i < (int)x.size() ; i++){
if(terminals.find(x[i]) == terminals.end()){
//cout << x[i] << "<- non-terminal found" << "\n";
string c = "";c += x[i];;
set<string> st = firstof(c);
for(auto as : st)
first[leftprod].insert(as);
}
else{
// cout << x[0] << "<- terminal found" << "\n";
string a = ""; a += x[0];
first[leftprod].insert(a);
}
}
}
else{
first[leftprod].insert(as);
}
}
}
else if(x[0] == lambda){
if(x.size() == 1) first[leftprod].insert("$");
else{
for(int i = 1 ; i < (int)x.size() ; i++){
// cout << leftprod << " " << x[i] << "<----\n";
if(terminals.find(x[i]) == terminals.end()){
// cout << x[i] << "<- non-terminal found" << "\n";
string c = "";c += x[i];;
set<string> st = firstof(c);
for(auto as : st)
first[leftprod].insert(as);
}
else{
//cout << x[0] << "<- terminal found" << "\n";
string a = ""; a += x[0];
first[leftprod].insert(a);
}
}
}
}
else{
// cout << x[0] << "<- terminal found" << "\n";
string a = ""; a += x[0];
first[leftprod].insert(a);
}
}
return first[leftprod];
}
int main(){
vector<string> lambda_prod;
vector<string> r_prod_check;
int n_prod;cout << "enter no productions" << "\n";cin >> n_prod;
vector<string> prod_strings;
cout << "enter the productions" << "\n";
for(int i = 0 ; i < n_prod ; i++){
string bs;cin >> bs;
prod_strings.push_back(bs);
}
cout << "enter the number of states: " << "\n";
int n;
cin >> n;
cout << "now enter the states: " << "\n";
map<char,int> states;
for(int i = 0 ; i < n ; i++){
char c;
cin >> c;
states[c]++;
}
cout << "enter the number of terminals" << "\n";
int n1;
cin >> n1;
cout << "now enter the terminals" << "\n";
for(int i = 0 ; i < n1 ; i++){
char c; cin >> c;
terminals[c]++;
}
cout << "enter the start symbol" << "\n";
string start_sy;
cin >> start_sy;
follow[start_sy].insert("$");
for(auto prod : prod_strings){
string left = "";
string right = "";
vector<string> rprod;
int i;
for(i = 0 ; prod[i] != '-' ; i++ ){
left += prod[i];
}
i+=2;
for(; i < (int)prod.size() ; i++){
if(prod[i] == '|'){
//cout << right << "\n";
rprod.push_back(right);
right = "";
}
else{
right += prod[i];
}
}
rprod.push_back(right);
//cout << left << "<- left production right production-> ";
for(auto x : rprod){
//cout << x << " ";
r_prod_check.push_back(x);
prods[left] = rprod;
for(auto y : x){
string sss = "";sss += y;
rprod_lprod[sss].insert(left);
if(y == lambda){
lambda_prod.push_back(left);
}
}
}
//cout << "\n";
}
cout << "\n\n";
//check right sub string
for(auto x : prods){
// cout << "called for " << x.first << "\n";
firstof(x.first);
}
/*
cout << "checking left prods " << "\n";
for(auto x : rprod_lprod) cout << x.first << " " << x.second << "\n";
cout << "*************\n";
*/
int nn = 5;
while(nn--){
for(auto x : r_prod_check){
for(int i = 0 ; i < (int)x.size() ;i++){
string xi = "";xi += x[i];
//cout<< "analysing: " << xi << "\n";
if(terminals.find(x[i]) == terminals.end() && x[i] != lambda){
if(i == (int)x.size()-1){
for(auto it : rprod_lprod[xi]){
//cout << "b entered here: " << xi <<" left prod = "<< it << "\n";
for(auto y : follow[it]){
string nextfollow = "";nextfollow += y;
follow[xi].insert(nextfollow);
}
}
}
else{
string nextxi = "";nextxi += x[i+1];
if(terminals.find(x[i+1]) != terminals.end()) {
follow[xi].insert(nextxi);
continue;
}
bool flagz = false;
for(auto y : first[nextxi]){
string nextfollow = "";nextfollow += y;
follow[xi].insert(nextfollow);
if(nextfollow == "$"){
flagz = true;
for(auto yy : follow[nextxi]){
string spt = "";spt += yy;
follow[xi].insert(spt);
}
}
}
}
}
}
}
}
/*
cout << "inside rprod_lprod" << "\n";
for(auto x : rprod_lprod){
cout << x.first << " ";
d for(auto y : x.second){
cout << y << " " ;
}
cout << "\n";
}
cout << "*****************\n";
*/
/*cout << "state" << " " << "first" << "\n";
for(auto x : first){
cout << x.first << " ";
for(auto y : x.second) cout << y << " ";
cout << "\n";
}
cout << "state" << " " << "follow" << "\n";
for(auto x : follow){
cout << x.first << " ";
for(auto y : x.second) cout << y << " ";
cout << "\n";
}
for(auto x : prods){
cout << x.first << " : ";
for(auto y : x.second) cout << y << " " ;
cout << "\n";
}*/
cout << "Non-terminal\t" << "first\t" << "follow\t" << "\n";
for(auto x : prods){
cout << x.first << "\t\t";
for(auto y : first[x.first]){
for(auto xx : y)
cout << xx << " ";
}cout << "\t";
for(auto y : follow[x.first])
for(auto xx : y)
cout << xx << " ";
cout << "\n";
}
cout << "\n\n";
for(auto x: prods){
//cout << "analysing: " << x.first << " ";
for(auto y : x.second){
//cout << y << "\n";
vector<pair<string,string>> v;
string sss = "";sss += y[0];
if(y.size() == 1 && y[0] == lambda){
// cout << "lambda production" << "\n";
for(auto x2 : follow[x.first]){
//cout << x2 << " ";
v.push_back({x.first,x2});
}
//cout << "\n";
}
else if(terminals.find(y[0]) != terminals.end()){
// cout << sss << "<-- terminal in right's first" << "\n";
v.push_back({x.first,sss});
}
else{
//cout << "activated" << "\n";
for(int i = 0 ; i < (int)y.size() ; i++){
string s = "";s += y[i];
bool flag = false;
// cout << y[i] << "<---->" << s << "\n";
for(auto xx : first[s]){
// cout << x.first <<"*************hello********* " << xx << "<--" << "\n";
if(xx[0] == lambda){
flag = true;
}
else{
// cout << xx << " ";
v.push_back({x.first,xx});
}
// cout << "\n";
}
if(!flag) break;
}
}
string lef = "";
lef = lef + x.first +"->"+y;
string pp = y;
for(auto xy : v){
//if(xy.second != "$")
parsing_table_lprod[pp].push_back(xy);
parsing_table[lef].push_back(xy);
}
}
}
// cout <<
cout << "\n";
cout << "parse table: - " << "\n";
for(auto x : parsing_table){
cout << x.first << "\t:\t";
for(auto xx : x.second){
cout <<" { " << xx.first << ","<< xx.second << " }\t";
}
cout << "\n";
}
//}
cout << "enter the string to parse" << "\n";
string input ;
cin >> input;
input += "$";
stack<char> stac;
stac.push('$');
stac.push(start_sy[0]);
int i = 0;
while(stac.top() != '$' || !stac.empty()){
cout << "stack top " << stac.top() << "\n";
for(auto x : parsing_table_lprod){
bool flag = false;
for(auto y : x.second){
if(y.first[0] == stac.top() && y.second[0] == input[i]){
cout << stac.top() << "<-stack top = character on tape-> " << input[i] << " so pop() and pushing:-\n";
stac.pop();
cout << "-->" << x.first << " : " ;
vector<char> temp;
for(auto xx : x.first){
//cout << xx << " ";
temp.push_back(xx);
}
if(temp.size() == 1 && temp[0] == lambda){
cout << "lambda production so only popping" << "\n";;
}
else{
reverse(temp.begin(), temp.end());
for(auto xx : temp){
cout << xx << " ";
stac.push(xx);
}
cout << "\n";
flag = true;
}
break;
}
else if(stac.top() == input[i] && input[i] != '$'){
stac.pop();i++;
cout << "stack top terminal matches input tape terminal so pop" << "\n";
flag = true;
break;
}
else if(input[i] == '$'){
cout << "top of stack is $ and input tape's character is $ so:-" << "\n";
cout << "parsed" << "\n";
flag = true;
return 0;
break;
}
}
if(flag) break;
}
}
}