-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNFA.cpp
328 lines (288 loc) · 9.67 KB
/
NFA.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#include "NFA.h"
#include "nodedfa.h"
#include <stack>
#include"QStack"
QList<char> NFA::Alphabetic;
NFA::NFA()
{
StartState = new NodeNFA("q0") ;
AllStates.insert(StartState);
StartState->link(' ',StartState);
Finit_wordsState = new NodeNFA(">");
Finit_wordsState->setFinite();
FinitStates.insert(Finit_wordsState);
AllStates.insert(Finit_wordsState);
int i = 0 ;
for (char ch = 'a';ch<='z';ch++)
{
Alphabetic.insert(i++,ch);
}
for (char ch = 'A';ch<='Z';ch++)
{
Alphabetic.insert(i++,ch);
}
Alphabetic.insert(i++,' ');
}
NFA::NFA(QList<QString>KeyWords)
{
StartState = new NodeNFA("q0") ;
AllStates.insert(StartState);
StartState->link(' ',StartState);
if (KeyWords.length() > 0)
{
Finit_wordsState = new NodeNFA(">");
Finit_wordsState->setFinite();
FinitStates.insert(Finit_wordsState);
AllStates.insert(Finit_wordsState);
}
//
int i = 0 ;
for (char ch = 'a';ch<='z';ch++)
{
Alphabetic.insert(i++,ch);
}
for (char ch = 'A';ch<='Z';ch++)
{
Alphabetic.insert(i++,ch);
}
Alphabetic.insert(i++,' ');
LoadNFA(KeyWords);
}
//GET
NodeNFA * NFA::getStartState()
{ return StartState; }
NodeNFA * NFA::getFinit_WordsState()
{ return Finit_wordsState; }
QList<char> NFA::getAlphabetic()
{ return Alphabetic; }
QList<char> NFA::getSeparate_wordsAlphabetic()
{ return Separate_wordsAlphabetic; }
QSet<NodeNFA*> NFA::getFinitStates()
{ return FinitStates; }
QSet<NodeNFA *> NFA::getAllStates()
{ return AllStates; }
QSet<NodeNFA*> NFA::getNonFinitStates()
{ return AllStates.subtract(FinitStates); }
QMultiMap<QString, QPair<QString, char> >* NFA::getConvertTable()
{
return convertTable;
}
//Set
void NFA::setStartState(NodeNFA *state)
{ StartState = state; }
void NFA::setAlphabetic(QList<char> alphabetic)
{ Alphabetic = alphabetic; }
void NFA::setFinit_WordsState(NodeNFA *state)
{ Finit_wordsState = state; }
void NFA::addToFinitState(NodeNFA* state)
{ FinitStates.insert(state); }
void NFA::addToState(NodeNFA* state)
{ AllStates.insert(state); }
void NFA::setSeparate_wordsAlphabetic(QList<char> alphabetic)
{ Separate_wordsAlphabetic = alphabetic; }
void NFA::LoadNFA(QList<QString>KeyWords)
{
NodeNFA *CurrentState = StartState,*NextState = StartState;
int CounterState = 1 ; //for generate and save name of states(NodeDFA) q0,1,2,3....
//for (int i=0;i<numberWords;i++)
foreach (QString s ,KeyWords)
{
CurrentState = StartState;
//For link each NodeNFA with another NodeNFA Based input
for (int j=0;j<s.length();j++)
{
if (s[j].cell() == '*')
{
for(char c='a';c<='z';c++)
CurrentState->link(c);
for(char c='A';c<='Z';c++)
CurrentState->link(c);
if (j!=s.length()-1)
NextState->link(' ', StartState);
continue;
}
NextState = new NodeNFA(CounterState++);
AllStates.insert(NextState);
CurrentState->link(s[j].cell(),NextState);
if (j!=s.length()-1)
NextState->link(' ',StartState);
CurrentState = NextState ;
}
//For set Finit of the last Node after node of ' '
CurrentState->link(' ',Finit_wordsState);
}//For number Words
//for copy of Start State map into Finite States
foreach (NodeNFA *state , FinitStates)
{
foreach (char ch ,Alphabetic)
{
QList<NodeNFA*> nodes = StartState->getNextNode(ch);
foreach(NodeNFA* temp , nodes)
{
state->link(ch,temp);
}
}
}
}
void NFA::addToList(NodeNFA* node)
{
if (!temp.contains(node))
temp.append(node);
}
void NFA::addFinitState(NodeNFA * state)
{
FinitStates.insert(state);
}
void NFA::addState(NodeNFA *state)
{
AllStates.insert(state);
}
QString* NFA::setToString(QSet<NodeNFA*> set)
{
QString* str = new QString();
foreach (NodeNFA* node, set)
str->append(node->getName());
return str;
}
QString* NFA::listToString(QList<NodeNFA*> list)
{
QString* str = new QString();
foreach (NodeNFA* node, list)
str->append(node->getName());
return str;
}
DFA* NFA::convertToDFA()
{
NodeDFA* DFANode;
DFA* dfa = new DFA();
convertTable = new QMultiMap<QString, QPair<QString, char> >();
//list of new groups of nodes
QList< QSet<NodeNFA*> > groups;
//hash take gruops of NFA retrun DFA node
QHash<QString, NodeDFA*>* nodes
= new QHash<QString, NodeDFA*>();
//Helper Set
QSet<NodeNFA*> set;
//insert Start State
set.insert(StartState);
//make NodeDFA start state of NFA
DFANode = new NodeDFA(0);
nodes->insert(StartState->getName(), DFANode);
dfa->setStartState(DFANode);
//add the node to states of DFA
dfa->addToState(DFANode);
//add first set
groups.append(set);
//initialized variables
int i=0, nodeNum=0, AllNodes = 1;
//Helper String
QString toSet;
//useed it to know if node is finite
bool finite = false;
//while we dont see all groups
while (i != groups.count()){
//for all symbol in language
foreach (char symbol, Alphabetic)
{
nodeNum = 0;
//for all nodes in groups
foreach (NodeNFA* node, groups.at(i))
{
nodeNum++;
//get all nodes if symbol come
QList<NodeNFA*> list = node->getNextNode(symbol);
//if there is node if symbol comes
if (list.length() > 0)
{
//for all nodes
for(int k=0;k<list.length();k++)
{
//add it to temp list to bulid Union
addToList(list.at(k));
//add its name to string
toSet.append(list.at(k)->getName());
//if node finite
if (list.at(k)->isFiniteState())
finite = true;
}
}
else //if there is no node if symbol comes
{
//get the nodes name
QString str = *setToString(groups.at(i));
//get DFA node of this group and link it with separate words
nodes->value(str)->link(symbol, dfa->getSeparate_wordsState());
}
//convert to set
set = list.toSet();
//if there is node and the node is the last one in group
if (temp.count()>0 && nodeNum == groups.at(i).count())
{
//if node not in group
if (!groups.contains(temp.toSet()))
{
//there is finit node in this groups
if (finite)
{
//get the groups
QString str = *setToString(groups.at(i));
//link the DFA node of this group with Finit Word
nodes->value(str)->link(symbol, dfa->getFinit_WordsState());
//add to table
convertTable->insert(str, QPair<QString, char>(*listToString(temp), symbol));
}
else //if node not finite
{
//add to groups
groups.append(temp.toSet());
//Create new DFA node
NodeDFA* Dfa = new NodeDFA(AllNodes);
AllNodes++;
//if this is first add
if (nodes->count()-1 < 1)
{
DFANode->link(symbol, Dfa);
convertTable->insert("q0", QPair<QString, char>(*listToString(temp), symbol));
}
else //if not first add
{
//get the group and link the DFA node of this group with Finit Word
QString str = *setToString(groups.at(i));
nodes->value(str)->link(symbol, Dfa);
//add to table
convertTable->insert(str, QPair<QString, char>(*listToString(temp), symbol));
}
//get the last one and add it to nodes
QString str = *setToString(groups.last());
nodes->insert(str, Dfa);
dfa->addToState(Dfa);
}
}
else //if group already exist
{
//get group and node to connect them
QString str = *setToString(groups.at(i));
QString str2 = *setToString(temp.toSet());
nodes->value(str)->link(symbol, nodes->value(str2));
//add to table
convertTable->insert(str, QPair<QString, char>(str2, symbol));
}
temp.clear();
finite = false;
toSet.clear();
}
}
temp.clear();
}
//see the next group
i++;
}
return dfa;
}
NFA::~NFA()
{
foreach (NodeNFA* state , AllStates)
{
delete state ;
}
}