-
Notifications
You must be signed in to change notification settings - Fork 0
/
machine.cpp
306 lines (252 loc) · 7.54 KB
/
machine.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
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
//
// Created by Marwan Tammam on 3/21/18.
//
#include <iostream>
#include "machine.h"
#include "thirdparty/json.hpp"
machine::state::transition::transition(int to, char input) {
to_identifier = to;
on_input = input;
}
int machine::state::transition::get_to_identifier() const {
return to_identifier;
}
char machine::state::transition::get_transition_char() const {
return on_input;
}
machine::state::state() {
}
machine::state::state(std::string _token_class) {
token_class = _token_class;
}
machine::state::state(std::string _token_class, std::string _key) {
token_class = _token_class;
key = _key;
}
std::string machine::state::get_token_class() const {
return token_class;
}
std::string machine::state::get_key() const {
return key;
}
void machine::state::set_key(std::string _key) {
key = _key;
}
void machine::state::set_token_class(std::string new_token_class) {
token_class = new_token_class;
}
std::vector<int> machine::state::get_transitions_for(char input) {
std::vector<int> t;
for (machine::state::transition &trans : transitions[input]) {
t.push_back(trans.get_to_identifier());
}
return t;
}
bool machine::state::add_new_transition(int to_id, char on_input) {
if (transitions.count(on_input) == 0) {
transitions[on_input] = std::vector<machine::state::transition>();
}
transitions[on_input].push_back(machine::state::transition(to_id, on_input));
return true;
}
machine::machine(std::string _machine_identifier) {
machine_identifier = _machine_identifier;
language.insert(EPS_C);
starting = -1;
}
int machine::add_new_state(bool is_starting, bool is_accepting) {
states.push_back(machine::state());
if (is_accepting) {
accepting.insert((int) states.size());
}
if (is_starting) {
starting = (int) states.size();
}
return (int) states.size();
}
int machine::add_new_state(std::string key, std::string token_class, bool is_starting, bool is_accepting) {
states.push_back(machine::state(token_class, key));
if (is_accepting) {
accepting.insert((int) states.size());
}
if (is_starting) {
starting = (int) states.size();
}
return (int) states.size();
}
int machine::add_new_state(std::string token_class, bool is_starting, bool is_accepting) {
machine::state s = machine::state(token_class);
states.push_back(s);
if (is_accepting) {
accepting.insert((int) states.size());
}
int id = (int) states.size();
if (is_starting) {
starting = id;
}
s.set_key("" + id);
return id;
}
bool machine::add_new_transition(int from_id, int to_id, char on_input) {
if (from_id - 1 >= get_states_count())
return false;
states[from_id - 1].add_new_transition(to_id, on_input);
language.insert(on_input);
return true;
}
bool machine::set_starting_state(int _starting_id) {
if (_starting_id - 1 >= get_states_count())
return false;
starting = _starting_id;
return true;
}
int machine::add_starting_state(std::string key, std::string token_class, bool is_accepting) {
return machine::add_new_state(key, token_class, true, is_accepting);
}
int machine::get_starting_state() const {
return starting;
}
std::string machine::get_machine_identifier() const {
return machine_identifier;
}
std::set<int> machine::get_accepting_states() const {
return accepting;
}
std::vector<int> machine::get_transitions(int id, char input) {
return states[id - 1].get_transitions_for(input);
}
std::string machine::get_token_class(int id) const {
return states[id - 1].get_token_class();
}
std::string machine::get_key_for(int id) const {
return states[id - 1].get_key();
}
void machine::set_machine_identifier (std::string identifier)
{
machine_identifier = identifier;
}
void machine::set_key_for(sid_t id, std::string new_key) {
states[id - 1].set_key(new_key);
}
bool machine::set_token_class(sid_t id, std::string new_token_class) {
if (id < 1 && id > states.size())
return false;
states[id - 1].set_token_class(new_token_class);
return true;
}
bool machine::set_token_class(std::string new_token_class) {
for (auto i = accepting.begin (); i != accepting.end (); i++)
states[(*i)-1].set_token_class (new_token_class);
return true;
}
std::set<char> machine::get_language() const {
return language;
}
int machine::get_states_count() const {
return (int) states.size();
}
bool machine::is_starting(int id) {
return starting == id;
}
bool machine::is_accepting(int id) {
return accepting.count(id) != 0;
}
void machine::clear_accepting_states() {
accepting.clear();
}
bool machine::set_accepting(int id) {
if (id - 1 >= get_states_count()) {
return false;
}
accepting.insert(id);
return true;
}
int machine::merge(machine other) {
int starting_other = other.get_starting_state();
int old_cnt = get_states_count();
for (int i = 1; i <= other.get_states_count(); i++) {
add_new_state(other.get_key_for(i), other.get_token_class(i), false, other.is_accepting(i));
}
for (int i = 1; i <= other.get_states_count(); i++) {
for (char c : other.get_language()) {
std::vector<int> t = other.get_transitions(i, c);
for (int id : t) {
add_new_transition(i + old_cnt, id + old_cnt, c);
}
}
}
return starting_other + old_cnt;
}
std::ostream &operator <<(std::ostream &os, machine &m) {
using json = nlohmann::json;
json j;
j["size"] = m.get_states_count();
j["starting"] = m.get_starting_state();
j["accepting"] = m.get_accepting_states();
json trans = json::array();
json tok = json::array();
for (int i = 1; i <= m.get_states_count(); i++) {
for (char c : m.get_language()) {
std::vector<int> v = m.get_transitions(i, c);
for (int t : v)
trans.push_back({std::string(1, c), i, t});
}
tok.push_back(m.get_token_class(i));
}
j["transitions"] = trans;
j["tokens"] = tok;
std::cout << std::endl;
os << j;
return os;
}
std::istream &operator >>(std::istream &is, machine &m) {
using json = nlohmann::json;
std::string str;
is >> str;
json j = json::parse(str);
int i = 0;
int count = j["size"];
int starting = j["starting"];
for (i = 0; i < count; i++)
m.add_new_state(false, false);
m.set_starting_state(starting);
for (int s : j["accepting"])
m.set_accepting(s);
for (json j_trans : j["transitions"]) {
std::string s = j_trans[0];
char c = s[0];
int u = j_trans[1];
int v = j_trans[2];
m.add_new_transition(u, v, c);
}
i = 0;
for (std::string j_tok : j["tokens"]) {
m.set_token_class(++i, j_tok);
}
return is;
}
void machine::print_machine() {
using namespace std;
cout << "digraph {\n";
cout << "node [shape = doublecircle]; ";
for (int i : get_accepting_states())
cout << i << " ";
cout << ";\n";
cout << "node [shape = point ]; qi;\n";
cout << "node [shape = circle]; \n";
cout << "qi -> " << get_starting_state() << ";\n";
for (int i = 1; i <= states.size(); i++) {
for (char c : language) {
vector<int> v = get_transitions(i, c);
for (auto to : v) {
cout << i << " -> " << to << ' ';
if (c == EPS_C)
cout << "[label=\"ε\"];";
else
cout << "[label=\"" << c << "\"];";
cout << endl;
}
}
}
cout << endl << "}";
}