Annotation of src/usr.bin/lex/gen.c, Revision 1.1.1.1
1.1 deraadt 1: /* gen - actual generation (writing) of flex scanners */
2:
3: /*-
4: * Copyright (c) 1990 The Regents of the University of California.
5: * All rights reserved.
6: *
7: * This code is derived from software contributed to Berkeley by
8: * Vern Paxson.
9: *
10: * The United States Government has rights in this work pursuant
11: * to contract no. DE-AC03-76SF00098 between the United States
12: * Department of Energy and the University of California.
13: *
14: * Redistribution and use in source and binary forms are permitted provided
15: * that: (1) source distributions retain this entire copyright notice and
16: * comment, and (2) distributions including binaries display the following
17: * acknowledgement: ``This product includes software developed by the
18: * University of California, Berkeley and its contributors'' in the
19: * documentation or other materials provided with the distribution and in
20: * all advertising materials mentioning features or use of this software.
21: * Neither the name of the University nor the names of its contributors may
22: * be used to endorse or promote products derived from this software without
23: * specific prior written permission.
24: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
25: * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
26: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27: */
28:
29: /* $Header: /a/cvsroot/src/usr.bin/lex/gen.c,v 1.10 1995/05/05 05:35:25 jtc Exp $ */
30:
31: #include "flexdef.h"
32:
33:
34: /* declare functions that have forward references */
35:
36: void gen_next_state PROTO((int));
37: void genecs PROTO((void));
38: void indent_put2s PROTO((char [], char []));
39: void indent_puts PROTO((char []));
40:
41:
42: static int indent_level = 0; /* each level is 8 spaces */
43:
44: #define indent_up() (++indent_level)
45: #define indent_down() (--indent_level)
46: #define set_indent(indent_val) indent_level = indent_val
47:
48: /* Almost everything is done in terms of arrays starting at 1, so provide
49: * a null entry for the zero element of all C arrays. (The exception
50: * to this is that the fast table representation generally uses the
51: * 0 elements of its arrays, too.)
52: */
53: static char C_int_decl[] = "static yyconst int %s[%d] =\n { 0,\n";
54: static char C_short_decl[] = "static yyconst short int %s[%d] =\n { 0,\n";
55: static char C_long_decl[] = "static yyconst long int %s[%d] =\n { 0,\n";
56: static char C_state_decl[] =
57: "static yyconst yy_state_type %s[%d] =\n { 0,\n";
58:
59:
60: /* Indent to the current level. */
61:
62: void do_indent()
63: {
64: register int i = indent_level * 8;
65:
66: while ( i >= 8 )
67: {
68: outc( '\t' );
69: i -= 8;
70: }
71:
72: while ( i > 0 )
73: {
74: outc( ' ' );
75: --i;
76: }
77: }
78:
79:
80: /* Generate the code to keep backing-up information. */
81:
82: void gen_backing_up()
83: {
84: if ( reject || num_backing_up == 0 )
85: return;
86:
87: if ( fullspd )
88: indent_puts( "if ( yy_current_state[-1].yy_nxt )" );
89: else
90: indent_puts( "if ( yy_accept[yy_current_state] )" );
91:
92: indent_up();
93: indent_puts( "{" );
94: indent_puts( "yy_last_accepting_state = yy_current_state;" );
95: indent_puts( "yy_last_accepting_cpos = yy_cp;" );
96: indent_puts( "}" );
97: indent_down();
98: }
99:
100:
101: /* Generate the code to perform the backing up. */
102:
103: void gen_bu_action()
104: {
105: if ( reject || num_backing_up == 0 )
106: return;
107:
108: set_indent( 3 );
109:
110: indent_puts( "case 0: /* must back up */" );
111: indent_puts( "/* undo the effects of YY_DO_BEFORE_ACTION */" );
112: indent_puts( "*yy_cp = yy_hold_char;" );
113:
114: if ( fullspd || fulltbl )
115: indent_puts( "yy_cp = yy_last_accepting_cpos + 1;" );
116: else
117: /* Backing-up info for compressed tables is taken \after/
118: * yy_cp has been incremented for the next state.
119: */
120: indent_puts( "yy_cp = yy_last_accepting_cpos;" );
121:
122: indent_puts( "yy_current_state = yy_last_accepting_state;" );
123: indent_puts( "goto yy_find_action;" );
124: outc( '\n' );
125:
126: set_indent( 0 );
127: }
128:
129:
130: /* genctbl - generates full speed compressed transition table */
131:
132: void genctbl()
133: {
134: register int i;
135: int end_of_buffer_action = num_rules + 1;
136:
137: /* Table of verify for transition and offset to next state. */
138: out_dec( "static yyconst struct yy_trans_info yy_transition[%d] =\n",
139: tblend + numecs + 1 );
140: outn( " {" );
141:
142: /* We want the transition to be represented as the offset to the
143: * next state, not the actual state number, which is what it currently
144: * is. The offset is base[nxt[i]] - (base of current state)]. That's
145: * just the difference between the starting points of the two involved
146: * states (to - from).
147: *
148: * First, though, we need to find some way to put in our end-of-buffer
149: * flags and states. We do this by making a state with absolutely no
150: * transitions. We put it at the end of the table.
151: */
152:
153: /* We need to have room in nxt/chk for two more slots: One for the
154: * action and one for the end-of-buffer transition. We now *assume*
155: * that we're guaranteed the only character we'll try to index this
156: * nxt/chk pair with is EOB, i.e., 0, so we don't have to make sure
157: * there's room for jam entries for other characters.
158: */
159:
160: while ( tblend + 2 >= current_max_xpairs )
161: expand_nxt_chk();
162:
163: while ( lastdfa + 1 >= current_max_dfas )
164: increase_max_dfas();
165:
166: base[lastdfa + 1] = tblend + 2;
167: nxt[tblend + 1] = end_of_buffer_action;
168: chk[tblend + 1] = numecs + 1;
169: chk[tblend + 2] = 1; /* anything but EOB */
170:
171: /* So that "make test" won't show arb. differences. */
172: nxt[tblend + 2] = 0;
173:
174: /* Make sure every state has an end-of-buffer transition and an
175: * action #.
176: */
177: for ( i = 0; i <= lastdfa; ++i )
178: {
179: int anum = dfaacc[i].dfaacc_state;
180: int offset = base[i];
181:
182: chk[offset] = EOB_POSITION;
183: chk[offset - 1] = ACTION_POSITION;
184: nxt[offset - 1] = anum; /* action number */
185: }
186:
187: for ( i = 0; i <= tblend; ++i )
188: {
189: if ( chk[i] == EOB_POSITION )
190: transition_struct_out( 0, base[lastdfa + 1] - i );
191:
192: else if ( chk[i] == ACTION_POSITION )
193: transition_struct_out( 0, nxt[i] );
194:
195: else if ( chk[i] > numecs || chk[i] == 0 )
196: transition_struct_out( 0, 0 ); /* unused slot */
197:
198: else /* verify, transition */
199: transition_struct_out( chk[i],
200: base[nxt[i]] - (i - chk[i]) );
201: }
202:
203:
204: /* Here's the final, end-of-buffer state. */
205: transition_struct_out( chk[tblend + 1], nxt[tblend + 1] );
206: transition_struct_out( chk[tblend + 2], nxt[tblend + 2] );
207:
208: outn( " };\n" );
209:
210: /* Table of pointers to start states. */
211: out_dec(
212: "static yyconst struct yy_trans_info *yy_start_state_list[%d] =\n",
213: lastsc * 2 + 1 );
214: outn( " {" ); /* } so vi doesn't get confused */
215:
216: for ( i = 0; i <= lastsc * 2; ++i )
217: out_dec( " &yy_transition[%d],\n", base[i] );
218:
219: dataend();
220:
221: if ( useecs )
222: genecs();
223: }
224:
225:
226: /* Generate equivalence-class tables. */
227:
228: void genecs()
229: {
230: register int i, j;
231: int numrows;
232:
233: out_str_dec( C_int_decl, "yy_ec", csize );
234:
235: for ( i = 1; i < csize; ++i )
236: {
237: if ( caseins && (i >= 'A') && (i <= 'Z') )
238: ecgroup[i] = ecgroup[clower( i )];
239:
240: ecgroup[i] = ABS( ecgroup[i] );
241: mkdata( ecgroup[i] );
242: }
243:
244: dataend();
245:
246: if ( trace )
247: {
248: fputs( _( "\n\nEquivalence Classes:\n\n" ), stderr );
249:
250: numrows = csize / 8;
251:
252: for ( j = 0; j < numrows; ++j )
253: {
254: for ( i = j; i < csize; i = i + numrows )
255: {
256: fprintf( stderr, "%4s = %-2d",
257: readable_form( i ), ecgroup[i] );
258:
259: putc( ' ', stderr );
260: }
261:
262: putc( '\n', stderr );
263: }
264: }
265: }
266:
267:
268: /* Generate the code to find the action number. */
269:
270: void gen_find_action()
271: {
272: if ( fullspd )
273: indent_puts( "yy_act = yy_current_state[-1].yy_nxt;" );
274:
275: else if ( fulltbl )
276: indent_puts( "yy_act = yy_accept[yy_current_state];" );
277:
278: else if ( reject )
279: {
280: indent_puts( "yy_current_state = *--yy_state_ptr;" );
281: indent_puts( "yy_lp = yy_accept[yy_current_state];" );
282:
283: outn(
284: "find_rule: /* we branch to this label when backing up */" );
285:
286: indent_puts(
287: "for ( ; ; ) /* until we find what rule we matched */" );
288:
289: indent_up();
290:
291: indent_puts( "{" );
292:
293: indent_puts(
294: "if ( yy_lp && yy_lp < yy_accept[yy_current_state + 1] )" );
295: indent_up();
296: indent_puts( "{" );
297: indent_puts( "yy_act = yy_acclist[yy_lp];" );
298:
299: if ( variable_trailing_context_rules )
300: {
301: indent_puts( "if ( yy_act & YY_TRAILING_HEAD_MASK ||" );
302: indent_puts( " yy_looking_for_trail_begin )" );
303: indent_up();
304: indent_puts( "{" );
305:
306: indent_puts(
307: "if ( yy_act == yy_looking_for_trail_begin )" );
308: indent_up();
309: indent_puts( "{" );
310: indent_puts( "yy_looking_for_trail_begin = 0;" );
311: indent_puts( "yy_act &= ~YY_TRAILING_HEAD_MASK;" );
312: indent_puts( "break;" );
313: indent_puts( "}" );
314: indent_down();
315:
316: indent_puts( "}" );
317: indent_down();
318:
319: indent_puts( "else if ( yy_act & YY_TRAILING_MASK )" );
320: indent_up();
321: indent_puts( "{" );
322: indent_puts(
323: "yy_looking_for_trail_begin = yy_act & ~YY_TRAILING_MASK;" );
324: indent_puts(
325: "yy_looking_for_trail_begin |= YY_TRAILING_HEAD_MASK;" );
326:
327: if ( real_reject )
328: {
329: /* Remember matched text in case we back up
330: * due to REJECT.
331: */
332: indent_puts( "yy_full_match = yy_cp;" );
333: indent_puts( "yy_full_state = yy_state_ptr;" );
334: indent_puts( "yy_full_lp = yy_lp;" );
335: }
336:
337: indent_puts( "}" );
338: indent_down();
339:
340: indent_puts( "else" );
341: indent_up();
342: indent_puts( "{" );
343: indent_puts( "yy_full_match = yy_cp;" );
344: indent_puts( "yy_full_state = yy_state_ptr;" );
345: indent_puts( "yy_full_lp = yy_lp;" );
346: indent_puts( "break;" );
347: indent_puts( "}" );
348: indent_down();
349:
350: indent_puts( "++yy_lp;" );
351: indent_puts( "goto find_rule;" );
352: }
353:
354: else
355: {
356: /* Remember matched text in case we back up due to
357: * trailing context plus REJECT.
358: */
359: indent_up();
360: indent_puts( "{" );
361: indent_puts( "yy_full_match = yy_cp;" );
362: indent_puts( "break;" );
363: indent_puts( "}" );
364: indent_down();
365: }
366:
367: indent_puts( "}" );
368: indent_down();
369:
370: indent_puts( "--yy_cp;" );
371:
372: /* We could consolidate the following two lines with those at
373: * the beginning, but at the cost of complaints that we're
374: * branching inside a loop.
375: */
376: indent_puts( "yy_current_state = *--yy_state_ptr;" );
377: indent_puts( "yy_lp = yy_accept[yy_current_state];" );
378:
379: indent_puts( "}" );
380:
381: indent_down();
382: }
383:
384: else
385: { /* compressed */
386: indent_puts( "yy_act = yy_accept[yy_current_state];" );
387:
388: if ( interactive && ! reject )
389: {
390: /* Do the guaranteed-needed backing up to figure out
391: * the match.
392: */
393: indent_puts( "if ( yy_act == 0 )" );
394: indent_up();
395: indent_puts( "{ /* have to back up */" );
396: indent_puts( "yy_cp = yy_last_accepting_cpos;" );
397: indent_puts(
398: "yy_current_state = yy_last_accepting_state;" );
399: indent_puts( "yy_act = yy_accept[yy_current_state];" );
400: indent_puts( "}" );
401: indent_down();
402: }
403: }
404: }
405:
406:
407: /* genftbl - generate full transition table */
408:
409: void genftbl()
410: {
411: register int i;
412: int end_of_buffer_action = num_rules + 1;
413:
414: out_str_dec( long_align ? C_long_decl : C_short_decl,
415: "yy_accept", lastdfa + 1 );
416:
417: dfaacc[end_of_buffer_state].dfaacc_state = end_of_buffer_action;
418:
419: for ( i = 1; i <= lastdfa; ++i )
420: {
421: register int anum = dfaacc[i].dfaacc_state;
422:
423: mkdata( anum );
424:
425: if ( trace && anum )
426: fprintf( stderr, _( "state # %d accepts: [%d]\n" ),
427: i, anum );
428: }
429:
430: dataend();
431:
432: if ( useecs )
433: genecs();
434:
435: /* Don't have to dump the actual full table entries - they were
436: * created on-the-fly.
437: */
438: }
439:
440:
441: /* Generate the code to find the next compressed-table state. */
442:
443: void gen_next_compressed_state( char_map )
444: char *char_map;
445: {
446: indent_put2s( "register YY_CHAR yy_c = %s;", char_map );
447:
448: /* Save the backing-up info \before/ computing the next state
449: * because we always compute one more state than needed - we
450: * always proceed until we reach a jam state
451: */
452: gen_backing_up();
453:
454: indent_puts(
455: "while ( yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state )" );
456: indent_up();
457: indent_puts( "{" );
458: indent_puts( "yy_current_state = (int) yy_def[yy_current_state];" );
459:
460: if ( usemecs )
461: {
462: /* We've arrange it so that templates are never chained
463: * to one another. This means we can afford to make a
464: * very simple test to see if we need to convert to
465: * yy_c's meta-equivalence class without worrying
466: * about erroneously looking up the meta-equivalence
467: * class twice
468: */
469: do_indent();
470:
471: /* lastdfa + 2 is the beginning of the templates */
472: out_dec( "if ( yy_current_state >= %d )\n", lastdfa + 2 );
473:
474: indent_up();
475: indent_puts( "yy_c = yy_meta[(unsigned int) yy_c];" );
476: indent_down();
477: }
478:
479: indent_puts( "}" );
480: indent_down();
481:
482: indent_puts(
483: "yy_current_state = yy_nxt[yy_base[yy_current_state] + (unsigned int) yy_c];" );
484: }
485:
486:
487: /* Generate the code to find the next match. */
488:
489: void gen_next_match()
490: {
491: /* NOTE - changes in here should be reflected in gen_next_state() and
492: * gen_NUL_trans().
493: */
494: char *char_map = useecs ?
495: "yy_ec[YY_SC_TO_UI(*yy_cp)]" :
496: "YY_SC_TO_UI(*yy_cp)";
497:
498: char *char_map_2 = useecs ?
499: "yy_ec[YY_SC_TO_UI(*++yy_cp)]" :
500: "YY_SC_TO_UI(*++yy_cp)";
501:
502: if ( fulltbl )
503: {
504: indent_put2s(
505: "while ( (yy_current_state = yy_nxt[yy_current_state][%s]) > 0 )",
506: char_map );
507:
508: indent_up();
509:
510: if ( num_backing_up > 0 )
511: {
512: indent_puts( "{" ); /* } for vi */
513: gen_backing_up();
514: outc( '\n' );
515: }
516:
517: indent_puts( "++yy_cp;" );
518:
519: if ( num_backing_up > 0 )
520: /* { for vi */
521: indent_puts( "}" );
522:
523: indent_down();
524:
525: outc( '\n' );
526: indent_puts( "yy_current_state = -yy_current_state;" );
527: }
528:
529: else if ( fullspd )
530: {
531: indent_puts( "{" ); /* } for vi */
532: indent_puts(
533: "register yyconst struct yy_trans_info *yy_trans_info;\n" );
534: indent_puts( "register YY_CHAR yy_c;\n" );
535: indent_put2s( "for ( yy_c = %s;", char_map );
536: indent_puts(
537: " (yy_trans_info = &yy_current_state[(unsigned int) yy_c])->" );
538: indent_puts( "yy_verify == yy_c;" );
539: indent_put2s( " yy_c = %s )", char_map_2 );
540:
541: indent_up();
542:
543: if ( num_backing_up > 0 )
544: indent_puts( "{" ); /* } for vi */
545:
546: indent_puts( "yy_current_state += yy_trans_info->yy_nxt;" );
547:
548: if ( num_backing_up > 0 )
549: {
550: outc( '\n' );
551: gen_backing_up(); /* { for vi */
552: indent_puts( "}" );
553: }
554:
555: indent_down(); /* { for vi */
556: indent_puts( "}" );
557: }
558:
559: else
560: { /* compressed */
561: indent_puts( "do" );
562:
563: indent_up();
564: indent_puts( "{" ); /* } for vi */
565:
566: gen_next_state( false );
567:
568: indent_puts( "++yy_cp;" );
569:
570: /* { for vi */
571: indent_puts( "}" );
572: indent_down();
573:
574: do_indent();
575:
576: if ( interactive )
577: out_dec( "while ( yy_base[yy_current_state] != %d );\n",
578: jambase );
579: else
580: out_dec( "while ( yy_current_state != %d );\n",
581: jamstate );
582:
583: if ( ! reject && ! interactive )
584: {
585: /* Do the guaranteed-needed backing up to figure out
586: * the match.
587: */
588: indent_puts( "yy_cp = yy_last_accepting_cpos;" );
589: indent_puts(
590: "yy_current_state = yy_last_accepting_state;" );
591: }
592: }
593: }
594:
595:
596: /* Generate the code to find the next state. */
597:
598: void gen_next_state( worry_about_NULs )
599: int worry_about_NULs;
600: { /* NOTE - changes in here should be reflected in gen_next_match() */
601: char char_map[256];
602:
603: if ( worry_about_NULs && ! nultrans )
604: {
605: if ( useecs )
606: (void) sprintf( char_map,
607: "(*yy_cp ? yy_ec[YY_SC_TO_UI(*yy_cp)] : %d)",
608: NUL_ec );
609: else
610: (void) sprintf( char_map,
611: "(*yy_cp ? YY_SC_TO_UI(*yy_cp) : %d)", NUL_ec );
612: }
613:
614: else
615: strcpy( char_map, useecs ?
616: "yy_ec[YY_SC_TO_UI(*yy_cp)]" : "YY_SC_TO_UI(*yy_cp)" );
617:
618: if ( worry_about_NULs && nultrans )
619: {
620: if ( ! fulltbl && ! fullspd )
621: /* Compressed tables back up *before* they match. */
622: gen_backing_up();
623:
624: indent_puts( "if ( *yy_cp )" );
625: indent_up();
626: indent_puts( "{" ); /* } for vi */
627: }
628:
629: if ( fulltbl )
630: indent_put2s(
631: "yy_current_state = yy_nxt[yy_current_state][%s];",
632: char_map );
633:
634: else if ( fullspd )
635: indent_put2s(
636: "yy_current_state += yy_current_state[%s].yy_nxt;",
637: char_map );
638:
639: else
640: gen_next_compressed_state( char_map );
641:
642: if ( worry_about_NULs && nultrans )
643: {
644: /* { for vi */
645: indent_puts( "}" );
646: indent_down();
647: indent_puts( "else" );
648: indent_up();
649: indent_puts(
650: "yy_current_state = yy_NUL_trans[yy_current_state];" );
651: indent_down();
652: }
653:
654: if ( fullspd || fulltbl )
655: gen_backing_up();
656:
657: if ( reject )
658: indent_puts( "*yy_state_ptr++ = yy_current_state;" );
659: }
660:
661:
662: /* Generate the code to make a NUL transition. */
663:
664: void gen_NUL_trans()
665: { /* NOTE - changes in here should be reflected in gen_next_match() */
666: /* Only generate a definition for "yy_cp" if we'll generate code
667: * that uses it. Otherwise lint and the like complain.
668: */
669: int need_backing_up = (num_backing_up > 0 && ! reject);
670:
671: if ( need_backing_up && (! nultrans || fullspd || fulltbl) )
672: /* We're going to need yy_cp lying around for the call
673: * below to gen_backing_up().
674: */
675: indent_puts( "register char *yy_cp = yy_c_buf_p;" );
676:
677: outc( '\n' );
678:
679: if ( nultrans )
680: {
681: indent_puts(
682: "yy_current_state = yy_NUL_trans[yy_current_state];" );
683: indent_puts( "yy_is_jam = (yy_current_state == 0);" );
684: }
685:
686: else if ( fulltbl )
687: {
688: do_indent();
689: out_dec( "yy_current_state = yy_nxt[yy_current_state][%d];\n",
690: NUL_ec );
691: indent_puts( "yy_is_jam = (yy_current_state <= 0);" );
692: }
693:
694: else if ( fullspd )
695: {
696: do_indent();
697: out_dec( "register int yy_c = %d;\n", NUL_ec );
698:
699: indent_puts(
700: "register yyconst struct yy_trans_info *yy_trans_info;\n" );
701: indent_puts(
702: "yy_trans_info = &yy_current_state[(unsigned int) yy_c];" );
703: indent_puts( "yy_current_state += yy_trans_info->yy_nxt;" );
704:
705: indent_puts(
706: "yy_is_jam = (yy_trans_info->yy_verify != yy_c);" );
707: }
708:
709: else
710: {
711: char NUL_ec_str[20];
712:
713: (void) sprintf( NUL_ec_str, "%d", NUL_ec );
714: gen_next_compressed_state( NUL_ec_str );
715:
716: if ( reject )
717: indent_puts( "*yy_state_ptr++ = yy_current_state;" );
718:
719: do_indent();
720:
721: out_dec( "yy_is_jam = (yy_current_state == %d);\n", jamstate );
722: }
723:
724: /* If we've entered an accepting state, back up; note that
725: * compressed tables have *already* done such backing up, so
726: * we needn't bother with it again.
727: */
728: if ( need_backing_up && (fullspd || fulltbl) )
729: {
730: outc( '\n' );
731: indent_puts( "if ( ! yy_is_jam )" );
732: indent_up();
733: indent_puts( "{" );
734: gen_backing_up();
735: indent_puts( "}" );
736: indent_down();
737: }
738: }
739:
740:
741: /* Generate the code to find the start state. */
742:
743: void gen_start_state()
744: {
745: if ( fullspd )
746: {
747: if ( bol_needed )
748: {
749: indent_puts(
750: "yy_current_state = yy_start_state_list[yy_start + YY_AT_BOL()];" );
751: }
752: else
753: indent_puts(
754: "yy_current_state = yy_start_state_list[yy_start];" );
755: }
756:
757: else
758: {
759: indent_puts( "yy_current_state = yy_start;" );
760:
761: if ( bol_needed )
762: indent_puts( "yy_current_state += YY_AT_BOL();" );
763:
764: if ( reject )
765: {
766: /* Set up for storing up states. */
767: indent_puts( "yy_state_ptr = yy_state_buf;" );
768: indent_puts( "*yy_state_ptr++ = yy_current_state;" );
769: }
770: }
771: }
772:
773:
774: /* gentabs - generate data statements for the transition tables */
775:
776: void gentabs()
777: {
778: int i, j, k, *accset, nacc, *acc_array, total_states;
779: int end_of_buffer_action = num_rules + 1;
780:
781: acc_array = allocate_integer_array( current_max_dfas );
782: nummt = 0;
783:
784: /* The compressed table format jams by entering the "jam state",
785: * losing information about the previous state in the process.
786: * In order to recover the previous state, we effectively need
787: * to keep backing-up information.
788: */
789: ++num_backing_up;
790:
791: if ( reject )
792: {
793: /* Write out accepting list and pointer list.
794: *
795: * First we generate the "yy_acclist" array. In the process,
796: * we compute the indices that will go into the "yy_accept"
797: * array, and save the indices in the dfaacc array.
798: */
799: int EOB_accepting_list[2];
800:
801: /* Set up accepting structures for the End Of Buffer state. */
802: EOB_accepting_list[0] = 0;
803: EOB_accepting_list[1] = end_of_buffer_action;
804: accsiz[end_of_buffer_state] = 1;
805: dfaacc[end_of_buffer_state].dfaacc_set = EOB_accepting_list;
806:
807: out_str_dec( long_align ? C_long_decl : C_short_decl,
808: "yy_acclist", MAX( numas, 1 ) + 1 );
809:
810: j = 1; /* index into "yy_acclist" array */
811:
812: for ( i = 1; i <= lastdfa; ++i )
813: {
814: acc_array[i] = j;
815:
816: if ( accsiz[i] != 0 )
817: {
818: accset = dfaacc[i].dfaacc_set;
819: nacc = accsiz[i];
820:
821: if ( trace )
822: fprintf( stderr,
823: _( "state # %d accepts: " ),
824: i );
825:
826: for ( k = 1; k <= nacc; ++k )
827: {
828: int accnum = accset[k];
829:
830: ++j;
831:
832: if ( variable_trailing_context_rules &&
833: ! (accnum & YY_TRAILING_HEAD_MASK) &&
834: accnum > 0 && accnum <= num_rules &&
835: rule_type[accnum] == RULE_VARIABLE )
836: {
837: /* Special hack to flag
838: * accepting number as part
839: * of trailing context rule.
840: */
841: accnum |= YY_TRAILING_MASK;
842: }
843:
844: mkdata( accnum );
845:
846: if ( trace )
847: {
848: fprintf( stderr, "[%d]",
849: accset[k] );
850:
851: if ( k < nacc )
852: fputs( ", ", stderr );
853: else
854: putc( '\n', stderr );
855: }
856: }
857: }
858: }
859:
860: /* add accepting number for the "jam" state */
861: acc_array[i] = j;
862:
863: dataend();
864: }
865:
866: else
867: {
868: dfaacc[end_of_buffer_state].dfaacc_state = end_of_buffer_action;
869:
870: for ( i = 1; i <= lastdfa; ++i )
871: acc_array[i] = dfaacc[i].dfaacc_state;
872:
873: /* add accepting number for jam state */
874: acc_array[i] = 0;
875: }
876:
877: /* Spit out "yy_accept" array. If we're doing "reject", it'll be
878: * pointers into the "yy_acclist" array. Otherwise it's actual
879: * accepting numbers. In either case, we just dump the numbers.
880: */
881:
882: /* "lastdfa + 2" is the size of "yy_accept"; includes room for C arrays
883: * beginning at 0 and for "jam" state.
884: */
885: k = lastdfa + 2;
886:
887: if ( reject )
888: /* We put a "cap" on the table associating lists of accepting
889: * numbers with state numbers. This is needed because we tell
890: * where the end of an accepting list is by looking at where
891: * the list for the next state starts.
892: */
893: ++k;
894:
895: out_str_dec( long_align ? C_long_decl : C_short_decl, "yy_accept", k );
896:
897: for ( i = 1; i <= lastdfa; ++i )
898: {
899: mkdata( acc_array[i] );
900:
901: if ( ! reject && trace && acc_array[i] )
902: fprintf( stderr, _( "state # %d accepts: [%d]\n" ),
903: i, acc_array[i] );
904: }
905:
906: /* Add entry for "jam" state. */
907: mkdata( acc_array[i] );
908:
909: if ( reject )
910: /* Add "cap" for the list. */
911: mkdata( acc_array[i] );
912:
913: dataend();
914:
915: if ( useecs )
916: genecs();
917:
918: if ( usemecs )
919: {
920: /* Write out meta-equivalence classes (used to index
921: * templates with).
922: */
923:
924: if ( trace )
925: fputs( _( "\n\nMeta-Equivalence Classes:\n" ),
926: stderr );
927:
928: out_str_dec( C_int_decl, "yy_meta", numecs + 1 );
929:
930: for ( i = 1; i <= numecs; ++i )
931: {
932: if ( trace )
933: fprintf( stderr, "%d = %d\n",
934: i, ABS( tecbck[i] ) );
935:
936: mkdata( ABS( tecbck[i] ) );
937: }
938:
939: dataend();
940: }
941:
942: total_states = lastdfa + numtemps;
943:
944: out_str_dec( (tblend >= MAX_SHORT || long_align) ?
945: C_long_decl : C_short_decl,
946: "yy_base", total_states + 1 );
947:
948: for ( i = 1; i <= lastdfa; ++i )
949: {
950: register int d = def[i];
951:
952: if ( base[i] == JAMSTATE )
953: base[i] = jambase;
954:
955: if ( d == JAMSTATE )
956: def[i] = jamstate;
957:
958: else if ( d < 0 )
959: {
960: /* Template reference. */
961: ++tmpuses;
962: def[i] = lastdfa - d + 1;
963: }
964:
965: mkdata( base[i] );
966: }
967:
968: /* Generate jam state's base index. */
969: mkdata( base[i] );
970:
971: for ( ++i /* skip jam state */; i <= total_states; ++i )
972: {
973: mkdata( base[i] );
974: def[i] = jamstate;
975: }
976:
977: dataend();
978:
979: out_str_dec( (total_states >= MAX_SHORT || long_align) ?
980: C_long_decl : C_short_decl,
981: "yy_def", total_states + 1 );
982:
983: for ( i = 1; i <= total_states; ++i )
984: mkdata( def[i] );
985:
986: dataend();
987:
988: out_str_dec( (total_states >= MAX_SHORT || long_align) ?
989: C_long_decl : C_short_decl,
990: "yy_nxt", tblend + 1 );
991:
992: for ( i = 1; i <= tblend; ++i )
993: {
994: /* Note, the order of the following test is important.
995: * If chk[i] is 0, then nxt[i] is undefined.
996: */
997: if ( chk[i] == 0 || nxt[i] == 0 )
998: nxt[i] = jamstate; /* new state is the JAM state */
999:
1000: mkdata( nxt[i] );
1001: }
1002:
1003: dataend();
1004:
1005: out_str_dec( (total_states >= MAX_SHORT || long_align) ?
1006: C_long_decl : C_short_decl,
1007: "yy_chk", tblend + 1 );
1008:
1009: for ( i = 1; i <= tblend; ++i )
1010: {
1011: if ( chk[i] == 0 )
1012: ++nummt;
1013:
1014: mkdata( chk[i] );
1015: }
1016:
1017: dataend();
1018: }
1019:
1020:
1021: /* Write out a formatted string (with a secondary string argument) at the
1022: * current indentation level, adding a final newline.
1023: */
1024:
1025: void indent_put2s( fmt, arg )
1026: char fmt[], arg[];
1027: {
1028: do_indent();
1029: out_str( fmt, arg );
1030: outn( "" );
1031: }
1032:
1033:
1034: /* Write out a string at the current indentation level, adding a final
1035: * newline.
1036: */
1037:
1038: void indent_puts( str )
1039: char str[];
1040: {
1041: do_indent();
1042: outn( str );
1043: }
1044:
1045:
1046: /* make_tables - generate transition tables and finishes generating output file
1047: */
1048:
1049: void make_tables()
1050: {
1051: register int i;
1052: int did_eof_rule = false;
1053:
1054: skelout();
1055:
1056: /* First, take care of YY_DO_BEFORE_ACTION depending on yymore
1057: * being used.
1058: */
1059: set_indent( 1 );
1060:
1061: if ( yymore_used )
1062: {
1063: indent_puts( "yytext_ptr -= yy_more_len; \\" );
1064: indent_puts( "yyleng = (int) (yy_cp - yytext_ptr); \\" );
1065: }
1066:
1067: else
1068: indent_puts( "yyleng = (int) (yy_cp - yy_bp); \\" );
1069:
1070: /* Now also deal with copying yytext_ptr to yytext if needed. */
1071: skelout();
1072: if ( yytext_is_array )
1073: {
1074: indent_puts( "if ( yyleng >= YYLMAX ) \\" );
1075: indent_up();
1076: indent_puts(
1077: "YY_FATAL_ERROR( \"token too large, exceeds YYLMAX\" ); \\" );
1078: indent_down();
1079: indent_puts(
1080: "yy_flex_strncpy( yytext, yytext_ptr, yyleng + 1 ); \\" );
1081: }
1082:
1083: set_indent( 0 );
1084:
1085: skelout();
1086:
1087:
1088: out_dec( "#define YY_NUM_RULES %d\n", num_rules );
1089: out_dec( "#define YY_END_OF_BUFFER %d\n", num_rules + 1 );
1090:
1091: if ( fullspd )
1092: {
1093: /* Need to define the transet type as a size large
1094: * enough to hold the biggest offset.
1095: */
1096: int total_table_size = tblend + numecs + 1;
1097: char *trans_offset_type =
1098: (total_table_size >= MAX_SHORT || long_align) ?
1099: "long" : "short";
1100:
1101: set_indent( 0 );
1102: indent_puts( "struct yy_trans_info" );
1103: indent_up();
1104: indent_puts( "{" ); /* } for vi */
1105:
1106: if ( long_align )
1107: indent_puts( "long yy_verify;" );
1108: else
1109: indent_puts( "short yy_verify;" );
1110:
1111: /* In cases where its sister yy_verify *is* a "yes, there is
1112: * a transition", yy_nxt is the offset (in records) to the
1113: * next state. In most cases where there is no transition,
1114: * the value of yy_nxt is irrelevant. If yy_nxt is the -1th
1115: * record of a state, though, then yy_nxt is the action number
1116: * for that state.
1117: */
1118:
1119: indent_put2s( "%s yy_nxt;", trans_offset_type );
1120: indent_puts( "};" );
1121: indent_down();
1122: }
1123:
1124: if ( fullspd )
1125: genctbl();
1126: else if ( fulltbl )
1127: genftbl();
1128: else
1129: gentabs();
1130:
1131: /* Definitions for backing up. We don't need them if REJECT
1132: * is being used because then we use an alternative backin-up
1133: * technique instead.
1134: */
1135: if ( num_backing_up > 0 && ! reject )
1136: {
1137: if ( ! C_plus_plus )
1138: {
1139: indent_puts(
1140: "static yy_state_type yy_last_accepting_state;" );
1141: indent_puts(
1142: "static char *yy_last_accepting_cpos;\n" );
1143: }
1144: }
1145:
1146: if ( nultrans )
1147: {
1148: out_str_dec( C_state_decl, "yy_NUL_trans", lastdfa + 1 );
1149:
1150: for ( i = 1; i <= lastdfa; ++i )
1151: {
1152: if ( fullspd )
1153: out_dec( " &yy_transition[%d],\n", base[i] );
1154: else
1155: mkdata( nultrans[i] );
1156: }
1157:
1158: dataend();
1159: }
1160:
1161: if ( ddebug )
1162: { /* Spit out table mapping rules to line numbers. */
1163: if ( ! C_plus_plus )
1164: {
1165: indent_puts( "extern int yy_flex_debug;" );
1166: indent_puts( "int yy_flex_debug = 1;\n" );
1167: }
1168:
1169: out_str_dec( long_align ? C_long_decl : C_short_decl,
1170: "yy_rule_linenum", num_rules );
1171: for ( i = 1; i < num_rules; ++i )
1172: mkdata( rule_linenum[i] );
1173: dataend();
1174: }
1175:
1176: if ( reject )
1177: {
1178: /* Declare state buffer variables. */
1179: if ( ! C_plus_plus )
1180: {
1181: outn(
1182: "static yy_state_type yy_state_buf[YY_BUF_SIZE + 2], *yy_state_ptr;" );
1183: outn( "static char *yy_full_match;" );
1184: outn( "static int yy_lp;" );
1185: }
1186:
1187: if ( variable_trailing_context_rules )
1188: {
1189: if ( ! C_plus_plus )
1190: {
1191: outn(
1192: "static int yy_looking_for_trail_begin = 0;" );
1193: outn( "static int yy_full_lp;" );
1194: outn( "static int *yy_full_state;" );
1195: }
1196:
1197: out_hex( "#define YY_TRAILING_MASK 0x%x\n",
1198: (unsigned int) YY_TRAILING_MASK );
1199: out_hex( "#define YY_TRAILING_HEAD_MASK 0x%x\n",
1200: (unsigned int) YY_TRAILING_HEAD_MASK );
1201: }
1202:
1203: outn( "#define REJECT \\" );
1204: outn( "{ \\" ); /* } for vi */
1205: outn(
1206: "*yy_cp = yy_hold_char; /* undo effects of setting up yytext */ \\" );
1207: outn(
1208: "yy_cp = yy_full_match; /* restore poss. backed-over text */ \\" );
1209:
1210: if ( variable_trailing_context_rules )
1211: {
1212: outn(
1213: "yy_lp = yy_full_lp; /* restore orig. accepting pos. */ \\" );
1214: outn(
1215: "yy_state_ptr = yy_full_state; /* restore orig. state */ \\" );
1216: outn(
1217: "yy_current_state = *yy_state_ptr; /* restore curr. state */ \\" );
1218: }
1219:
1220: outn( "++yy_lp; \\" );
1221: outn( "goto find_rule; \\" );
1222: /* { for vi */
1223: outn( "}" );
1224: }
1225:
1226: else
1227: {
1228: outn(
1229: "/* The intent behind this definition is that it'll catch" );
1230: outn( " * any uses of REJECT which flex missed." );
1231: outn( " */" );
1232: outn( "#define REJECT reject_used_but_not_detected" );
1233: }
1234:
1235: if ( yymore_used )
1236: {
1237: if ( ! C_plus_plus )
1238: {
1239: indent_puts( "static int yy_more_flag = 0;" );
1240: indent_puts( "static int yy_more_len = 0;" );
1241: }
1242:
1243: indent_puts( "#define yymore() (yy_more_flag = 1)" );
1244: indent_puts( "#define YY_MORE_ADJ yy_more_len" );
1245: }
1246:
1247: else
1248: {
1249: indent_puts( "#define yymore() yymore_used_but_not_detected" );
1250: indent_puts( "#define YY_MORE_ADJ 0" );
1251: }
1252:
1253: if ( ! C_plus_plus )
1254: {
1255: if ( yytext_is_array )
1256: {
1257: outn( "#ifndef YYLMAX" );
1258: outn( "#define YYLMAX 8192" );
1259: outn( "#endif\n" );
1260: outn( "char yytext[YYLMAX];" );
1261: outn( "char *yytext_ptr;" );
1262: }
1263:
1264: else
1265: outn( "char *yytext;" );
1266: }
1267:
1268: out( &action_array[defs1_offset] );
1269:
1270: line_directive_out( stdout, 0 );
1271:
1272: skelout();
1273:
1274: if ( ! C_plus_plus )
1275: {
1276: if ( use_read )
1277: {
1278: outn(
1279: "\tif ( (result = read( fileno(yyin), (char *) buf, max_size )) < 0 ) \\" );
1280: outn(
1281: "\t\tYY_FATAL_ERROR( \"input in flex scanner failed\" );" );
1282: }
1283:
1284: else
1285: {
1286: outn(
1287: "\tif ( yy_current_buffer->yy_is_interactive ) \\" );
1288: outn( "\t\t{ \\" );
1289: outn( "\t\tint c = '*', n; \\" );
1290: outn( "\t\tfor ( n = 0; n < max_size && \\" );
1291: outn( "\t\t\t (c = getc( yyin )) != EOF && c != '\\n'; ++n ) \\" );
1292: outn( "\t\t\tbuf[n] = (char) c; \\" );
1293: outn( "\t\tif ( c == '\\n' ) \\" );
1294: outn( "\t\t\tbuf[n++] = (char) c; \\" );
1295: outn( "\t\tif ( c == EOF && ferror( yyin ) ) \\" );
1296: outn(
1297: "\t\t\tYY_FATAL_ERROR( \"input in flex scanner failed\" ); \\" );
1298: outn( "\t\tresult = n; \\" );
1299: outn( "\t\t} \\" );
1300: outn(
1301: "\telse if ( ((result = fread( buf, 1, max_size, yyin )) == 0) \\" );
1302: outn( "\t\t && ferror( yyin ) ) \\" );
1303: outn(
1304: "\t\tYY_FATAL_ERROR( \"input in flex scanner failed\" );" );
1305: }
1306: }
1307:
1308: skelout();
1309:
1310: indent_puts( "#define YY_RULE_SETUP \\" );
1311: indent_up();
1312: if ( bol_needed )
1313: {
1314: indent_puts( "if ( yyleng > 0 ) \\" );
1315: indent_up();
1316: indent_puts( "yy_current_buffer->yy_at_bol = \\" );
1317: indent_puts( "\t\t(yytext[yyleng - 1] == '\\n'); \\" );
1318: indent_down();
1319: }
1320: indent_puts( "YY_USER_ACTION" );
1321: indent_down();
1322:
1323: skelout();
1324:
1325: /* Copy prolog to output file. */
1326: out( &action_array[prolog_offset] );
1327:
1328: line_directive_out( stdout, 0 );
1329:
1330: skelout();
1331:
1332: set_indent( 2 );
1333:
1334: if ( yymore_used )
1335: {
1336: indent_puts( "yy_more_len = 0;" );
1337: indent_puts( "if ( yy_more_flag )" );
1338: indent_up();
1339: indent_puts( "{" );
1340: indent_puts( "yy_more_len = yyleng;" );
1341: indent_puts( "yy_more_flag = 0;" );
1342: indent_puts( "}" );
1343: indent_down();
1344: }
1345:
1346: skelout();
1347:
1348: gen_start_state();
1349:
1350: /* Note, don't use any indentation. */
1351: outn( "yy_match:" );
1352: gen_next_match();
1353:
1354: skelout();
1355: set_indent( 2 );
1356: gen_find_action();
1357:
1358: skelout();
1359: if ( do_yylineno )
1360: {
1361: indent_puts( "if ( yy_act != YY_END_OF_BUFFER )" );
1362: indent_up();
1363: indent_puts( "{" );
1364: indent_puts( "int yyl;" );
1365: indent_puts( "for ( yyl = 0; yyl < yyleng; ++yyl )" );
1366: indent_up();
1367: indent_puts( "if ( yytext[yyl] == '\\n' )" );
1368: indent_up();
1369: indent_puts( "++yylineno;" );
1370: indent_down();
1371: indent_down();
1372: indent_puts( "}" );
1373: indent_down();
1374: }
1375:
1376: skelout();
1377: if ( ddebug )
1378: {
1379: indent_puts( "if ( yy_flex_debug )" );
1380: indent_up();
1381:
1382: indent_puts( "{" );
1383: indent_puts( "if ( yy_act == 0 )" );
1384: indent_up();
1385: indent_puts( C_plus_plus ?
1386: "cerr << \"--scanner backing up\\n\";" :
1387: "fprintf( stderr, \"--scanner backing up\\n\" );" );
1388: indent_down();
1389:
1390: do_indent();
1391: out_dec( "else if ( yy_act < %d )\n", num_rules );
1392: indent_up();
1393:
1394: if ( C_plus_plus )
1395: {
1396: indent_puts(
1397: "cerr << \"--accepting rule at line \" << yy_rule_linenum[yy_act] <<" );
1398: indent_puts(
1399: " \"(\\\"\" << yytext << \"\\\")\\n\";" );
1400: }
1401: else
1402: {
1403: indent_puts(
1404: "fprintf( stderr, \"--accepting rule at line %d (\\\"%s\\\")\\n\"," );
1405:
1406: indent_puts(
1407: " yy_rule_linenum[yy_act], yytext );" );
1408: }
1409:
1410: indent_down();
1411:
1412: do_indent();
1413: out_dec( "else if ( yy_act == %d )\n", num_rules );
1414: indent_up();
1415:
1416: if ( C_plus_plus )
1417: {
1418: indent_puts(
1419: "cerr << \"--accepting default rule (\\\"\" << yytext << \"\\\")\\n\";" );
1420: }
1421: else
1422: {
1423: indent_puts(
1424: "fprintf( stderr, \"--accepting default rule (\\\"%s\\\")\\n\"," );
1425: indent_puts( " yytext );" );
1426: }
1427:
1428: indent_down();
1429:
1430: do_indent();
1431: out_dec( "else if ( yy_act == %d )\n", num_rules + 1 );
1432: indent_up();
1433:
1434: indent_puts( C_plus_plus ?
1435: "cerr << \"--(end of buffer or a NUL)\\n\";" :
1436: "fprintf( stderr, \"--(end of buffer or a NUL)\\n\" );" );
1437:
1438: indent_down();
1439:
1440: do_indent();
1441: outn( "else" );
1442: indent_up();
1443:
1444: if ( C_plus_plus )
1445: {
1446: indent_puts(
1447: "cerr << \"--EOF (start condition \" << YY_START << \")\\n\";" );
1448: }
1449: else
1450: {
1451: indent_puts(
1452: "fprintf( stderr, \"--EOF (start condition %d)\\n\", YY_START );" );
1453: }
1454:
1455: indent_down();
1456:
1457: indent_puts( "}" );
1458: indent_down();
1459: }
1460:
1461: /* Copy actions to output file. */
1462: skelout();
1463: indent_up();
1464: gen_bu_action();
1465: out( &action_array[action_offset] );
1466:
1467: line_directive_out( stdout, 0 );
1468:
1469: /* generate cases for any missing EOF rules */
1470: for ( i = 1; i <= lastsc; ++i )
1471: if ( ! sceof[i] )
1472: {
1473: do_indent();
1474: out_str( "case YY_STATE_EOF(%s):\n", scname[i] );
1475: did_eof_rule = true;
1476: }
1477:
1478: if ( did_eof_rule )
1479: {
1480: indent_up();
1481: indent_puts( "yyterminate();" );
1482: indent_down();
1483: }
1484:
1485:
1486: /* Generate code for handling NUL's, if needed. */
1487:
1488: /* First, deal with backing up and setting up yy_cp if the scanner
1489: * finds that it should JAM on the NUL.
1490: */
1491: skelout();
1492: set_indent( 4 );
1493:
1494: if ( fullspd || fulltbl )
1495: indent_puts( "yy_cp = yy_c_buf_p;" );
1496:
1497: else
1498: { /* compressed table */
1499: if ( ! reject && ! interactive )
1500: {
1501: /* Do the guaranteed-needed backing up to figure
1502: * out the match.
1503: */
1504: indent_puts( "yy_cp = yy_last_accepting_cpos;" );
1505: indent_puts(
1506: "yy_current_state = yy_last_accepting_state;" );
1507: }
1508:
1509: else
1510: /* Still need to initialize yy_cp, though
1511: * yy_current_state was set up by
1512: * yy_get_previous_state().
1513: */
1514: indent_puts( "yy_cp = yy_c_buf_p;" );
1515: }
1516:
1517:
1518: /* Generate code for yy_get_previous_state(). */
1519: set_indent( 1 );
1520: skelout();
1521:
1522: gen_start_state();
1523:
1524: set_indent( 2 );
1525: skelout();
1526: gen_next_state( true );
1527:
1528: set_indent( 1 );
1529: skelout();
1530: gen_NUL_trans();
1531:
1532: skelout();
1533: if ( do_yylineno )
1534: { /* update yylineno inside of unput() */
1535: indent_puts( "if ( c == '\\n' )" );
1536: indent_up();
1537: indent_puts( "--yylineno;" );
1538: indent_down();
1539: }
1540:
1541: skelout();
1542: /* Update BOL and yylineno inside of input(). */
1543: if ( bol_needed )
1544: {
1545: indent_puts( "yy_current_buffer->yy_at_bol = (c == '\\n');" );
1546: if ( do_yylineno )
1547: {
1548: indent_puts( "if ( yy_current_buffer->yy_at_bol )" );
1549: indent_up();
1550: indent_puts( "++yylineno;" );
1551: indent_down();
1552: }
1553: }
1554:
1555: else if ( do_yylineno )
1556: {
1557: indent_puts( "if ( c == '\\n' )" );
1558: indent_up();
1559: indent_puts( "++yylineno;" );
1560: indent_down();
1561: }
1562:
1563: skelout();
1564:
1565: /* Copy remainder of input to output. */
1566:
1567: line_directive_out( stdout, 1 );
1568:
1569: if ( sectnum == 3 )
1570: (void) flexscan(); /* copy remainder of input to output */
1571: }