-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPanic_mode.cpp
More file actions
executable file
·99 lines (89 loc) · 2.51 KB
/
Copy pathPanic_mode.cpp
File metadata and controls
executable file
·99 lines (89 loc) · 2.51 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
#include "Panic_mode.h"
#include "recource.h"
bool isNonTerminal(string str)
{
std::vector<string>::iterator it;
it = std::find(terminals.begin(), terminals.end(), str);
if (it != terminals.end())
{
return false;
}
else
{
return true;
}
}
void parser_panic_mode(string start_NonTerminal, vector<string> input)
{
ofstream myfile;
myfile.open ("output.txt");
stack<string> stck;
stck.push("$");
input.push_back("$");
stck.push(start_NonTerminal);
int input_index = 0;
while (!stck.empty())
{
//top of stack
string X = stck.top();
//element in input
string a = input[input_index];
// cout << X << endl;
//check if top of stack is terminal or non terminal
if (isNonTerminal(X))
{
auto table_row = parse_table[X];
if (table_row.count(a))
{
if (table_row[a].size() == 1 && table_row[a].back() == "synch")
{
myfile << "Error here : skip " << stck.top() << endl;
stck.pop();
}
else if (table_row[a].size() == 1 && table_row[a].back() == "^")
{
myfile << X <<" -> epsilon "<< endl;
stck.pop();
}
else
{
vector<string> production = table_row[a];
stck.pop();
int j=0;
myfile <<X<<" -> ";
for (int i = production.size() - 1; i >= 0; i--)
{
myfile <<production[j++]<<" ";
stck.push(production[i]);
}
myfile<<endl;
}
}
else
{
myfile << "Error here : (Illegal " << stck.top() << ") - discard " << input[input_index] << endl;
input_index++;
}
}
else
{
if (X == a)
{
stck.pop();
input_index++;
if (X == "$" && a == "$")
{
myfile << "accept" << endl;
}else{
myfile<<"Match "<<a<<endl;
}
}
else
{
myfile << "Error here: missing " << stck.top() << ", inserted" << endl;
stck.pop();
}
}
}
myfile.close();
}