-
Notifications
You must be signed in to change notification settings - Fork 0
/
MakeNfa.cs
551 lines (472 loc) · 15.1 KB
/
MakeNfa.cs
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
namespace Lex
{
/*
* Class: MakeNfa
*/
using System;
using System.Collections;
using BitSet;
public class MakeNfa
{
/*
* Member Variables
*/
private static Spec spec;
private static Gen gen;
private static Input input;
/*
* Expands character class to include special BOL and
* EOF characters. Puts numeric index of these characters in
* input Spec.
*/
public static void Allocate_BOL_EOF(Spec s)
{
#if DEBUG
Utility.assert(Spec.NUM_PSEUDO == 2);
#endif
s.BOL = s.dtrans_ncols++;
s.EOF = s.dtrans_ncols++;
}
/*
* Function: CreateMachine
* Description: High level access function to module.
* Deposits result in input Spec.
*/
public static void CreateMachine(Gen cmg, Spec cms, Input cmi)
{
int i;
Nfa elem;
int size;
spec = cms;
gen = cmg;
input = cmi;
size = spec.states.Count;
spec.state_rules = new ArrayList[size];
for (i = 0; i < size; ++i)
{
spec.state_rules[i] = new ArrayList();
}
/*
* Initialize current token variable and create nfa.
*/
spec.nfa_start = machine();
/* Set labels in created nfa machine. */
size = spec.nfa_states.Count;
for (i = 0; i < size; ++i)
{
elem = (Nfa)spec.nfa_states[i];
elem.SetLabel(i);
}
/* Debugging output. */
#if DO_DEBUG
gen.print_nfa();
#endif
if (spec.verbose)
{
Console.WriteLine("NFA comprised of "
+ (spec.nfa_states.Count + 1)
+ " states.");
}
}
/*
* Function: discardCNfa
*/
private static void discardNfa(Nfa nfa)
{
spec.nfa_states.Remove(nfa);
}
/*
* Function: ProcessStates
*/
private static void ProcessStates(BitSet bset, Nfa current)
{
foreach (int rule in bset)
{
ArrayList p = spec.state_rules[rule];
#if DEBUG
Utility.assert(p != null);
#endif
p.Add(current);
}
}
/*
* Function: machine
* Description: Recursive descent regular expression parser.
*/
private static Nfa machine()
{
Nfa start;
Nfa p;
BitSet states;
#if DESCENT_DEBUG
Utility.enter("machine",spec.lexeme,spec.current_token);
#endif
start = Alloc.NewNfa(spec);
p = start;
states = gen.GetStates();
/* Begin: Added for states. */
spec.current_token = Gen.EOS;
gen.Advance();
/* End: Added for states. */
if (Gen.END_OF_INPUT != spec.current_token)
{
p.SetNext(rule());
ProcessStates(states, p.GetNext());
}
while (Gen.END_OF_INPUT != spec.current_token)
{
/* Make state changes HERE. */
states = gen.GetStates();
/* Begin: Added for states. */
gen.Advance();
if (Gen.END_OF_INPUT == spec.current_token)
break;
/* End: Added for states. */
p.SetSib(Alloc.NewNfa(spec));
p = p.GetSib();
p.SetNext(rule());
ProcessStates(states, p.GetNext());
}
/*
* add pseudo-rules for BOL and EOF
*/
p.SetSib(Alloc.NewNfa(spec));
p = p.GetSib();
p.SetNext(Alloc.NewNfa(spec));
Nfa pnext = p.GetNext();
pnext.SetEdge(Nfa.CCL);
pnext.SetNext(Alloc.NewNfa(spec));
pnext.SetCharSet(new CharSet());
pnext.GetCharSet().add(spec.BOL);
pnext.GetCharSet().add(spec.EOF);
// do-nothing accept rule
pnext.GetNext().SetAccept(new Accept(null, input.line_number + 1));
/* add the pseudo rules */
for (int i = 0; i < spec.states.Count; i++)
{
ArrayList srule = spec.state_rules[i];
srule.Add(pnext);
}
#if DESCENT_DEBUG
Utility.leave("machine",spec.lexeme,spec.current_token);
#endif
return start;
}
/*
* Function: rule
* Description: Recursive descent regular expression parser.
*/
private static Nfa rule()
{
NfaPair pair;
Nfa start = null;
Nfa end = null;
int anchor = Spec.NONE;
#if DESCENT_DEBUG
Utility.enter("rule", spec.lexeme, spec.current_token);
#endif
pair = Alloc.NewNfaPair();
if (Gen.AT_BOL == spec.current_token)
{
anchor = anchor | Spec.START;
gen.Advance();
expr(pair);
start = Alloc.NewNfa(spec);
start.SetEdge(spec.BOL);
start.SetNext(pair.start);
end = pair.end;
}
else
{
expr(pair);
start = pair.start;
end = pair.end;
}
if (Gen.AT_EOL == spec.current_token)
{
gen.Advance();
NfaPair nlpair = Alloc.NewNLPair(spec);
end.SetNext(Alloc.NewNfa(spec));
Nfa enext = end.GetNext();
enext.SetNext(nlpair.start);
enext.SetSib(Alloc.NewNfa(spec));
enext.GetSib().SetEdge(spec.EOF);
enext.GetSib().SetNext(nlpair.end);
end = nlpair.end;
anchor = anchor | Spec.END;
}
/* check for null rules */
if (end == null)
Error.parse_error(Error.E_ZERO, input.line_number);
/* Handle end of regular expression */
end.SetAccept(gen.packAccept());
end.SetAnchor(anchor);
#if DESCENT_DEBUG
Utility.leave("rule",spec.lexeme,spec.current_token);
#endif
return start;
}
/*
* Function: expr
* Description: Recursive descent regular expression parser.
*/
private static void expr(NfaPair pair)
{
NfaPair e2_pair;
Nfa p;
#if DESCENT_DEBUG
Utility.enter("expr",spec.lexeme,spec.current_token);
#endif
#if DEBUG
Utility.assert(null != pair);
#endif
e2_pair = Alloc.NewNfaPair();
cat_expr(pair);
while (Gen.OR == spec.current_token)
{
gen.Advance();
cat_expr(e2_pair);
p = Alloc.NewNfa(spec);
p.SetSib(e2_pair.start);
p.SetNext(pair.start);
pair.start = p;
p = Alloc.NewNfa(spec);
pair.end.SetNext(p);
e2_pair.end.SetNext(p);
pair.end = p;
}
#if DESCENT_DEBUG
Utility.leave("expr",spec.lexeme,spec.current_token);
#endif
}
/*
* Function: cat_expr
* Description: Recursive descent regular expression parser.
*/
private static void cat_expr(NfaPair pair)
{
NfaPair e2_pair;
#if DESCENT_DEBUG
Utility.enter("cat_expr",spec.lexeme,spec.current_token);
#endif
#if DEBUG
Utility.assert(null != pair);
#endif
e2_pair = Alloc.NewNfaPair();
if (first_in_cat(spec.current_token))
{
factor(pair);
}
while (first_in_cat(spec.current_token))
{
factor(e2_pair);
/* Destroy */
pair.end.mimic(e2_pair.start);
discardNfa(e2_pair.start);
pair.end = e2_pair.end;
}
#if DESCENT_DEBUG
Utility.leave("cat_expr",spec.lexeme,spec.current_token);
#endif
}
/*
* Function: first_in_cat
* Description: Recursive descent regular expression parser.
*/
private static bool first_in_cat(int token)
{
if (token == Gen.CLOSE_PAREN || token == Gen.AT_EOL
|| token == Gen.OR || token == Gen.EOS)
return false;
if (token == Gen.CLOSURE || token == Gen.PLUS_CLOSE
|| token == Gen.OPTIONAL)
{
Error.parse_error(Error.E_CLOSE, input.line_number);
return false;
}
if (token == Gen.CCL_END)
{
Error.parse_error(Error.E_BRACKET, input.line_number);
return false;
}
if (token == Gen.AT_BOL)
{
Error.parse_error(Error.E_BOL, input.line_number);
return false;
}
return true;
}
/*
* Function: factor
* Description: Recursive descent regular expression parser.
*/
private static void factor(NfaPair pair)
{
Nfa start = null;
Nfa end = null;
#if DESCENT_DEBUG
Utility.enter("factor",spec.lexeme,spec.current_token);
#endif
term(pair);
if (Gen.CLOSURE == spec.current_token
|| Gen.PLUS_CLOSE == spec.current_token
|| Gen.OPTIONAL == spec.current_token)
{
start = Alloc.NewNfa(spec);
end = Alloc.NewNfa(spec);
start.SetNext(pair.start);
pair.end.SetNext(end);
if (Gen.CLOSURE == spec.current_token
|| Gen.OPTIONAL == spec.current_token)
{
start.SetSib(end);
}
if (Gen.CLOSURE == spec.current_token
|| Gen.PLUS_CLOSE == spec.current_token)
{
pair.end.SetSib(pair.start);
}
pair.start = start;
pair.end = end;
gen.Advance();
}
#if DESCENT_DEBUG
Utility.leave("factor",spec.lexeme,spec.current_token);
#endif
}
/*
* Function: term
* Description: Recursive descent regular expression parser.
*/
private static void term(NfaPair pair)
{
Nfa start;
bool isAlphaL;
#if DESCENT_DEBUG
Utility.enter("term",spec.lexeme,spec.current_token);
#endif
if (Gen.OPEN_PAREN == spec.current_token)
{
gen.Advance();
expr(pair);
if (Gen.CLOSE_PAREN == spec.current_token)
{
gen.Advance();
}
else
{
Error.parse_error(Error.E_SYNTAX, input.line_number);
}
}
else
{
start = Alloc.NewNfa(spec);
pair.start = start;
start.SetNext(Alloc.NewNfa(spec));
pair.end = start.GetNext();
if (Gen.L == spec.current_token && Char.IsLetter(spec.lexeme))
{
isAlphaL = true;
}
else
{
isAlphaL = false;
}
if (false == (Gen.ANY == spec.current_token
|| Gen.CCL_START == spec.current_token
|| (spec.ignorecase && isAlphaL)))
{
start.SetEdge(spec.lexeme);
gen.Advance();
}
else
{
start.SetEdge(Nfa.CCL);
start.SetCharSet(new CharSet());
CharSet cset = start.GetCharSet();
/* Match case-insensitive letters using character class. */
if (spec.ignorecase && isAlphaL)
{
cset.addncase(spec.lexeme);
}
/* Match dot (.) using character class. */
else if (Gen.ANY == spec.current_token)
{
cset.add('\n');
cset.add('\r');
/* exclude BOL and EOF from character classes */
cset.add(spec.BOL);
cset.add(spec.EOF);
cset.complement();
}
else
{
gen.Advance();
if (Gen.AT_BOL == spec.current_token)
{
gen.Advance();
/* exclude BOL and EOF from character classes */
cset.add(spec.BOL);
cset.add(spec.EOF);
cset.complement();
}
if (!(Gen.CCL_END == spec.current_token))
{
dodash(cset);
}
}
gen.Advance();
}
}
#if DESCENT_DEBUG
Utility.leave("term",spec.lexeme,spec.current_token);
#endif
}
/*
* Function: dodash
* Description: Recursive descent regular expression parser.
*/
private static void dodash(CharSet set)
{
int first = -1;
#if DESCENT_DEBUG
Utility.enter("dodash",spec.lexeme,spec.current_token);
#endif
while (Gen.EOS != spec.current_token
&& Gen.CCL_END != spec.current_token)
{
// DASH loses its special meaning if it is first in class.
if (Gen.DASH == spec.current_token && -1 != first)
{
gen.Advance();
// DASH loses its special meaning if it is last in class.
if (spec.current_token == Gen.CCL_END)
{
// 'first' already in set.
set.add('-');
break;
}
for (; first <= spec.lexeme; ++first)
{
if (spec.ignorecase)
set.addncase((char)first);
else
set.add(first);
}
}
else
{
first = spec.lexeme;
if (spec.ignorecase)
set.addncase(spec.lexeme);
else
set.add(spec.lexeme);
}
gen.Advance();
}
#if DESCENT_DEBUG
Utility.leave("dodash",spec.lexeme,spec.current_token);
#endif
}
}
}