-
Notifications
You must be signed in to change notification settings - Fork 0
/
table.cpp
111 lines (106 loc) · 3.1 KB
/
table.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
#include "table.h"
#include <iomanip>
#include <fstream>
using namespace std;
table::table(vector<item*>a,vector<string>T,vector<string>N,vector<rule*>origin) {
for (int i = 0; i < a.size(); i++) {
state.push_back(a[i]->numm);
}
for (int i = 0; i < T.size(); i++) {
action.push_back(T[i]);
}
for (int i = 0; i < N.size(); i++) {
Goto.push_back(N[i]);
}
//for (int i = 0; i < state.size(); i++) {
// for (int j = 0; j < action.size(); j++)
// f[{state[i], action[j]}].push_back(" ");
// for(int j=0;j<Goto.size();j++)
// f[{state[i], Goto[j]}].push_back(" ");
//}
//添加转移
for (int i = 0; i < a.size(); i++) {
for (auto j : a[i]->mp) {
if ((j.first.second >= "A"&& j.first.second <= "Z")|| (j.first.second >= "0" && j.first.second <= "6")) {//要改,改成判断是终结符还是非终结符
//f[{a[i]->numm, j.first.second}].pop_back();
f[{a[i]->numm, j.first.second}].push_back(to_string(j.second->numm));
}
else {
//f[{a[i]->numm, j.first.second}].pop_back();
f[{a[i]->numm, j.first.second}].push_back("S" + to_string(j.second->numm));
}
}
}
//添加归约
for (int i = 0; i < a.size(); i++) {
if (a[i]->numm == 1) continue;
for (int j = 0; j < a[i]->I.size(); j++) {
auto it = find(a[i]->I[j]->r_r[0].begin(), a[i]->I[j]->r_r[0].end(), '.');
it++;
if (it == a[i]->I[j]->r_r[0].end()) {//形如A->BC.
string templ = a[i]->I[j]->r_l[0];
string tempr = a[i]->I[j]->r_r[0];
if (tempr==".") {
tempr = "@";
}
else
tempr.pop_back();//变成A->B
rule* temp = new rule(templ, tempr);
for (int k = 0; k < origin.size(); k++) {
if (*origin[k] == *temp) {
int num = origin[k]->numm;
for (int u = 0; u < T.size(); u++) {
f[{a[i]->numm, T[u]}].push_back("R" + to_string(num));
}
}
}
}
}
}
f[{1, "#"}].push_back("ACC");
}
void table::show() {
//cout <<setw(8) << " ";
//for (int j = 0; j < action.size(); j++) {
// cout << action[j] << " ";
//}
//for (int j = 0; j < Goto.size(); j++) {
// cout << Goto[j] << " ";
//}
//cout << endl;
//for (int i = 0; i < state.size(); i++) {
// cout << setw(2) << state[i] << " ";
// for (int j = 0; j < action.size(); j++) {
//
// for (int k = 0; k < f[{state[i], action[j]}].size(); k++) {
// cout << f[{state[i], action[j]}][k] << " ";
// }
// }
// for (int j = 0; j < Goto.size(); j++) {
// for(int k=0;k<f[{state[i], Goto[j]}].size();k++)
// cout << f[{state[i],Goto[j]}][k] << " ";
// }
// cout << endl;
//}
ofstream out("table.csv");
out << ",";
for (int j = 0; j < action.size(); j++) out << action[j] << ",";
for (int j = 0; j < Goto.size(); j++) out << Goto[j] << ",";
out << endl;
for (int i = 0; i < state.size(); i++) {
out << state[i]<<",";
for (int j = 0; j < action.size(); j++) {
for (int k = 0; k < f[{state[i], action[j]}].size(); k++) {
out << f[{state[i], action[j]}][k] << ";";
}
out << ",";
}
for (int j = 0; j < Goto.size(); j++) {
for (int k = 0; k < f[{state[i], Goto[j]}].size(); k++) {
out << f[{state[i], Goto[j]}][k] << " ";
}
out << ",";
}
out << endl;
}
}