forked from opnsense/src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen.c
2170 lines (1745 loc) · 54.4 KB
/
gen.c
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
/* gen - actual generation (writing) of flex scanners */
/* Copyright (c) 1990 The Regents of the University of California. */
/* All rights reserved. */
/* This code is derived from software contributed to Berkeley by */
/* Vern Paxson. */
/* The United States Government has rights in this work pursuant */
/* to contract no. DE-AC03-76SF00098 between the United States */
/* Department of Energy and the University of California. */
/* This file is part of flex. */
/* Redistribution and use in source and binary forms, with or without */
/* modification, are permitted provided that the following conditions */
/* are met: */
/* 1. Redistributions of source code must retain the above copyright */
/* notice, this list of conditions and the following disclaimer. */
/* 2. Redistributions in binary form must reproduce the above copyright */
/* notice, this list of conditions and the following disclaimer in the */
/* documentation and/or other materials provided with the distribution. */
/* Neither the name of the University nor the names of its contributors */
/* may be used to endorse or promote products derived from this software */
/* without specific prior written permission. */
/* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR */
/* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED */
/* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR */
/* PURPOSE. */
#include "flexdef.h"
#include "tables.h"
/* declare functions that have forward references */
void gen_next_state PROTO ((int));
void genecs PROTO ((void));
void indent_put2s PROTO ((const char *, const char *));
void indent_puts PROTO ((const char *));
static int indent_level = 0; /* each level is 8 spaces */
#define indent_up() (++indent_level)
#define indent_down() (--indent_level)
#define set_indent(indent_val) indent_level = indent_val
/* Almost everything is done in terms of arrays starting at 1, so provide
* a null entry for the zero element of all C arrays. (The exception
* to this is that the fast table representation generally uses the
* 0 elements of its arrays, too.)
*/
static const char *get_int16_decl (void)
{
return (gentables)
? "static yyconst flex_int16_t %s[%d] =\n { 0,\n"
: "static yyconst flex_int16_t * %s = 0;\n";
}
static const char *get_int32_decl (void)
{
return (gentables)
? "static yyconst flex_int32_t %s[%d] =\n { 0,\n"
: "static yyconst flex_int32_t * %s = 0;\n";
}
static const char *get_state_decl (void)
{
return (gentables)
? "static yyconst yy_state_type %s[%d] =\n { 0,\n"
: "static yyconst yy_state_type * %s = 0;\n";
}
/* Indent to the current level. */
void do_indent ()
{
int i = indent_level * 8;
while (i >= 8) {
outc ('\t');
i -= 8;
}
while (i > 0) {
outc (' ');
--i;
}
}
/** Make the table for possible eol matches.
* @return the newly allocated rule_can_match_eol table
*/
static struct yytbl_data *mkeoltbl (void)
{
int i;
flex_int8_t *tdata = 0;
struct yytbl_data *tbl;
tbl = (struct yytbl_data *) calloc (1, sizeof (struct yytbl_data));
yytbl_data_init (tbl, YYTD_ID_RULE_CAN_MATCH_EOL);
tbl->td_flags = YYTD_DATA8;
tbl->td_lolen = num_rules + 1;
tbl->td_data = tdata =
(flex_int8_t *) calloc (tbl->td_lolen, sizeof (flex_int8_t));
for (i = 1; i <= num_rules; i++)
tdata[i] = rule_has_nl[i] ? 1 : 0;
buf_prints (&yydmap_buf,
"\t{YYTD_ID_RULE_CAN_MATCH_EOL, (void**)&yy_rule_can_match_eol, sizeof(%s)},\n",
"flex_int32_t");
return tbl;
}
/* Generate the table for possible eol matches. */
static void geneoltbl (void)
{
int i;
outn ("m4_ifdef( [[M4_YY_USE_LINENO]],[[");
outn ("/* Table of booleans, true if rule could match eol. */");
out_str_dec (get_int32_decl (), "yy_rule_can_match_eol",
num_rules + 1);
if (gentables) {
for (i = 1; i <= num_rules; i++) {
out_dec ("%d, ", rule_has_nl[i] ? 1 : 0);
/* format nicely, 20 numbers per line. */
if ((i % 20) == 19)
out ("\n ");
}
out (" };\n");
}
outn ("]])");
}
/* Generate the code to keep backing-up information. */
void gen_backing_up ()
{
if (reject || num_backing_up == 0)
return;
if (fullspd)
indent_puts ("if ( yy_current_state[-1].yy_nxt )");
else
indent_puts ("if ( yy_accept[yy_current_state] )");
indent_up ();
indent_puts ("{");
indent_puts ("YY_G(yy_last_accepting_state) = yy_current_state;");
indent_puts ("YY_G(yy_last_accepting_cpos) = yy_cp;");
indent_puts ("}");
indent_down ();
}
/* Generate the code to perform the backing up. */
void gen_bu_action ()
{
if (reject || num_backing_up == 0)
return;
set_indent (3);
indent_puts ("case 0: /* must back up */");
indent_puts ("/* undo the effects of YY_DO_BEFORE_ACTION */");
indent_puts ("*yy_cp = YY_G(yy_hold_char);");
if (fullspd || fulltbl)
indent_puts ("yy_cp = YY_G(yy_last_accepting_cpos) + 1;");
else
/* Backing-up info for compressed tables is taken \after/
* yy_cp has been incremented for the next state.
*/
indent_puts ("yy_cp = YY_G(yy_last_accepting_cpos);");
indent_puts ("yy_current_state = YY_G(yy_last_accepting_state);");
indent_puts ("goto yy_find_action;");
outc ('\n');
set_indent (0);
}
/** mkctbl - make full speed compressed transition table
* This is an array of structs; each struct a pair of integers.
* You should call mkssltbl() immediately after this.
* Then, I think, mkecstbl(). Arrrg.
* @return the newly allocated trans table
*/
static struct yytbl_data *mkctbl (void)
{
int i;
struct yytbl_data *tbl = 0;
flex_int32_t *tdata = 0, curr = 0;
int end_of_buffer_action = num_rules + 1;
buf_prints (&yydmap_buf,
"\t{YYTD_ID_TRANSITION, (void**)&yy_transition, sizeof(%s)},\n",
((tblend + numecs + 1) >= INT16_MAX
|| long_align) ? "flex_int32_t" : "flex_int16_t");
tbl = (struct yytbl_data *) calloc (1, sizeof (struct yytbl_data));
yytbl_data_init (tbl, YYTD_ID_TRANSITION);
tbl->td_flags = YYTD_DATA32 | YYTD_STRUCT;
tbl->td_hilen = 0;
tbl->td_lolen = tblend + numecs + 1; /* number of structs */
tbl->td_data = tdata =
(flex_int32_t *) calloc (tbl->td_lolen * 2, sizeof (flex_int32_t));
/* We want the transition to be represented as the offset to the
* next state, not the actual state number, which is what it currently
* is. The offset is base[nxt[i]] - (base of current state)]. That's
* just the difference between the starting points of the two involved
* states (to - from).
*
* First, though, we need to find some way to put in our end-of-buffer
* flags and states. We do this by making a state with absolutely no
* transitions. We put it at the end of the table.
*/
/* We need to have room in nxt/chk for two more slots: One for the
* action and one for the end-of-buffer transition. We now *assume*
* that we're guaranteed the only character we'll try to index this
* nxt/chk pair with is EOB, i.e., 0, so we don't have to make sure
* there's room for jam entries for other characters.
*/
while (tblend + 2 >= current_max_xpairs)
expand_nxt_chk ();
while (lastdfa + 1 >= current_max_dfas)
increase_max_dfas ();
base[lastdfa + 1] = tblend + 2;
nxt[tblend + 1] = end_of_buffer_action;
chk[tblend + 1] = numecs + 1;
chk[tblend + 2] = 1; /* anything but EOB */
/* So that "make test" won't show arb. differences. */
nxt[tblend + 2] = 0;
/* Make sure every state has an end-of-buffer transition and an
* action #.
*/
for (i = 0; i <= lastdfa; ++i) {
int anum = dfaacc[i].dfaacc_state;
int offset = base[i];
chk[offset] = EOB_POSITION;
chk[offset - 1] = ACTION_POSITION;
nxt[offset - 1] = anum; /* action number */
}
for (i = 0; i <= tblend; ++i) {
if (chk[i] == EOB_POSITION) {
tdata[curr++] = 0;
tdata[curr++] = base[lastdfa + 1] - i;
}
else if (chk[i] == ACTION_POSITION) {
tdata[curr++] = 0;
tdata[curr++] = nxt[i];
}
else if (chk[i] > numecs || chk[i] == 0) {
tdata[curr++] = 0;
tdata[curr++] = 0;
}
else { /* verify, transition */
tdata[curr++] = chk[i];
tdata[curr++] = base[nxt[i]] - (i - chk[i]);
}
}
/* Here's the final, end-of-buffer state. */
tdata[curr++] = chk[tblend + 1];
tdata[curr++] = nxt[tblend + 1];
tdata[curr++] = chk[tblend + 2];
tdata[curr++] = nxt[tblend + 2];
return tbl;
}
/** Make start_state_list table.
* @return the newly allocated start_state_list table
*/
static struct yytbl_data *mkssltbl (void)
{
struct yytbl_data *tbl = 0;
flex_int32_t *tdata = 0;
flex_int32_t i;
tbl = (struct yytbl_data *) calloc (1, sizeof (struct yytbl_data));
yytbl_data_init (tbl, YYTD_ID_START_STATE_LIST);
tbl->td_flags = YYTD_DATA32 | YYTD_PTRANS;
tbl->td_hilen = 0;
tbl->td_lolen = lastsc * 2 + 1;
tbl->td_data = tdata =
(flex_int32_t *) calloc (tbl->td_lolen, sizeof (flex_int32_t));
for (i = 0; i <= lastsc * 2; ++i)
tdata[i] = base[i];
buf_prints (&yydmap_buf,
"\t{YYTD_ID_START_STATE_LIST, (void**)&yy_start_state_list, sizeof(%s)},\n",
"struct yy_trans_info*");
return tbl;
}
/* genctbl - generates full speed compressed transition table */
void genctbl ()
{
int i;
int end_of_buffer_action = num_rules + 1;
/* Table of verify for transition and offset to next state. */
if (gentables)
out_dec ("static yyconst struct yy_trans_info yy_transition[%d] =\n {\n", tblend + numecs + 1);
else
outn ("static yyconst struct yy_trans_info *yy_transition = 0;");
/* We want the transition to be represented as the offset to the
* next state, not the actual state number, which is what it currently
* is. The offset is base[nxt[i]] - (base of current state)]. That's
* just the difference between the starting points of the two involved
* states (to - from).
*
* First, though, we need to find some way to put in our end-of-buffer
* flags and states. We do this by making a state with absolutely no
* transitions. We put it at the end of the table.
*/
/* We need to have room in nxt/chk for two more slots: One for the
* action and one for the end-of-buffer transition. We now *assume*
* that we're guaranteed the only character we'll try to index this
* nxt/chk pair with is EOB, i.e., 0, so we don't have to make sure
* there's room for jam entries for other characters.
*/
while (tblend + 2 >= current_max_xpairs)
expand_nxt_chk ();
while (lastdfa + 1 >= current_max_dfas)
increase_max_dfas ();
base[lastdfa + 1] = tblend + 2;
nxt[tblend + 1] = end_of_buffer_action;
chk[tblend + 1] = numecs + 1;
chk[tblend + 2] = 1; /* anything but EOB */
/* So that "make test" won't show arb. differences. */
nxt[tblend + 2] = 0;
/* Make sure every state has an end-of-buffer transition and an
* action #.
*/
for (i = 0; i <= lastdfa; ++i) {
int anum = dfaacc[i].dfaacc_state;
int offset = base[i];
chk[offset] = EOB_POSITION;
chk[offset - 1] = ACTION_POSITION;
nxt[offset - 1] = anum; /* action number */
}
for (i = 0; i <= tblend; ++i) {
if (chk[i] == EOB_POSITION)
transition_struct_out (0, base[lastdfa + 1] - i);
else if (chk[i] == ACTION_POSITION)
transition_struct_out (0, nxt[i]);
else if (chk[i] > numecs || chk[i] == 0)
transition_struct_out (0, 0); /* unused slot */
else /* verify, transition */
transition_struct_out (chk[i],
base[nxt[i]] - (i -
chk[i]));
}
/* Here's the final, end-of-buffer state. */
transition_struct_out (chk[tblend + 1], nxt[tblend + 1]);
transition_struct_out (chk[tblend + 2], nxt[tblend + 2]);
if (gentables)
outn (" };\n");
/* Table of pointers to start states. */
if (gentables)
out_dec ("static yyconst struct yy_trans_info *yy_start_state_list[%d] =\n", lastsc * 2 + 1);
else
outn ("static yyconst struct yy_trans_info **yy_start_state_list =0;");
if (gentables) {
outn (" {");
for (i = 0; i <= lastsc * 2; ++i)
out_dec (" &yy_transition[%d],\n", base[i]);
dataend ();
}
if (useecs)
genecs ();
}
/* mkecstbl - Make equivalence-class tables. */
static struct yytbl_data *mkecstbl (void)
{
int i;
struct yytbl_data *tbl = 0;
flex_int32_t *tdata = 0;
tbl = (struct yytbl_data *) calloc (1, sizeof (struct yytbl_data));
yytbl_data_init (tbl, YYTD_ID_EC);
tbl->td_flags |= YYTD_DATA32;
tbl->td_hilen = 0;
tbl->td_lolen = csize;
tbl->td_data = tdata =
(flex_int32_t *) calloc (tbl->td_lolen, sizeof (flex_int32_t));
for (i = 1; i < csize; ++i) {
ecgroup[i] = ABS (ecgroup[i]);
tdata[i] = ecgroup[i];
}
buf_prints (&yydmap_buf,
"\t{YYTD_ID_EC, (void**)&yy_ec, sizeof(%s)},\n",
"flex_int32_t");
return tbl;
}
/* Generate equivalence-class tables. */
void genecs ()
{
int i, j;
int numrows;
out_str_dec (get_int32_decl (), "yy_ec", csize);
for (i = 1; i < csize; ++i) {
ecgroup[i] = ABS (ecgroup[i]);
mkdata (ecgroup[i]);
}
dataend ();
if (trace) {
fputs (_("\n\nEquivalence Classes:\n\n"), stderr);
numrows = csize / 8;
for (j = 0; j < numrows; ++j) {
for (i = j; i < csize; i = i + numrows) {
fprintf (stderr, "%4s = %-2d",
readable_form (i), ecgroup[i]);
putc (' ', stderr);
}
putc ('\n', stderr);
}
}
}
/* Generate the code to find the action number. */
void gen_find_action ()
{
if (fullspd)
indent_puts ("yy_act = yy_current_state[-1].yy_nxt;");
else if (fulltbl)
indent_puts ("yy_act = yy_accept[yy_current_state];");
else if (reject) {
indent_puts ("yy_current_state = *--YY_G(yy_state_ptr);");
indent_puts ("YY_G(yy_lp) = yy_accept[yy_current_state];");
outn ("goto find_rule; /* avoid `defined but not used' warning */");
outn ("find_rule: /* we branch to this label when backing up */");
indent_puts
("for ( ; ; ) /* until we find what rule we matched */");
indent_up ();
indent_puts ("{");
indent_puts
("if ( YY_G(yy_lp) && YY_G(yy_lp) < yy_accept[yy_current_state + 1] )");
indent_up ();
indent_puts ("{");
indent_puts ("yy_act = yy_acclist[YY_G(yy_lp)];");
if (variable_trailing_context_rules) {
indent_puts
("if ( yy_act & YY_TRAILING_HEAD_MASK ||");
indent_puts (" YY_G(yy_looking_for_trail_begin) )");
indent_up ();
indent_puts ("{");
indent_puts
("if ( yy_act == YY_G(yy_looking_for_trail_begin) )");
indent_up ();
indent_puts ("{");
indent_puts ("YY_G(yy_looking_for_trail_begin) = 0;");
indent_puts ("yy_act &= ~YY_TRAILING_HEAD_MASK;");
indent_puts ("break;");
indent_puts ("}");
indent_down ();
indent_puts ("}");
indent_down ();
indent_puts
("else if ( yy_act & YY_TRAILING_MASK )");
indent_up ();
indent_puts ("{");
indent_puts
("YY_G(yy_looking_for_trail_begin) = yy_act & ~YY_TRAILING_MASK;");
indent_puts
("YY_G(yy_looking_for_trail_begin) |= YY_TRAILING_HEAD_MASK;");
if (real_reject) {
/* Remember matched text in case we back up
* due to REJECT.
*/
indent_puts
("YY_G(yy_full_match) = yy_cp;");
indent_puts
("YY_G(yy_full_state) = YY_G(yy_state_ptr);");
indent_puts ("YY_G(yy_full_lp) = YY_G(yy_lp);");
}
indent_puts ("}");
indent_down ();
indent_puts ("else");
indent_up ();
indent_puts ("{");
indent_puts ("YY_G(yy_full_match) = yy_cp;");
indent_puts
("YY_G(yy_full_state) = YY_G(yy_state_ptr);");
indent_puts ("YY_G(yy_full_lp) = YY_G(yy_lp);");
indent_puts ("break;");
indent_puts ("}");
indent_down ();
indent_puts ("++YY_G(yy_lp);");
indent_puts ("goto find_rule;");
}
else {
/* Remember matched text in case we back up due to
* trailing context plus REJECT.
*/
indent_up ();
indent_puts ("{");
indent_puts ("YY_G(yy_full_match) = yy_cp;");
indent_puts ("break;");
indent_puts ("}");
indent_down ();
}
indent_puts ("}");
indent_down ();
indent_puts ("--yy_cp;");
/* We could consolidate the following two lines with those at
* the beginning, but at the cost of complaints that we're
* branching inside a loop.
*/
indent_puts ("yy_current_state = *--YY_G(yy_state_ptr);");
indent_puts ("YY_G(yy_lp) = yy_accept[yy_current_state];");
indent_puts ("}");
indent_down ();
}
else { /* compressed */
indent_puts ("yy_act = yy_accept[yy_current_state];");
if (interactive && !reject) {
/* Do the guaranteed-needed backing up to figure out
* the match.
*/
indent_puts ("if ( yy_act == 0 )");
indent_up ();
indent_puts ("{ /* have to back up */");
indent_puts
("yy_cp = YY_G(yy_last_accepting_cpos);");
indent_puts
("yy_current_state = YY_G(yy_last_accepting_state);");
indent_puts
("yy_act = yy_accept[yy_current_state];");
indent_puts ("}");
indent_down ();
}
}
}
/* mkftbl - make the full table and return the struct .
* you should call mkecstbl() after this.
*/
struct yytbl_data *mkftbl (void)
{
int i;
int end_of_buffer_action = num_rules + 1;
struct yytbl_data *tbl;
flex_int32_t *tdata = 0;
tbl = (struct yytbl_data *) calloc (1, sizeof (struct yytbl_data));
yytbl_data_init (tbl, YYTD_ID_ACCEPT);
tbl->td_flags |= YYTD_DATA32;
tbl->td_hilen = 0; /* it's a one-dimensional array */
tbl->td_lolen = lastdfa + 1;
tbl->td_data = tdata =
(flex_int32_t *) calloc (tbl->td_lolen, sizeof (flex_int32_t));
dfaacc[end_of_buffer_state].dfaacc_state = end_of_buffer_action;
for (i = 1; i <= lastdfa; ++i) {
int anum = dfaacc[i].dfaacc_state;
tdata[i] = anum;
if (trace && anum)
fprintf (stderr, _("state # %d accepts: [%d]\n"),
i, anum);
}
buf_prints (&yydmap_buf,
"\t{YYTD_ID_ACCEPT, (void**)&yy_accept, sizeof(%s)},\n",
long_align ? "flex_int32_t" : "flex_int16_t");
return tbl;
}
/* genftbl - generate full transition table */
void genftbl ()
{
int i;
int end_of_buffer_action = num_rules + 1;
out_str_dec (long_align ? get_int32_decl () : get_int16_decl (),
"yy_accept", lastdfa + 1);
dfaacc[end_of_buffer_state].dfaacc_state = end_of_buffer_action;
for (i = 1; i <= lastdfa; ++i) {
int anum = dfaacc[i].dfaacc_state;
mkdata (anum);
if (trace && anum)
fprintf (stderr, _("state # %d accepts: [%d]\n"),
i, anum);
}
dataend ();
if (useecs)
genecs ();
/* Don't have to dump the actual full table entries - they were
* created on-the-fly.
*/
}
/* Generate the code to find the next compressed-table state. */
void gen_next_compressed_state (char_map)
char *char_map;
{
indent_put2s ("YY_CHAR yy_c = %s;", char_map);
/* Save the backing-up info \before/ computing the next state
* because we always compute one more state than needed - we
* always proceed until we reach a jam state
*/
gen_backing_up ();
indent_puts
("while ( yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state )");
indent_up ();
indent_puts ("{");
indent_puts ("yy_current_state = (int) yy_def[yy_current_state];");
if (usemecs) {
/* We've arrange it so that templates are never chained
* to one another. This means we can afford to make a
* very simple test to see if we need to convert to
* yy_c's meta-equivalence class without worrying
* about erroneously looking up the meta-equivalence
* class twice
*/
do_indent ();
/* lastdfa + 2 is the beginning of the templates */
out_dec ("if ( yy_current_state >= %d )\n", lastdfa + 2);
indent_up ();
indent_puts ("yy_c = yy_meta[(unsigned int) yy_c];");
indent_down ();
}
indent_puts ("}");
indent_down ();
indent_puts
("yy_current_state = yy_nxt[yy_base[yy_current_state] + (unsigned int) yy_c];");
}
/* Generate the code to find the next match. */
void gen_next_match ()
{
/* NOTE - changes in here should be reflected in gen_next_state() and
* gen_NUL_trans().
*/
char *char_map = useecs ?
"yy_ec[YY_SC_TO_UI(*yy_cp)] " : "YY_SC_TO_UI(*yy_cp)";
char *char_map_2 = useecs ?
"yy_ec[YY_SC_TO_UI(*++yy_cp)] " : "YY_SC_TO_UI(*++yy_cp)";
if (fulltbl) {
if (gentables)
indent_put2s
("while ( (yy_current_state = yy_nxt[yy_current_state][ %s ]) > 0 )",
char_map);
else
indent_put2s
("while ( (yy_current_state = yy_nxt[yy_current_state*YY_NXT_LOLEN + %s ]) > 0 )",
char_map);
indent_up ();
if (num_backing_up > 0) {
indent_puts ("{");
gen_backing_up ();
outc ('\n');
}
indent_puts ("++yy_cp;");
if (num_backing_up > 0)
indent_puts ("}");
indent_down ();
outc ('\n');
indent_puts ("yy_current_state = -yy_current_state;");
}
else if (fullspd) {
indent_puts ("{");
indent_puts
("yyconst struct yy_trans_info *yy_trans_info;\n");
indent_puts ("YY_CHAR yy_c;\n");
indent_put2s ("for ( yy_c = %s;", char_map);
indent_puts
(" (yy_trans_info = &yy_current_state[(unsigned int) yy_c])->");
indent_puts ("yy_verify == yy_c;");
indent_put2s (" yy_c = %s )", char_map_2);
indent_up ();
if (num_backing_up > 0)
indent_puts ("{");
indent_puts ("yy_current_state += yy_trans_info->yy_nxt;");
if (num_backing_up > 0) {
outc ('\n');
gen_backing_up ();
indent_puts ("}");
}
indent_down ();
indent_puts ("}");
}
else { /* compressed */
indent_puts ("do");
indent_up ();
indent_puts ("{");
gen_next_state (false);
indent_puts ("++yy_cp;");
indent_puts ("}");
indent_down ();
do_indent ();
if (interactive)
out_dec ("while ( yy_base[yy_current_state] != %d );\n", jambase);
else
out_dec ("while ( yy_current_state != %d );\n",
jamstate);
if (!reject && !interactive) {
/* Do the guaranteed-needed backing up to figure out
* the match.
*/
indent_puts
("yy_cp = YY_G(yy_last_accepting_cpos);");
indent_puts
("yy_current_state = YY_G(yy_last_accepting_state);");
}
}
}
/* Generate the code to find the next state. */
void gen_next_state (worry_about_NULs)
int worry_about_NULs;
{ /* NOTE - changes in here should be reflected in gen_next_match() */
char char_map[256];
if (worry_about_NULs && !nultrans) {
if (useecs)
snprintf (char_map, sizeof(char_map),
"(*yy_cp ? yy_ec[YY_SC_TO_UI(*yy_cp)] : %d)",
NUL_ec);
else
snprintf (char_map, sizeof(char_map),
"(*yy_cp ? YY_SC_TO_UI(*yy_cp) : %d)",
NUL_ec);
}
else
strcpy (char_map, useecs ?
"yy_ec[YY_SC_TO_UI(*yy_cp)] " :
"YY_SC_TO_UI(*yy_cp)");
if (worry_about_NULs && nultrans) {
if (!fulltbl && !fullspd)
/* Compressed tables back up *before* they match. */
gen_backing_up ();
indent_puts ("if ( *yy_cp )");
indent_up ();
indent_puts ("{");
}
if (fulltbl) {
if (gentables)
indent_put2s
("yy_current_state = yy_nxt[yy_current_state][%s];",
char_map);
else
indent_put2s
("yy_current_state = yy_nxt[yy_current_state*YY_NXT_LOLEN + %s];",
char_map);
}
else if (fullspd)
indent_put2s
("yy_current_state += yy_current_state[%s].yy_nxt;",
char_map);
else
gen_next_compressed_state (char_map);
if (worry_about_NULs && nultrans) {
indent_puts ("}");
indent_down ();
indent_puts ("else");
indent_up ();
indent_puts
("yy_current_state = yy_NUL_trans[yy_current_state];");
indent_down ();
}
if (fullspd || fulltbl)
gen_backing_up ();
if (reject)
indent_puts ("*YY_G(yy_state_ptr)++ = yy_current_state;");
}
/* Generate the code to make a NUL transition. */
void gen_NUL_trans ()
{ /* NOTE - changes in here should be reflected in gen_next_match() */
/* Only generate a definition for "yy_cp" if we'll generate code
* that uses it. Otherwise lint and the like complain.
*/
int need_backing_up = (num_backing_up > 0 && !reject);
if (need_backing_up && (!nultrans || fullspd || fulltbl))
/* We're going to need yy_cp lying around for the call
* below to gen_backing_up().
*/
indent_puts ("char *yy_cp = YY_G(yy_c_buf_p);");
outc ('\n');
if (nultrans) {
indent_puts
("yy_current_state = yy_NUL_trans[yy_current_state];");
indent_puts ("yy_is_jam = (yy_current_state == 0);");
}
else if (fulltbl) {
do_indent ();
if (gentables)
out_dec ("yy_current_state = yy_nxt[yy_current_state][%d];\n", NUL_ec);
else
out_dec ("yy_current_state = yy_nxt[yy_current_state*YY_NXT_LOLEN + %d];\n", NUL_ec);
indent_puts ("yy_is_jam = (yy_current_state <= 0);");
}
else if (fullspd) {
do_indent ();
out_dec ("int yy_c = %d;\n", NUL_ec);
indent_puts
("yyconst struct yy_trans_info *yy_trans_info;\n");
indent_puts
("yy_trans_info = &yy_current_state[(unsigned int) yy_c];");
indent_puts ("yy_current_state += yy_trans_info->yy_nxt;");
indent_puts
("yy_is_jam = (yy_trans_info->yy_verify != yy_c);");
}
else {
char NUL_ec_str[20];
snprintf (NUL_ec_str, sizeof(NUL_ec_str), "%d", NUL_ec);
gen_next_compressed_state (NUL_ec_str);
do_indent ();
out_dec ("yy_is_jam = (yy_current_state == %d);\n",
jamstate);
if (reject) {
/* Only stack this state if it's a transition we
* actually make. If we stack it on a jam, then
* the state stack and yy_c_buf_p get out of sync.
*/
indent_puts ("if ( ! yy_is_jam )");
indent_up ();
indent_puts
("*YY_G(yy_state_ptr)++ = yy_current_state;");
indent_down ();
}
}
/* If we've entered an accepting state, back up; note that
* compressed tables have *already* done such backing up, so
* we needn't bother with it again.