-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.cpp
More file actions
491 lines (426 loc) · 13 KB
/
Copy pathparser.cpp
File metadata and controls
491 lines (426 loc) · 13 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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
#include <string.h>
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
#define REJECT 22
ifstream input;
ofstream output;
bool ended;
bool show_steps = 0;
bool valid(vector<string> v, string key)
{
return (find(v.begin(), v.end(), key) != v.end());
}
void clear_whitespaces(string &s)
{
s.erase(remove_if(s.begin(), s.end(), ::isspace), s.end());
}
map<string, int> state_list = {
{ "0", 0 }, { "1", 1 }, { "2", 2 }, { "3", 3 },
{ "4", 4 }, { "5", 5 }, { "6", 6 }, { "7", 7 },
{ "8", 8 }, { "9", 9 }, {"10", 10}, {"11", 11},
{"12", 12}, {"13", 13}, {"14", 14}, {"15", 15},
{"16", 16}, {"17", 17}, {"18", 18}, {"19", 19},
{"20", 20}, {"21", 21}, {"reject", 22}
};
vector<string> valid_tokens = {"num", "id", "+", "-", "*", "/", "=", "(", ")"};
int accept()
{
cout << "ACCEPTED\n\n";
ended = true;
return 0;
}
string action_go_to(string state, string prod)
{
if(state == "0")
{
if(prod == "E") return "2";
if(prod == "F") return "3";
if(prod == "S") return "4";
if(prod == "T") return "5";
if(prod == "V") return "6";
}
if(state == "1")
{
if(prod == "E") return "9";
if(prod == "F") return "3";
if(prod == "T") return "5";
if(prod == "V") return "10";
}
if(state == "11")
{
if(prod == "F") return "3";
if(prod == "T") return "17";
if(prod == "V") return "10";
}
if(state == "12")
{
if(prod == "F") return "3";
if(prod == "T") return "18";
if(prod == "V") return "10";
}
if(state == "13")
{
if(prod == "F") return "19";
if(prod == "V") return "10";
}
if(state == "14")
{
if(prod == "F") return "20";
if(prod == "V") return "10";
}
if(state == "15")
{
if(prod == "E") return "21";
if(prod == "F") return "3";
if(prod == "T") return "5";
if(prod == "V") return "10";
}
if(show_steps) cout << "impossible combination of production and state\n";
return "reject";
}
int go_to(stack<string> &s)
{
string production = s.top();
s.pop();
string state = s.top();
s.push(production);
s.push(action_go_to(state, production));
if(show_steps) cout << "go to " << s.top() << endl;
return state_list[s.top()];
}
int reduce(stack<string> &s, int production)
{
string top;
if(show_steps) cout << "reduce by production ";
switch(production)
{
case 1:
if(show_steps) cout << "01 [S -> E]\n";
s.pop(); //pop state
s.pop(); //pop E
s.push("S");
break;
case 2:
if(show_steps) cout << "02 [S -> V=E]\n";
do { top = s.top(), s.pop(); } while(top != "V");
s.push("S");
break;
case 3:
if(show_steps) cout << "03 [E -> E+T]\n";
do { top = s.top(), s.pop(); } while(top != "E");
s.push("E");
break;
case 4:
if(show_steps) cout << "04 [E -> E-T]\n";
do { top = s.top(), s.pop(); } while(top != "E");
s.push("E");
break;
case 5:
if(show_steps) cout << "05 [E -> T]\n";
s.pop(); //pop state
s.pop(); //pop T
s.push("E");
break;
case 6:
if(show_steps) cout << "06 [T -> T*F]\n";
do { top = s.top(), s.pop(); } while(top != "T");
s.push("T");
break;
case 7:
if(show_steps) cout << "07 [T -> T/F]\n";
do { top = s.top(), s.pop(); } while(top != "T");
s.push("T");
break;
case 8:
if(show_steps) cout << "08 [T -> F]\n";
s.pop(); //pop state
s.pop(); //pop F
s.push("T");
break;
case 9:
if(show_steps) cout << "09 [F -> (E)]\n";
do { top = s.top(), s.pop(); } while(top != "(");
s.push("F");
break;
case 10:
if(show_steps) cout << "10 [F -> V]\n";
s.pop(); //pop state
s.pop(); //pop V
s.push("F");
break;
case 11:
if(show_steps) cout << "11 [F -> num]\n";
s.pop(); //pop state
s.pop(); //pop num
s.push("F");
break;
case 12:
if(show_steps) cout << "12 [V -> id]\n";
s.pop(); //pop state
s.pop(); //pop id
s.push("V");
break;
}
return go_to(s);
}
int shift(stack<string> &s, queue<string> &str, string state)
{
s.push(str.front());
s.push(state);
str.pop();
if(show_steps) cout << "shift to " << s.top() << endl;
return state_list[s.top()];
}
int q00(stack<string> &s, queue<string> &str)
{
if(str.front() == "(") return shift(s, str, "1");
if(str.front() == "id") return shift(s, str, "7");
if(str.front() == "num") return shift(s, str, "8");
return REJECT;
}
int q01(stack<string> &s, queue<string> &str)
{
if(str.front() == "(") return shift(s, str, "1");
if(str.front() == "id") return shift(s, str, "7");
if(str.front() == "num") return shift(s, str, "8");
return REJECT;
}
int q02(stack<string> &s, queue<string> &str)
{
if(str.front() == "+") return shift(s, str, "11");
if(str.front() == "-") return shift(s, str, "12");
if(str.front() == "$") return reduce(s, 1);
return REJECT;
}
int q03(stack<string> &s, queue<string> &str)
{
if(str.front() == ")") return reduce(s, 8);
if(str.front() == "+") return reduce(s, 8);
if(str.front() == "-") return reduce(s, 8);
if(str.front() == "*") return reduce(s, 8);
if(str.front() == "/") return reduce(s, 8);
if(str.front() == "$") return reduce(s, 8);
return REJECT;
}
int q04(stack<string> &s, queue<string> &str)
{
if(str.front() == "$") return accept();
return REJECT;
}
int q05(stack<string> &s, queue<string> &str)
{
if(str.front() == ")") return reduce(s, 5);
if(str.front() == "+") return reduce(s, 5);
if(str.front() == "-") return reduce(s, 5);
if(str.front() == "*") return shift(s, str, "13");
if(str.front() == "/") return shift(s, str, "14");
if(str.front() == "$") return reduce(s, 5);
return REJECT;
}
int q06(stack<string> &s, queue<string> &str)
{
if(str.front() == ")") return reduce(s, 10);
if(str.front() == "+") return reduce(s, 10);
if(str.front() == "-") return reduce(s, 10);
if(str.front() == "*") return reduce(s, 10);
if(str.front() == "/") return reduce(s, 10);
if(str.front() == "=") return shift(s, str, "15");
if(str.front() == "$") return reduce(s, 10);
return REJECT;
}
int q07(stack<string> &s, queue<string> &str)
{
if(str.front() == ")") return reduce(s, 12);
if(str.front() == "+") return reduce(s, 12);
if(str.front() == "-") return reduce(s, 12);
if(str.front() == "*") return reduce(s, 12);
if(str.front() == "/") return reduce(s, 12);
if(str.front() == "=") return reduce(s, 12);
if(str.front() == "$") return reduce(s, 12);
return REJECT;
}
int q08(stack<string> &s, queue<string> &str)
{
if(str.front() == ")") return reduce(s, 11);
if(str.front() == "+") return reduce(s, 11);
if(str.front() == "-") return reduce(s, 11);
if(str.front() == "*") return reduce(s, 11);
if(str.front() == "/") return reduce(s, 11);
if(str.front() == "$") return reduce(s, 11);
return REJECT;
}
int q09(stack<string> &s, queue<string> &str)
{
if(str.front() == ")") return shift(s, str, "16");
if(str.front() == "+") return shift(s, str, "11");
if(str.front() == "-") return shift(s, str, "12");
return REJECT;
}
int q10(stack<string> &s, queue<string> &str)
{
if(str.front() == ")") return reduce(s, 10);
if(str.front() == "+") return reduce(s, 10);
if(str.front() == "-") return reduce(s, 10);
if(str.front() == "*") return reduce(s, 10);
if(str.front() == "/") return reduce(s, 10);
if(str.front() == "$") return reduce(s, 10);
return REJECT;
}
int q11(stack<string> &s, queue<string> &str)
{
if(str.front() == "(") return shift(s, str, "1");
if(str.front() == "id") return shift(s, str, "7");
if(str.front() == "num") return shift(s, str, "8");
return REJECT;
}
int q12(stack<string> &s, queue<string> &str)
{
if(str.front() == "(") return shift(s, str, "1");
if(str.front() == "id") return shift(s, str, "7");
if(str.front() == "num") return shift(s, str, "8");
return REJECT;
}
int q13(stack<string> &s, queue<string> &str)
{
if(str.front() == "(") return shift(s, str, "1");
if(str.front() == "id") return shift(s, str, "7");
if(str.front() == "num") return shift(s, str, "8");
return REJECT;
}
int q14(stack<string> &s, queue<string> &str)
{
if(str.front() == "(") return shift(s, str, "1");
if(str.front() == "id") return shift(s, str, "7");
if(str.front() == "num") return shift(s, str, "8");
return REJECT;
}
int q15(stack<string> &s, queue<string> &str)
{
if(str.front() == "(") return shift(s, str, "1");
if(str.front() == "id") return shift(s, str, "7");
if(str.front() == "num") return shift(s, str, "8");
return REJECT;
}
int q16(stack<string> &s, queue<string> &str)
{
if(str.front() == ")") return reduce(s, 9);
if(str.front() == "+") return reduce(s, 9);
if(str.front() == "-") return reduce(s, 9);
if(str.front() == "*") return reduce(s, 9);
if(str.front() == "/") return reduce(s, 9);
if(str.front() == "$") return reduce(s, 9);
return REJECT;
}
int q17(stack<string> &s, queue<string> &str)
{
if(str.front() == ")") return reduce(s, 3);
if(str.front() == "+") return reduce(s, 3);
if(str.front() == "-") return reduce(s, 3);
if(str.front() == "*") return shift(s, str, "13");
if(str.front() == "/") return shift(s, str, "14");
if(str.front() == "$") return reduce(s, 3);
return REJECT;
}
int q18(stack<string> &s, queue<string> &str)
{
if(str.front() == ")") return reduce(s, 4);
if(str.front() == "+") return reduce(s, 4);
if(str.front() == "-") return reduce(s, 4);
if(str.front() == "*") return shift(s, str, "13");
if(str.front() == "/") return shift(s, str, "14");
if(str.front() == "$") return reduce(s, 4);
return REJECT;
}
int q19(stack<string> &s, queue<string> &str)
{
if(str.front() == ")") return reduce(s, 6);
if(str.front() == "+") return reduce(s, 6);
if(str.front() == "-") return reduce(s, 6);
if(str.front() == "*") return reduce(s, 6);
if(str.front() == "/") return reduce(s, 6);
if(str.front() == "$") return reduce(s, 6);
return REJECT;
}
int q20(stack<string> &s, queue<string> &str)
{
if(str.front() == ")") return reduce(s, 7);
if(str.front() == "+") return reduce(s, 7);
if(str.front() == "-") return reduce(s, 7);
if(str.front() == "*") return reduce(s, 7);
if(str.front() == "/") return reduce(s, 7);
if(str.front() == "$") return reduce(s, 7);
return REJECT;
}
int q21(stack<string> &s, queue<string> &str)
{
if(str.front() == "+") return shift(s, str, "11");
if(str.front() == "-") return shift(s, str, "12");
if(str.front() == "$") return reduce(s, 2);
return REJECT;
}
int reject(stack<string> &s, queue<string> &str)
{
cout << "REJECTED\n\n";
ended = true;
return REJECT;
}
typedef int(*next_state)(stack<string> &s, queue<string> &str);
int main(int argc, char *argv[])
{
if(argc < 2)
{
cout << "Use: <input_file>\n";
exit(0);
}
input.open(argv[1]);
if(!input.good())
{
cout << "Error opening input file.\n";
exit(0);
}
char q;
cout << "display steps? (y/n) ";
cin >> q;
if(q == 'y' || q == 'Y') show_steps = 1;
else show_steps = 0;
cout << endl;
stringstream strstream;
strstream << input.rdbuf();
string in_string = strstream.str();
clear_whitespaces(in_string);
string buff;
queue<string> str;
string::iterator it = in_string.begin();
while(it != in_string.end())
{
buff += *it++;
if(valid(valid_tokens, buff))
{
str.push(buff);
buff.clear();
}
}
str.push("$");
stack<string> s;
s.push("$");
s.push("0");
next_state delta[] = {
&q00, &q01, &q02, &q03, &q04, &q05, &q06, &q07,
&q08, &q09, &q10, &q11, &q12, &q13, &q14, &q15,
&q16, &q17, &q18, &q19, &q20, &q21, &reject
};
ended = false;
int cur_state = state_list[s.top()];
while(!ended)
{
cur_state = (*delta[cur_state])(s, str);
}
}