-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdfa.cpp
149 lines (121 loc) · 3.1 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
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
// $ g++ --std=c++11 -o dfa dfa.cpp
// $ ./dfa
// a => false
// baa => false
// baba => true
#include <algorithm>
#include <initializer_list>
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
typedef int State;
typedef std::vector<State> States;
class FARule
{
public:
explicit FARule(State state, char c, State nextState) : state_(state), c_(c), nextState_(nextState)
{
}
bool appliesTo(State state, char c) const
{
return (state_ == state) && (c_ == c);
}
State follow() const
{
return nextState_;
}
std::string inspect() const
{
std::ostringstream oss;
oss << "#<FARule " << state_ << " --" << c_ << "--> " << nextState_ << ">" << std::flush;
return oss.str();
}
private:
State state_;
char c_;
State nextState_;
};
typedef std::vector<FARule> Rules;
class DFARulebook
{
public:
explicit DFARulebook(const Rules& rules) : rules_(rules)
{
}
const FARule& ruleFor(State state, char c) const
{
Rules::const_iterator i = std::find_if(std::begin(rules_), std::end(rules_), [&](const FARule& rule) { return rule.appliesTo(state, c); });
return *i;
}
State nextState(State state, char c) const
{
return ruleFor(state, c).follow();
}
private:
Rules rules_;
};
class DFA
{
public:
explicit DFA(State currentState, States acceptStates, DFARulebook rullbook) : currentState_(currentState), acceptStates_(acceptStates), rullbook_(rullbook)
{
}
bool isAccepting() const
{
return std::find(std::begin(acceptStates_), std::end(acceptStates_), currentState_) != std::end(acceptStates_);
}
void readCharacter(char c)
{
currentState_ = rullbook_.nextState(currentState_, c);
}
void readString(const std::string& s)
{
std::for_each(std::begin(s), std::end(s), [&](char c) { this->readCharacter(c); });
}
private:
State currentState_;
States acceptStates_;
DFARulebook rullbook_;
};
class DFADesign
{
public:
explicit DFADesign(State currentState, States acceptStates, DFARulebook rullbook) : currentState_(currentState), acceptStates_(acceptStates), rullbook_(rullbook)
{
}
DFA toDFA() const
{
return DFA(currentState_, acceptStates_, rullbook_);
}
bool isAccepts(const std::string& s) const
{
DFA dfa = toDFA();
dfa.readString(s);
return dfa.isAccepting();
}
private:
State currentState_;
States acceptStates_;
DFARulebook rullbook_;
};
void test(std::ostream& out, const DFADesign& design, const std::string& s)
{
out << s << " => " << design.isAccepts(s) << "\n";
}
int main(int argc, char* argv[])
{
Rules rules =
{
FARule(1, 'a', 2), FARule(1, 'b', 1),
FARule(2, 'a', 2), FARule(2, 'b', 3),
FARule(3, 'a', 3), FARule(3, 'b', 3)
};
DFARulebook rulebook(rules);
DFADesign design(1, {3}, rulebook);
std::cout << std::boolalpha;
test(std::cout, design, "a");
test(std::cout, design, "baa");
test(std::cout, design, "baba");
return 0;
}