-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAutomaton.cpp
103 lines (96 loc) · 2.68 KB
/
Automaton.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
#include "Automaton.h"
#include <iostream>
using namespace std;
bool Automaton::run(std::string inp) {
//Clear the states queue
std::queue <AutomatonState> q;
States = q;
accepted = false;
input = inp;
stack<unsigned int> stk;
AutomatonState init (0, 0, stk);
States.push(init);
while (!States.empty() && !accepted) {
AutomatonState s = States.front();
States.pop();
processState(s);
}
if (accepted)
return true;
else
return false;
}
void Automaton::processState(AutomatonState s) {
// cout << "processing "<<s.getNodeId()<<endl;
Node currNode = Nodes.at(s.getNodeId());
unsigned int progress = s.getProgress();
stack<unsigned int> stk = s.getStack();
if (progress == input.size()) {
if (currNode.getFinal()) {
if (!stk.empty())
cout<<"in state "<<currNode.getId()<<" but stk is bad\n" << stk.top() << " "<<stk.size();
accepted = true;
}
return;
}
LocalTransition lTransitions;
StackTransition sTransitions;
unsigned int topOfStack;
char inputBit = input.at(progress);
switch (inputBit) {
case 'a' :
sTransitions = currNode.getATransitions();
for (int i = 0; i < sTransitions.size(); i++) {
stk.push(sTransitions.at(i).second);
AutomatonState newState (progress + 1, sTransitions.at(i).first, stk);
States.push(newState);
stk.pop();
}
break;
case 'b':
sTransitions = currNode.getBTransitions();
for (int i = 0; i < sTransitions.size(); i++) {
stk.push(sTransitions.at(i).second);
AutomatonState newState (progress + 1, sTransitions.at(i).first, stk);
States.push(newState);
stk.pop();
}
break;
case 'c':
lTransitions = currNode.getCTransitions();
for (int i = 0; i < lTransitions.size(); i++) {
AutomatonState newState (progress + 1, lTransitions.at(i), stk);
States.push(newState);
}
break;
case 'd':
lTransitions = currNode.getDTransitions();
for (int i = 0; i < lTransitions.size(); i++) {
AutomatonState newState (progress + 1, lTransitions.at(i), stk);
States.push(newState);
}
break;
case 'e':
topOfStack = stk.top();
sTransitions = currNode.getETransitions();
stk.pop();
for (int i = 0; i < sTransitions.size(); i++) {
if (sTransitions.at(i).second == topOfStack || sTransitions.at(i).second == 0) {
AutomatonState newState (progress + 1, sTransitions.at(i).first, stk);
States.push(newState);
}
}
break;
case 'f':
topOfStack = stk.top();
sTransitions = currNode.getFTransitions();
stk.pop();
for (int i = 0; i < sTransitions.size(); i++) {
if (sTransitions.at(i).second == topOfStack || sTransitions.at(i).second == 0) {
AutomatonState newState (progress + 1, sTransitions.at(i).first, stk);
States.push(newState);
}
}
break;
}
}