-
Notifications
You must be signed in to change notification settings - Fork 0
/
DFA.cpp
292 lines (262 loc) · 9.86 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
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
/*
* DFA.cpp
*
* Created on: May 16, 2015
* Author: Hristiyan Nikolov
* Copyright (c) 2015. All rights reserved.
*/
#include "DFA.h"
int DFA::transitionFunction(int from, char letter) {
if (table.find(from) != table.end()
&& table[from].find(letter) != table[from].end())
return table[from][letter];
return -2;
}
int DFA::extendedTransitionFunction(int from, const char* word) {
for (int i = 0; i < (int) strlen(word); i++)
from = transitionFunction(from, word[i]);
return from;
}
bool DFA::isFinalState(int state) {
if (isValidState(state)) {
for (int i = 0; i < countOfFinalStates; i++)
if (finalStates[i] == state)
return true;
}
return false;
}
bool DFA::isValidState(int state) {
for (int i = 0; i < countOfStates; i++)
if (states[i] == state)
return true;
return false;
}
int DFA::getCountOfStates() {
int count = 0, k = 0;
int allStates[table.size() * 2]; // в него записваме абсолютно всички състояния от таблицата, дори да се повтарят
bool isIn = false;
for (std::map<int, std::map<char, int> >::const_iterator i = table.begin();
i != table.end(); i++)
for (std::map<char, int>::const_iterator j = i->second.begin();
j != i->second.end(); j++) {
allStates[k++] = i->first;
allStates[k++] = j->second;
}
int newAllStates[k];
for (int i = 0; i < k; i++)
newAllStates[i] = -2;
for (int i = 0; i < k; i++) { // в newAllStates записваме числата (т.е. състоянията), които не се повтарят и след това връщаме броя им
isIn = false;
for (int j = 0; j < k; j++)
if (allStates[i] == newAllStates[j])
isIn = true;
if (isIn == false) {
newAllStates[count++] = allStates[i];
}
}
return count;
}
int* DFA::getStates() {
int count = 0, k = 0;
int allStates[table.size() * 2]; // в него записваме абсолютно всички състояния от таблицата, дори да се повтарят
int* pointerToStates;
bool isIn = false;
for (std::map<int, std::map<char, int> >::iterator i = table.begin();
i != table.end(); i++)
for (std::map<char, int>::iterator j = i->second.begin();
j != i->second.end(); j++) {
allStates[k++] = i->first;
allStates[k++] = j->second;
}
int newAllStates[countOfStates];
pointerToStates = new int[countOfStates];
for (int i = 0; i < countOfStates; i++)
newAllStates[i] = -2;
for (int i = 0; i < k; i++) { // в newAllStates записваме числата (т.е. състоянията), които не се повтарят
isIn = false;
for (int j = 0; j < countOfStates; j++)
if (allStates[i] == newAllStates[j])
isIn = true;
if (isIn == false) {
newAllStates[count] = allStates[i];
pointerToStates[count] = allStates[i];
count++;
}
}
std::sort(pointerToStates, pointerToStates + countOfStates); // Сортираме състоянията във възходящ ред
return pointerToStates;
}
int DFA::maxNameOfState() {
int max = states[0];
for (int i = 1; i < countOfStates; i++) {
if (max < states[i])
max = states[i];
}
return max;
}
void DFA::changeAllStatesName(int start) {
int newStates[countOfStates];
int k = 0, oldState, index;
for (int i = 0; i < countOfStates; i++) {
newStates[i] = start;
if (isFinalState(states[i]))
finalStates[k++] = start;
start++;
}
for (int i = 0; i < countOfStates; i++) { // Презаписваме таблицата с новите състояния
for (int j = 0; j < countOfAlphabet; j++) {
if (transitionFunction(states[i], alphabet[j]) != -2
&& transitionFunction(states[i], alphabet[j]) != -1) {
oldState = table[states[i]][alphabet[j]]; //трябва да вземем индекса на oldState и да вземем новото име така newStates[индекс на oldState]
for (int z = 0; z < countOfStates; z++)
if (states[z] == oldState)
index = z;
table[newStates[i]][alphabet[j]] = newStates[index];
table[states[i]].erase(alphabet[j]); // Изтриваме старите състояния
}
}
}
for (int i = 0; i < countOfStates; i++) {
if (startState == states[i])
startState = newStates[i];
states[i] = newStates[i];
}
}
DFA::DFA(const DFA& automat) {
countOfAlphabet = automat.countOfAlphabet;
for (int i = 0; i < countOfAlphabet; i++)
alphabet[i] = automat.alphabet[i];
table = automat.table;
startState = automat.startState;
countOfFinalStates = automat.countOfFinalStates;
countOfStates = automat.countOfStates;
finalStates = new int[countOfFinalStates];
states = new int[countOfStates];
for (int i = 0; i < countOfFinalStates; i++)
finalStates[i] = automat.finalStates[i];
for (int i = 0; i < countOfStates; i++)
states[i] = automat.states[i];
}
DFA::DFA(int _startState, int* _finalStates, int _countOfFinalStates,
std::map<int, std::map<char, int> > _table) {
int k = 0;
for (int i = 97; i <= 122; i++)
alphabet[k++] = (char) i;
for (int i = 48; i <= 57; i++)
alphabet[k++] = (char) i;
alphabet[k] = '\0';
countOfAlphabet = k;
table = _table;
countOfFinalStates = _countOfFinalStates;
countOfStates = getCountOfStates();
states = new int[countOfStates];
states = getStates();
finalStates = new int[countOfFinalStates];
std::sort(_finalStates, _finalStates + countOfFinalStates);
for (int i = 0; i < countOfFinalStates; i++)
finalStates[i] = _finalStates[i];
if (isValidState(_startState))
startState = _startState;
else
throw "Error: start state is not valid.";
}
DFA::~DFA() {
delete[] states;
delete[] finalStates;
}
bool DFA::checkWord(char* word) {
if (isFinalState(extendedTransitionFunction(startState, word)))
return true;
return false;
}
void DFA::addErrorState() {
for (int i = 0; i < countOfStates; i++)
for (int j = 0; j < countOfAlphabet; j++)
if (table.find(states[i]) == table.end()
|| table[states[i]].find(alphabet[j])
== table[states[i]].end())
table[states[i]][alphabet[j]] = -1;
}
std::ostream& operator<<(std::ostream& os, DFA automat) {
os << "Start state: " << automat.startState << std::endl;
os << "Final states: ";
for (int i = 0; i < automat.countOfFinalStates; i++)
if (i != automat.countOfFinalStates - 1)
os << automat.finalStates[i] << ",";
else
os << automat.finalStates[i] << std::endl;
for (int i = 0; i < automat.countOfStates; i++) {
for (int j = 0; j < automat.countOfAlphabet; j++) {
std::cout << "i:" << i << " j:" << j << std::endl;
os << "table[" << automat.states[i] << "]['" << automat.alphabet[j]
<< "']="
<< automat.transitionFunction(automat.states[i],
automat.alphabet[j]) << ";" << std::endl;
}
}
return os;
}
void unionOfTwoAutomata(DFA automat1, DFA automat2) {
int newStartState, newFinalStates[automat1.countOfFinalStates
+ automat2.countOfFinalStates];
int maxStateName1 = automat1.maxNameOfState() + 1;
int maxStateName2 = automat2.maxNameOfState() + 1;
std::map<int, std::map<char, int> >::iterator i;
std::map<char, int>::iterator j;
if (maxStateName2 >= maxStateName1) { // променяме автомат2 и извеждаме пъвро автомат1 след това автомат2
automat2.changeAllStatesName(maxStateName2);
newStartState = automat2.maxNameOfState() + 1;
for (i = automat1.table.begin(); i != automat1.table.end(); i++)
for (j = i->second.begin(); j != i->second.end(); j++)
std::cout << "table[" << i->first << "][" << j->first << "]="
<< j->second << std::endl;
for (i = automat2.table.begin(); i != automat2.table.end(); i++)
for (j = i->second.begin(); j != i->second.end(); j++)
std::cout << "table[" << i->first << "][" << j->first << "]="
<< j->second << std::endl;
} else { // променяме автомат1 и извеждаме пъвро автомат2 след това автомат1
automat1.changeAllStatesName(maxStateName1);
newStartState = automat1.maxNameOfState() + 1;
for (i = automat2.table.begin(); i != automat2.table.end(); i++)
for (j = i->second.begin(); j != i->second.end(); j++)
std::cout << "table[" << i->first << "][" << j->first << "]="
<< j->second << std::endl;
for (i = automat1.table.begin(); i != automat1.table.end(); i++)
for (j = i->second.begin(); j != i->second.end(); j++)
std::cout << "table[" << i->first << "][" << j->first << "]="
<< j->second << std::endl;
}
// извеждаме прегодите от новото начално състояния
for (int i = 0; i < automat1.countOfAlphabet; i++) {
if (automat1.transitionFunction(automat1.startState,
automat1.alphabet[i]) != -2) {
std::cout << "table[" << newStartState << "]["
<< automat1.alphabet[i] << "]="
<< automat1.transitionFunction(automat1.startState,
automat1.alphabet[i]) << std::endl;
}
if (automat2.transitionFunction(automat2.startState,
automat2.alphabet[i]) != -2) {
std::cout << "table[" << newStartState << "]["
<< automat2.alphabet[i] << "]="
<< automat2.transitionFunction(automat2.startState,
automat2.alphabet[i]) << std::endl;
}
}
int k = 0;
// вземаме финалните състояния на новия автомат
for (int i = 0; i < automat1.countOfFinalStates; i++)
newFinalStates[k++] = automat1.finalStates[i];
for (int i = 0; i < automat2.countOfFinalStates; i++)
newFinalStates[k++] = automat2.finalStates[i];
// ако поне едно от двете начални състояния на двата автомата е финално, правим и началното на новия автомат финално
if (automat1.isFinalState(automat1.startState)
|| automat2.isFinalState(automat2.startState)) {
newFinalStates[k++] = newStartState;
}
// извеждаме автомата
std::cout << "Start state: " << newStartState << std::endl
<< "Final States: ";
for (int i = 0; i < k; i++)
std::cout << newFinalStates[i] << " ";
}