-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdfa.cpp
53 lines (46 loc) · 1.38 KB
/
dfa.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
#include "dfa.hpp"
#include "fort.hpp"
std::ostream &DFA::print(std::ostream &out) const {
auto table = makeTable();
for (auto [label, state] : states) {
std::string pre = state->accept ? "+" : "";
table << pre + (init->label == label ? "-" : "") + label;
for (auto input : alphabet) {
table << state->next(input)->label;
}
table << fort::endr;
}
return out << table.to_string();
}
void DFA::addState(String label, bool accept) {
if (states.find(label) != states.end()) {
throw "duplicate state label " + label;
}
states.emplace(label, std::make_shared<DFAState>(label, accept));
}
void DFA::addInitialState(String label, bool accept) {
addState(label, accept);
init = states.at(label);
}
void DFA::addTransition(String from, String to, char input) {
auto itFrom = states.find(from);
if (itFrom == states.end()) {
throw "no such state " + from;
}
auto itTo = states.find(to);
if (itTo == states.end()) {
throw "no such state " + to;
}
itFrom->second->addTransition(input, itTo->second);
alphabet.insert(input);
}
bool DFA::process(String input) const {
auto state = init;
for (char ch : input) {
state = state->next(ch);
}
return state->accept;
}
bool DFA::hasState(String label) const {
return states.find(label) != states.end();
}