forked from Rolso22/edsl_cw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLrZero.h
156 lines (136 loc) · 4.99 KB
/
LrZero.h
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
#include <set>
#include <map>
#include "list"
#ifndef EDSL_CW_LR_ZERO_H
#define EDSL_CW_LR_ZERO_H
using namespace std;
#include "Grammar.h"
class LrZero {
public:
vector<set<pair<int, int>>> states;
map<set<pair<int, int>>, int> id_from_state;
map<pair<int, string>, int> dfa_goto;
Grammar* gr;
LrZero(Grammar* grammar) {
gr = grammar;
states = {closure({{make_pair(0, 0)}})};
auto next_id = 0;
id_from_state[states.back()] = next_id;
next_id += 1;
set<set<pair<int, int>>> seen(states.begin(), states.end());
auto set_queue = states;
vector<set<pair<int, int>>> new_elements;
while (!set_queue.empty()) {
new_elements = {};
for (const auto& item_set : set_queue) {
auto item_set_id = id_from_state[item_set];
for (const auto& symbol : gr->symbols) {
auto next_item_set = _goto(item_set, symbol);
if (next_item_set.empty()) {
continue;
}
if (seen.find(next_item_set) == seen.end()) {
new_elements.push_back(next_item_set);
seen.insert(next_item_set);
states.push_back(next_item_set);
id_from_state[states.back()] = next_id;
next_id += 1;
}
dfa_goto[make_pair(item_set_id, symbol)] = id_from_state[next_item_set];
}
}
set_queue = new_elements;
}
// cout << "states: ";
// for (const auto& elem : states) {
// cout << "set";
// for (auto pr : elem) {
// cout << "(" << pr.first << ", " << pr.second << ")" << ", ";
// }
// }
// cout << endl;
// cout << "id_from_state: ";
// for (const auto& state : id_from_state) {
// cout << "set";
// for (auto pr : state.first) {
// cout << "(" << pr.first << ", " << pr.second << ")" << ", ";
// }
// cout << ": ";
// cout << state.second << " ; ";
// }
// cout << endl;
// cout << "goto: ";
// for (const auto& rule : dfa_goto) {
// cout << "(" << rule.first.first << ", " << rule.first.second << ")" << ", ";
// cout << ": ";
// cout << rule.second << " ; ";
// }
// vector<set<pair<int, int>>> kstates;
// for (const auto& state : states) {
// kstates.push_back(kernels(state));
// }
// cout << endl;
// cout << "kstates: ";
// for (const auto& k : kstates) {
// cout << "set";
// for (auto st : k) {
// cout << "(" << st.first << ", " << st.second << ")" << ", ";
// }
// }
}
set<pair<int, int>> closure(const set<pair<int, int>>& item_set) {
auto result = item_set;
vector<pair<int, int>> set_queue(item_set.begin(), item_set.end());
vector<pair<int, int>> new_elements;
while (!set_queue.empty()) {
new_elements = {};
for (auto pr : set_queue) {
auto item_prod_id = pr.first;
auto dot = pr.second;
auto prod = gr->productions[item_prod_id];
auto pname = prod.first;
auto pbody = prod.second;
if (dot == pbody.size() or !gr->is_nonterm(pbody[dot])) {
continue;
}
auto nt_name = pbody[dot];
auto nt = gr->nt_by_name[nt_name];
auto nt_offset = gr->nonterm_offset[nt];
for (auto idx = 0; idx < nt->productions.size(); idx++) {
auto new_item_set = make_pair(nt_offset + idx, 0);
if (result.find(new_item_set) == result.end()) {
new_elements.push_back(new_item_set);
result.insert(new_item_set);
}
}
}
set_queue = new_elements;
}
return result;
}
set<pair<int, int>> _goto(const set<pair<int, int>>& item_set, const string& inp) {
set<pair<int, int>> result;
for (auto pr : item_set) {
auto prod_index = pr.first;
auto dot = pr.second;
auto prod = gr->productions[prod_index];
auto pname = prod.first;
auto pbody = prod.second;
if (dot < pbody.size() and pbody[dot] == inp) {
result.insert(make_pair(prod_index, dot + 1));
}
}
result = closure(result);
return result;
}
set<pair<int, int>> kernels(const set<pair<int, int>>& item_set) {
set<pair<int, int>> result;
for (auto pr : item_set) {
if (pr.first == 0 or pr.second > 0) {
result.insert(pr);
}
}
return result;
}
};
#endif //EDSL_CW_LR_ZERO_H