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