No empty .Rs/.Re
[netbsd-mini2440.git] / usr.bin / lex / gen.c
blobd70c4cb3c7d2c6a54330c9ec42733585dabca4d6
1 /* gen - actual generation (writing) of flex scanners */
3 /*-
4 * Copyright (c) 1990 The Regents of the University of California.
5 * All rights reserved.
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.
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.
29 /* $NetBSD: gen.c,v 1.17 2003/11/18 21:37:39 christos Exp $ */
31 #include "flexdef.h"
34 /* declare functions that have forward references */
36 void genecs PROTO((void));
39 static int indent_level = 0; /* each level is 8 spaces */
41 #define indent_up() (++indent_level)
42 #define indent_down() (--indent_level)
43 #define set_indent(indent_val) indent_level = indent_val
45 /* Almost everything is done in terms of arrays starting at 1, so provide
46 * a null entry for the zero element of all C arrays. (The exception
47 * to this is that the fast table representation generally uses the
48 * 0 elements of its arrays, too.)
50 static char C_int_decl[] = "static yyconst int %s[%d] =\n { 0,\n";
51 static char C_short_decl[] = "static yyconst short int %s[%d] =\n { 0,\n";
52 static char C_long_decl[] = "static yyconst long int %s[%d] =\n { 0,\n";
53 static char C_state_decl[] =
54 "static yyconst yy_state_type %s[%d] =\n { 0,\n";
57 /* Indent to the current level. */
59 void do_indent()
61 register int i = indent_level * 8;
63 while ( i >= 8 )
65 outc( '\t' );
66 i -= 8;
69 while ( i > 0 )
71 outc( ' ' );
72 --i;
77 /* Generate the code to keep backing-up information. */
79 void gen_backing_up()
81 if ( reject || num_backing_up == 0 )
82 return;
84 if ( fullspd )
85 indent_puts( "if ( yy_current_state[-1].yy_nxt )" );
86 else
87 indent_puts( "if ( yy_accept[yy_current_state] )" );
89 indent_up();
90 indent_puts( "{" );
91 indent_puts( "yy_last_accepting_state = yy_current_state;" );
92 indent_puts( "yy_last_accepting_cpos = yy_cp;" );
93 indent_puts( "}" );
94 indent_down();
98 /* Generate the code to perform the backing up. */
100 void gen_bu_action()
102 if ( reject || num_backing_up == 0 )
103 return;
105 set_indent( 3 );
107 indent_puts( "case 0: /* must back up */" );
108 indent_puts( "/* undo the effects of YY_DO_BEFORE_ACTION */" );
109 indent_puts( "*yy_cp = yy_hold_char;" );
111 if ( fullspd || fulltbl )
112 indent_puts( "yy_cp = yy_last_accepting_cpos + 1;" );
113 else
114 /* Backing-up info for compressed tables is taken \after/
115 * yy_cp has been incremented for the next state.
117 indent_puts( "yy_cp = yy_last_accepting_cpos;" );
119 indent_puts( "yy_current_state = yy_last_accepting_state;" );
120 indent_puts( "goto yy_find_action;" );
121 outc( '\n' );
123 set_indent( 0 );
127 /* genctbl - generates full speed compressed transition table */
129 void genctbl()
131 register int i;
132 int end_of_buffer_action = num_rules + 1;
134 /* Table of verify for transition and offset to next state. */
135 out_dec( "static yyconst struct yy_trans_info yy_transition[%d] =\n",
136 tblend + numecs + 1 );
137 outn( " {" );
139 /* We want the transition to be represented as the offset to the
140 * next state, not the actual state number, which is what it currently
141 * is. The offset is base[nxt[i]] - (base of current state)]. That's
142 * just the difference between the starting points of the two involved
143 * states (to - from).
145 * First, though, we need to find some way to put in our end-of-buffer
146 * flags and states. We do this by making a state with absolutely no
147 * transitions. We put it at the end of the table.
150 /* We need to have room in nxt/chk for two more slots: One for the
151 * action and one for the end-of-buffer transition. We now *assume*
152 * that we're guaranteed the only character we'll try to index this
153 * nxt/chk pair with is EOB, i.e., 0, so we don't have to make sure
154 * there's room for jam entries for other characters.
157 while ( tblend + 2 >= current_max_xpairs )
158 expand_nxt_chk();
160 while ( lastdfa + 1 >= current_max_dfas )
161 increase_max_dfas();
163 base[lastdfa + 1] = tblend + 2;
164 nxt[tblend + 1] = end_of_buffer_action;
165 chk[tblend + 1] = numecs + 1;
166 chk[tblend + 2] = 1; /* anything but EOB */
168 /* So that "make test" won't show arb. differences. */
169 nxt[tblend + 2] = 0;
171 /* Make sure every state has an end-of-buffer transition and an
172 * action #.
174 for ( i = 0; i <= lastdfa; ++i )
176 int anum = dfaacc[i].dfaacc_state;
177 int offset = base[i];
179 chk[offset] = EOB_POSITION;
180 chk[offset - 1] = ACTION_POSITION;
181 nxt[offset - 1] = anum; /* action number */
184 for ( i = 0; i <= tblend; ++i )
186 if ( chk[i] == EOB_POSITION )
187 transition_struct_out( 0, base[lastdfa + 1] - i );
189 else if ( chk[i] == ACTION_POSITION )
190 transition_struct_out( 0, nxt[i] );
192 else if ( chk[i] > numecs || chk[i] == 0 )
193 transition_struct_out( 0, 0 ); /* unused slot */
195 else /* verify, transition */
196 transition_struct_out( chk[i],
197 base[nxt[i]] - (i - chk[i]) );
201 /* Here's the final, end-of-buffer state. */
202 transition_struct_out( chk[tblend + 1], nxt[tblend + 1] );
203 transition_struct_out( chk[tblend + 2], nxt[tblend + 2] );
205 outn( " };\n" );
207 /* Table of pointers to start states. */
208 out_dec(
209 "static yyconst struct yy_trans_info *yy_start_state_list[%d] =\n",
210 lastsc * 2 + 1 );
211 outn( " {" ); /* } so vi doesn't get confused */
213 for ( i = 0; i <= lastsc * 2; ++i )
214 out_dec( " &yy_transition[%d],\n", base[i] );
216 dataend();
218 if ( useecs )
219 genecs();
223 /* Generate equivalence-class tables. */
225 void genecs()
227 register int i, j;
228 int numrows;
230 out_str_dec( C_int_decl, "yy_ec", csize );
232 for ( i = 1; i < csize; ++i )
234 if ( caseins && (i >= 'A') && (i <= 'Z') )
235 ecgroup[i] = ecgroup[clower( i )];
237 ecgroup[i] = ABS( ecgroup[i] );
238 mkdata( ecgroup[i] );
241 dataend();
243 if ( trace )
245 fputs( _( "\n\nEquivalence Classes:\n\n" ), stderr );
247 numrows = csize / 8;
249 for ( j = 0; j < numrows; ++j )
251 for ( i = j; i < csize; i = i + numrows )
253 fprintf( stderr, "%4s = %-2d",
254 readable_form( i ), ecgroup[i] );
256 putc( ' ', stderr );
259 putc( '\n', stderr );
265 /* Generate the code to find the action number. */
267 void gen_find_action()
269 if ( fullspd )
270 indent_puts( "yy_act = yy_current_state[-1].yy_nxt;" );
272 else if ( fulltbl )
273 indent_puts( "yy_act = yy_accept[yy_current_state];" );
275 else if ( reject )
277 indent_puts( "yy_current_state = *--yy_state_ptr;" );
278 indent_puts( "yy_lp = yy_accept[yy_current_state];" );
280 outn(
281 "find_rule: /* we branch to this label when backing up */" );
283 indent_puts(
284 "for ( ; ; ) /* until we find what rule we matched */" );
286 indent_up();
288 indent_puts( "{" );
290 indent_puts(
291 "if ( yy_lp && yy_lp < yy_accept[yy_current_state + 1] )" );
292 indent_up();
293 indent_puts( "{" );
294 indent_puts( "yy_act = yy_acclist[yy_lp];" );
296 if ( variable_trailing_context_rules )
298 indent_puts( "if ( yy_act & YY_TRAILING_HEAD_MASK ||" );
299 indent_puts( " yy_looking_for_trail_begin )" );
300 indent_up();
301 indent_puts( "{" );
303 indent_puts(
304 "if ( yy_act == yy_looking_for_trail_begin )" );
305 indent_up();
306 indent_puts( "{" );
307 indent_puts( "yy_looking_for_trail_begin = 0;" );
308 indent_puts( "yy_act &= ~YY_TRAILING_HEAD_MASK;" );
309 indent_puts( "break;" );
310 indent_puts( "}" );
311 indent_down();
313 indent_puts( "}" );
314 indent_down();
316 indent_puts( "else if ( yy_act & YY_TRAILING_MASK )" );
317 indent_up();
318 indent_puts( "{" );
319 indent_puts(
320 "yy_looking_for_trail_begin = yy_act & ~YY_TRAILING_MASK;" );
321 indent_puts(
322 "yy_looking_for_trail_begin |= YY_TRAILING_HEAD_MASK;" );
324 if ( real_reject )
326 /* Remember matched text in case we back up
327 * due to REJECT.
329 indent_puts( "yy_full_match = yy_cp;" );
330 indent_puts( "yy_full_state = yy_state_ptr;" );
331 indent_puts( "yy_full_lp = yy_lp;" );
334 indent_puts( "}" );
335 indent_down();
337 indent_puts( "else" );
338 indent_up();
339 indent_puts( "{" );
340 indent_puts( "yy_full_match = yy_cp;" );
341 indent_puts( "yy_full_state = yy_state_ptr;" );
342 indent_puts( "yy_full_lp = yy_lp;" );
343 indent_puts( "break;" );
344 indent_puts( "}" );
345 indent_down();
347 indent_puts( "++yy_lp;" );
348 indent_puts( "goto find_rule;" );
351 else
353 /* Remember matched text in case we back up due to
354 * trailing context plus REJECT.
356 indent_up();
357 indent_puts( "{" );
358 indent_puts( "yy_full_match = yy_cp;" );
359 indent_puts( "break;" );
360 indent_puts( "}" );
361 indent_down();
364 indent_puts( "}" );
365 indent_down();
367 indent_puts( "--yy_cp;" );
369 /* We could consolidate the following two lines with those at
370 * the beginning, but at the cost of complaints that we're
371 * branching inside a loop.
373 indent_puts( "yy_current_state = *--yy_state_ptr;" );
374 indent_puts( "yy_lp = yy_accept[yy_current_state];" );
376 indent_puts( "}" );
378 indent_down();
381 else
382 { /* compressed */
383 indent_puts( "yy_act = yy_accept[yy_current_state];" );
385 if ( interactive && ! reject )
387 /* Do the guaranteed-needed backing up to figure out
388 * the match.
390 indent_puts( "if ( yy_act == 0 )" );
391 indent_up();
392 indent_puts( "{ /* have to back up */" );
393 indent_puts( "yy_cp = yy_last_accepting_cpos;" );
394 indent_puts(
395 "yy_current_state = yy_last_accepting_state;" );
396 indent_puts( "yy_act = yy_accept[yy_current_state];" );
397 indent_puts( "}" );
398 indent_down();
404 /* genftbl - generate full transition table */
406 void genftbl()
408 register int i;
409 int end_of_buffer_action = num_rules + 1;
411 out_str_dec( long_align ? C_long_decl : C_short_decl,
412 "yy_accept", lastdfa + 1 );
414 dfaacc[end_of_buffer_state].dfaacc_state = end_of_buffer_action;
416 for ( i = 1; i <= lastdfa; ++i )
418 register int anum = dfaacc[i].dfaacc_state;
420 mkdata( anum );
422 if ( trace && anum )
423 fprintf( stderr, _( "state # %d accepts: [%d]\n" ),
424 i, anum );
427 dataend();
429 if ( useecs )
430 genecs();
432 /* Don't have to dump the actual full table entries - they were
433 * created on-the-fly.
438 /* Generate the code to find the next compressed-table state. */
440 void gen_next_compressed_state( char_map )
441 char *char_map;
443 indent_put2s( "register YY_CHAR yy_c = %s;", char_map );
445 /* Save the backing-up info \before/ computing the next state
446 * because we always compute one more state than needed - we
447 * always proceed until we reach a jam state
449 gen_backing_up();
451 indent_puts(
452 "while ( yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state )" );
453 indent_up();
454 indent_puts( "{" );
455 indent_puts( "yy_current_state = (int) yy_def[yy_current_state];" );
457 if ( usemecs )
459 /* We've arrange it so that templates are never chained
460 * to one another. This means we can afford to make a
461 * very simple test to see if we need to convert to
462 * yy_c's meta-equivalence class without worrying
463 * about erroneously looking up the meta-equivalence
464 * class twice
466 do_indent();
468 /* lastdfa + 2 is the beginning of the templates */
469 out_dec( "if ( yy_current_state >= %d )\n", lastdfa + 2 );
471 indent_up();
472 indent_puts( "yy_c = yy_meta[(unsigned int) yy_c];" );
473 indent_down();
476 indent_puts( "}" );
477 indent_down();
479 indent_puts(
480 "yy_current_state = yy_nxt[yy_base[yy_current_state] + (unsigned int) yy_c];" );
484 /* Generate the code to find the next match. */
486 void gen_next_match()
488 /* NOTE - changes in here should be reflected in gen_next_state() and
489 * gen_NUL_trans().
491 char *char_map = useecs ?
492 "yy_ec[YY_SC_TO_UI(*yy_cp)]" :
493 "YY_SC_TO_UI(*yy_cp)";
495 char *char_map_2 = useecs ?
496 "yy_ec[YY_SC_TO_UI(*++yy_cp)]" :
497 "YY_SC_TO_UI(*++yy_cp)";
499 if ( fulltbl )
501 indent_put2s(
502 "while ( (yy_current_state = yy_nxt[yy_current_state][%s]) > 0 )",
503 char_map );
505 indent_up();
507 if ( num_backing_up > 0 )
509 indent_puts( "{" ); /* } for vi */
510 gen_backing_up();
511 outc( '\n' );
514 indent_puts( "++yy_cp;" );
516 if ( num_backing_up > 0 )
517 /* { for vi */
518 indent_puts( "}" );
520 indent_down();
522 outc( '\n' );
523 indent_puts( "yy_current_state = -yy_current_state;" );
526 else if ( fullspd )
528 indent_puts( "{" ); /* } for vi */
529 indent_puts(
530 "register yyconst struct yy_trans_info *yy_trans_info;\n" );
531 indent_puts( "register YY_CHAR yy_c;\n" );
532 indent_put2s( "for ( yy_c = %s;", char_map );
533 indent_puts(
534 " (yy_trans_info = &yy_current_state[(unsigned int) yy_c])->" );
535 indent_puts( "yy_verify == yy_c;" );
536 indent_put2s( " yy_c = %s )", char_map_2 );
538 indent_up();
540 if ( num_backing_up > 0 )
541 indent_puts( "{" ); /* } for vi */
543 indent_puts( "yy_current_state += yy_trans_info->yy_nxt;" );
545 if ( num_backing_up > 0 )
547 outc( '\n' );
548 gen_backing_up(); /* { for vi */
549 indent_puts( "}" );
552 indent_down(); /* { for vi */
553 indent_puts( "}" );
556 else
557 { /* compressed */
558 indent_puts( "do" );
560 indent_up();
561 indent_puts( "{" ); /* } for vi */
563 gen_next_state( false );
565 indent_puts( "++yy_cp;" );
567 /* { for vi */
568 indent_puts( "}" );
569 indent_down();
571 do_indent();
573 if ( interactive )
574 out_dec( "while ( yy_base[yy_current_state] != %d );\n",
575 jambase );
576 else
577 out_dec( "while ( yy_current_state != %d );\n",
578 jamstate );
580 if ( ! reject && ! interactive )
582 /* Do the guaranteed-needed backing up to figure out
583 * the match.
585 indent_puts( "yy_cp = yy_last_accepting_cpos;" );
586 indent_puts(
587 "yy_current_state = yy_last_accepting_state;" );
593 /* Generate the code to find the next state. */
595 void gen_next_state( worry_about_NULs )
596 int worry_about_NULs;
597 { /* NOTE - changes in here should be reflected in gen_next_match() */
598 char char_map[256];
600 if ( worry_about_NULs && ! nultrans )
602 if ( useecs )
603 (void) snprintf(char_map, sizeof(char_map),
604 "(*yy_cp ? yy_ec[YY_SC_TO_UI(*yy_cp)] : %d)",
605 NUL_ec );
606 else
607 (void) snprintf(char_map, sizeof(char_map),
608 "(*yy_cp ? YY_SC_TO_UI(*yy_cp) : %d)", NUL_ec );
611 else
612 strlcpy(char_map, useecs ?
613 "yy_ec[YY_SC_TO_UI(*yy_cp)]" : "YY_SC_TO_UI(*yy_cp)",
614 sizeof(char_map));
616 if ( worry_about_NULs && nultrans )
618 if ( ! fulltbl && ! fullspd )
619 /* Compressed tables back up *before* they match. */
620 gen_backing_up();
622 indent_puts( "if ( *yy_cp )" );
623 indent_up();
624 indent_puts( "{" ); /* } for vi */
627 if ( fulltbl )
628 indent_put2s(
629 "yy_current_state = yy_nxt[yy_current_state][%s];",
630 char_map );
632 else if ( fullspd )
633 indent_put2s(
634 "yy_current_state += yy_current_state[%s].yy_nxt;",
635 char_map );
637 else
638 gen_next_compressed_state( char_map );
640 if ( worry_about_NULs && nultrans )
642 /* { for vi */
643 indent_puts( "}" );
644 indent_down();
645 indent_puts( "else" );
646 indent_up();
647 indent_puts(
648 "yy_current_state = yy_NUL_trans[yy_current_state];" );
649 indent_down();
652 if ( fullspd || fulltbl )
653 gen_backing_up();
655 if ( reject )
656 indent_puts( "*yy_state_ptr++ = yy_current_state;" );
660 /* Generate the code to make a NUL transition. */
662 void gen_NUL_trans()
663 { /* NOTE - changes in here should be reflected in gen_next_match() */
664 /* Only generate a definition for "yy_cp" if we'll generate code
665 * that uses it. Otherwise lint and the like complain.
667 int need_backing_up = (num_backing_up > 0 && ! reject);
669 if ( need_backing_up && (! nultrans || fullspd || fulltbl) )
670 /* We're going to need yy_cp lying around for the call
671 * below to gen_backing_up().
673 indent_puts( "register char *yy_cp = yy_c_buf_p;" );
675 outc( '\n' );
677 if ( nultrans )
679 indent_puts(
680 "yy_current_state = yy_NUL_trans[yy_current_state];" );
681 indent_puts( "yy_is_jam = (yy_current_state == 0);" );
684 else if ( fulltbl )
686 do_indent();
687 out_dec( "yy_current_state = yy_nxt[yy_current_state][%d];\n",
688 NUL_ec );
689 indent_puts( "yy_is_jam = (yy_current_state <= 0);" );
692 else if ( fullspd )
694 do_indent();
695 out_dec( "register int yy_c = %d;\n", NUL_ec );
697 indent_puts(
698 "register yyconst struct yy_trans_info *yy_trans_info;\n" );
699 indent_puts(
700 "yy_trans_info = &yy_current_state[(unsigned int) yy_c];" );
701 indent_puts( "yy_current_state += yy_trans_info->yy_nxt;" );
703 indent_puts(
704 "yy_is_jam = (yy_trans_info->yy_verify != yy_c);" );
707 else
709 char NUL_ec_str[20];
711 (void) snprintf(NUL_ec_str, sizeof(NUL_ec_str), "%d", NUL_ec);
712 gen_next_compressed_state( NUL_ec_str );
714 do_indent();
715 out_dec( "yy_is_jam = (yy_current_state == %d);\n", jamstate );
717 if ( reject )
719 /* Only stack this state if it's a transition we
720 * actually make. If we stack it on a jam, then
721 * the state stack and yy_c_buf_p get out of sync.
723 indent_puts( "if ( ! yy_is_jam )" );
724 indent_up();
725 indent_puts( "*yy_state_ptr++ = yy_current_state;" );
726 indent_down();
730 /* If we've entered an accepting state, back up; note that
731 * compressed tables have *already* done such backing up, so
732 * we needn't bother with it again.
734 if ( need_backing_up && (fullspd || fulltbl) )
736 outc( '\n' );
737 indent_puts( "if ( ! yy_is_jam )" );
738 indent_up();
739 indent_puts( "{" );
740 gen_backing_up();
741 indent_puts( "}" );
742 indent_down();
747 /* Generate the code to find the start state. */
749 void gen_start_state()
751 if ( fullspd )
753 if ( bol_needed )
755 indent_puts(
756 "yy_current_state = yy_start_state_list[yy_start + YY_AT_BOL()];" );
758 else
759 indent_puts(
760 "yy_current_state = yy_start_state_list[yy_start];" );
763 else
765 indent_puts( "yy_current_state = yy_start;" );
767 if ( bol_needed )
768 indent_puts( "yy_current_state += YY_AT_BOL();" );
770 if ( reject )
772 /* Set up for storing up states. */
773 indent_puts( "yy_state_ptr = yy_state_buf;" );
774 indent_puts( "*yy_state_ptr++ = yy_current_state;" );
780 /* gentabs - generate data statements for the transition tables */
782 void gentabs()
784 int i, j, k, *accset, nacc, *acc_array, total_states;
785 int end_of_buffer_action = num_rules + 1;
787 acc_array = allocate_integer_array( current_max_dfas );
788 nummt = 0;
790 /* The compressed table format jams by entering the "jam state",
791 * losing information about the previous state in the process.
792 * In order to recover the previous state, we effectively need
793 * to keep backing-up information.
795 ++num_backing_up;
797 if ( reject )
799 /* Write out accepting list and pointer list.
801 * First we generate the "yy_acclist" array. In the process,
802 * we compute the indices that will go into the "yy_accept"
803 * array, and save the indices in the dfaacc array.
805 int EOB_accepting_list[2];
807 /* Set up accepting structures for the End Of Buffer state. */
808 EOB_accepting_list[0] = 0;
809 EOB_accepting_list[1] = end_of_buffer_action;
810 accsiz[end_of_buffer_state] = 1;
811 dfaacc[end_of_buffer_state].dfaacc_set = EOB_accepting_list;
813 out_str_dec( long_align ? C_long_decl : C_short_decl,
814 "yy_acclist", MAX( numas, 1 ) + 1 );
816 j = 1; /* index into "yy_acclist" array */
818 for ( i = 1; i <= lastdfa; ++i )
820 acc_array[i] = j;
822 if ( accsiz[i] != 0 )
824 accset = dfaacc[i].dfaacc_set;
825 nacc = accsiz[i];
827 if ( trace )
828 fprintf( stderr,
829 _( "state # %d accepts: " ),
830 i );
832 for ( k = 1; k <= nacc; ++k )
834 int accnum = accset[k];
836 ++j;
838 if ( variable_trailing_context_rules &&
839 ! (accnum & YY_TRAILING_HEAD_MASK) &&
840 accnum > 0 && accnum <= num_rules &&
841 rule_type[accnum] == RULE_VARIABLE )
843 /* Special hack to flag
844 * accepting number as part
845 * of trailing context rule.
847 accnum |= YY_TRAILING_MASK;
850 mkdata( accnum );
852 if ( trace )
854 fprintf( stderr, "[%d]",
855 accset[k] );
857 if ( k < nacc )
858 fputs( ", ", stderr );
859 else
860 putc( '\n', stderr );
866 /* add accepting number for the "jam" state */
867 acc_array[i] = j;
869 dataend();
872 else
874 dfaacc[end_of_buffer_state].dfaacc_state = end_of_buffer_action;
876 for ( i = 1; i <= lastdfa; ++i )
877 acc_array[i] = dfaacc[i].dfaacc_state;
879 /* add accepting number for jam state */
880 acc_array[i] = 0;
883 /* Spit out "yy_accept" array. If we're doing "reject", it'll be
884 * pointers into the "yy_acclist" array. Otherwise it's actual
885 * accepting numbers. In either case, we just dump the numbers.
888 /* "lastdfa + 2" is the size of "yy_accept"; includes room for C arrays
889 * beginning at 0 and for "jam" state.
891 k = lastdfa + 2;
893 if ( reject )
894 /* We put a "cap" on the table associating lists of accepting
895 * numbers with state numbers. This is needed because we tell
896 * where the end of an accepting list is by looking at where
897 * the list for the next state starts.
899 ++k;
901 out_str_dec( long_align ? C_long_decl : C_short_decl, "yy_accept", k );
903 for ( i = 1; i <= lastdfa; ++i )
905 mkdata( acc_array[i] );
907 if ( ! reject && trace && acc_array[i] )
908 fprintf( stderr, _( "state # %d accepts: [%d]\n" ),
909 i, acc_array[i] );
912 /* Add entry for "jam" state. */
913 mkdata( acc_array[i] );
915 if ( reject )
916 /* Add "cap" for the list. */
917 mkdata( acc_array[i] );
919 dataend();
921 if ( useecs )
922 genecs();
924 if ( usemecs )
926 /* Write out meta-equivalence classes (used to index
927 * templates with).
930 if ( trace )
931 fputs( _( "\n\nMeta-Equivalence Classes:\n" ),
932 stderr );
934 out_str_dec( C_int_decl, "yy_meta", numecs + 1 );
936 for ( i = 1; i <= numecs; ++i )
938 if ( trace )
939 fprintf( stderr, "%d = %d\n",
940 i, ABS( tecbck[i] ) );
942 mkdata( ABS( tecbck[i] ) );
945 dataend();
948 total_states = lastdfa + numtemps;
950 out_str_dec( (tblend >= MAX_SHORT || long_align) ?
951 C_long_decl : C_short_decl,
952 "yy_base", total_states + 1 );
954 for ( i = 1; i <= lastdfa; ++i )
956 register int d = def[i];
958 if ( base[i] == JAMSTATE )
959 base[i] = jambase;
961 if ( d == JAMSTATE )
962 def[i] = jamstate;
964 else if ( d < 0 )
966 /* Template reference. */
967 ++tmpuses;
968 def[i] = lastdfa - d + 1;
971 mkdata( base[i] );
974 /* Generate jam state's base index. */
975 mkdata( base[i] );
977 for ( ++i /* skip jam state */; i <= total_states; ++i )
979 mkdata( base[i] );
980 def[i] = jamstate;
983 dataend();
985 out_str_dec( (total_states >= MAX_SHORT || long_align) ?
986 C_long_decl : C_short_decl,
987 "yy_def", total_states + 1 );
989 for ( i = 1; i <= total_states; ++i )
990 mkdata( def[i] );
992 dataend();
994 out_str_dec( (total_states >= MAX_SHORT || long_align) ?
995 C_long_decl : C_short_decl,
996 "yy_nxt", tblend + 1 );
998 for ( i = 1; i <= tblend; ++i )
1000 /* Note, the order of the following test is important.
1001 * If chk[i] is 0, then nxt[i] is undefined.
1003 if ( chk[i] == 0 || nxt[i] == 0 )
1004 nxt[i] = jamstate; /* new state is the JAM state */
1006 mkdata( nxt[i] );
1009 dataend();
1011 out_str_dec( (total_states >= MAX_SHORT || long_align) ?
1012 C_long_decl : C_short_decl,
1013 "yy_chk", tblend + 1 );
1015 for ( i = 1; i <= tblend; ++i )
1017 if ( chk[i] == 0 )
1018 ++nummt;
1020 mkdata( chk[i] );
1023 dataend();
1024 free(acc_array);
1028 /* Write out a formatted string (with a secondary string argument) at the
1029 * current indentation level, adding a final newline.
1032 void indent_put2s( fmt, arg )
1033 char fmt[], arg[];
1035 do_indent();
1036 out_str( fmt, arg );
1037 outn( "" );
1041 /* Write out a string at the current indentation level, adding a final
1042 * newline.
1045 void indent_puts( str )
1046 char str[];
1048 do_indent();
1049 outn( str );
1053 /* make_tables - generate transition tables and finishes generating output file
1056 void make_tables()
1058 register int i;
1059 int did_eof_rule = false;
1061 skelout();
1063 /* First, take care of YY_DO_BEFORE_ACTION depending on yymore
1064 * being used.
1066 set_indent( 1 );
1068 if ( yymore_used && ! yytext_is_array )
1070 indent_puts( "yytext_ptr -= yy_more_len; \\" );
1071 indent_puts( "yyleng = (int) (yy_cp - yytext_ptr); \\" );
1074 else
1075 indent_puts( "yyleng = (int) (yy_cp - yy_bp); \\" );
1077 /* Now also deal with copying yytext_ptr to yytext if needed. */
1078 skelout();
1079 if ( yytext_is_array )
1081 if ( yymore_used )
1082 indent_puts(
1083 "if ( yyleng + yy_more_offset >= YYLMAX ) \\" );
1084 else
1085 indent_puts( "if ( yyleng >= YYLMAX ) \\" );
1087 indent_up();
1088 indent_puts(
1089 "YY_FATAL_ERROR( \"token too large, exceeds YYLMAX\" ); \\" );
1090 indent_down();
1092 if ( yymore_used )
1094 indent_puts(
1095 "yy_flex_strncpy( &yytext[yy_more_offset], yytext_ptr, yyleng + 1 ); \\" );
1096 indent_puts( "yyleng += yy_more_offset; \\" );
1097 indent_puts(
1098 "yy_prev_more_offset = yy_more_offset; \\" );
1099 indent_puts( "yy_more_offset = 0; \\" );
1101 else
1103 indent_puts(
1104 "yy_flex_strncpy( yytext, yytext_ptr, yyleng + 1 ); \\" );
1108 set_indent( 0 );
1110 skelout();
1113 out_dec( "#define YY_NUM_RULES %d\n", num_rules );
1114 out_dec( "#define YY_END_OF_BUFFER %d\n", num_rules + 1 );
1116 if ( fullspd )
1118 /* Need to define the transet type as a size large
1119 * enough to hold the biggest offset.
1121 int total_table_size = tblend + numecs + 1;
1122 char *trans_offset_type =
1123 (total_table_size >= MAX_SHORT || long_align) ?
1124 "long" : "short";
1126 set_indent( 0 );
1127 indent_puts( "struct yy_trans_info" );
1128 indent_up();
1129 indent_puts( "{" ); /* } for vi */
1131 if ( long_align )
1132 indent_puts( "long yy_verify;" );
1133 else
1134 indent_puts( "short yy_verify;" );
1136 /* In cases where its sister yy_verify *is* a "yes, there is
1137 * a transition", yy_nxt is the offset (in records) to the
1138 * next state. In most cases where there is no transition,
1139 * the value of yy_nxt is irrelevant. If yy_nxt is the -1th
1140 * record of a state, though, then yy_nxt is the action number
1141 * for that state.
1144 indent_put2s( "%s yy_nxt;", trans_offset_type );
1145 indent_puts( "};" );
1146 indent_down();
1149 if ( fullspd )
1150 genctbl();
1151 else if ( fulltbl )
1152 genftbl();
1153 else
1154 gentabs();
1156 /* Definitions for backing up. We don't need them if REJECT
1157 * is being used because then we use an alternative backin-up
1158 * technique instead.
1160 if ( num_backing_up > 0 && ! reject )
1162 if ( ! C_plus_plus )
1164 indent_puts(
1165 "static yy_state_type yy_last_accepting_state;" );
1166 indent_puts(
1167 "static char *yy_last_accepting_cpos;\n" );
1171 if ( nultrans )
1173 out_str_dec( C_state_decl, "yy_NUL_trans", lastdfa + 1 );
1175 for ( i = 1; i <= lastdfa; ++i )
1177 if ( fullspd )
1178 out_dec( " &yy_transition[%d],\n", base[i] );
1179 else
1180 mkdata( nultrans[i] );
1183 dataend();
1186 if ( ddebug )
1187 { /* Spit out table mapping rules to line numbers. */
1188 if ( ! C_plus_plus )
1190 indent_puts( "extern int yy_flex_debug;" );
1191 indent_puts( "int yy_flex_debug = 1;\n" );
1194 out_str_dec( long_align ? C_long_decl : C_short_decl,
1195 "yy_rule_linenum", num_rules );
1196 for ( i = 1; i < num_rules; ++i )
1197 mkdata( rule_linenum[i] );
1198 dataend();
1201 if ( reject )
1203 /* Declare state buffer variables. */
1204 if ( ! C_plus_plus )
1206 outn(
1207 "static yy_state_type yy_state_buf[YY_BUF_SIZE + 2], *yy_state_ptr;" );
1208 outn( "static char *yy_full_match;" );
1209 outn( "static int yy_lp;" );
1212 if ( variable_trailing_context_rules )
1214 if ( ! C_plus_plus )
1216 outn(
1217 "static int yy_looking_for_trail_begin = 0;" );
1218 outn( "static int yy_full_lp;" );
1219 outn( "static int *yy_full_state;" );
1222 out_hex( "#define YY_TRAILING_MASK 0x%x\n",
1223 (unsigned int) YY_TRAILING_MASK );
1224 out_hex( "#define YY_TRAILING_HEAD_MASK 0x%x\n",
1225 (unsigned int) YY_TRAILING_HEAD_MASK );
1228 outn( "#define REJECT \\" );
1229 outn( "{ \\" ); /* } for vi */
1230 outn(
1231 "*yy_cp = yy_hold_char; /* undo effects of setting up yytext */ \\" );
1232 outn(
1233 "yy_cp = yy_full_match; /* restore poss. backed-over text */ \\" );
1235 if ( variable_trailing_context_rules )
1237 outn(
1238 "yy_lp = yy_full_lp; /* restore orig. accepting pos. */ \\" );
1239 outn(
1240 "yy_state_ptr = yy_full_state; /* restore orig. state */ \\" );
1241 outn(
1242 "yy_current_state = *yy_state_ptr; /* restore curr. state */ \\" );
1245 outn( "++yy_lp; \\" );
1246 outn( "goto find_rule; \\" );
1247 /* { for vi */
1248 outn( "}" );
1251 else
1253 outn(
1254 "/* The intent behind this definition is that it'll catch" );
1255 outn( " * any uses of REJECT which flex missed." );
1256 outn( " */" );
1257 outn( "#define REJECT reject_used_but_not_detected" );
1260 if ( yymore_used )
1262 if ( ! C_plus_plus )
1264 if ( yytext_is_array )
1266 indent_puts( "static int yy_more_offset = 0;" );
1267 indent_puts(
1268 "static int yy_prev_more_offset = 0;" );
1270 else
1272 indent_puts( "static int yy_more_flag = 0;" );
1273 indent_puts( "static int yy_more_len = 0;" );
1277 if ( yytext_is_array )
1279 indent_puts(
1280 "#define yymore() (yy_more_offset = yy_flex_strlen( yytext ))" );
1281 indent_puts( "#define YY_NEED_STRLEN" );
1282 indent_puts( "#define YY_MORE_ADJ 0" );
1283 indent_puts( "#define YY_RESTORE_YY_MORE_OFFSET \\" );
1284 indent_up();
1285 indent_puts( "{ \\" );
1286 indent_puts( "yy_more_offset = yy_prev_more_offset; \\" );
1287 indent_puts( "yyleng -= yy_more_offset; \\" );
1288 indent_puts( "}" );
1289 indent_down();
1291 else
1293 indent_puts( "#define yymore() (yy_more_flag = 1)" );
1294 indent_puts( "#define YY_MORE_ADJ yy_more_len" );
1295 indent_puts( "#define YY_RESTORE_YY_MORE_OFFSET" );
1299 else
1301 indent_puts( "#define yymore() yymore_used_but_not_detected" );
1302 indent_puts( "#define YY_MORE_ADJ 0" );
1303 indent_puts( "#define YY_RESTORE_YY_MORE_OFFSET" );
1306 if ( ! C_plus_plus )
1308 if ( yytext_is_array )
1310 outn( "#ifndef YYLMAX" );
1311 outn( "#define YYLMAX 8192" );
1312 outn( "#endif\n" );
1313 outn( "char yytext[YYLMAX];" );
1314 outn( "char *yytext_ptr;" );
1317 else
1318 outn( "char *yytext;" );
1321 out( &action_array[defs1_offset] );
1323 line_directive_out( stdout, 0 );
1325 skelout();
1327 if ( ! C_plus_plus )
1329 if ( use_read )
1331 outn(
1332 "\tif ( (result = read( fileno(yyin), (char *) buf, max_size )) < 0 ) \\" );
1333 outn(
1334 "\t\tYY_FATAL_ERROR( \"input in flex scanner failed\" );" );
1337 else
1339 outn(
1340 "\tif ( yy_current_buffer->yy_is_interactive ) \\" );
1341 outn( "\t\t{ \\" );
1342 outn( "\t\tint c = '*', n; \\" );
1343 outn( "\t\tfor ( n = 0; n < max_size && \\" );
1344 outn( "\t\t\t (c = getc( yyin )) != EOF && c != '\\n'; ++n ) \\" );
1345 outn( "\t\t\tbuf[n] = (char) c; \\" );
1346 outn( "\t\tif ( c == '\\n' ) \\" );
1347 outn( "\t\t\tbuf[n++] = (char) c; \\" );
1348 outn( "\t\tif ( c == EOF && ferror( yyin ) ) \\" );
1349 outn(
1350 "\t\t\tYY_FATAL_ERROR( \"input in flex scanner failed\" ); \\" );
1351 outn( "\t\tresult = n; \\" );
1352 outn( "\t\t} \\" );
1353 outn(
1354 "\telse if ( ((result = fread( buf, 1, (size_t)max_size, yyin )) == 0) \\" );
1355 outn( "\t\t && ferror( yyin ) ) \\" );
1356 outn(
1357 "\t\tYY_FATAL_ERROR( \"input in flex scanner failed\" );" );
1361 skelout();
1363 indent_puts( "#define YY_RULE_SETUP \\" );
1364 indent_up();
1365 if ( bol_needed )
1367 indent_puts( "if ( yyleng > 0 ) \\" );
1368 indent_up();
1369 indent_puts( "yy_current_buffer->yy_at_bol = \\" );
1370 indent_puts( "\t\t(yytext[yyleng - 1] == '\\n'); \\" );
1371 indent_down();
1373 indent_puts( "YY_USER_ACTION" );
1374 indent_down();
1376 skelout();
1378 /* Copy prolog to output file. */
1379 out( &action_array[prolog_offset] );
1381 line_directive_out( stdout, 0 );
1383 skelout();
1385 set_indent( 2 );
1387 if ( yymore_used && ! yytext_is_array )
1389 indent_puts( "yy_more_len = 0;" );
1390 indent_puts( "if ( yy_more_flag )" );
1391 indent_up();
1392 indent_puts( "{" );
1393 indent_puts( "yy_more_len = yy_c_buf_p - yytext_ptr;" );
1394 indent_puts( "yy_more_flag = 0;" );
1395 indent_puts( "}" );
1396 indent_down();
1399 skelout();
1401 gen_start_state();
1403 /* Note, don't use any indentation. */
1404 outn( "yy_match:" );
1405 gen_next_match();
1407 skelout();
1408 set_indent( 2 );
1409 gen_find_action();
1411 skelout();
1412 if ( do_yylineno )
1414 indent_puts( "if ( yy_act != YY_END_OF_BUFFER )" );
1415 indent_up();
1416 indent_puts( "{" );
1417 indent_puts( "int yyl;" );
1418 indent_puts( "for ( yyl = 0; yyl < yyleng; ++yyl )" );
1419 indent_up();
1420 indent_puts( "if ( yytext[yyl] == '\\n' )" );
1421 indent_up();
1422 indent_puts( "++yylineno;" );
1423 indent_down();
1424 indent_down();
1425 indent_puts( "}" );
1426 indent_down();
1429 skelout();
1430 if ( ddebug )
1432 indent_puts( "if ( yy_flex_debug )" );
1433 indent_up();
1435 indent_puts( "{" );
1436 indent_puts( "if ( yy_act == 0 )" );
1437 indent_up();
1438 indent_puts( C_plus_plus ?
1439 "std::cerr << \"--scanner backing up\" << std::endl;" :
1440 "fprintf( stderr, \"--scanner backing up\\n\" );" );
1441 indent_down();
1443 do_indent();
1444 out_dec( "else if ( yy_act < %d )\n", num_rules );
1445 indent_up();
1447 if ( C_plus_plus )
1449 indent_puts(
1450 "std::cerr << \"--accepting rule at line \" << yy_rule_linenum[yy_act] <<" );
1451 indent_puts(
1452 " \"(\\\"\" << yytext << \"\\\")\" << std::endl;" );
1454 else
1456 indent_puts(
1457 "fprintf( stderr, \"--accepting rule at line %d (\\\"%s\\\")\\n\"," );
1459 indent_puts(
1460 " yy_rule_linenum[yy_act], yytext );" );
1463 indent_down();
1465 do_indent();
1466 out_dec( "else if ( yy_act == %d )\n", num_rules );
1467 indent_up();
1469 if ( C_plus_plus )
1471 indent_puts(
1472 "std::cerr << \"--accepting default rule (\\\"\" << yytext << \"\\\")\" << std::endl;" );
1474 else
1476 indent_puts(
1477 "fprintf( stderr, \"--accepting default rule (\\\"%s\\\")\\n\"," );
1478 indent_puts( " yytext );" );
1481 indent_down();
1483 do_indent();
1484 out_dec( "else if ( yy_act == %d )\n", num_rules + 1 );
1485 indent_up();
1487 indent_puts( C_plus_plus ?
1488 "std::cerr << \"--(end of buffer or a NUL)\" << std::endl;" :
1489 "fprintf( stderr, \"--(end of buffer or a NUL)\\n\" );" );
1491 indent_down();
1493 do_indent();
1494 outn( "else" );
1495 indent_up();
1497 if ( C_plus_plus )
1499 indent_puts(
1500 "std::cerr << \"--EOF (start condition \" << YY_START << \")\" << std::endl;" );
1502 else
1504 indent_puts(
1505 "fprintf( stderr, \"--EOF (start condition %d)\\n\", YY_START );" );
1508 indent_down();
1510 indent_puts( "}" );
1511 indent_down();
1514 /* Copy actions to output file. */
1515 skelout();
1516 indent_up();
1517 gen_bu_action();
1518 out( &action_array[action_offset] );
1520 line_directive_out( stdout, 0 );
1522 /* generate cases for any missing EOF rules */
1523 for ( i = 1; i <= lastsc; ++i )
1524 if ( ! sceof[i] )
1526 do_indent();
1527 out_str( "case YY_STATE_EOF(%s):\n", scname[i] );
1528 did_eof_rule = true;
1531 if ( did_eof_rule )
1533 indent_up();
1534 indent_puts( "yyterminate();" );
1535 indent_down();
1539 /* Generate code for handling NUL's, if needed. */
1541 /* First, deal with backing up and setting up yy_cp if the scanner
1542 * finds that it should JAM on the NUL.
1544 skelout();
1545 set_indent( 4 );
1547 if ( fullspd || fulltbl )
1548 indent_puts( "yy_cp = yy_c_buf_p;" );
1550 else
1551 { /* compressed table */
1552 if ( ! reject && ! interactive )
1554 /* Do the guaranteed-needed backing up to figure
1555 * out the match.
1557 indent_puts( "yy_cp = yy_last_accepting_cpos;" );
1558 indent_puts(
1559 "yy_current_state = yy_last_accepting_state;" );
1562 else
1563 /* Still need to initialize yy_cp, though
1564 * yy_current_state was set up by
1565 * yy_get_previous_state().
1567 indent_puts( "yy_cp = yy_c_buf_p;" );
1571 /* Generate code for yy_get_previous_state(). */
1572 set_indent( 1 );
1573 skelout();
1575 gen_start_state();
1577 set_indent( 2 );
1578 skelout();
1579 gen_next_state( true );
1581 set_indent( 1 );
1582 skelout();
1583 gen_NUL_trans();
1585 skelout();
1586 if ( do_yylineno )
1587 { /* update yylineno inside of unput() */
1588 indent_puts( "if ( c == '\\n' )" );
1589 indent_up();
1590 indent_puts( "--yylineno;" );
1591 indent_down();
1594 skelout();
1595 /* Update BOL and yylineno inside of input(). */
1596 if ( bol_needed )
1598 indent_puts( "yy_current_buffer->yy_at_bol = (c == '\\n');" );
1599 if ( do_yylineno )
1601 indent_puts( "if ( yy_current_buffer->yy_at_bol )" );
1602 indent_up();
1603 indent_puts( "++yylineno;" );
1604 indent_down();
1608 else if ( do_yylineno )
1610 indent_puts( "if ( c == '\\n' )" );
1611 indent_up();
1612 indent_puts( "++yylineno;" );
1613 indent_down();
1616 skelout();
1618 /* Copy remainder of input to output. */
1620 line_directive_out( stdout, 1 );
1622 if ( sectnum == 3 )
1623 (void) flexscan(); /* copy remainder of input to output */