-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gen.cs
2026 lines (1806 loc) · 66.1 KB
/
Gen.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
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
namespace Lex
{
/*
* Class: Gen
*/
using System;
using System.Text;
using System.IO;
using System.Collections;
using BitSet;
public class Gen
{
/*
* Member Variables
*/
private StreamReader instream; /* Lex specification file. */
private StreamWriter outstream; /* Lexical analyzer source file. */
private Input ibuf; /* Input buffer class. */
private Hashtable tokens; /* Hashtable that maps characters to their
corresponding lexical code for
the internal lexical analyzer. */
private Spec spec; /* Spec class holds information
about the generated lexer. */
private bool init_flag; /* Flag set to true only upon
successful initialization. */
private MakeNfa makeNfa; /* NFA machine generator module. */
private Nfa2Dfa nfa2dfa; /* NFA to DFA machine (transition table)
conversion module. */
private Minimize minimize; /* Transition table compressor. */
//private SimplifyNfa simplifyNfa; /* NFA simplifier using char classes */
private Emit emit; /* Output module that emits source code
into the generated lexer file. */
private String usercode; /* temporary to hold the user supplied code */
/*
* Constants
*/
private const bool ERROR = false;
private const bool NOT_ERROR = true;
private const int BUFFER_SIZE = 1024;
/*
* Constants: Token Types
*/
public const int EOS = 1;
public const int ANY = 2;
public const int AT_BOL = 3;
public const int AT_EOL = 4;
public const int CCL_END = 5;
public const int CCL_START = 6;
public const int CLOSE_CURLY = 7;
public const int CLOSE_PAREN = 8;
public const int CLOSURE = 9;
public const int DASH = 10;
public const int END_OF_INPUT = 11;
public const int L = 12;
public const int OPEN_CURLY = 13;
public const int OPEN_PAREN = 14;
public const int OPTIONAL = 15;
public const int OR = 16;
public const int PLUS_CLOSE = 17;
/*
* Function: LexGen
*/
public Gen(String filename)
{
/* Successful initialization flag. */
init_flag = false;
/* Open input stream. */
instream = new StreamReader(
new FileStream(filename, FileMode.Open,
FileAccess.Read, FileShare.Read,
Lex.MAXBUF)
);
if (instream == null)
{
Console.WriteLine("Error: Unable to open input file " + filename + ".");
return;
}
int j = filename.LastIndexOf('\\');
if (j < 0)
j = 0;
else
j++;
String outfile = filename.Substring(j, filename.Length - j).Replace('.', '_')
+ ".cs";
Console.WriteLine("Creating output file [" + outfile + "]");
/* Open output stream. */
outstream = new StreamWriter(
new FileStream(outfile,
FileMode.Create,
FileAccess.Write,
FileShare.Write, 8192)
);
if (outstream == null)
{
Console.WriteLine("Error: Unable to open output file " + filename + ".java.");
return;
}
/* Create input buffer class. */
ibuf = new Input(instream);
/* Initialize character hash table. */
tokens = new Hashtable();
tokens['$'] = AT_EOL;
tokens['('] = OPEN_PAREN;
tokens[')'] = CLOSE_PAREN;
tokens['*'] = CLOSURE;
tokens['+'] = PLUS_CLOSE;
tokens['-'] = DASH;
tokens['.'] = ANY;
tokens['?'] = OPTIONAL;
tokens['['] = CCL_START;
tokens[']'] = CCL_END;
tokens['^'] = AT_BOL;
tokens['{'] = OPEN_CURLY;
tokens['|'] = OR;
tokens['}'] = CLOSE_CURLY;
/* Initialize spec structure. */
// spec = new Spec(this);
spec = new Spec();
/* Nfa to dfa converter. */
nfa2dfa = new Nfa2Dfa();
minimize = new Minimize();
makeNfa = new MakeNfa();
// simplifyNfa = new SimplifyNfa();
emit = new Emit();
/* Successful initialization flag. */
init_flag = true;
}
/*
* Function: generate
* Description:
*/
public void generate()
{
if (!init_flag)
{
Error.parse_error(Error.E_INIT, 0);
}
#if DEBUG
Utility.assert(this != null);
Utility.assert(outstream != null);
Utility.assert(ibuf != null);
Utility.assert(tokens != null);
Utility.assert(spec != null);
Utility.assert(init_flag);
#endif
if (spec.verbose)
{
Console.WriteLine("Processing first section -- user code.");
}
userCode();
if (ibuf.eof_reached)
{
Error.parse_error(Error.E_EOF, ibuf.line_number);
}
if (spec.verbose)
{
Console.WriteLine("Processing second section -- Lex declarations.");
}
userDeclare();
if (ibuf.eof_reached)
{
Error.parse_error(Error.E_EOF, ibuf.line_number);
}
if (spec.verbose)
{
Console.WriteLine("Processing third section -- lexical rules.");
}
userRules();
#if DO_DEBUG
Console.WriteLine("Printing DO_DEBUG header");
print_header();
#endif
if (spec.verbose)
{
Console.WriteLine("Outputting lexical analyzer code.");
}
outstream.Write("namespace " + spec.namespace_name + "\n{\n");
outstream.Write(usercode);
outstream.Write("/* test */\n");
emit.Write(spec, outstream);
outstream.Write("\n}\n");
#if OLD_DUMP_DEBUG
details();
#endif
outstream.Close();
}
/*
* Function: userCode
* Description: Process first section of specification,
* echoing it into output file.
*/
private void userCode()
{
StringBuilder sb = new StringBuilder();
if (!init_flag)
{
Error.parse_error(Error.E_INIT, 0);
}
#if DEBUG
Utility.assert(null != this);
Utility.assert(null != outstream);
Utility.assert(null != ibuf);
Utility.assert(null != tokens);
Utility.assert(null != spec);
#endif
if (ibuf.eof_reached)
{
Error.parse_error(Error.E_EOF, 0);
}
while (true)
{
if (ibuf.GetLine())
{
/* Eof reached. */
Error.parse_error(Error.E_EOF, 0);
}
if (ibuf.line_read >= 2
&& ibuf.line[0] == '%'
&& ibuf.line[1] == '%')
{
usercode = sb.ToString();
/* Discard remainder of line. */
ibuf.line_index = ibuf.line_read;
return;
}
sb.Append(new String(ibuf.line, 0, ibuf.line_read));
}
}
/*
* Function: getName
*/
private String getName()
{
/* Skip white space. */
while (ibuf.line_index < ibuf.line_read
&& Utility.IsSpace(ibuf.line[ibuf.line_index]))
{
ibuf.line_index++;
}
/* No name? */
if (ibuf.line_index >= ibuf.line_read)
{
Error.parse_error(Error.E_DIRECT, 0);
}
/* Determine length. */
int elem = ibuf.line_index;
while (elem < ibuf.line_read && !Utility.IsNewline(ibuf.line[elem]))
elem++;
/* Allocate non-terminated buffer of exact length. */
StringBuilder sb = new StringBuilder(elem - ibuf.line_index);
/* Copy. */
elem = 0;
while (ibuf.line_index < ibuf.line_read
&& !Utility.IsNewline(ibuf.line[ibuf.line_index]))
{
sb.Append(ibuf.line[ibuf.line_index]);
ibuf.line_index++;
}
return sb.ToString();
}
private const int CLASS_CODE = 0;
private const int INIT_CODE = 1;
private const int EOF_CODE = 2;
private const int EOF_VALUE_CODE = 3;
/*
* Function: packCode
* Description:
*/
//private String packCode(String start_dir, String end_dir,
// String prev_code, int prev_read, int specified)
private String packCode(String st_dir, String end_dir, String prev, int code)
{
#if DEBUG
Utility.assert(INIT_CODE == code
|| CLASS_CODE == code
|| EOF_CODE == code
|| EOF_VALUE_CODE == code
);
#endif
if (Utility.Compare(ibuf.line, st_dir) != 0)
Error.parse_error(Error.E_INTERNAL, 0);
/*
* build up the text to be stored in sb
*/
StringBuilder sb;
if (prev != null)
sb = new StringBuilder(prev, prev.Length * 2); // use any previous text
else
sb = new StringBuilder(Lex.MAXSTR);
ibuf.line_index = st_dir.Length;
while (true)
{
while (ibuf.line_index >= ibuf.line_read)
{
if (ibuf.GetLine())
Error.parse_error(Error.E_EOF, ibuf.line_number);
if (Utility.Compare(ibuf.line, end_dir) == 0)
{
ibuf.line_index = end_dir.Length - 1;
return sb.ToString();
}
}
while (ibuf.line_index < ibuf.line_read)
{
sb.Append(ibuf.line[ibuf.line_index]);
ibuf.line_index++;
}
}
}
/*
* Member Variables: Lex directives.
*/
private String state_dir = "%state";
private String char_dir = "%char";
private String line_dir = "%line";
private String class_dir = "%class";
private String implements_dir = "%implements";
private String function_dir = "%function";
private String type_dir = "%type";
private String integer_dir = "%integer";
private String intwrap_dir = "%intwrap";
private String full_dir = "%full";
private String unicode_dir = "%unicode";
private String ignorecase_dir = "%ignorecase";
private String init_code_dir = "%init{";
private String init_code_end_dir = "%init}";
private String eof_code_dir = "%eof{";
private String eof_code_end_dir = "%eof}";
private String eof_value_code_dir = "%eofval{";
private String eof_value_code_end_dir = "%eofval}";
private String class_code_dir = "%{";
private String class_code_end_dir = "%}";
private String yyeof_dir = "%yyeof";
private String public_dir = "%public";
private String namespace_dir = "%namespace";
/*
* Function: userDeclare
* Description:
*/
private void userDeclare()
{
#if DEBUG
Utility.assert(null != this);
Utility.assert(null != outstream);
Utility.assert(null != ibuf);
Utility.assert(null != tokens);
Utility.assert(null != spec);
#endif
if (ibuf.eof_reached)
{
/* End-of-file. */
Error.parse_error(Error.E_EOF, ibuf.line_number);
}
while (!ibuf.GetLine())
{
/* Look for double percent. */
if (ibuf.line_read >= 2
&& '%' == ibuf.line[0]
&& '%' == ibuf.line[1])
{
/* Mess around with line. */
for (int elem = 0; elem < ibuf.line.Length - 2; ++elem)
ibuf.line[elem] = ibuf.line[elem + 2];
ibuf.line_read = ibuf.line_read - 2;
ibuf.pushback_line = true;
/* Check for and discard empty line. */
if (ibuf.line_read == 0
|| '\r' == ibuf.line[0]
|| '\n' == ibuf.line[0])
{
ibuf.pushback_line = false;
}
return;
}
if (ibuf.line_read == 0)
continue;
if (ibuf.line[0] == '%')
{
/* Special lex declarations. */
if (1 >= ibuf.line_read)
{
Error.parse_error(Error.E_DIRECT, ibuf.line_number);
continue;
}
switch (ibuf.line[1])
{
case '{':
if (Utility.Compare(ibuf.line, class_code_dir) == 0)
{
spec.class_code = packCode(class_code_dir,
class_code_end_dir,
spec.class_code,
CLASS_CODE);
break;
}
/* Bad directive. */
Error.parse_error(Error.E_DIRECT, ibuf.line_number);
break;
case 'c':
if (Utility.Compare(ibuf.line, char_dir) == 0)
{
/* Set line counting to ON. */
ibuf.line_index = char_dir.Length;
spec.count_chars = true;
break;
}
if (Utility.Compare(ibuf.line, class_dir) == 0)
{
ibuf.line_index = class_dir.Length;
spec.class_name = getName();
break;
}
/* Bad directive. */
Error.parse_error(Error.E_DIRECT, ibuf.line_number);
break;
case 'e':
if (Utility.Compare(ibuf.line, eof_code_dir) == 0)
{
spec.eof_code = packCode(eof_code_dir,
eof_code_end_dir,
spec.eof_code,
EOF_CODE);
break;
}
if (Utility.Compare(ibuf.line, eof_value_code_dir) == 0)
{
spec.eof_value_code = packCode(eof_value_code_dir,
eof_value_code_end_dir,
spec.eof_value_code,
EOF_VALUE_CODE);
break;
}
/* Bad directive. */
Error.parse_error(Error.E_DIRECT, ibuf.line_number);
break;
case 'f':
if (Utility.Compare(ibuf.line, function_dir) == 0)
{
/* Set line counting to ON. */
ibuf.line_index = function_dir.Length;
spec.function_name = getName();
break;
}
if (Utility.Compare(ibuf.line, full_dir) == 0)
{
ibuf.line_index = full_dir.Length;
spec.dtrans_ncols = Utility.MAX_EIGHT_BIT + 1;
break;
}
/* Bad directive. */
Error.parse_error(Error.E_DIRECT, ibuf.line_number);
break;
case 'i':
if (Utility.Compare(ibuf.line, integer_dir) == 0)
{
/* Set line counting to ON. */
ibuf.line_index = integer_dir.Length;
spec.integer_type = true;
break;
}
if (Utility.Compare(ibuf.line, intwrap_dir) == 0)
{
/* Set line counting to ON. */
ibuf.line_index = integer_dir.Length;
spec.intwrap_type = true;
break;
}
if (Utility.Compare(ibuf.line, init_code_dir) == 0)
{
spec.init_code = packCode(init_code_dir,
init_code_end_dir,
spec.init_code,
INIT_CODE);
break;
}
if (Utility.Compare(ibuf.line, implements_dir) == 0)
{
ibuf.line_index = implements_dir.Length;
spec.implements_name = getName();
break;
}
if (Utility.Compare(ibuf.line, ignorecase_dir) == 0)
{
/* Set ignorecase to ON. */
ibuf.line_index = ignorecase_dir.Length;
spec.ignorecase = true;
break;
}
/* Bad directive. */
Error.parse_error(Error.E_DIRECT, ibuf.line_number);
break;
case 'l':
if (Utility.Compare(ibuf.line, line_dir) == 0)
{
/* Set line counting to ON. */
ibuf.line_index = line_dir.Length;
spec.count_lines = true;
break;
}
/* Bad directive. */
Error.parse_error(Error.E_DIRECT, ibuf.line_number);
break;
case 'p':
if (Utility.Compare(ibuf.line, public_dir) == 0)
{
/* Set public flag. */
ibuf.line_index = public_dir.Length;
spec.lex_public = true;
break;
}
/* Bad directive. */
Error.parse_error(Error.E_DIRECT, ibuf.line_number);
break;
case 's':
if (Utility.Compare(ibuf.line, state_dir) == 0)
{
/* Recognize state list. */
ibuf.line_index = state_dir.Length;
saveStates();
break;
}
/* Undefined directive. */
Error.parse_error(Error.E_DIRECT, ibuf.line_number);
break;
case 't':
if (Utility.Compare(ibuf.line, type_dir) == 0)
{
ibuf.line_index = type_dir.Length;
spec.type_name = getName();
break;
}
/* Undefined directive. */
Error.parse_error(Error.E_DIRECT, ibuf.line_number);
break;
case 'u':
if (Utility.Compare(ibuf.line, unicode_dir) == 0)
{
ibuf.line_index = unicode_dir.Length;
/* UNDONE: What to do here? */
break;
}
/* Bad directive. */
Error.parse_error(Error.E_DIRECT, ibuf.line_number);
break;
case 'y':
if (Utility.Compare(ibuf.line, yyeof_dir) == 0)
{
ibuf.line_index = yyeof_dir.Length;
spec.yyeof = true;
break;
}
/* Bad directive. */
Error.parse_error(Error.E_DIRECT, ibuf.line_number);
break;
case 'n':
if (Utility.Compare(ibuf.line, namespace_dir) == 0)
{
ibuf.line_index = namespace_dir.Length;
spec.namespace_name = getName();
break;
}
/* Bad directive. */
Error.parse_error(Error.E_DIRECT, ibuf.line_number);
break;
default:
/* Undefined directive. */
Error.parse_error(Error.E_DIRECT, ibuf.line_number);
break;
}
}
else
{
/* Regular expression macro. */
ibuf.line_index = 0;
saveMacro();
}
#if OLD_DEBUG
Console.WriteLine("Line number " + ibuf.line_number + ":");
Console.Write(new String(ibuf.line, 0,ibuf.line_read));
#endif
}
}
/*
* Function: userRules
* Description: Processes third section of Lex
* specification and creates minimized transition table.
*/
private void userRules()
{
if (false == init_flag)
{
Error.parse_error(Error.E_INIT, 0);
}
#if DEBUG
Utility.assert(null != this);
Utility.assert(null != outstream);
Utility.assert(null != ibuf);
Utility.assert(null != tokens);
Utility.assert(null != spec);
#endif
/* UNDONE: Need to handle states preceding rules. */
if (spec.verbose)
{
Console.WriteLine("Creating NFA machine representation.");
}
MakeNfa.Allocate_BOL_EOF(spec);
MakeNfa.CreateMachine(this, spec, ibuf);
SimplifyNfa.simplify(spec);
#if DUMMY
print_nfa();
#endif
#if DEBUG
Utility.assert(END_OF_INPUT == spec.current_token);
#endif
if (spec.verbose)
{
Console.WriteLine("Creating DFA transition table.");
}
Nfa2Dfa.MakeDFA(spec);
#if FOODEBUG
Console.WriteLine("Printing FOODEBUG header");
print_header();
#endif
if (spec.verbose)
{
Console.WriteLine("Minimizing DFA transition table.");
}
minimize.min_dfa(spec);
}
/*
* Function: printccl
* Description: Debuggng routine that outputs readable form
* of character class.
*/
private void printccl(CharSet cset)
{
int i;
Console.Write(" [");
for (i = 0; i < spec.dtrans_ncols; ++i)
{
if (cset.contains(i))
{
Console.Write(interp_int(i));
}
}
Console.Write(']');
}
/*
* Function: plab
* Description:
*/
private String plab(Nfa state)
{
if (null == state)
{
return "--";
}
int index = spec.nfa_states.IndexOf(state, 0, spec.nfa_states.Count);
return index.ToString();
}
/*
* Function: interp_int
* Description:
*/
private String interp_int(int i)
{
switch (i)
{
case (int)'\b':
return "\\b";
case (int)'\t':
return "\\t";
case (int)'\n':
return "\\n";
case (int)'\f':
return "\\f";
case (int)'\r':
return "\\r";
case (int)' ':
return "\\ ";
default:
{
return Char.ToString((char)i);
}
}
}
/*
* Function: print_nfa
* Description:
*/
public void print_nfa()
{
int elem;
Nfa nfa;
int j;
int vsize;
Console.WriteLine("--------------------- NFA -----------------------");
for (elem = 0; elem < spec.nfa_states.Count; elem++)
{
nfa = (Nfa)spec.nfa_states[elem];
Console.Write("Nfa state " + plab(nfa) + ": ");
if (null == nfa.GetNext())
{
Console.Write("(TERMINAL)");
}
else
{
Console.Write("--> " + plab(nfa.GetNext()));
Console.Write("--> " + plab(nfa.GetSib()));
switch (nfa.GetEdge())
{
case Nfa.CCL:
printccl(nfa.GetCharSet());
break;
case Nfa.EPSILON:
Console.Write(" EPSILON ");
break;
default:
Console.Write(" " + interp_int(nfa.GetEdge()));
break;
}
}
if (0 == elem)
{
Console.Write(" (START STATE)");
}
if (null != nfa.GetAccept())
{
Console.Write(" accepting "
+ ((0 != (nfa.GetAnchor() & Spec.START)) ? "^" : "")
+ "<"
+ nfa.GetAccept().action
+ ">"
+ ((0 != (nfa.GetAnchor() & Spec.END)) ? "$" : ""));
}
Console.WriteLine("");
}
foreach (string state in spec.states.Keys)
{
int index = (int)spec.states[state];
#if DEBUG
Utility.assert(null != state);
#endif
Console.WriteLine("State \"" + state
+ "\" has identifying index " + index + ".");
Console.Write("\tStart states of matching rules: ");
vsize = spec.state_rules[index].Count;
for (j = 0; j < vsize; ++j)
{
ArrayList a = spec.state_rules[index];
#if DEBUG
Utility.assert(null != a);
#endif
object o = a[j];
#if DEBUG
Utility.assert(null != o);
#endif
nfa = (Nfa)o;
Console.Write(spec.nfa_states.IndexOf(nfa) + " ");
}
Console.WriteLine("");
}
Console.WriteLine("-------------------- NFA ----------------------");
}
/*
* Function: getStates
* Description: Parses the state area of a rule,
* from the beginning of a line.
* < state1, state2 ... > regular_expression { action }
* Returns null on only EOF. Returns all_states,
* initialied properly to correspond to all states,
* if no states are found.
* Special Notes: This function treats commas as optional
* and permits states to be spread over multiple lines.
*/
private BitSet all_states = null;
public BitSet GetStates()
{
int start_state;
int count_state;
BitSet states;
String name;
int index;
#if DEBUG
Utility.assert(null != this);
Utility.assert(null != outstream);
Utility.assert(null != ibuf);
Utility.assert(null != tokens);
Utility.assert(null != spec);
#endif
states = null;
/* Skip white space. */
while (Utility.IsSpace(ibuf.line[ibuf.line_index]))
{
ibuf.line_index++;
while (ibuf.line_index >= ibuf.line_read)
{
/* Must just be an empty line. */
if (ibuf.GetLine())
{
/* EOF found. */
return null;
}
}
}
/* Look for states. */
if ('<' == ibuf.line[ibuf.line_index])
{
ibuf.line_index++;
states = new BitSet(); // create new BitSet
/* Parse states. */
while (true)
{
/* We may have reached the end of the line. */
while (ibuf.line_index >= ibuf.line_read)
{
if (ibuf.GetLine())
{
/* EOF found. */
Error.parse_error(Error.E_EOF, ibuf.line_number);
return states;
}
}
while (true)
{
/* Skip white space. */
while (Utility.IsSpace(ibuf.line[ibuf.line_index]))
{
ibuf.line_index++;
while (ibuf.line_index >= ibuf.line_read)
{
if (ibuf.GetLine())
{
/* EOF found. */
Error.parse_error(Error.E_EOF, ibuf.line_number);
return states;
}
}
}
if (',' != ibuf.line[ibuf.line_index])
{
break;
}
ibuf.line_index++;
}
if ('>' == ibuf.line[ibuf.line_index])
{
ibuf.line_index++;
if (ibuf.line_index < ibuf.line_read)
{
advance_stop = true;
}
return states;
}
/* Read in state name. */
start_state = ibuf.line_index;
while (false == Utility.IsSpace(ibuf.line[ibuf.line_index])
&& ',' != ibuf.line[ibuf.line_index]
&& '>' != ibuf.line[ibuf.line_index])
{
ibuf.line_index++;
if (ibuf.line_index >= ibuf.line_read)
{
/* End of line means end of state name. */
break;
}
}
count_state = ibuf.line_index - start_state;
/* Save name after checking definition. */
name = new String(ibuf.line, start_state, count_state);
Object o = spec.states[name];
if (o == null)
{
/* Uninitialized state. */
Console.WriteLine("Uninitialized State Name: [" + name + "]");
Error.parse_error(Error.E_STATE, ibuf.line_number);
}
index = (int)o;
states.Set(index, true);
}
}
if (null == all_states)
{
all_states = new BitSet(spec.states.Count, true);
}
if (ibuf.line_index < ibuf.line_read)
{
advance_stop = true;
}