-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNFA.cc
378 lines (301 loc) · 7.51 KB
/
NFA.cc
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
// See the file "COPYING" in the main distribution directory for copyright.
#include "zeek/NFA.h"
#include "zeek/zeek-config.h"
#include <algorithm>
#include "zeek/Desc.h"
#include "zeek/EquivClass.h"
#include "zeek/IntSet.h"
namespace zeek::detail
{
static int nfa_state_id = 0;
NFA_State::NFA_State(int arg_sym, EquivClass* ec)
{
sym = arg_sym;
ccl = nullptr;
accept = NO_ACCEPT;
first_trans_is_back_ref = false;
mark = nullptr;
epsclosure = nullptr;
id = ++nfa_state_id;
// Fix up equivalence classes based on this transition. Note that any
// character which has its own transition gets its own equivalence
// class. Thus only characters which are only in character classes
// have a chance at being in the same equivalence class. E.g. "a|b"
// puts 'a' and 'b' into two different equivalence classes. "[ab]"
// puts them in the same equivalence class (barring other differences
// elsewhere in the input).
if ( ec && sym != SYM_EPSILON /* no associated symbol */ )
ec->UniqueChar(sym);
}
NFA_State::NFA_State(CCL* arg_ccl)
{
sym = SYM_CCL;
ccl = arg_ccl;
accept = NO_ACCEPT;
first_trans_is_back_ref = false;
mark = nullptr;
id = ++nfa_state_id;
epsclosure = nullptr;
}
NFA_State::~NFA_State()
{
for ( int i = 0; i < xtions.length(); ++i )
if ( i > 0 || ! first_trans_is_back_ref )
Unref(xtions[i]);
delete epsclosure;
}
void NFA_State::AddXtionsTo(NFA_state_list* ns)
{
for ( int i = 0; i < xtions.length(); ++i )
ns->push_back(xtions[i]);
}
NFA_State* NFA_State::DeepCopy()
{
if ( mark )
{
Ref(mark);
return mark;
}
NFA_State* copy = ccl ? new NFA_State(ccl) : new NFA_State(sym, nullptr);
SetMark(copy);
for ( int i = 0; i < xtions.length(); ++i )
copy->AddXtion(xtions[i]->DeepCopy());
return copy;
}
void NFA_State::ClearMarks()
{
if ( mark )
{
SetMark(nullptr);
for ( int i = 0; i < xtions.length(); ++i )
xtions[i]->ClearMarks();
}
}
NFA_state_list* NFA_State::EpsilonClosure()
{
if ( epsclosure )
return epsclosure;
epsclosure = new NFA_state_list;
NFA_state_list states;
states.push_back(this);
SetMark(this);
int i;
for ( i = 0; i < states.length(); ++i )
{
NFA_State* ns = states[i];
if ( ns->TransSym() == SYM_EPSILON )
{
NFA_state_list* x = ns->Transitions();
for ( int j = 0; j < x->length(); ++j )
{
NFA_State* nxt = (*x)[j];
if ( ! nxt->Mark() )
{
states.push_back(nxt);
nxt->SetMark(nxt);
}
}
if ( ns->Accept() != NO_ACCEPT )
epsclosure->push_back(ns);
}
else
// Non-epsilon transition - keep it.
epsclosure->push_back(ns);
}
// Clear out markers.
for ( i = 0; i < states.length(); ++i )
states[i]->SetMark(nullptr);
// Make it fit.
epsclosure->resize(0);
return epsclosure;
}
void NFA_State::Describe(ODesc* d) const
{
d->Add("NFA state");
}
void NFA_State::Dump(FILE* f)
{
if ( mark )
return;
fprintf(f, "NFA state %d, sym = %d, accept = %d:\n", id, sym, accept);
for ( int i = 0; i < xtions.length(); ++i )
fprintf(f, "\ttransition to %d\n", xtions[i]->ID());
SetMark(this);
for ( int i = 0; i < xtions.length(); ++i )
xtions[i]->Dump(f);
}
unsigned int NFA_State::TotalMemoryAllocation() const
{
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
return padded_sizeof(*this) + xtions.MemoryAllocation() - padded_sizeof(xtions) +
(epsclosure ? epsclosure->MemoryAllocation() : 0);
#pragma GCC diagnostic pop
}
NFA_Machine::NFA_Machine(NFA_State* first, NFA_State* final)
{
first_state = first;
final_state = final ? final : first;
eol = bol = 0;
}
NFA_Machine::~NFA_Machine()
{
Unref(first_state);
}
void NFA_Machine::InsertEpsilon()
{
NFA_State* eps = new EpsilonState();
eps->AddXtion(first_state);
first_state = eps;
}
void NFA_Machine::AppendEpsilon()
{
AppendState(new EpsilonState());
}
void NFA_Machine::AddAccept(int accept_val)
{
// Hang the accepting number off an epsilon state. If it is associated
// with a state that has a non-epsilon out-transition, then the state
// will accept BEFORE it makes that transition, i.e., one character
// too soon.
if ( final_state->TransSym() != SYM_EPSILON )
AppendState(new EpsilonState());
final_state->SetAccept(accept_val);
}
void NFA_Machine::LinkCopies(int n)
{
if ( n <= 0 )
return;
// Make all the copies before doing any appending, otherwise
// subsequent DuplicateMachine()'s will include the extra
// copies!
NFA_Machine** copies = new NFA_Machine*[n];
int i;
for ( i = 0; i < n; ++i )
copies[i] = DuplicateMachine();
for ( i = 0; i < n; ++i )
AppendMachine(copies[i]);
delete[] copies;
}
NFA_Machine* NFA_Machine::DuplicateMachine()
{
NFA_State* new_first_state = first_state->DeepCopy();
NFA_Machine* new_m = new NFA_Machine(new_first_state, final_state->Mark());
first_state->ClearMarks();
return new_m;
}
void NFA_Machine::AppendState(NFA_State* s)
{
final_state->AddXtion(s);
final_state = s;
}
void NFA_Machine::AppendMachine(NFA_Machine* m)
{
AppendEpsilon();
final_state->AddXtion(m->FirstState());
final_state = m->FinalState();
Ref(m->FirstState()); // so states stay around after the following
Unref(m);
}
void NFA_Machine::MakeOptional()
{
InsertEpsilon();
AppendEpsilon();
first_state->AddXtion(final_state);
Ref(final_state);
}
void NFA_Machine::MakePositiveClosure()
{
AppendEpsilon();
final_state->AddXtion(first_state);
// Don't Ref the state the final epsilon points to, otherwise we'll
// have reference cycles that lead to leaks.
final_state->SetFirstTransIsBackRef();
}
void NFA_Machine::MakeRepl(int lower, int upper)
{
NFA_Machine* dup = nullptr;
if ( upper > lower || upper == NO_UPPER_BOUND )
dup = DuplicateMachine();
LinkCopies(lower - 1);
if ( upper == NO_UPPER_BOUND )
{
dup->MakeClosure();
AppendMachine(dup);
return;
}
while ( upper > lower )
{
NFA_Machine* dup2;
if ( --upper == lower )
// Don't need "dup" for any further copies
dup2 = dup;
else
dup2 = dup->DuplicateMachine();
dup2->MakeOptional();
AppendMachine(dup2);
}
}
void NFA_Machine::Describe(ODesc* d) const
{
d->Add("NFA machine");
}
void NFA_Machine::Dump(FILE* f)
{
first_state->Dump(f);
first_state->ClearMarks();
}
NFA_Machine* make_alternate(NFA_Machine* m1, NFA_Machine* m2)
{
if ( ! m1 )
return m2;
if ( ! m2 )
return m1;
NFA_State* first = new EpsilonState();
NFA_State* last = new EpsilonState();
first->AddXtion(m1->FirstState());
first->AddXtion(m2->FirstState());
m1->AppendState(last);
m2->AppendState(last);
Ref(last);
// Keep these around.
Ref(m1->FirstState());
Ref(m2->FirstState());
Unref(m1);
Unref(m2);
return new NFA_Machine(first, last);
}
NFA_state_list* epsilon_closure(NFA_state_list* states)
{
// We just keep one of this as it may get quite large.
static IntSet closuremap;
closuremap.Clear();
NFA_state_list* closure = new NFA_state_list;
for ( int i = 0; i < states->length(); ++i )
{
NFA_state_list* stateclosure = (*states)[i]->EpsilonClosure();
for ( int j = 0; j < stateclosure->length(); ++j )
{
NFA_State* ns = (*stateclosure)[j];
if ( ! closuremap.Contains(ns->ID()) )
{
closuremap.Insert(ns->ID());
closure->push_back(ns);
}
}
}
// Sort all of the closures in the list by ID
std::sort(closure->begin(), closure->end(), NFA_state_cmp_neg);
// Make it fit.
closure->resize(0);
delete states;
return closure;
}
bool NFA_state_cmp_neg(const NFA_State* v1, const NFA_State* v2)
{
if ( v1->ID() < v2->ID() )
return true;
else
return false;
}
} // namespace zeek::detail