-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnfa2dfa.cpp
More file actions
175 lines (153 loc) · 3.36 KB
/
nfa2dfa.cpp
File metadata and controls
175 lines (153 loc) · 3.36 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
#include "nfa2dfa.h"
#include <iostream>
#include <sstream>
#include <queue>
#include <algorithm>
#include <map>
using namespace std;
typedef vector<Status *> Set;
Set calClosure(Status *status)
{
Set result;
queue<Status *> workList;
workList.push(status);
result.push_back(status);
while (!workList.empty()) {
Status *s = workList.front();
const vector<Edge *> &outEdges = s->outEdges;
for (auto edges : outEdges) {
if (edges->symbol == nullChar) {
Status *newStatus = edges->end;
if (find(result.begin(), result.end(), newStatus) == result.end()) {
result.push_back(newStatus);
workList.push(newStatus);
}
}
}
workList.pop();
}
return result;
}
void merge(Set &result, Set &temp)
{
for (auto &x : temp) {
if (find(result.begin(), result.end(), x) == result.end())
result.push_back(x);
}
}
Set calClosureFS(const Set &set) //calClosure from a set
{
Set result;
if (set.size() == 0)
return result;
result = calClosure(set[0]);
for (int i = 1; i < set.size(); i++) {
Set temp = calClosure(set[i]);
merge(result, temp);
}
return result;
}
bool equal(const Set &a, const Set &b)
{
if (a.size() != b.size())
return false;
for (const auto &x: a) {
if (find(b.begin(), b.end(), x) == b.end())
return false;
}
for (const auto &y: b) {
if (find(a.begin(), a.end(), y) == a.end())
return false;
}
return true;
}
int find_set(const vector<Set> &result, const Set &a)
{
int len = result.size();
for (int i = 0; i < len; i++) {
if (equal(result[i], a))
return i;
}
return -1;
}
bool isEndStatus(const Set &set)
{
for (const auto &x : set) {
if (x->finalStatus)
return true;
}
return false;
}
int find(const vector<char> &line, char c)
{
int len = line.size();
for (int i = 0; i < len; i++) {
if (line[i] == c)
return i;
}
return -1;
}
bool simulateDfa(const string &s, Dfa &dfa)
{
auto end = dfa.graph.end();
istringstream stream(s);
char c;
int current_state = 0;
while (stream >> c) {
if (dfa.graph.find(make_pair(current_state, anyChar)) != end) {
current_state = dfa.graph[make_pair(current_state, anyChar)];
continue;
}
if (dfa.graph.find(make_pair(current_state, c)) == end)
return false;
current_state = dfa.graph[make_pair(current_state, int(c))];
}
if (find(dfa.endStatus.begin(), dfa.endStatus.end(), current_state) == dfa.endStatus.end())
return false;
return true;
}
Set delta(const Set & set, int c)
{
Set result;
for (const auto &x : set) {
const vector<Edge *> &outEdges = x->outEdges;
for (const auto &e : outEdges) {
if (e->symbol == c)
result.push_back(e->end);
}
}
return result;
}
Dfa nfa2dfa(const Nfa &nfa, vector<int> &character)
{
uniqueVector(character);
map<pair<int , int>, int> Map;
Set q0 = calClosure(nfa.start);
vector<Set> result;
queue<Set> workList;
result.push_back(q0);
workList.push(q0);
int src_index = -1;
vector<int> endStatus;
//构建DFA表
while (!workList.empty()) {
++src_index;
Set q = workList.front();
if (isEndStatus(q))
endStatus.push_back(src_index);
for (auto x : character) {
Set t = calClosureFS(delta(q, x));
if (t.size() == 0)
continue;
int des_index = find_set(result, t);
if (des_index == -1) {
des_index = result.size();
result.push_back(t);
workList.push(t);
}
Map[make_pair(src_index, x)] = des_index;
}
workList.pop();
}
return Dfa(Map, endStatus);
}