etc/services - sync with NetBSD-8
[minix.git] / external / bsd / flex / dist / gen.c
blob157a04edeb0d0c35d3d190e3fb1bdf753e96697d
1 /* $NetBSD: gen.c,v 1.11 2014/10/30 18:44:05 christos Exp $ */
3 /* gen - actual generation (writing) of flex scanners */
5 /* Copyright (c) 1990 The Regents of the University of California. */
6 /* All rights reserved. */
8 /* This code is derived from software contributed to Berkeley by */
9 /* Vern Paxson. */
11 /* The United States Government has rights in this work pursuant */
12 /* to contract no. DE-AC03-76SF00098 between the United States */
13 /* Department of Energy and the University of California. */
15 /* This file is part of flex. */
17 /* Redistribution and use in source and binary forms, with or without */
18 /* modification, are permitted provided that the following conditions */
19 /* are met: */
21 /* 1. Redistributions of source code must retain the above copyright */
22 /* notice, this list of conditions and the following disclaimer. */
23 /* 2. Redistributions in binary form must reproduce the above copyright */
24 /* notice, this list of conditions and the following disclaimer in the */
25 /* documentation and/or other materials provided with the distribution. */
27 /* Neither the name of the University nor the names of its contributors */
28 /* may be used to endorse or promote products derived from this software */
29 /* without specific prior written permission. */
31 /* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR */
32 /* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED */
33 /* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR */
34 /* PURPOSE. */
35 #include "flexdef.h"
36 __RCSID("$NetBSD: gen.c,v 1.11 2014/10/30 18:44:05 christos Exp $");
38 #include "tables.h"
41 /* declare functions that have forward references */
43 void gen_next_state PROTO ((int));
44 void genecs PROTO ((void));
45 void indent_put2s PROTO ((const char *, const char *));
46 void indent_puts PROTO ((const char *));
49 static int indent_level = 0; /* each level is 8 spaces */
51 #define indent_up() (++indent_level)
52 #define indent_down() (--indent_level)
53 #define set_indent(indent_val) indent_level = indent_val
55 /* Almost everything is done in terms of arrays starting at 1, so provide
56 * a null entry for the zero element of all C arrays. (The exception
57 * to this is that the fast table representation generally uses the
58 * 0 elements of its arrays, too.)
61 static const char *get_int16_decl (void)
63 return (gentables)
64 ? "static yyconst flex_int16_t %s[%d] =\n { 0,\n"
65 : "static yyconst flex_int16_t * %s = 0;\n";
69 static const char *get_int32_decl (void)
71 return (gentables)
72 ? "static yyconst flex_int32_t %s[%d] =\n { 0,\n"
73 : "static yyconst flex_int32_t * %s = 0;\n";
76 static const char *get_uint16_decl (void)
78 return (gentables)
79 ? "static yyconst flex_uint16_t %s[%d] =\n { 0,\n"
80 : "static yyconst flex_uint16_t * %s = 0;\n";
83 static const char *get_uint32_decl (void)
85 return (gentables)
86 ? "static yyconst flex_uint32_t %s[%d] =\n { 0,\n"
87 : "static yyconst flex_uint32_t * %s = 0;\n";
90 static const char *get_yy_char_decl (void)
92 return (gentables)
93 ? "static yyconst YY_CHAR %s[%d] =\n { 0,\n"
94 : "static yyconst YY_CHAR * %s = 0;\n";
96 static const char *get_state_decl (void)
98 return (gentables)
99 ? "static yyconst yy_state_type %s[%d] =\n { 0,\n"
100 : "static yyconst yy_state_type * %s = 0;\n";
103 /* Indent to the current level. */
105 void do_indent ()
107 register int i = indent_level * 8;
109 while (i >= 8) {
110 outc ('\t');
111 i -= 8;
114 while (i > 0) {
115 outc (' ');
116 --i;
121 /** Make the table for possible eol matches.
122 * @return the newly allocated rule_can_match_eol table
124 static struct yytbl_data *mkeoltbl (void)
126 int i;
127 flex_int8_t *tdata = 0;
128 struct yytbl_data *tbl;
130 tbl = (struct yytbl_data *) calloc (1, sizeof (struct yytbl_data));
131 yytbl_data_init (tbl, YYTD_ID_RULE_CAN_MATCH_EOL);
132 tbl->td_flags = YYTD_DATA8;
133 tbl->td_lolen = num_rules + 1;
134 tbl->td_data = tdata =
135 (flex_int8_t *) calloc (tbl->td_lolen, sizeof (flex_int8_t));
137 for (i = 1; i <= num_rules; i++)
138 tdata[i] = rule_has_nl[i] ? 1 : 0;
140 buf_prints (&yydmap_buf,
141 "\t{YYTD_ID_RULE_CAN_MATCH_EOL, (void**)&yy_rule_can_match_eol, sizeof(%s)},\n",
142 "flex_int32_t");
143 return tbl;
146 /* Generate the table for possible eol matches. */
147 static void geneoltbl (void)
149 int i;
151 outn ("m4_ifdef( [[M4_YY_USE_LINENO]],[[");
152 outn ("/* Table of booleans, true if rule could match eol. */");
153 out_str_dec (get_int32_decl (), "yy_rule_can_match_eol",
154 num_rules + 1);
156 if (gentables) {
157 for (i = 1; i <= num_rules; i++) {
158 out_dec ("%d, ", rule_has_nl[i] ? 1 : 0);
159 /* format nicely, 20 numbers per line. */
160 if ((i % 20) == 19)
161 out ("\n ");
163 out (" };\n");
165 outn ("]])");
169 /* Generate the code to keep backing-up information. */
171 void gen_backing_up ()
173 if (reject || num_backing_up == 0)
174 return;
176 if (fullspd)
177 indent_puts ("if ( yy_current_state[-1].yy_nxt )");
178 else
179 indent_puts ("if ( yy_accept[yy_current_state] )");
181 indent_up ();
182 indent_puts ("{");
183 indent_puts ("YY_G(yy_last_accepting_state) = yy_current_state;");
184 indent_puts ("YY_G(yy_last_accepting_cpos) = yy_cp;");
185 indent_puts ("}");
186 indent_down ();
190 /* Generate the code to perform the backing up. */
192 void gen_bu_action ()
194 if (reject || num_backing_up == 0)
195 return;
197 set_indent (3);
199 indent_puts ("case 0: /* must back up */");
200 indent_puts ("/* undo the effects of YY_DO_BEFORE_ACTION */");
201 indent_puts ("*yy_cp = YY_G(yy_hold_char);");
203 if (fullspd || fulltbl)
204 indent_puts ("yy_cp = YY_G(yy_last_accepting_cpos) + 1;");
205 else
206 /* Backing-up info for compressed tables is taken \after/
207 * yy_cp has been incremented for the next state.
209 indent_puts ("yy_cp = YY_G(yy_last_accepting_cpos);");
211 indent_puts ("yy_current_state = YY_G(yy_last_accepting_state);");
212 indent_puts ("goto yy_find_action;");
213 outc ('\n');
215 set_indent (0);
218 /** mkctbl - make full speed compressed transition table
219 * This is an array of structs; each struct a pair of integers.
220 * You should call mkssltbl() immediately after this.
221 * Then, I think, mkecstbl(). Arrrg.
222 * @return the newly allocated trans table
225 static struct yytbl_data *mkctbl (void)
227 register int i;
228 struct yytbl_data *tbl = 0;
229 flex_int32_t *tdata = 0, curr = 0;
230 int end_of_buffer_action = num_rules + 1;
232 buf_prints (&yydmap_buf,
233 "\t{YYTD_ID_TRANSITION, (void**)&yy_transition, sizeof(%s)},\n",
234 ((tblend + numecs + 1) >= INT16_MAX
235 || long_align) ? "flex_int32_t" : "flex_int16_t");
237 tbl = (struct yytbl_data *) calloc (1, sizeof (struct yytbl_data));
238 yytbl_data_init (tbl, YYTD_ID_TRANSITION);
239 tbl->td_flags = YYTD_DATA32 | YYTD_STRUCT;
240 tbl->td_hilen = 0;
241 tbl->td_lolen = tblend + numecs + 1; /* number of structs */
243 tbl->td_data = tdata =
244 (flex_int32_t *) calloc (tbl->td_lolen * 2, sizeof (flex_int32_t));
246 /* We want the transition to be represented as the offset to the
247 * next state, not the actual state number, which is what it currently
248 * is. The offset is base[nxt[i]] - (base of current state)]. That's
249 * just the difference between the starting points of the two involved
250 * states (to - from).
252 * First, though, we need to find some way to put in our end-of-buffer
253 * flags and states. We do this by making a state with absolutely no
254 * transitions. We put it at the end of the table.
257 /* We need to have room in nxt/chk for two more slots: One for the
258 * action and one for the end-of-buffer transition. We now *assume*
259 * that we're guaranteed the only character we'll try to index this
260 * nxt/chk pair with is EOB, i.e., 0, so we don't have to make sure
261 * there's room for jam entries for other characters.
264 while (tblend + 2 >= current_max_xpairs)
265 expand_nxt_chk ();
267 while (lastdfa + 1 >= current_max_dfas)
268 increase_max_dfas ();
270 base[lastdfa + 1] = tblend + 2;
271 nxt[tblend + 1] = end_of_buffer_action;
272 chk[tblend + 1] = numecs + 1;
273 chk[tblend + 2] = 1; /* anything but EOB */
275 /* So that "make test" won't show arb. differences. */
276 nxt[tblend + 2] = 0;
278 /* Make sure every state has an end-of-buffer transition and an
279 * action #.
281 for (i = 0; i <= lastdfa; ++i) {
282 int anum = dfaacc[i].dfaacc_state;
283 int offset = base[i];
285 chk[offset] = EOB_POSITION;
286 chk[offset - 1] = ACTION_POSITION;
287 nxt[offset - 1] = anum; /* action number */
290 for (i = 0; i <= tblend; ++i) {
291 if (chk[i] == EOB_POSITION) {
292 tdata[curr++] = 0;
293 tdata[curr++] = base[lastdfa + 1] - i;
296 else if (chk[i] == ACTION_POSITION) {
297 tdata[curr++] = 0;
298 tdata[curr++] = nxt[i];
301 else if (chk[i] > numecs || chk[i] == 0) {
302 tdata[curr++] = 0;
303 tdata[curr++] = 0;
305 else { /* verify, transition */
307 tdata[curr++] = chk[i];
308 tdata[curr++] = base[nxt[i]] - (i - chk[i]);
313 /* Here's the final, end-of-buffer state. */
314 tdata[curr++] = chk[tblend + 1];
315 tdata[curr++] = nxt[tblend + 1];
317 tdata[curr++] = chk[tblend + 2];
318 tdata[curr++] = nxt[tblend + 2];
320 return tbl;
324 /** Make start_state_list table.
325 * @return the newly allocated start_state_list table
327 static struct yytbl_data *mkssltbl (void)
329 struct yytbl_data *tbl = 0;
330 flex_int32_t *tdata = 0;
331 flex_int32_t i;
333 tbl = (struct yytbl_data *) calloc (1, sizeof (struct yytbl_data));
334 yytbl_data_init (tbl, YYTD_ID_START_STATE_LIST);
335 tbl->td_flags = YYTD_DATA32 | YYTD_PTRANS;
336 tbl->td_hilen = 0;
337 tbl->td_lolen = lastsc * 2 + 1;
339 tbl->td_data = tdata =
340 (flex_int32_t *) calloc (tbl->td_lolen, sizeof (flex_int32_t));
342 for (i = 0; i <= lastsc * 2; ++i)
343 tdata[i] = base[i];
345 buf_prints (&yydmap_buf,
346 "\t{YYTD_ID_START_STATE_LIST, (void**)&yy_start_state_list, sizeof(%s)},\n",
347 "struct yy_trans_info*");
349 return tbl;
354 /* genctbl - generates full speed compressed transition table */
356 void genctbl ()
358 register int i;
359 int end_of_buffer_action = num_rules + 1;
361 /* Table of verify for transition and offset to next state. */
362 if (gentables)
363 out_dec ("static yyconst struct yy_trans_info yy_transition[%d] =\n {\n", tblend + numecs + 1);
364 else
365 outn ("static yyconst struct yy_trans_info *yy_transition = 0;");
367 /* We want the transition to be represented as the offset to the
368 * next state, not the actual state number, which is what it currently
369 * is. The offset is base[nxt[i]] - (base of current state)]. That's
370 * just the difference between the starting points of the two involved
371 * states (to - from).
373 * First, though, we need to find some way to put in our end-of-buffer
374 * flags and states. We do this by making a state with absolutely no
375 * transitions. We put it at the end of the table.
378 /* We need to have room in nxt/chk for two more slots: One for the
379 * action and one for the end-of-buffer transition. We now *assume*
380 * that we're guaranteed the only character we'll try to index this
381 * nxt/chk pair with is EOB, i.e., 0, so we don't have to make sure
382 * there's room for jam entries for other characters.
385 while (tblend + 2 >= current_max_xpairs)
386 expand_nxt_chk ();
388 while (lastdfa + 1 >= current_max_dfas)
389 increase_max_dfas ();
391 base[lastdfa + 1] = tblend + 2;
392 nxt[tblend + 1] = end_of_buffer_action;
393 chk[tblend + 1] = numecs + 1;
394 chk[tblend + 2] = 1; /* anything but EOB */
396 /* So that "make test" won't show arb. differences. */
397 nxt[tblend + 2] = 0;
399 /* Make sure every state has an end-of-buffer transition and an
400 * action #.
402 for (i = 0; i <= lastdfa; ++i) {
403 int anum = dfaacc[i].dfaacc_state;
404 int offset = base[i];
406 chk[offset] = EOB_POSITION;
407 chk[offset - 1] = ACTION_POSITION;
408 nxt[offset - 1] = anum; /* action number */
411 for (i = 0; i <= tblend; ++i) {
412 if (chk[i] == EOB_POSITION)
413 transition_struct_out (0, base[lastdfa + 1] - i);
415 else if (chk[i] == ACTION_POSITION)
416 transition_struct_out (0, nxt[i]);
418 else if (chk[i] > numecs || chk[i] == 0)
419 transition_struct_out (0, 0); /* unused slot */
421 else /* verify, transition */
422 transition_struct_out (chk[i],
423 base[nxt[i]] - (i -
424 chk[i]));
428 /* Here's the final, end-of-buffer state. */
429 transition_struct_out (chk[tblend + 1], nxt[tblend + 1]);
430 transition_struct_out (chk[tblend + 2], nxt[tblend + 2]);
432 if (gentables)
433 outn (" };\n");
435 /* Table of pointers to start states. */
436 if (gentables)
437 out_dec ("static yyconst struct yy_trans_info *yy_start_state_list[%d] =\n", lastsc * 2 + 1);
438 else
439 outn ("static yyconst struct yy_trans_info **yy_start_state_list =0;");
441 if (gentables) {
442 outn (" {");
444 for (i = 0; i <= lastsc * 2; ++i)
445 out_dec (" &yy_transition[%d],\n", base[i]);
447 dataend ();
450 if (useecs)
451 genecs ();
455 /* mkecstbl - Make equivalence-class tables. */
456 static struct yytbl_data *mkecstbl (void)
458 register int i;
459 struct yytbl_data *tbl = 0;
460 flex_int32_t *tdata = 0;
462 tbl = (struct yytbl_data *) calloc (1, sizeof (struct yytbl_data));
463 yytbl_data_init (tbl, YYTD_ID_EC);
464 tbl->td_flags |= YYTD_DATA32;
465 tbl->td_hilen = 0;
466 tbl->td_lolen = csize;
468 tbl->td_data = tdata =
469 (flex_int32_t *) calloc (tbl->td_lolen, sizeof (flex_int32_t));
471 for (i = 1; i < csize; ++i) {
472 ecgroup[i] = ABS (ecgroup[i]);
473 tdata[i] = ecgroup[i];
476 buf_prints (&yydmap_buf,
477 "\t{YYTD_ID_EC, (void**)&yy_ec, sizeof(%s)},\n",
478 "YY_CHAR");
480 return tbl;
483 /* Generate equivalence-class tables. */
485 void genecs ()
487 register int i, j;
488 int numrows;
490 out_str_dec (get_yy_char_decl (), "yy_ec", csize);
492 for (i = 1; i < csize; ++i) {
493 ecgroup[i] = ABS (ecgroup[i]);
494 mkdata (ecgroup[i]);
497 dataend ();
499 if (trace) {
500 fputs (_("\n\nEquivalence Classes:\n\n"), stderr);
502 numrows = csize / 8;
504 for (j = 0; j < numrows; ++j) {
505 for (i = j; i < csize; i = i + numrows) {
506 fprintf (stderr, "%4s = %-2d",
507 readable_form (i), ecgroup[i]);
509 putc (' ', stderr);
512 putc ('\n', stderr);
518 /* Generate the code to find the action number. */
520 void gen_find_action ()
522 if (fullspd)
523 indent_puts ("yy_act = yy_current_state[-1].yy_nxt;");
525 else if (fulltbl)
526 indent_puts ("yy_act = yy_accept[yy_current_state];");
528 else if (reject) {
529 indent_puts ("yy_current_state = *--YY_G(yy_state_ptr);");
530 indent_puts ("YY_G(yy_lp) = yy_accept[yy_current_state];");
532 if (!variable_trailing_context_rules)
533 outn ("m4_ifdef( [[M4_YY_USES_REJECT]],\n[[");
534 outn ("find_rule: /* we branch to this label when backing up */");
535 if (!variable_trailing_context_rules)
536 outn ("]])\n");
538 indent_puts
539 ("for ( ; ; ) /* until we find what rule we matched */");
541 indent_up ();
543 indent_puts ("{");
545 indent_puts
546 ("if ( YY_G(yy_lp) && YY_G(yy_lp) < yy_accept[yy_current_state + 1] )");
547 indent_up ();
548 indent_puts ("{");
549 indent_puts ("yy_act = yy_acclist[YY_G(yy_lp)];");
551 if (variable_trailing_context_rules) {
552 indent_puts
553 ("if ( yy_act & YY_TRAILING_HEAD_MASK ||");
554 indent_puts (" YY_G(yy_looking_for_trail_begin) )");
555 indent_up ();
556 indent_puts ("{");
558 indent_puts
559 ("if ( yy_act == YY_G(yy_looking_for_trail_begin) )");
560 indent_up ();
561 indent_puts ("{");
562 indent_puts ("YY_G(yy_looking_for_trail_begin) = 0;");
563 indent_puts ("yy_act &= ~YY_TRAILING_HEAD_MASK;");
564 indent_puts ("break;");
565 indent_puts ("}");
566 indent_down ();
568 indent_puts ("}");
569 indent_down ();
571 indent_puts
572 ("else if ( yy_act & YY_TRAILING_MASK )");
573 indent_up ();
574 indent_puts ("{");
575 indent_puts
576 ("YY_G(yy_looking_for_trail_begin) = yy_act & ~YY_TRAILING_MASK;");
577 indent_puts
578 ("YY_G(yy_looking_for_trail_begin) |= YY_TRAILING_HEAD_MASK;");
580 if (real_reject) {
581 /* Remember matched text in case we back up
582 * due to REJECT.
584 indent_puts
585 ("YY_G(yy_full_match) = yy_cp;");
586 indent_puts
587 ("YY_G(yy_full_state) = YY_G(yy_state_ptr);");
588 indent_puts ("YY_G(yy_full_lp) = YY_G(yy_lp);");
591 indent_puts ("}");
592 indent_down ();
594 indent_puts ("else");
595 indent_up ();
596 indent_puts ("{");
597 indent_puts ("YY_G(yy_full_match) = yy_cp;");
598 indent_puts
599 ("YY_G(yy_full_state) = YY_G(yy_state_ptr);");
600 indent_puts ("YY_G(yy_full_lp) = YY_G(yy_lp);");
601 indent_puts ("break;");
602 indent_puts ("}");
603 indent_down ();
605 indent_puts ("++YY_G(yy_lp);");
606 indent_puts ("goto find_rule;");
609 else {
610 /* Remember matched text in case we back up due to
611 * trailing context plus REJECT.
613 indent_up ();
614 indent_puts ("{");
615 indent_puts ("YY_G(yy_full_match) = yy_cp;");
616 indent_puts ("break;");
617 indent_puts ("}");
618 indent_down ();
621 indent_puts ("}");
622 indent_down ();
624 indent_puts ("--yy_cp;");
626 /* We could consolidate the following two lines with those at
627 * the beginning, but at the cost of complaints that we're
628 * branching inside a loop.
630 indent_puts ("yy_current_state = *--YY_G(yy_state_ptr);");
631 indent_puts ("YY_G(yy_lp) = yy_accept[yy_current_state];");
633 indent_puts ("}");
635 indent_down ();
638 else { /* compressed */
639 indent_puts ("yy_act = yy_accept[yy_current_state];");
641 if (interactive && !reject) {
642 /* Do the guaranteed-needed backing up to figure out
643 * the match.
645 indent_puts ("if ( yy_act == 0 )");
646 indent_up ();
647 indent_puts ("{ /* have to back up */");
648 indent_puts
649 ("yy_cp = YY_G(yy_last_accepting_cpos);");
650 indent_puts
651 ("yy_current_state = YY_G(yy_last_accepting_state);");
652 indent_puts
653 ("yy_act = yy_accept[yy_current_state];");
654 indent_puts ("}");
655 indent_down ();
660 /* mkftbl - make the full table and return the struct .
661 * you should call mkecstbl() after this.
664 struct yytbl_data *mkftbl (void)
666 register int i;
667 int end_of_buffer_action = num_rules + 1;
668 struct yytbl_data *tbl;
669 flex_int32_t *tdata = 0;
671 tbl = (struct yytbl_data *) calloc (1, sizeof (struct yytbl_data));
672 yytbl_data_init (tbl, YYTD_ID_ACCEPT);
673 tbl->td_flags |= YYTD_DATA32;
674 tbl->td_hilen = 0; /* it's a one-dimensional array */
675 tbl->td_lolen = lastdfa + 1;
677 tbl->td_data = tdata =
678 (flex_int32_t *) calloc (tbl->td_lolen, sizeof (flex_int32_t));
680 dfaacc[end_of_buffer_state].dfaacc_state = end_of_buffer_action;
682 for (i = 1; i <= lastdfa; ++i) {
683 register int anum = dfaacc[i].dfaacc_state;
685 tdata[i] = anum;
687 if (trace && anum)
688 fprintf (stderr, _("state # %d accepts: [%d]\n"),
689 i, anum);
692 buf_prints (&yydmap_buf,
693 "\t{YYTD_ID_ACCEPT, (void**)&yy_accept, sizeof(%s)},\n",
694 long_align ? "flex_int32_t" : "flex_int16_t");
695 return tbl;
699 /* genftbl - generate full transition table */
701 void genftbl ()
703 register int i;
704 int end_of_buffer_action = num_rules + 1;
706 out_str_dec (long_align ? get_int32_decl () : get_int16_decl (),
707 "yy_accept", lastdfa + 1);
709 dfaacc[end_of_buffer_state].dfaacc_state = end_of_buffer_action;
711 for (i = 1; i <= lastdfa; ++i) {
712 register int anum = dfaacc[i].dfaacc_state;
714 mkdata (anum);
716 if (trace && anum)
717 fprintf (stderr, _("state # %d accepts: [%d]\n"),
718 i, anum);
721 dataend ();
723 if (useecs)
724 genecs ();
726 /* Don't have to dump the actual full table entries - they were
727 * created on-the-fly.
732 /* Generate the code to find the next compressed-table state. */
734 void gen_next_compressed_state (char_map)
735 char *char_map;
737 indent_put2s ("register YY_CHAR yy_c = %s;", char_map);
739 /* Save the backing-up info \before/ computing the next state
740 * because we always compute one more state than needed - we
741 * always proceed until we reach a jam state
743 gen_backing_up ();
745 indent_puts
746 ("while ( yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state )");
747 indent_up ();
748 indent_puts ("{");
749 indent_puts ("yy_current_state = (int) yy_def[yy_current_state];");
751 if (usemecs) {
752 /* We've arrange it so that templates are never chained
753 * to one another. This means we can afford to make a
754 * very simple test to see if we need to convert to
755 * yy_c's meta-equivalence class without worrying
756 * about erroneously looking up the meta-equivalence
757 * class twice
759 do_indent ();
761 /* lastdfa + 2 is the beginning of the templates */
762 out_dec ("if ( yy_current_state >= %d )\n", lastdfa + 2);
764 indent_up ();
765 indent_puts ("yy_c = yy_meta[(unsigned int) yy_c];");
766 indent_down ();
769 indent_puts ("}");
770 indent_down ();
772 indent_puts
773 ("yy_current_state = yy_nxt[yy_base[yy_current_state] + (unsigned int) yy_c];");
777 /* Generate the code to find the next match. */
779 void gen_next_match ()
781 /* NOTE - changes in here should be reflected in gen_next_state() and
782 * gen_NUL_trans().
784 char *char_map = useecs ?
785 "yy_ec[YY_SC_TO_UI(*yy_cp)] " : "YY_SC_TO_UI(*yy_cp)";
787 char *char_map_2 = useecs ?
788 "yy_ec[YY_SC_TO_UI(*++yy_cp)] " : "YY_SC_TO_UI(*++yy_cp)";
790 if (fulltbl) {
791 if (gentables)
792 indent_put2s
793 ("while ( (yy_current_state = yy_nxt[yy_current_state][ %s ]) > 0 )",
794 char_map);
795 else
796 indent_put2s
797 ("while ( (yy_current_state = yy_nxt[yy_current_state*YY_NXT_LOLEN + %s ]) > 0 )",
798 char_map);
800 indent_up ();
802 if (num_backing_up > 0) {
803 indent_puts ("{");
804 gen_backing_up ();
805 outc ('\n');
808 indent_puts ("++yy_cp;");
810 if (num_backing_up > 0)
812 indent_puts ("}");
814 indent_down ();
816 outc ('\n');
817 indent_puts ("yy_current_state = -yy_current_state;");
820 else if (fullspd) {
821 indent_puts ("{");
822 indent_puts
823 ("register yyconst struct yy_trans_info *yy_trans_info;\n");
824 indent_puts ("register YY_CHAR yy_c;\n");
825 indent_put2s ("for ( yy_c = %s;", char_map);
826 indent_puts
827 (" (yy_trans_info = &yy_current_state[(unsigned int) yy_c])->");
828 indent_puts ("yy_verify == yy_c;");
829 indent_put2s (" yy_c = %s )", char_map_2);
831 indent_up ();
833 if (num_backing_up > 0)
834 indent_puts ("{");
836 indent_puts ("yy_current_state += yy_trans_info->yy_nxt;");
838 if (num_backing_up > 0) {
839 outc ('\n');
840 gen_backing_up ();
841 indent_puts ("}");
844 indent_down ();
845 indent_puts ("}");
848 else { /* compressed */
849 indent_puts ("do");
851 indent_up ();
852 indent_puts ("{");
854 gen_next_state (false);
856 indent_puts ("++yy_cp;");
859 indent_puts ("}");
860 indent_down ();
862 do_indent ();
864 if (interactive)
865 out_dec ("while ( yy_base[yy_current_state] != %d );\n", jambase);
866 else
867 out_dec ("while ( yy_current_state != %d );\n",
868 jamstate);
870 if (!reject && !interactive) {
871 /* Do the guaranteed-needed backing up to figure out
872 * the match.
874 indent_puts
875 ("yy_cp = YY_G(yy_last_accepting_cpos);");
876 indent_puts
877 ("yy_current_state = YY_G(yy_last_accepting_state);");
883 /* Generate the code to find the next state. */
885 void gen_next_state (worry_about_NULs)
886 int worry_about_NULs;
887 { /* NOTE - changes in here should be reflected in gen_next_match() */
888 char char_map[256];
890 if (worry_about_NULs && !nultrans) {
891 if (useecs)
892 snprintf (char_map, sizeof(char_map),
893 "(*yy_cp ? yy_ec[YY_SC_TO_UI(*yy_cp)] : %d)",
894 NUL_ec);
895 else
896 snprintf (char_map, sizeof(char_map),
897 "(*yy_cp ? YY_SC_TO_UI(*yy_cp) : %d)",
898 NUL_ec);
901 else
902 strcpy (char_map, useecs ?
903 "yy_ec[YY_SC_TO_UI(*yy_cp)] " :
904 "YY_SC_TO_UI(*yy_cp)");
906 if (worry_about_NULs && nultrans) {
907 if (!fulltbl && !fullspd)
908 /* Compressed tables back up *before* they match. */
909 gen_backing_up ();
911 indent_puts ("if ( *yy_cp )");
912 indent_up ();
913 indent_puts ("{");
916 if (fulltbl) {
917 if (gentables)
918 indent_put2s
919 ("yy_current_state = yy_nxt[yy_current_state][%s];",
920 char_map);
921 else
922 indent_put2s
923 ("yy_current_state = yy_nxt[yy_current_state*YY_NXT_LOLEN + %s];",
924 char_map);
927 else if (fullspd)
928 indent_put2s
929 ("yy_current_state += yy_current_state[%s].yy_nxt;",
930 char_map);
932 else
933 gen_next_compressed_state (char_map);
935 if (worry_about_NULs && nultrans) {
937 indent_puts ("}");
938 indent_down ();
939 indent_puts ("else");
940 indent_up ();
941 indent_puts
942 ("yy_current_state = yy_NUL_trans[yy_current_state];");
943 indent_down ();
946 if (fullspd || fulltbl)
947 gen_backing_up ();
949 if (reject)
950 indent_puts ("*YY_G(yy_state_ptr)++ = yy_current_state;");
954 /* Generate the code to make a NUL transition. */
956 void gen_NUL_trans ()
957 { /* NOTE - changes in here should be reflected in gen_next_match() */
958 /* Only generate a definition for "yy_cp" if we'll generate code
959 * that uses it. Otherwise lint and the like complain.
961 int need_backing_up = (num_backing_up > 0 && !reject);
963 if (need_backing_up && (!nultrans || fullspd || fulltbl))
964 /* We're going to need yy_cp lying around for the call
965 * below to gen_backing_up().
967 indent_puts ("register char *yy_cp = YY_G(yy_c_buf_p);");
969 outc ('\n');
971 if (nultrans) {
972 indent_puts
973 ("yy_current_state = yy_NUL_trans[yy_current_state];");
974 indent_puts ("yy_is_jam = (yy_current_state == 0);");
977 else if (fulltbl) {
978 do_indent ();
979 if (gentables)
980 out_dec ("yy_current_state = yy_nxt[yy_current_state][%d];\n", NUL_ec);
981 else
982 out_dec ("yy_current_state = yy_nxt[yy_current_state*YY_NXT_LOLEN + %d];\n", NUL_ec);
983 indent_puts ("yy_is_jam = (yy_current_state <= 0);");
986 else if (fullspd) {
987 do_indent ();
988 out_dec ("register int yy_c = %d;\n", NUL_ec);
990 indent_puts
991 ("register yyconst struct yy_trans_info *yy_trans_info;\n");
992 indent_puts
993 ("yy_trans_info = &yy_current_state[(unsigned int) yy_c];");
994 indent_puts ("yy_current_state += yy_trans_info->yy_nxt;");
996 indent_puts
997 ("yy_is_jam = (yy_trans_info->yy_verify != yy_c);");
1000 else {
1001 char NUL_ec_str[20];
1003 snprintf (NUL_ec_str, sizeof(NUL_ec_str), "%d", NUL_ec);
1004 gen_next_compressed_state (NUL_ec_str);
1006 do_indent ();
1007 out_dec ("yy_is_jam = (yy_current_state == %d);\n",
1008 jamstate);
1010 if (reject) {
1011 /* Only stack this state if it's a transition we
1012 * actually make. If we stack it on a jam, then
1013 * the state stack and yy_c_buf_p get out of sync.
1015 indent_puts ("if ( ! yy_is_jam )");
1016 indent_up ();
1017 indent_puts
1018 ("*YY_G(yy_state_ptr)++ = yy_current_state;");
1019 indent_down ();
1023 /* If we've entered an accepting state, back up; note that
1024 * compressed tables have *already* done such backing up, so
1025 * we needn't bother with it again.
1027 if (need_backing_up && (fullspd || fulltbl)) {
1028 outc ('\n');
1029 indent_puts ("if ( ! yy_is_jam )");
1030 indent_up ();
1031 indent_puts ("{");
1032 gen_backing_up ();
1033 indent_puts ("}");
1034 indent_down ();
1039 /* Generate the code to find the start state. */
1041 void gen_start_state ()
1043 if (fullspd) {
1044 if (bol_needed) {
1045 indent_puts
1046 ("yy_current_state = yy_start_state_list[YY_G(yy_start) + YY_AT_BOL()];");
1048 else
1049 indent_puts
1050 ("yy_current_state = yy_start_state_list[YY_G(yy_start)];");
1053 else {
1054 indent_puts ("yy_current_state = YY_G(yy_start);");
1056 if (bol_needed)
1057 indent_puts ("yy_current_state += YY_AT_BOL();");
1059 if (reject) {
1060 /* Set up for storing up states. */
1061 outn ("m4_ifdef( [[M4_YY_USES_REJECT]],\n[[");
1062 indent_puts
1063 ("YY_G(yy_state_ptr) = YY_G(yy_state_buf);");
1064 indent_puts
1065 ("*YY_G(yy_state_ptr)++ = yy_current_state;");
1066 outn ("]])");
1072 /* gentabs - generate data statements for the transition tables */
1074 void gentabs ()
1076 int i, j, k, *accset, nacc, *acc_array, total_states;
1077 int end_of_buffer_action = num_rules + 1;
1078 struct yytbl_data *yyacc_tbl = 0, *yymeta_tbl = 0, *yybase_tbl = 0,
1079 *yydef_tbl = 0, *yynxt_tbl = 0, *yychk_tbl = 0, *yyacclist_tbl=0;
1080 flex_int32_t *yyacc_data = 0, *yybase_data = 0, *yydef_data = 0,
1081 *yynxt_data = 0, *yychk_data = 0, *yyacclist_data=0;
1082 flex_int32_t yybase_curr = 0, yyacclist_curr=0,yyacc_curr=0;
1084 acc_array = allocate_integer_array (current_max_dfas);
1085 nummt = 0;
1087 /* The compressed table format jams by entering the "jam state",
1088 * losing information about the previous state in the process.
1089 * In order to recover the previous state, we effectively need
1090 * to keep backing-up information.
1092 ++num_backing_up;
1094 if (reject) {
1095 /* Write out accepting list and pointer list.
1097 * First we generate the "yy_acclist" array. In the process,
1098 * we compute the indices that will go into the "yy_accept"
1099 * array, and save the indices in the dfaacc array.
1101 int EOB_accepting_list[2];
1103 /* Set up accepting structures for the End Of Buffer state. */
1104 EOB_accepting_list[0] = 0;
1105 EOB_accepting_list[1] = end_of_buffer_action;
1106 accsiz[end_of_buffer_state] = 1;
1107 dfaacc[end_of_buffer_state].dfaacc_set =
1108 EOB_accepting_list;
1110 out_str_dec (long_align ? get_int32_decl () :
1111 get_int16_decl (), "yy_acclist", MAX (numas,
1112 1) + 1);
1114 buf_prints (&yydmap_buf,
1115 "\t{YYTD_ID_ACCLIST, (void**)&yy_acclist, sizeof(%s)},\n",
1116 long_align ? "flex_int32_t" : "flex_int16_t");
1118 yyacclist_tbl = (struct yytbl_data*)calloc(1,sizeof(struct yytbl_data));
1119 yytbl_data_init (yyacclist_tbl, YYTD_ID_ACCLIST);
1120 yyacclist_tbl->td_lolen = MAX(numas,1) + 1;
1121 yyacclist_tbl->td_data = yyacclist_data =
1122 (flex_int32_t *) calloc (yyacclist_tbl->td_lolen, sizeof (flex_int32_t));
1123 yyacclist_curr = 1;
1125 j = 1; /* index into "yy_acclist" array */
1127 for (i = 1; i <= lastdfa; ++i) {
1128 acc_array[i] = j;
1130 if (accsiz[i] != 0) {
1131 accset = dfaacc[i].dfaacc_set;
1132 nacc = accsiz[i];
1134 if (trace)
1135 fprintf (stderr,
1136 _("state # %d accepts: "),
1139 for (k = 1; k <= nacc; ++k) {
1140 int accnum = accset[k];
1142 ++j;
1144 if (variable_trailing_context_rules
1145 && !(accnum &
1146 YY_TRAILING_HEAD_MASK)
1147 && accnum > 0
1148 && accnum <= num_rules
1149 && rule_type[accnum] ==
1150 RULE_VARIABLE) {
1151 /* Special hack to flag
1152 * accepting number as part
1153 * of trailing context rule.
1155 accnum |= YY_TRAILING_MASK;
1158 mkdata (accnum);
1159 yyacclist_data[yyacclist_curr++] = accnum;
1161 if (trace) {
1162 fprintf (stderr, "[%d]",
1163 accset[k]);
1165 if (k < nacc)
1166 fputs (", ",
1167 stderr);
1168 else
1169 putc ('\n',
1170 stderr);
1176 /* add accepting number for the "jam" state */
1177 acc_array[i] = j;
1179 dataend ();
1180 if (tablesext) {
1181 yytbl_data_compress (yyacclist_tbl);
1182 if (yytbl_data_fwrite (&tableswr, yyacclist_tbl) < 0)
1183 flexerror (_("Could not write yyacclist_tbl"));
1184 yytbl_data_destroy (yyacclist_tbl);
1185 yyacclist_tbl = NULL;
1189 else {
1190 dfaacc[end_of_buffer_state].dfaacc_state =
1191 end_of_buffer_action;
1193 for (i = 1; i <= lastdfa; ++i)
1194 acc_array[i] = dfaacc[i].dfaacc_state;
1196 /* add accepting number for jam state */
1197 acc_array[i] = 0;
1200 /* Begin generating yy_accept */
1202 /* Spit out "yy_accept" array. If we're doing "reject", it'll be
1203 * pointers into the "yy_acclist" array. Otherwise it's actual
1204 * accepting numbers. In either case, we just dump the numbers.
1207 /* "lastdfa + 2" is the size of "yy_accept"; includes room for C arrays
1208 * beginning at 0 and for "jam" state.
1210 k = lastdfa + 2;
1212 if (reject)
1213 /* We put a "cap" on the table associating lists of accepting
1214 * numbers with state numbers. This is needed because we tell
1215 * where the end of an accepting list is by looking at where
1216 * the list for the next state starts.
1218 ++k;
1220 out_str_dec (long_align ? get_int32_decl () : get_int16_decl (),
1221 "yy_accept", k);
1223 buf_prints (&yydmap_buf,
1224 "\t{YYTD_ID_ACCEPT, (void**)&yy_accept, sizeof(%s)},\n",
1225 long_align ? "flex_int32_t" : "flex_int16_t");
1227 yyacc_tbl =
1228 (struct yytbl_data *) calloc (1,
1229 sizeof (struct yytbl_data));
1230 yytbl_data_init (yyacc_tbl, YYTD_ID_ACCEPT);
1231 yyacc_tbl->td_lolen = k;
1232 yyacc_tbl->td_data = yyacc_data =
1233 (flex_int32_t *) calloc (yyacc_tbl->td_lolen, sizeof (flex_int32_t));
1234 yyacc_curr=1;
1236 for (i = 1; i <= lastdfa; ++i) {
1237 mkdata (acc_array[i]);
1238 yyacc_data[yyacc_curr++] = acc_array[i];
1240 if (!reject && trace && acc_array[i])
1241 fprintf (stderr, _("state # %d accepts: [%d]\n"),
1242 i, acc_array[i]);
1245 /* Add entry for "jam" state. */
1246 mkdata (acc_array[i]);
1247 yyacc_data[yyacc_curr++] = acc_array[i];
1249 if (reject) {
1250 /* Add "cap" for the list. */
1251 mkdata (acc_array[i]);
1252 yyacc_data[yyacc_curr++] = acc_array[i];
1255 dataend ();
1256 if (tablesext) {
1257 yytbl_data_compress (yyacc_tbl);
1258 if (yytbl_data_fwrite (&tableswr, yyacc_tbl) < 0)
1259 flexerror (_("Could not write yyacc_tbl"));
1260 yytbl_data_destroy (yyacc_tbl);
1261 yyacc_tbl = NULL;
1263 /* End generating yy_accept */
1265 if (useecs) {
1267 genecs ();
1268 if (tablesext) {
1269 struct yytbl_data *tbl;
1271 tbl = mkecstbl ();
1272 yytbl_data_compress (tbl);
1273 if (yytbl_data_fwrite (&tableswr, tbl) < 0)
1274 flexerror (_("Could not write ecstbl"));
1275 yytbl_data_destroy (tbl);
1276 tbl = 0;
1280 if (usemecs) {
1281 /* Begin generating yy_meta */
1282 /* Write out meta-equivalence classes (used to index
1283 * templates with).
1285 flex_int32_t *yymecs_data = 0;
1286 yymeta_tbl =
1287 (struct yytbl_data *) calloc (1,
1288 sizeof (struct
1289 yytbl_data));
1290 yytbl_data_init (yymeta_tbl, YYTD_ID_META);
1291 yymeta_tbl->td_lolen = numecs + 1;
1292 yymeta_tbl->td_data = yymecs_data =
1293 (flex_int32_t *) calloc (yymeta_tbl->td_lolen,
1294 sizeof (flex_int32_t));
1296 if (trace)
1297 fputs (_("\n\nMeta-Equivalence Classes:\n"),
1298 stderr);
1300 out_str_dec (get_yy_char_decl (), "yy_meta", numecs + 1);
1301 buf_prints (&yydmap_buf,
1302 "\t{YYTD_ID_META, (void**)&yy_meta, sizeof(%s)},\n",
1303 "YY_CHAR");
1305 for (i = 1; i <= numecs; ++i) {
1306 if (trace)
1307 fprintf (stderr, "%d = %d\n",
1308 i, ABS (tecbck[i]));
1310 mkdata (ABS (tecbck[i]));
1311 yymecs_data[i] = ABS (tecbck[i]);
1314 dataend ();
1315 if (tablesext) {
1316 yytbl_data_compress (yymeta_tbl);
1317 if (yytbl_data_fwrite (&tableswr, yymeta_tbl) < 0)
1318 flexerror (_
1319 ("Could not write yymeta_tbl"));
1320 yytbl_data_destroy (yymeta_tbl);
1321 yymeta_tbl = NULL;
1323 /* End generating yy_meta */
1326 total_states = lastdfa + numtemps;
1328 /* Begin generating yy_base */
1329 out_str_dec ((tblend >= INT16_MAX || long_align) ?
1330 get_uint32_decl () : get_uint16_decl (),
1331 "yy_base", total_states + 1);
1333 buf_prints (&yydmap_buf,
1334 "\t{YYTD_ID_BASE, (void**)&yy_base, sizeof(%s)},\n",
1335 (tblend >= INT16_MAX
1336 || long_align) ? "flex_uint32_t" : "flex_uint16_t");
1337 yybase_tbl =
1338 (struct yytbl_data *) calloc (1,
1339 sizeof (struct yytbl_data));
1340 yytbl_data_init (yybase_tbl, YYTD_ID_BASE);
1341 yybase_tbl->td_lolen = total_states + 1;
1342 yybase_tbl->td_data = yybase_data =
1343 (flex_int32_t *) calloc (yybase_tbl->td_lolen,
1344 sizeof (flex_int32_t));
1345 yybase_curr = 1;
1347 for (i = 1; i <= lastdfa; ++i) {
1348 register int d = def[i];
1350 if (base[i] == JAMSTATE)
1351 base[i] = jambase;
1353 if (d == JAMSTATE)
1354 def[i] = jamstate;
1356 else if (d < 0) {
1357 /* Template reference. */
1358 ++tmpuses;
1359 def[i] = lastdfa - d + 1;
1362 mkdata (base[i]);
1363 yybase_data[yybase_curr++] = base[i];
1366 /* Generate jam state's base index. */
1367 mkdata (base[i]);
1368 yybase_data[yybase_curr++] = base[i];
1370 for (++i /* skip jam state */ ; i <= total_states; ++i) {
1371 mkdata (base[i]);
1372 yybase_data[yybase_curr++] = base[i];
1373 def[i] = jamstate;
1376 dataend ();
1377 if (tablesext) {
1378 yytbl_data_compress (yybase_tbl);
1379 if (yytbl_data_fwrite (&tableswr, yybase_tbl) < 0)
1380 flexerror (_("Could not write yybase_tbl"));
1381 yytbl_data_destroy (yybase_tbl);
1382 yybase_tbl = NULL;
1384 /* End generating yy_base */
1387 /* Begin generating yy_def */
1388 out_str_dec ((total_states >= INT16_MAX || long_align) ?
1389 get_int32_decl () : get_int16_decl (),
1390 "yy_def", total_states + 1);
1392 buf_prints (&yydmap_buf,
1393 "\t{YYTD_ID_DEF, (void**)&yy_def, sizeof(%s)},\n",
1394 (total_states >= INT16_MAX
1395 || long_align) ? "flex_int32_t" : "flex_int16_t");
1397 yydef_tbl =
1398 (struct yytbl_data *) calloc (1,
1399 sizeof (struct yytbl_data));
1400 yytbl_data_init (yydef_tbl, YYTD_ID_DEF);
1401 yydef_tbl->td_lolen = total_states + 1;
1402 yydef_tbl->td_data = yydef_data =
1403 (flex_int32_t *) calloc (yydef_tbl->td_lolen, sizeof (flex_int32_t));
1405 for (i = 1; i <= total_states; ++i) {
1406 mkdata (def[i]);
1407 yydef_data[i] = def[i];
1410 dataend ();
1411 if (tablesext) {
1412 yytbl_data_compress (yydef_tbl);
1413 if (yytbl_data_fwrite (&tableswr, yydef_tbl) < 0)
1414 flexerror (_("Could not write yydef_tbl"));
1415 yytbl_data_destroy (yydef_tbl);
1416 yydef_tbl = NULL;
1418 /* End generating yy_def */
1421 /* Begin generating yy_nxt */
1422 out_str_dec ((total_states >= INT16_MAX || long_align) ?
1423 get_uint32_decl () : get_uint16_decl (), "yy_nxt",
1424 tblend + 1);
1426 buf_prints (&yydmap_buf,
1427 "\t{YYTD_ID_NXT, (void**)&yy_nxt, sizeof(%s)},\n",
1428 (total_states >= INT16_MAX
1429 || long_align) ? "flex_uint32_t" : "flex_uint16_t");
1431 yynxt_tbl =
1432 (struct yytbl_data *) calloc (1,
1433 sizeof (struct yytbl_data));
1434 yytbl_data_init (yynxt_tbl, YYTD_ID_NXT);
1435 yynxt_tbl->td_lolen = tblend + 1;
1436 yynxt_tbl->td_data = yynxt_data =
1437 (flex_int32_t *) calloc (yynxt_tbl->td_lolen, sizeof (flex_int32_t));
1439 for (i = 1; i <= tblend; ++i) {
1440 /* Note, the order of the following test is important.
1441 * If chk[i] is 0, then nxt[i] is undefined.
1443 if (chk[i] == 0 || nxt[i] == 0)
1444 nxt[i] = jamstate; /* new state is the JAM state */
1446 mkdata (nxt[i]);
1447 yynxt_data[i] = nxt[i];
1450 dataend ();
1451 if (tablesext) {
1452 yytbl_data_compress (yynxt_tbl);
1453 if (yytbl_data_fwrite (&tableswr, yynxt_tbl) < 0)
1454 flexerror (_("Could not write yynxt_tbl"));
1455 yytbl_data_destroy (yynxt_tbl);
1456 yynxt_tbl = NULL;
1458 /* End generating yy_nxt */
1460 /* Begin generating yy_chk */
1461 out_str_dec ((total_states >= INT16_MAX || long_align) ?
1462 get_int32_decl () : get_int16_decl (), "yy_chk",
1463 tblend + 1);
1465 buf_prints (&yydmap_buf,
1466 "\t{YYTD_ID_CHK, (void**)&yy_chk, sizeof(%s)},\n",
1467 (total_states >= INT16_MAX
1468 || long_align) ? "flex_int32_t" : "flex_int16_t");
1470 yychk_tbl =
1471 (struct yytbl_data *) calloc (1,
1472 sizeof (struct yytbl_data));
1473 yytbl_data_init (yychk_tbl, YYTD_ID_CHK);
1474 yychk_tbl->td_lolen = tblend + 1;
1475 yychk_tbl->td_data = yychk_data =
1476 (flex_int32_t *) calloc (yychk_tbl->td_lolen, sizeof (flex_int32_t));
1478 for (i = 1; i <= tblend; ++i) {
1479 if (chk[i] == 0)
1480 ++nummt;
1482 mkdata (chk[i]);
1483 yychk_data[i] = chk[i];
1486 dataend ();
1487 if (tablesext) {
1488 yytbl_data_compress (yychk_tbl);
1489 if (yytbl_data_fwrite (&tableswr, yychk_tbl) < 0)
1490 flexerror (_("Could not write yychk_tbl"));
1491 yytbl_data_destroy (yychk_tbl);
1492 yychk_tbl = NULL;
1494 /* End generating yy_chk */
1496 flex_free ((void *) acc_array);
1500 /* Write out a formatted string (with a secondary string argument) at the
1501 * current indentation level, adding a final newline.
1504 void indent_put2s (fmt, arg)
1505 const char *fmt, *arg;
1507 do_indent ();
1508 out_str (fmt, arg);
1509 outn ("");
1513 /* Write out a string at the current indentation level, adding a final
1514 * newline.
1517 void indent_puts (str)
1518 const char *str;
1520 do_indent ();
1521 outn (str);
1525 /* make_tables - generate transition tables and finishes generating output file
1528 void make_tables ()
1530 register int i;
1531 int did_eof_rule = false;
1532 struct yytbl_data *yynultrans_tbl;
1535 skelout (); /* %% [2.0] - break point in skel */
1537 /* First, take care of YY_DO_BEFORE_ACTION depending on yymore
1538 * being used.
1540 set_indent (1);
1542 if (yymore_used && !yytext_is_array) {
1543 indent_puts ("YY_G(yytext_ptr) -= YY_G(yy_more_len); \\");
1544 indent_puts
1545 ("yyleng = (size_t) (yy_cp - YY_G(yytext_ptr)); \\");
1548 else
1549 indent_puts ("yyleng = (size_t) (yy_cp - yy_bp); \\");
1551 /* Now also deal with copying yytext_ptr to yytext if needed. */
1552 skelout (); /* %% [3.0] - break point in skel */
1553 if (yytext_is_array) {
1554 if (yymore_used)
1555 indent_puts
1556 ("if ( yyleng + YY_G(yy_more_offset) >= YYLMAX ) \\");
1557 else
1558 indent_puts ("if ( yyleng >= YYLMAX ) \\");
1560 indent_up ();
1561 indent_puts
1562 ("YY_FATAL_ERROR( \"token too large, exceeds YYLMAX\" ); \\");
1563 indent_down ();
1565 if (yymore_used) {
1566 indent_puts
1567 ("yy_flex_strncpy( &yytext[YY_G(yy_more_offset)], YY_G(yytext_ptr), yyleng + 1 M4_YY_CALL_LAST_ARG); \\");
1568 indent_puts ("yyleng += YY_G(yy_more_offset); \\");
1569 indent_puts
1570 ("YY_G(yy_prev_more_offset) = YY_G(yy_more_offset); \\");
1571 indent_puts ("YY_G(yy_more_offset) = 0; \\");
1573 else {
1574 indent_puts
1575 ("yy_flex_strncpy( yytext, YY_G(yytext_ptr), yyleng + 1 M4_YY_CALL_LAST_ARG); \\");
1579 set_indent (0);
1581 skelout (); /* %% [4.0] - break point in skel */
1584 /* This is where we REALLY begin generating the tables. */
1586 out_dec ("#define YY_NUM_RULES %d\n", num_rules);
1587 out_dec ("#define YY_END_OF_BUFFER %d\n", num_rules + 1);
1589 if (fullspd) {
1590 /* Need to define the transet type as a size large
1591 * enough to hold the biggest offset.
1593 int total_table_size = tblend + numecs + 1;
1594 char *trans_offset_type =
1595 (total_table_size >= INT16_MAX || long_align) ?
1596 "flex_int32_t" : "flex_int16_t";
1598 set_indent (0);
1599 indent_puts ("struct yy_trans_info");
1600 indent_up ();
1601 indent_puts ("{");
1603 /* We require that yy_verify and yy_nxt must be of the same size int. */
1604 indent_put2s ("%s yy_verify;", trans_offset_type);
1606 /* In cases where its sister yy_verify *is* a "yes, there is
1607 * a transition", yy_nxt is the offset (in records) to the
1608 * next state. In most cases where there is no transition,
1609 * the value of yy_nxt is irrelevant. If yy_nxt is the -1th
1610 * record of a state, though, then yy_nxt is the action number
1611 * for that state.
1614 indent_put2s ("%s yy_nxt;", trans_offset_type);
1615 indent_puts ("};");
1616 indent_down ();
1618 else {
1619 /* We generate a bogus 'struct yy_trans_info' data type
1620 * so we can guarantee that it is always declared in the skel.
1621 * This is so we can compile "sizeof(struct yy_trans_info)"
1622 * in any scanner.
1624 indent_puts
1625 ("/* This struct is not used in this scanner,");
1626 indent_puts (" but its presence is necessary. */");
1627 indent_puts ("struct yy_trans_info");
1628 indent_up ();
1629 indent_puts ("{");
1630 indent_puts ("flex_int32_t yy_verify;");
1631 indent_puts ("flex_int32_t yy_nxt;");
1632 indent_puts ("};");
1633 indent_down ();
1636 if (fullspd) {
1637 genctbl ();
1638 if (tablesext) {
1639 struct yytbl_data *tbl;
1641 tbl = mkctbl ();
1642 yytbl_data_compress (tbl);
1643 if (yytbl_data_fwrite (&tableswr, tbl) < 0)
1644 flexerror (_("Could not write ftbl"));
1645 yytbl_data_destroy (tbl);
1647 tbl = mkssltbl ();
1648 yytbl_data_compress (tbl);
1649 if (yytbl_data_fwrite (&tableswr, tbl) < 0)
1650 flexerror (_("Could not write ssltbl"));
1651 yytbl_data_destroy (tbl);
1652 tbl = 0;
1654 if (useecs) {
1655 tbl = mkecstbl ();
1656 yytbl_data_compress (tbl);
1657 if (yytbl_data_fwrite (&tableswr, tbl) < 0)
1658 flexerror (_
1659 ("Could not write ecstbl"));
1660 yytbl_data_destroy (tbl);
1661 tbl = 0;
1665 else if (fulltbl) {
1666 genftbl ();
1667 if (tablesext) {
1668 struct yytbl_data *tbl;
1670 tbl = mkftbl ();
1671 yytbl_data_compress (tbl);
1672 if (yytbl_data_fwrite (&tableswr, tbl) < 0)
1673 flexerror (_("Could not write ftbl"));
1674 yytbl_data_destroy (tbl);
1675 tbl = 0;
1677 if (useecs) {
1678 tbl = mkecstbl ();
1679 yytbl_data_compress (tbl);
1680 if (yytbl_data_fwrite (&tableswr, tbl) < 0)
1681 flexerror (_
1682 ("Could not write ecstbl"));
1683 yytbl_data_destroy (tbl);
1684 tbl = 0;
1688 else
1689 gentabs ();
1691 if (do_yylineno) {
1693 geneoltbl ();
1695 if (tablesext) {
1696 struct yytbl_data *tbl;
1698 tbl = mkeoltbl ();
1699 yytbl_data_compress (tbl);
1700 if (yytbl_data_fwrite (&tableswr, tbl) < 0)
1701 flexerror (_("Could not write eoltbl"));
1702 yytbl_data_destroy (tbl);
1703 tbl = 0;
1707 /* Definitions for backing up. We don't need them if REJECT
1708 * is being used because then we use an alternative backin-up
1709 * technique instead.
1711 if (num_backing_up > 0 && !reject) {
1712 if (!C_plus_plus && !reentrant) {
1713 indent_puts
1714 ("static yy_state_type yy_last_accepting_state;");
1715 indent_puts
1716 ("static char *yy_last_accepting_cpos;\n");
1720 if (nultrans) {
1721 flex_int32_t *yynultrans_data = 0;
1723 /* Begin generating yy_NUL_trans */
1724 out_str_dec (get_state_decl (), "yy_NUL_trans",
1725 lastdfa + 1);
1726 buf_prints (&yydmap_buf,
1727 "\t{YYTD_ID_NUL_TRANS, (void**)&yy_NUL_trans, sizeof(%s)},\n",
1728 (fullspd) ? "struct yy_trans_info*" :
1729 "flex_int32_t");
1731 yynultrans_tbl =
1732 (struct yytbl_data *) calloc (1,
1733 sizeof (struct
1734 yytbl_data));
1735 yytbl_data_init (yynultrans_tbl, YYTD_ID_NUL_TRANS);
1736 if (fullspd)
1737 yynultrans_tbl->td_flags |= YYTD_PTRANS;
1738 yynultrans_tbl->td_lolen = lastdfa + 1;
1739 yynultrans_tbl->td_data = yynultrans_data =
1740 (flex_int32_t *) calloc (yynultrans_tbl->td_lolen,
1741 sizeof (flex_int32_t));
1743 for (i = 1; i <= lastdfa; ++i) {
1744 if (fullspd) {
1745 out_dec (" &yy_transition[%d],\n",
1746 base[i]);
1747 yynultrans_data[i] = base[i];
1749 else {
1750 mkdata (nultrans[i]);
1751 yynultrans_data[i] = nultrans[i];
1755 dataend ();
1756 if (tablesext) {
1757 yytbl_data_compress (yynultrans_tbl);
1758 if (yytbl_data_fwrite (&tableswr, yynultrans_tbl) <
1760 flexerror (_
1761 ("Could not write yynultrans_tbl"));
1762 yytbl_data_destroy (yynultrans_tbl);
1763 yynultrans_tbl = NULL;
1765 /* End generating yy_NUL_trans */
1768 if (!C_plus_plus && !reentrant) {
1769 indent_puts ("extern int yy_flex_debug;");
1770 indent_put2s ("int yy_flex_debug = %s;\n",
1771 ddebug ? "1" : "0");
1774 if (ddebug) { /* Spit out table mapping rules to line numbers. */
1775 out_str_dec (long_align ? get_int32_decl () :
1776 get_int16_decl (), "yy_rule_linenum",
1777 num_rules);
1778 for (i = 1; i < num_rules; ++i)
1779 mkdata (rule_linenum[i]);
1780 dataend ();
1783 if (reject) {
1784 outn ("m4_ifdef( [[M4_YY_USES_REJECT]],\n[[");
1785 /* Declare state buffer variables. */
1786 if (!C_plus_plus && !reentrant) {
1787 outn ("static yy_state_type *yy_state_buf=0, *yy_state_ptr=0;");
1788 outn ("static char *yy_full_match;");
1789 outn ("static int yy_lp;");
1792 if (variable_trailing_context_rules) {
1793 if (!C_plus_plus && !reentrant) {
1794 outn ("static int yy_looking_for_trail_begin = 0;");
1795 outn ("static int yy_full_lp;");
1796 outn ("static int *yy_full_state;");
1799 out_hex ("#define YY_TRAILING_MASK 0x%x\n",
1800 (unsigned int) YY_TRAILING_MASK);
1801 out_hex ("#define YY_TRAILING_HEAD_MASK 0x%x\n",
1802 (unsigned int) YY_TRAILING_HEAD_MASK);
1805 outn ("#define REJECT \\");
1806 outn ("{ \\");
1807 outn ("*yy_cp = YY_G(yy_hold_char); /* undo effects of setting up yytext */ \\");
1808 outn ("yy_cp = YY_G(yy_full_match); /* restore poss. backed-over text */ \\");
1810 if (variable_trailing_context_rules) {
1811 outn ("YY_G(yy_lp) = YY_G(yy_full_lp); /* restore orig. accepting pos. */ \\");
1812 outn ("YY_G(yy_state_ptr) = YY_G(yy_full_state); /* restore orig. state */ \\");
1813 outn ("yy_current_state = *YY_G(yy_state_ptr); /* restore curr. state */ \\");
1816 outn ("++YY_G(yy_lp); \\");
1817 outn ("goto find_rule; \\");
1819 outn ("}");
1820 outn ("]])\n");
1823 else {
1824 outn ("/* The intent behind this definition is that it'll catch");
1825 outn (" * any uses of REJECT which flex missed.");
1826 outn (" */");
1827 outn ("#define REJECT reject_used_but_not_detected");
1830 if (yymore_used) {
1831 if (!C_plus_plus) {
1832 if (yytext_is_array) {
1833 if (!reentrant){
1834 indent_puts ("static int yy_more_offset = 0;");
1835 indent_puts ("static int yy_prev_more_offset = 0;");
1838 else if (!reentrant) {
1839 indent_puts
1840 ("static int yy_more_flag = 0;");
1841 indent_puts
1842 ("static int yy_more_len = 0;");
1846 if (yytext_is_array) {
1847 indent_puts
1848 ("#define yymore() (YY_G(yy_more_offset) = yy_flex_strlen( yytext M4_YY_CALL_LAST_ARG))");
1849 indent_puts ("#define YY_NEED_STRLEN");
1850 indent_puts ("#define YY_MORE_ADJ 0");
1851 indent_puts
1852 ("#define YY_RESTORE_YY_MORE_OFFSET \\");
1853 indent_up ();
1854 indent_puts ("{ \\");
1855 indent_puts
1856 ("YY_G(yy_more_offset) = YY_G(yy_prev_more_offset); \\");
1857 indent_puts ("yyleng -= YY_G(yy_more_offset); \\");
1858 indent_puts ("}");
1859 indent_down ();
1861 else {
1862 indent_puts
1863 ("#define yymore() (YY_G(yy_more_flag) = 1)");
1864 indent_puts
1865 ("#define YY_MORE_ADJ YY_G(yy_more_len)");
1866 indent_puts ("#define YY_RESTORE_YY_MORE_OFFSET");
1870 else {
1871 indent_puts
1872 ("#define yymore() yymore_used_but_not_detected");
1873 indent_puts ("#define YY_MORE_ADJ 0");
1874 indent_puts ("#define YY_RESTORE_YY_MORE_OFFSET");
1877 if (!C_plus_plus) {
1878 if (yytext_is_array) {
1879 outn ("#ifndef YYLMAX");
1880 outn ("#define YYLMAX 8192");
1881 outn ("#endif\n");
1882 if (!reentrant){
1883 outn ("char yytext[YYLMAX];");
1884 outn ("char *yytext_ptr;");
1888 else {
1889 if(! reentrant)
1890 outn ("char *yytext;");
1894 out (&action_array[defs1_offset]);
1896 line_directive_out (stdout, 0);
1898 skelout (); /* %% [5.0] - break point in skel */
1900 if (!C_plus_plus) {
1901 if (use_read) {
1902 outn ("\terrno=0; \\");
1903 outn ("\twhile ( (result = read( fileno(yyin), (char *) buf, max_size )) < 0 ) \\");
1904 outn ("\t{ \\");
1905 outn ("\t\tif( errno != EINTR) \\");
1906 outn ("\t\t{ \\");
1907 outn ("\t\t\tYY_FATAL_ERROR( \"input in flex scanner failed\" ); \\");
1908 outn ("\t\t\tbreak; \\");
1909 outn ("\t\t} \\");
1910 outn ("\t\terrno=0; \\");
1911 outn ("\t\tclearerr(yyin); \\");
1912 outn ("\t}\\");
1915 else {
1916 outn ("\tif ( YY_CURRENT_BUFFER_LVALUE->yy_is_interactive ) \\");
1917 outn ("\t\t{ \\");
1918 outn ("\t\tint c = '*'; \\");
1919 outn ("\t\tsize_t n; \\");
1920 outn ("\t\tfor ( n = 0; n < max_size && \\");
1921 outn ("\t\t\t (c = getc( yyin )) != EOF && c != '\\n'; ++n ) \\");
1922 outn ("\t\t\tbuf[n] = (char) c; \\");
1923 outn ("\t\tif ( c == '\\n' ) \\");
1924 outn ("\t\t\tbuf[n++] = (char) c; \\");
1925 outn ("\t\tif ( c == EOF && ferror( yyin ) ) \\");
1926 outn ("\t\t\tYY_FATAL_ERROR( \"input in flex scanner failed\" ); \\");
1927 outn ("\t\tresult = n; \\");
1928 outn ("\t\t} \\");
1929 outn ("\telse \\");
1930 outn ("\t\t{ \\");
1931 outn ("\t\terrno=0; \\");
1932 outn ("\t\twhile ( (result = fread(buf, 1, max_size, yyin))==0 && ferror(yyin)) \\");
1933 outn ("\t\t\t{ \\");
1934 outn ("\t\t\tif( errno != EINTR) \\");
1935 outn ("\t\t\t\t{ \\");
1936 outn ("\t\t\t\tYY_FATAL_ERROR( \"input in flex scanner failed\" ); \\");
1937 outn ("\t\t\t\tbreak; \\");
1938 outn ("\t\t\t\t} \\");
1939 outn ("\t\t\terrno=0; \\");
1940 outn ("\t\t\tclearerr(yyin); \\");
1941 outn ("\t\t\t} \\");
1942 outn ("\t\t}\\");
1946 skelout (); /* %% [6.0] - break point in skel */
1948 indent_puts ("#define YY_RULE_SETUP \\");
1949 indent_up ();
1950 if (bol_needed) {
1951 indent_puts ("if ( yyleng > 0 ) \\");
1952 indent_up ();
1953 indent_puts ("YY_CURRENT_BUFFER_LVALUE->yy_at_bol = \\");
1954 indent_puts ("\t\t(yytext[yyleng - 1] == '\\n'); \\");
1955 indent_down ();
1957 indent_puts ("YY_USER_ACTION");
1958 indent_down ();
1960 skelout (); /* %% [7.0] - break point in skel */
1962 /* Copy prolog to output file. */
1963 out (&action_array[prolog_offset]);
1965 line_directive_out (stdout, 0);
1967 skelout (); /* %% [8.0] - break point in skel */
1969 set_indent (2);
1971 if (yymore_used && !yytext_is_array) {
1972 indent_puts ("YY_G(yy_more_len) = 0;");
1973 indent_puts ("if ( YY_G(yy_more_flag) )");
1974 indent_up ();
1975 indent_puts ("{");
1976 indent_puts
1977 ("YY_G(yy_more_len) = YY_G(yy_c_buf_p) - YY_G(yytext_ptr);");
1978 indent_puts ("YY_G(yy_more_flag) = 0;");
1979 indent_puts ("}");
1980 indent_down ();
1983 skelout (); /* %% [9.0] - break point in skel */
1985 gen_start_state ();
1987 /* Note, don't use any indentation. */
1988 outn ("yy_match:");
1989 gen_next_match ();
1991 skelout (); /* %% [10.0] - break point in skel */
1992 set_indent (2);
1993 gen_find_action ();
1995 skelout (); /* %% [11.0] - break point in skel */
1996 outn ("m4_ifdef( [[M4_YY_USE_LINENO]],[[");
1997 indent_puts
1998 ("if ( yy_act != YY_END_OF_BUFFER && yy_rule_can_match_eol[yy_act] )");
1999 indent_up ();
2000 indent_puts ("{");
2001 indent_puts ("yy_size_t yyl;");
2002 do_indent ();
2003 out_str ("for ( yyl = %s; yyl < yyleng; ++yyl )\n",
2004 yymore_used ? (yytext_is_array ? "YY_G(yy_prev_more_offset)" :
2005 "YY_G(yy_more_len)") : "0");
2006 indent_up ();
2007 indent_puts ("if ( yytext[yyl] == '\\n' )");
2008 indent_up ();
2009 indent_puts ("M4_YY_INCR_LINENO();");
2010 indent_down ();
2011 indent_down ();
2012 indent_puts ("}");
2013 indent_down ();
2014 outn ("]])");
2016 skelout (); /* %% [12.0] - break point in skel */
2017 if (ddebug) {
2018 indent_puts ("if ( yy_flex_debug )");
2019 indent_up ();
2021 indent_puts ("{");
2022 indent_puts ("if ( yy_act == 0 )");
2023 indent_up ();
2024 indent_puts (C_plus_plus ?
2025 "std::cerr << \"--scanner backing up\\n\";" :
2026 "fprintf( stderr, \"--scanner backing up\\n\" );");
2027 indent_down ();
2029 do_indent ();
2030 out_dec ("else if ( yy_act < %d )\n", num_rules);
2031 indent_up ();
2033 if (C_plus_plus) {
2034 indent_puts
2035 ("std::cerr << \"--accepting rule at line \" << yy_rule_linenum[yy_act] <<");
2036 indent_puts
2037 (" \"(\\\"\" << yytext << \"\\\")\\n\";");
2039 else {
2040 indent_puts
2041 ("fprintf( stderr, \"--accepting rule at line %ld (\\\"%s\\\")\\n\",");
2043 indent_puts
2044 (" (long)yy_rule_linenum[yy_act], yytext );");
2047 indent_down ();
2049 do_indent ();
2050 out_dec ("else if ( yy_act == %d )\n", num_rules);
2051 indent_up ();
2053 if (C_plus_plus) {
2054 indent_puts
2055 ("std::cerr << \"--accepting default rule (\\\"\" << yytext << \"\\\")\\n\";");
2057 else {
2058 indent_puts
2059 ("fprintf( stderr, \"--accepting default rule (\\\"%s\\\")\\n\",");
2060 indent_puts (" yytext );");
2063 indent_down ();
2065 do_indent ();
2066 out_dec ("else if ( yy_act == %d )\n", num_rules + 1);
2067 indent_up ();
2069 indent_puts (C_plus_plus ?
2070 "std::cerr << \"--(end of buffer or a NUL)\\n\";" :
2071 "fprintf( stderr, \"--(end of buffer or a NUL)\\n\" );");
2073 indent_down ();
2075 do_indent ();
2076 outn ("else");
2077 indent_up ();
2079 if (C_plus_plus) {
2080 indent_puts
2081 ("std::cerr << \"--EOF (start condition \" << YY_START << \")\\n\";");
2083 else {
2084 indent_puts
2085 ("fprintf( stderr, \"--EOF (start condition %d)\\n\", YY_START );");
2088 indent_down ();
2090 indent_puts ("}");
2091 indent_down ();
2094 /* Copy actions to output file. */
2095 skelout (); /* %% [13.0] - break point in skel */
2096 indent_up ();
2097 gen_bu_action ();
2098 out (&action_array[action_offset]);
2100 line_directive_out (stdout, 0);
2102 /* generate cases for any missing EOF rules */
2103 for (i = 1; i <= lastsc; ++i)
2104 if (!sceof[i]) {
2105 do_indent ();
2106 out_str ("case YY_STATE_EOF(%s):\n", scname[i]);
2107 did_eof_rule = true;
2110 if (did_eof_rule) {
2111 indent_up ();
2112 indent_puts ("yyterminate();");
2113 indent_down ();
2117 /* Generate code for handling NUL's, if needed. */
2119 /* First, deal with backing up and setting up yy_cp if the scanner
2120 * finds that it should JAM on the NUL.
2122 skelout (); /* %% [14.0] - break point in skel */
2123 set_indent (4);
2125 if (fullspd || fulltbl)
2126 indent_puts ("yy_cp = YY_G(yy_c_buf_p);");
2128 else { /* compressed table */
2129 if (!reject && !interactive) {
2130 /* Do the guaranteed-needed backing up to figure
2131 * out the match.
2133 indent_puts
2134 ("yy_cp = YY_G(yy_last_accepting_cpos);");
2135 indent_puts
2136 ("yy_current_state = YY_G(yy_last_accepting_state);");
2139 else
2140 /* Still need to initialize yy_cp, though
2141 * yy_current_state was set up by
2142 * yy_get_previous_state().
2144 indent_puts ("yy_cp = YY_G(yy_c_buf_p);");
2148 /* Generate code for yy_get_previous_state(). */
2149 set_indent (1);
2150 skelout (); /* %% [15.0] - break point in skel */
2152 gen_start_state ();
2154 set_indent (2);
2155 skelout (); /* %% [16.0] - break point in skel */
2156 gen_next_state (true);
2158 set_indent (1);
2159 skelout (); /* %% [17.0] - break point in skel */
2160 gen_NUL_trans ();
2162 skelout (); /* %% [18.0] - break point in skel */
2163 skelout (); /* %% [19.0] - break point in skel */
2164 /* Update BOL and yylineno inside of input(). */
2165 if (bol_needed) {
2166 indent_puts
2167 ("YY_CURRENT_BUFFER_LVALUE->yy_at_bol = (c == '\\n');");
2168 if (do_yylineno) {
2169 indent_puts
2170 ("if ( YY_CURRENT_BUFFER_LVALUE->yy_at_bol )");
2171 indent_up ();
2172 indent_puts ("M4_YY_INCR_LINENO();");
2173 indent_down ();
2177 else if (do_yylineno) {
2178 indent_puts ("if ( c == '\\n' )");
2179 indent_up ();
2180 indent_puts ("M4_YY_INCR_LINENO();");
2181 indent_down ();
2184 skelout ();
2186 /* Copy remainder of input to output. */
2188 line_directive_out (stdout, 1);
2190 if (sectnum == 3) {
2191 OUT_BEGIN_CODE ();
2192 (void) flexscan (); /* copy remainder of input to output */
2193 OUT_END_CODE ();