-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMachineParser.cpp
More file actions
463 lines (390 loc) · 13 KB
/
MachineParser.cpp
File metadata and controls
463 lines (390 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
#include "MachineParser.h"
symbol_set& MachineParser::parseSymbolsLine(string line) {
symbol_set* res_set = new symbol_set();
for (const auto& ch : line) {
if (ch == ' ') {
continue;
}
else {
res_set->insert(ch);
}
}
return *res_set;
}
symbol_set& MachineParser::parseFileSymbolsLine(string line) {
symbol_set* res_set = new symbol_set();
for (int i = 0; i < line.length(); i++) {
if (i % 3 == 1 || i % 3 == 2) {
continue;
}
else {
res_set->emplace(line[i]);
}
}
return *res_set;
}
bool MachineParser::symbolSetsAreCorrect(symbol_set& sigma, symbol_set& gamma, char blank_symbol) {
if (gamma.find(blank_symbol) == gamma.end()) {
return false;
}
if (sigma.find(blank_symbol) != sigma.end()) {
return false;
}
for (auto it = sigma.cbegin(); it != sigma.cend(); it++) {
if (gamma.find(*it) == gamma.end()) {
return false;
}
}
return true;
}
bool MachineParser::tapeIsCorrect(Tape& tape, symbol_set& gamma, char blank_symbol) {
string tape_line = (string)tape;
if (tape_line.size() == 1 && tape_line[0] == blank_symbol) {
return true;
}
for (char ch : tape_line) {
if (gamma.find(ch) == gamma.end())
return false;
}
return true;
}
string MachineParser::getAfterColumn(string& s) {
size_t it = s.find(": ");
if (it == string::npos || it + 2 == s.length())
return "";
return s.substr(it + 2);
}
void MachineParser::readSymbolsFromConsole(symbol_set& sigma, symbol_set& gamma, char blank_symbol) {
cout << BRIGHT_CYAN_TEXT << "Blank symbol: " << RESET_COLORING;
string first_line;
getline(cin, first_line);
blank_symbol = first_line[0];
cout << BRIGHT_CYAN_TEXT << "Sigma: " << RESET_COLORING;
string sigma_line;
getline(cin, sigma_line);
sigma = parseSymbolsLine(sigma_line);
cout << BRIGHT_CYAN_TEXT << "Gamma: " << RESET_COLORING;
string gamma_line;
getline(cin, gamma_line);
gamma = parseSymbolsLine(gamma_line);
gamma.insert(blank_symbol);
}
void MachineParser::parseFromConsole() {
symbol_set sigma = *(new symbol_set());
symbol_set gamma = *(new symbol_set());
char blank_symbol = ' ';
readSymbolsFromConsole(sigma, gamma, blank_symbol);
if (!symbolSetsAreCorrect(sigma, gamma, blank_symbol)) {
cerr << RED_TEXT << "Incorrect sets! Sigma MUST be subset of gamma. Sigma MUST NOT contain the blank symbol." << RESET_COLORING;
return;
}
builder->setBlankSymbol(blank_symbol);
builder->setSigma(sigma);
builder->setGamma(gamma);
cout << BRIGHT_CYAN_TEXT << "Initialize tape with string [y/n]? " << RESET_COLORING;
char initialize_tape = 'n';
cin >> initialize_tape;
cin.get();
Tape tape = *(new Tape);
if (initialize_tape == 'y') {
cout << BRIGHT_CYAN_TEXT << "Tape: " << RESET_COLORING;
string tape_line;
getline(cin, tape_line);
tape = Tape(tape_line, blank_symbol);
}
else {
tape = Tape(blank_symbol);
}
while (!tapeIsCorrect(tape, gamma, blank_symbol)) {
cerr << RED_TEXT << "Tape contains symbols that are not present in gamma!" << RESET_COLORING;
cout << BRIGHT_CYAN_TEXT << "Tape: " << RESET_COLORING;
string tape_line;
getline(cin, tape_line);
tape = Tape(tape_line, blank_symbol);
}
builder->setTape(tape);
unsigned states_count;
cout << BRIGHT_CYAN_TEXT << "Number of states: " << RESET_COLORING;
cin >> states_count;
while (cin.fail() || states_count < 1) {
cin.clear();
cin.ignore();
cerr << RED_TEXT << "Incorrect input!" << RESET_COLORING << endl;
cout << BRIGHT_CYAN_TEXT << "Number of states: " << RESET_COLORING;
cin >> states_count;
}
cin.get();
state_map states = *(new state_map);
string starting = *(new string);
string current = *(new string);
transitions_map transitions = *(new transitions_map);
readStatesFromConsole(states, starting, current, states_count);
unsigned transitions_count;
cout << BRIGHT_CYAN_TEXT << "Number of transitions: " << RESET_COLORING;
cin >> transitions_count;
while (cin.fail() || transitions_count < 1) {
cin.clear();
cin.ignore();
cerr << RED_TEXT << "Incorrect input!" << RESET_COLORING << endl;
cout << BRIGHT_CYAN_TEXT << "Number of transitions: " << RESET_COLORING;
cin >> transitions_count;
}
cin.get();
readTransitionsFromConsole(transitions, gamma, states, transitions_count);
builder->setStateMap(states);
builder->setStartingState(starting);
builder->setCurrentState(current);
builder->setTransitionsMap(transitions);
cout << BRIGHT_GREEN_TEXT << "Machine read succesfuly!" << endl << RESET_COLORING;
builder->setLoaded();
}
void MachineParser::parseFromFile(istream& is) {
if (!is.good()) {
printFileErrorMessage();
return;
}
string line;
getline(is, line);
if (getAfterColumn(line).length() == 0) {
printFileErrorMessage();
return;
}
char blank_symbol = getAfterColumn(line)[0];
if (!blank_symbol) {
blank_symbol = ' ';
}
getline(is, line);
if (getAfterColumn(line).length() == 0) {
printFileErrorMessage();
return;
}
symbol_set gamma = parseFileSymbolsLine(getAfterColumn(line));
gamma.emplace(blank_symbol);
getline(is, line);
if (getAfterColumn(line).length() == 0) {
printFileErrorMessage();
return;
}
symbol_set sigma = parseFileSymbolsLine(getAfterColumn(line));
getline(is, line);
if (!symbolSetsAreCorrect(sigma, gamma, blank_symbol)) {
printFileErrorMessage();
return;
}
builder->setBlankSymbol(blank_symbol);
builder->setGamma(gamma);
builder->setSigma(sigma);
Tape tape;
if (getAfterColumn(line).length() == 0) {
tape = *(new Tape(blank_symbol));
}
else {
tape = *(new Tape(getAfterColumn(line), blank_symbol));
}
if (!tapeIsCorrect(tape, gamma, blank_symbol)) {
printFileErrorMessage();
return;
}
builder->setTape(tape);
getline(is, line);
if (!(line == "States: ")) {
printFileErrorMessage();
return;
}
state_map states = *(new state_map);
do {
getline(is, line);
if (isLineForStartingState(line)) {
break;
}
string name = line.substr(2);
if ((line[0] != 'A' && line[0] != 'R' && line[0] != 'N') || (states.find(name) != states.end())) { // checks whether a state is existing or not
printFileErrorMessage();
return;
}
StateType type;
switch (line[0]) {
case 'A': type = StateType::ACCEPTING; break;
case 'R': type = StateType::REJECTING; break;
case 'N': type = StateType::NORMAL; break;
default: throw invalid_argument("Cannot define type of state");
}
states.emplace(name, type);
} while (!isLineForStartingState(line));
string starting = getAfterColumn(line);
if (states.find(starting) == states.end()) {
printFileErrorMessage();
return;
}
getline(is, line);
if (!isLineForCurrentState(line)) {
printFileErrorMessage();
return;
}
string current = getAfterColumn(line);
if (states.find(current) == states.end()) {
printFileErrorMessage();
return;
}
getline(is, line);
if (!(line == "Transitions: ")) {
printFileErrorMessage();
return;
}
transitions_map transitions = *(new transitions_map);
try {
while (!is.eof()) {
transition t = parseTransition(gamma, states, 0, false, is);
transitions.insert(t);
}
}
catch (invalid_argument ia) {
if (strcmp(ia.what(), "STOP")) {
printFileErrorMessage();
return;
}
}
builder->setStateMap(states);
builder->setTransitionsMap(transitions);
builder->setStartingState(starting);
builder->setCurrentState(current);
builder->setLoaded();
cout << GREEN_TEXT << "Machine loaded from file successfully!" << RESET_COLORING << endl;
}
void MachineParser::readStatesFromConsole(state_map& states, string& starting, string& current, unsigned count) {
cout << endl;
cout << CYAN_TEXT << "Input states in format: \"<T> <statename>\"" << endl;
cout << '\t' << "<T> <- {A, R, N} where A = ACCEPTING; R = REJECTING; N = NORMAL" << endl;
cout << '\t' << "<statename> = A regular string" << endl << RESET_COLORING;
cout << endl;
for (size_t i = 0; i < count; i++) {
string state_line;
while (state_line.size() < 2) {
cout << BRIGHT_CYAN_TEXT << "State #" << i << " : " << RESET_COLORING;
getline(cin, state_line);
}
string state_name = state_line.substr(2);
while ((state_line[0] != 'A' && state_line[0] != 'R' && state_line[0] != 'N') || (states.find(state_name) != states.end())) { // checks whether a state is existing or not
cerr << RED_TEXT << "Incorrect state format or states with duplicate names!" << endl << RESET_COLORING;
cout << BRIGHT_CYAN_TEXT << "State #" << i << " : " << RESET_COLORING;
getline(cin, state_line);
state_name = state_line.substr(2);
}
StateType type;
switch (state_line[0]) {
case 'A': type = StateType::ACCEPTING; break;
case 'R': type = StateType::REJECTING; break;
case 'N': type = StateType::NORMAL; break;
default: throw invalid_argument("Cannot define type of state");
}
states.emplace(state_name, type);
}
cout << BRIGHT_CYAN_TEXT << "Initial state: " << RESET_COLORING;
getline(cin, starting);
while (states.find(starting) == states.end()) {
cerr << RED_TEXT << "Inexisting state!" << RESET_COLORING << endl;
cout << BRIGHT_CYAN_TEXT << "Initial state: " << RESET_COLORING;
getline(cin, starting);
}
cout << BRIGHT_CYAN_TEXT << "Current state: " << RESET_COLORING;
getline(cin, current);
while (states.find(current) == states.end()) {
cerr << RED_TEXT << "Inexisting state!" << RESET_COLORING << endl;
cout << BRIGHT_CYAN_TEXT << "Current state: " << RESET_COLORING;
getline(cin, current);
}
}
void MachineParser::readTransitionsFromConsole(transitions_map& transitions, symbol_set& gamma, state_map& states, unsigned count) {
cout << endl;
cout << CYAN_TEXT << "Input transitions in format: \"<statename> <symbol> => <statename> <symbol> <TM>\"" << endl;
cout << '\t' << "<TM> <- {L, R, S} where A = LEFT; R = RIGHT; S = STAY" << endl;
cout << '\t' << "<statename> = Existing state" << endl << RESET_COLORING;
cout << endl;
for (size_t i = 0; i < count; i++) {
transition current_transition = parseTransition(gamma, states, i, true, std::cin);
transitions.insert(current_transition);
}
}
transition MachineParser::parseTransition(symbol_set& gamma, state_map& states, size_t number, bool console, istream& is) {
if (console) {
cout << BRIGHT_CYAN_TEXT << "Transition #" << number << " : " << RESET_COLORING;
}
string transition_line;
getline(is, transition_line);
if (transition_line.empty()) {
throw invalid_argument("STOP");
}
size_t delimiter = transition_line.find("=>");
if (console) {
while (delimiter == string::npos
|| (transition_line[transition_line.length() - 1] != 'L'
&& transition_line[transition_line.length() - 1] != 'R'
&& transition_line[transition_line.length() - 1] != 'S'
)) {
cerr << RED_TEXT << "Incorrect transition format!" << RESET_COLORING << endl;
cout << BRIGHT_CYAN_TEXT << "Transition #" << number << " : " << RESET_COLORING;
getline(cin, transition_line);
delimiter = transition_line.find("=>");
}
}
else {
if ((delimiter == string::npos
|| (transition_line[transition_line.length() - 1] != 'L'
&& transition_line[transition_line.length() - 1] != 'R'
&& transition_line[transition_line.length() - 1] != 'S'
))) {
throw invalid_argument("Incorrect transition");
}
}
string first_part = transition_line.substr(0, delimiter - 1);
string second_part = transition_line.substr(delimiter + 3);
size_t first_space = first_part.find(" ");
size_t second_space = second_part.find(" ");
if (console) {
while (first_space == string::npos
|| second_space == string::npos
|| states.find(first_part.substr(0, first_space)) == states.end()
|| states.find(second_part.substr(0, second_space)) == states.end()
|| gamma.find(first_part[first_space + 1]) == gamma.end()
|| gamma.find(second_part[second_space + 1]) == gamma.end()
) {
cerr << RED_TEXT << "Incorrect transition format!" << RESET_COLORING << endl;
cout << BRIGHT_CYAN_TEXT << "Transition #" << number << " : " << RESET_COLORING;
getline(cin, transition_line);
delimiter = transition_line.find("=>");
if (delimiter == string::npos) {
continue;
}
string first_part = transition_line.substr(0, delimiter - 1);
string second_part = transition_line.substr(delimiter + 3);
size_t first_space = first_part.find(" ");
size_t second_space = second_part.find(" ");
}
}
else {
if (first_space == string::npos
|| second_space == string::npos
|| states.find(first_part.substr(0, first_space)) == states.end()
|| states.find(second_part.substr(0, second_space)) == states.end()
|| gamma.find(first_part[first_space + 1]) == gamma.end()
|| gamma.find(second_part[second_space + 1]) == gamma.end()) {
throw invalid_argument("Incorrect transition");
}
}
string first_state = first_part.substr(0, first_space);
char first_char = first_part[first_space + 1];
string second_state = second_part.substr(0, second_space);
char second_char = second_part[second_space + 1];
TapeMovement movement;
switch (transition_line[transition_line.length() - 1]) {
case 'L':
movement = TapeMovement::LEFT; break;
case 'R':
movement = TapeMovement::RIGHT; break;
case 'S':
movement = TapeMovement::STAY; break;
default:
throw invalid_argument("Illegal tape movement symbol for transition " + number);
}
return transition{ state_pair{first_state, first_char}, state_tuple{second_state, second_char, movement} };
}