This is Unofficial EPICS BASE Doxygen Site
gen.c
Go to the documentation of this file.
1 /*************************************************************************\
2 * Copyright (c) 2002 The University of Chicago, as Operator of Argonne
3 * National Laboratory.
4 * Copyright (c) 2002 The Regents of the University of California, as
5 * Operator of Los Alamos National Laboratory.
6 * EPICS BASE is distributed subject to a Software License Agreement found
7 * in file LICENSE that is included with this distribution.
8 \*************************************************************************/
9 /* gen - actual generation (writing) of flex scanners */
10 
11 /*-
12  * Copyright (c) 1990 The Regents of the University of California.
13  * All rights reserved.
14  *
15  * This code is derived from software contributed to Berkeley by
16  * Vern Paxson.
17  *
18  * The United States Government has rights in this work pursuant
19  * to contract no. DE-AC03-76SF00098 between the United States
20  * Department of Energy and the University of California.
21  *
22  * Redistribution and use in source and binary forms are permitted provided
23  * that: (1) source distributions retain this entire copyright notice and
24  * comment, and (2) distributions including binaries display the following
25  * acknowledgement: ``This product includes software developed by the
26  * University of California, Berkeley and its contributors'' in the
27  * documentation or other materials provided with the distribution and in
28  * all advertising materials mentioning features or use of this software.
29  * Neither the name of the University nor the names of its contributors may
30  * be used to endorse or promote products derived from this software without
31  * specific prior written permission.
32  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
33  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
34  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
35  */
36 
37 #include "flexdef.h"
38 
39 
40 /* declare functions that have forward references */
41 
42 void gen_next_state (int);
43 void genecs (void);
44 void indent_put2s (char [], char []);
45 void indent_puts (char []);
46 
47 
48 static int indent_level = 0; /* each level is 4 spaces */
49 
50 #define indent_up() (++indent_level)
51 #define indent_down() (--indent_level)
52 #define set_indent(indent_val) indent_level = indent_val
53 
54 /* *everything* is done in terms of arrays starting at 1, so provide
55  * a null entry for the zero element of all C arrays
56  */
57 static char C_short_decl[] = "static const short int %s[%d] =\n { 0,\n";
58 static char C_long_decl[] = "static const long int %s[%d] =\n { 0,\n";
59 static char C_state_decl[] =
60  "static const yy_state_type %s[%d] =\n { 0,\n";
61 
62 
63 /* indent to the current level */
64 
65 void do_indent(void)
66 {
67  int i = indent_level * 4;
68 
69  while ( i >= 8 )
70  {
71  putchar( '\t' );
72  i -= 8;
73  }
74 
75  while ( i > 0 )
76  {
77  putchar( ' ' );
78  --i;
79  }
80  }
81 
82 
83 /* generate the code to keep backtracking information */
84 
85 void gen_backtracking(void)
86 {
87  if ( reject || num_backtracking == 0 )
88  return;
89 
90  if ( fullspd )
91  indent_puts( "if ( yy_current_state[-1].yy_nxt )" );
92  else
93  indent_puts( "if ( yy_accept[yy_current_state] )" );
94 
95  indent_up();
96  indent_puts( "{" );
97  indent_puts( "yy_last_accepting_state = yy_current_state;" );
98  indent_puts( "yy_last_accepting_cpos = yy_cp;" );
99  indent_puts( "}" );
100  indent_down();
101  }
102 
103 
104 /* generate the code to perform the backtrack */
105 
106 void gen_bt_action(void)
107 {
108  if ( reject || num_backtracking == 0 )
109  return;
110 
111  set_indent( 3 );
112 
113  indent_puts( "case 0: /* must backtrack */" );
114  indent_puts( "/* undo the effects of YY_DO_BEFORE_ACTION */" );
115  indent_puts( "*yy_cp = yy_hold_char;" );
116 
117  if ( fullspd || fulltbl )
118  indent_puts( "yy_cp = yy_last_accepting_cpos + 1;" );
119  else
120  /* backtracking info for compressed tables is taken \after/
121  * yy_cp has been incremented for the next state
122  */
123  indent_puts( "yy_cp = yy_last_accepting_cpos;" );
124 
125  indent_puts( "yy_current_state = yy_last_accepting_state;" );
126  indent_puts( "goto yy_find_action;" );
127  putchar( '\n' );
128 
129  set_indent( 0 );
130  }
131 
132 
133 /* genctbl - generates full speed compressed transition table
134  *
135  * synopsis
136  * genctbl();
137  */
138 
139 void genctbl(void)
140 {
141  int i;
142  int end_of_buffer_action = num_rules + 1;
143 
144  /* table of verify for transition and offset to next state */
145  printf( "static const struct yy_trans_info yy_transition[%d] =\n",
146  tblend + numecs + 1 );
147  printf( " {\n" );
148 
149  /* We want the transition to be represented as the offset to the
150  * next state, not the actual state number, which is what it currently is.
151  * The offset is base[nxt[i]] - base[chk[i]]. That's just the
152  * difference between the starting points of the two involved states
153  * (to - from).
154  *
155  * first, though, we need to find some way to put in our end-of-buffer
156  * flags and states. We do this by making a state with absolutely no
157  * transitions. We put it at the end of the table.
158  */
159  /* at this point, we're guaranteed that there's enough room in nxt[]
160  * and chk[] to hold tblend + numecs entries. We need just two slots.
161  * One for the action and one for the end-of-buffer transition. We
162  * now *assume* that we're guaranteed the only character we'll try to
163  * index this nxt/chk pair with is EOB, i.e., 0, so we don't have to
164  * make sure there's room for jam entries for other characters.
165  */
166 
167  base[lastdfa + 1] = tblend + 2;
168  nxt[tblend + 1] = end_of_buffer_action;
169  chk[tblend + 1] = numecs + 1;
170  chk[tblend + 2] = 1; /* anything but EOB */
171  nxt[tblend + 2] = 0; /* so that "make test" won't show arb. differences */
172 
173  /* make sure every state has a end-of-buffer transition and an action # */
174  for ( i = 0; i <= lastdfa; ++i )
175  {
176  int anum = dfaacc[i].dfaacc_state;
177 
178  chk[base[i]] = EOB_POSITION;
179  chk[base[i] - 1] = ACTION_POSITION;
180  nxt[base[i] - 1] = anum; /* action number */
181  }
182 
183  for ( i = 0; i <= tblend; ++i )
184  {
185  if ( chk[i] == EOB_POSITION )
186  transition_struct_out( 0, base[lastdfa + 1] - i );
187 
188  else if ( chk[i] == ACTION_POSITION )
189  transition_struct_out( 0, nxt[i] );
190 
191  else if ( chk[i] > numecs || chk[i] == 0 )
192  transition_struct_out( 0, 0 ); /* unused slot */
193 
194  else /* verify, transition */
195  transition_struct_out( chk[i], base[nxt[i]] - (i - chk[i]) );
196  }
197 
198 
199  /* here's the final, end-of-buffer state */
200  transition_struct_out( chk[tblend + 1], nxt[tblend + 1] );
201  transition_struct_out( chk[tblend + 2], nxt[tblend + 2] );
202 
203  printf( " };\n" );
204  printf( "\n" );
205 
206  /* table of pointers to start states */
207  printf( "static const struct yy_trans_info *yy_start_state_list[%d] =\n",
208  lastsc * 2 + 1 );
209  printf( " {\n" );
210 
211  for ( i = 0; i <= lastsc * 2; ++i )
212  printf( " &yy_transition[%d],\n", base[i] );
213 
214  dataend();
215 
216  if ( useecs )
217  genecs();
218  }
219 
220 
221 /* generate equivalence-class tables */
222 
223 void genecs(void)
224 {
225  int i, j;
226  static char C_char_decl[] = "static const %s %s[%d] =\n { 0,\n";
227  int numrows;
228  Char clower();
229 
230  if ( numecs < csize )
231  printf( C_char_decl, "YY_CHAR", "yy_ec", csize );
232  else
233  printf( C_char_decl, "short", "yy_ec", csize );
234 
235  for ( i = 1; i < csize; ++i )
236  {
237  if ( caseins && (i >= 'A') && (i <= 'Z') )
238  ecgroup[i] = ecgroup[clower( i )];
239 
240  ecgroup[i] = abs( ecgroup[i] );
241  mkdata( ecgroup[i] );
242  }
243 
244  dataend();
245 
246  if ( trace )
247  {
248  char *readable_form();
249 
250  fputs( "\n\nEquivalence Classes:\n\n", stderr );
251 
252  numrows = csize / 8;
253 
254  for ( j = 0; j < numrows; ++j )
255  {
256  for ( i = j; i < csize; i = i + numrows )
257  {
258  fprintf( stderr, "%4s = %-2d", readable_form( i ), ecgroup[i] );
259 
260  putc( ' ', stderr );
261  }
262 
263  putc( '\n', stderr );
264  }
265  }
266  }
267 
268 
269 /* generate the code to find the action number */
270 
271 void gen_find_action(void)
272 {
273  if ( fullspd )
274  indent_puts( "yy_act = yy_current_state[-1].yy_nxt;" );
275 
276  else if ( fulltbl )
277  indent_puts( "yy_act = yy_accept[yy_current_state];" );
278 
279  else if ( reject )
280  {
281  indent_puts( "yy_current_state = *--yy_state_ptr;" );
282  indent_puts( "yy_lp = yy_accept[yy_current_state];" );
283 
284  puts( "find_rule: /* we branch to this label when backtracking */" );
285 
286  indent_puts( "for ( ; ; ) /* until we find what rule we matched */" );
287 
288  indent_up();
289 
290  indent_puts( "{" );
291 
292  indent_puts( "if ( yy_lp && yy_lp < yy_accept[yy_current_state + 1] )" );
293  indent_up();
294  indent_puts( "{" );
295  indent_puts( "yy_act = yy_acclist[yy_lp];" );
296 
298  {
299  indent_puts( "if ( yy_act & YY_TRAILING_HEAD_MASK ||" );
300  indent_puts( " yy_looking_for_trail_begin )" );
301  indent_up();
302  indent_puts( "{" );
303 
304  indent_puts( "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();
312 
313  indent_puts( "}" );
314  indent_down();
315 
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;" );
323 
324  if ( real_reject )
325  {
326  /* remember matched text in case we back up due to REJECT */
327  indent_puts( "yy_full_match = yy_cp;" );
328  indent_puts( "yy_full_state = yy_state_ptr;" );
329  indent_puts( "yy_full_lp = yy_lp;" );
330  }
331 
332  indent_puts( "}" );
333  indent_down();
334 
335  indent_puts( "else" );
336  indent_up();
337  indent_puts( "{" );
338  indent_puts( "yy_full_match = yy_cp;" );
339  indent_puts( "yy_full_state = yy_state_ptr;" );
340  indent_puts( "yy_full_lp = yy_lp;" );
341  indent_puts( "break;" );
342  indent_puts( "}" );
343  indent_down();
344 
345  indent_puts( "++yy_lp;" );
346  indent_puts( "goto find_rule;" );
347  }
348 
349  else
350  {
351  /* remember matched text in case we back up due to trailing context
352  * plus REJECT
353  */
354  indent_up();
355  indent_puts( "{" );
356  indent_puts( "yy_full_match = yy_cp;" );
357  indent_puts( "break;" );
358  indent_puts( "}" );
359  indent_down();
360  }
361 
362  indent_puts( "}" );
363  indent_down();
364 
365  indent_puts( "--yy_cp;" );
366 
367  /* we could consolidate the following two lines with those at
368  * the beginning, but at the cost of complaints that we're
369  * branching inside a loop
370  */
371  indent_puts( "yy_current_state = *--yy_state_ptr;" );
372  indent_puts( "yy_lp = yy_accept[yy_current_state];" );
373 
374  indent_puts( "}" );
375 
376  indent_down();
377  }
378 
379  else
380  /* compressed */
381  indent_puts( "yy_act = yy_accept[yy_current_state];" );
382  }
383 
384 
385 /* genftbl - generates full transition table
386  *
387  * synopsis
388  * genftbl();
389  */
390 
391 void genftbl(void)
392 {
393  int i;
394  int end_of_buffer_action = num_rules + 1;
395 
396  printf( C_short_decl, "yy_accept", lastdfa + 1 );
397 
398 
399  dfaacc[end_of_buffer_state].dfaacc_state = end_of_buffer_action;
400 
401  for ( i = 1; i <= lastdfa; ++i )
402  {
403  int anum = dfaacc[i].dfaacc_state;
404 
405  mkdata( anum );
406 
407  if ( trace && anum )
408  fprintf( stderr, "state # %d accepts: [%d]\n", i, anum );
409  }
410 
411  dataend();
412 
413  if ( useecs )
414  genecs();
415 
416  /* don't have to dump the actual full table entries - they were created
417  * on-the-fly
418  */
419  }
420 
421 
422 /* generate the code to find the next compressed-table state */
423 
424 void gen_next_compressed_state(char *char_map)
425 {
426  indent_put2s( "YY_CHAR yy_c = %s;", char_map );
427 
428  /* save the backtracking info \before/ computing the next state
429  * because we always compute one more state than needed - we
430  * always proceed until we reach a jam state
431  */
433 
434  indent_puts(
435  "while ( yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state )" );
436  indent_up();
437  indent_puts( "{" );
438  indent_puts( "yy_current_state = yy_def[yy_current_state];" );
439 
440  if ( usemecs )
441  {
442  /* we've arrange it so that templates are never chained
443  * to one another. This means we can afford make a
444  * very simple test to see if we need to convert to
445  * yy_c's meta-equivalence class without worrying
446  * about erroneously looking up the meta-equivalence
447  * class twice
448  */
449  do_indent();
450  /* lastdfa + 2 is the beginning of the templates */
451  printf( "if ( yy_current_state >= %d )\n", lastdfa + 2 );
452 
453  indent_up();
454  indent_puts( "yy_c = yy_meta[(int)yy_c];" );
455  indent_down();
456  }
457 
458  indent_puts( "}" );
459  indent_down();
460 
461  indent_puts(
462  "yy_current_state = yy_nxt[yy_base[yy_current_state] + yy_c];" );
463  }
464 
465 
466 /* generate the code to find the next match */
467 
468 void gen_next_match(void)
469 {
470  /* NOTE - changes in here should be reflected in gen_next_state() and
471  * gen_NUL_trans()
472  */
473  char *char_map = useecs ? "yy_ec[(int)*yy_cp]" : "*yy_cp";
474  char *char_map_2 = useecs ? "yy_ec[*++yy_cp]" : "*++yy_cp";
475 
476  if ( fulltbl )
477  {
478  indent_put2s(
479  "while ( (yy_current_state = yy_nxt[yy_current_state][%s]) > 0 )",
480  char_map );
481 
482  indent_up();
483 
484  if ( num_backtracking > 0 )
485  {
486  indent_puts( "{" );
488  putchar( '\n' );
489  }
490 
491  indent_puts( "++yy_cp;" );
492 
493  if ( num_backtracking > 0 )
494  indent_puts( "}" );
495 
496  indent_down();
497 
498  putchar( '\n' );
499  indent_puts( "yy_current_state = -yy_current_state;" );
500  }
501 
502  else if ( fullspd )
503  {
504  indent_puts( "{" );
505  indent_puts( "const struct yy_trans_info *yy_trans_info;\n" );
506  indent_puts( "YY_CHAR yy_c;\n" );
507  indent_put2s( "for ( yy_c = %s;", char_map );
508  indent_puts(
509  " (yy_trans_info = &yy_current_state[yy_c])->yy_verify == yy_c;" );
510  indent_put2s( " yy_c = %s )", char_map_2 );
511 
512  indent_up();
513 
514  if ( num_backtracking > 0 )
515  indent_puts( "{" );
516 
517  indent_puts( "yy_current_state += yy_trans_info->yy_nxt;" );
518 
519  if ( num_backtracking > 0 )
520  {
521  putchar( '\n' );
523  indent_puts( "}" );
524  }
525 
526  indent_down();
527  indent_puts( "}" );
528  }
529 
530  else
531  { /* compressed */
532  indent_puts( "do" );
533 
534  indent_up();
535  indent_puts( "{" );
536 
537  gen_next_state( false );
538 
539  indent_puts( "++yy_cp;" );
540 
541  indent_puts( "}" );
542  indent_down();
543 
544  do_indent();
545 
546  if ( interactive )
547  printf( "while ( yy_base[yy_current_state] != %d );\n", jambase );
548  else
549  printf( "while ( yy_current_state != %d );\n", jamstate );
550 
551  if ( ! reject && ! interactive )
552  {
553  /* do the guaranteed-needed backtrack to figure out the match */
554  indent_puts( "yy_cp = yy_last_accepting_cpos;" );
555  indent_puts( "yy_current_state = yy_last_accepting_state;" );
556  }
557  }
558  }
559 
560 
561 /* generate the code to find the next state */
562 
563 void gen_next_state(int worry_about_NULs)
564 { /* NOTE - changes in here should be reflected in get_next_match() */
565  char char_map[256];
566 
567  if ( worry_about_NULs && ! nultrans )
568  {
569  if ( useecs )
570  (void) sprintf( char_map, "(*yy_cp ? yy_ec[(int)*yy_cp] : %d)", NUL_ec );
571  else
572  (void) sprintf( char_map, "(*yy_cp ? *yy_cp : %d)", NUL_ec );
573  }
574 
575  else
576  (void) strcpy( char_map, useecs ? "yy_ec[(int)*yy_cp]" : "*yy_cp" );
577 
578  if ( worry_about_NULs && nultrans )
579  {
580  if ( ! fulltbl && ! fullspd )
581  /* compressed tables backtrack *before* they match */
583 
584  indent_puts( "if ( *yy_cp )" );
585  indent_up();
586  indent_puts( "{" );
587  }
588 
589  if ( fulltbl )
590  indent_put2s( "yy_current_state = yy_nxt[yy_current_state][%s];",
591  char_map );
592 
593  else if ( fullspd )
594  indent_put2s( "yy_current_state += yy_current_state[%s].yy_nxt;",
595  char_map );
596 
597  else
598  gen_next_compressed_state( char_map );
599 
600  if ( worry_about_NULs && nultrans )
601  {
602  indent_puts( "}" );
603  indent_down();
604  indent_puts( "else" );
605  indent_up();
606  indent_puts( "yy_current_state = yy_NUL_trans[yy_current_state];" );
607  indent_down();
608  }
609 
610  if ( fullspd || fulltbl )
612 
613  if ( reject )
614  indent_puts( "*yy_state_ptr++ = yy_current_state;" );
615  }
616 
617 
618 /* generate the code to make a NUL transition */
619 
620 void gen_NUL_trans(void)
621 { /* NOTE - changes in here should be reflected in get_next_match() */
622  int need_backtracking = (num_backtracking > 0 && ! reject);
623 
624  if ( need_backtracking )
625  /* we'll need yy_cp lying around for the gen_backtracking() */
626  indent_puts( "YY_CHAR *yy_cp = yy_c_buf_p;" );
627 
628  putchar( '\n' );
629 
630  if ( nultrans )
631  {
632  indent_puts( "yy_current_state = yy_NUL_trans[yy_current_state];" );
633  indent_puts( "yy_is_jam = (yy_current_state == 0);" );
634  }
635 
636  else if ( fulltbl )
637  {
638  do_indent();
639  printf( "yy_current_state = yy_nxt[yy_current_state][%d];\n",
640  NUL_ec );
641  indent_puts( "yy_is_jam = (yy_current_state <= 0);" );
642  }
643 
644  else if ( fullspd )
645  {
646  do_indent();
647  printf( "int yy_c = %d;\n", NUL_ec );
648 
649  indent_puts(
650  "const struct yy_trans_info *yy_trans_info;\n" );
651  indent_puts( "yy_trans_info = &yy_current_state[yy_c];" );
652  indent_puts( "yy_current_state += yy_trans_info->yy_nxt;" );
653 
654  indent_puts( "yy_is_jam = (yy_trans_info->yy_verify != yy_c);" );
655  }
656 
657  else
658  {
659  char NUL_ec_str[20];
660 
661  (void) sprintf( NUL_ec_str, "%d", NUL_ec );
662  gen_next_compressed_state( NUL_ec_str );
663 
664  if ( reject )
665  indent_puts( "*yy_state_ptr++ = yy_current_state;" );
666 
667  do_indent();
668 
669  if ( interactive )
670  printf( "yy_is_jam = (yy_base[yy_current_state] == %d);\n",
671  jambase );
672  else
673  printf( "yy_is_jam = (yy_current_state == %d);\n", jamstate );
674  }
675 
676  /* if we've entered an accepting state, backtrack; note that
677  * compressed tables have *already* done such backtracking, so
678  * we needn't bother with it again
679  */
680  if ( need_backtracking && (fullspd || fulltbl) )
681  {
682  putchar( '\n' );
683  indent_puts( "if ( ! yy_is_jam )" );
684  indent_up();
685  indent_puts( "{" );
687  indent_puts( "}" );
688  indent_down();
689  }
690  }
691 
692 
693 /* generate the code to find the start state */
694 
695 void gen_start_state(void)
696 {
697  if ( fullspd )
698  indent_put2s( "yy_current_state = yy_start_state_list[yy_start%s];",
699  bol_needed ? " + (yy_bp[-1] == '\\n' ? 1 : 0)" : "" );
700 
701  else
702  {
703  indent_puts( "yy_current_state = yy_start;" );
704 
705  if ( bol_needed )
706  {
707  indent_puts( "if ( yy_bp[-1] == '\\n' )" );
708  indent_up();
709  indent_puts( "++yy_current_state;" );
710  indent_down();
711  }
712 
713  if ( reject )
714  {
715  /* set up for storing up states */
716  indent_puts( "yy_state_ptr = yy_state_buf;" );
717  indent_puts( "*yy_state_ptr++ = yy_current_state;" );
718  }
719  }
720  }
721 
722 
723 /* gentabs - generate data statements for the transition tables
724  *
725  * synopsis
726  * gentabs();
727  */
728 
729 void gentabs(void)
730 {
731  int i, j, k, *accset, nacc, *acc_array, total_states;
732  int end_of_buffer_action = num_rules + 1;
733 
734  /* *everything* is done in terms of arrays starting at 1, so provide
735  * a null entry for the zero element of all C arrays
736  */
737  static char C_char_decl[] =
738  "static const YY_CHAR %s[%d] =\n { 0,\n";
739 
741  nummt = 0;
742 
743  /* the compressed table format jams by entering the "jam state",
744  * losing information about the previous state in the process.
745  * In order to recover the previous state, we effectively need
746  * to keep backtracking information.
747  */
749 
750  if ( reject )
751  {
752  /* write out accepting list and pointer list
753  *
754  * first we generate the "yy_acclist" array. In the process, we compute
755  * the indices that will go into the "yy_accept" array, and save the
756  * indices in the dfaacc array
757  */
758  int EOB_accepting_list[2];
759 
760  /* set up accepting structures for the End Of Buffer state */
761  EOB_accepting_list[0] = 0;
762  EOB_accepting_list[1] = end_of_buffer_action;
764  dfaacc[end_of_buffer_state].dfaacc_set = EOB_accepting_list;
765 
766  printf( C_short_decl, "yy_acclist", max( numas, 1 ) + 1 );
767 
768  j = 1; /* index into "yy_acclist" array */
769 
770  for ( i = 1; i <= lastdfa; ++i )
771  {
772  acc_array[i] = j;
773 
774  if ( accsiz[i] != 0 )
775  {
776  accset = dfaacc[i].dfaacc_set;
777  nacc = accsiz[i];
778 
779  if ( trace )
780  fprintf( stderr, "state # %d accepts: ", i );
781 
782  for ( k = 1; k <= nacc; ++k )
783  {
784  int accnum = accset[k];
785 
786  ++j;
787 
789  ! (accnum & YY_TRAILING_HEAD_MASK) &&
790  accnum > 0 && accnum <= num_rules &&
791  rule_type[accnum] == RULE_VARIABLE )
792  {
793  /* special hack to flag accepting number as part
794  * of trailing context rule
795  */
796  accnum |= YY_TRAILING_MASK;
797  }
798 
799  mkdata( accnum );
800 
801  if ( trace )
802  {
803  fprintf( stderr, "[%d]", accset[k] );
804 
805  if ( k < nacc )
806  fputs( ", ", stderr );
807  else
808  putc( '\n', stderr );
809  }
810  }
811  }
812  }
813 
814  /* add accepting number for the "jam" state */
815  acc_array[i] = j;
816 
817  dataend();
818  }
819 
820  else
821  {
822  dfaacc[end_of_buffer_state].dfaacc_state = end_of_buffer_action;
823 
824  for ( i = 1; i <= lastdfa; ++i )
825  acc_array[i] = dfaacc[i].dfaacc_state;
826 
827  /* add accepting number for jam state */
828  acc_array[i] = 0;
829  }
830 
831  /* spit out "yy_accept" array. If we're doing "reject", it'll be pointers
832  * into the "yy_acclist" array. Otherwise it's actual accepting numbers.
833  * In either case, we just dump the numbers.
834  */
835 
836  /* "lastdfa + 2" is the size of "yy_accept"; includes room for C arrays
837  * beginning at 0 and for "jam" state
838  */
839  k = lastdfa + 2;
840 
841  if ( reject )
842  /* we put a "cap" on the table associating lists of accepting
843  * numbers with state numbers. This is needed because we tell
844  * where the end of an accepting list is by looking at where
845  * the list for the next state starts.
846  */
847  ++k;
848 
849  printf( C_short_decl, "yy_accept", k );
850 
851  for ( i = 1; i <= lastdfa; ++i )
852  {
853  mkdata( acc_array[i] );
854 
855  if ( ! reject && trace && acc_array[i] )
856  fprintf( stderr, "state # %d accepts: [%d]\n", i, acc_array[i] );
857  }
858 
859  /* add entry for "jam" state */
860  mkdata( acc_array[i] );
861 
862  if ( reject )
863  /* add "cap" for the list */
864  mkdata( acc_array[i] );
865 
866  dataend();
867 
868  if ( useecs )
869  genecs();
870 
871  if ( usemecs )
872  {
873  /* write out meta-equivalence classes (used to index templates with) */
874 
875  if ( trace )
876  fputs( "\n\nMeta-Equivalence Classes:\n", stderr );
877 
878  printf( C_char_decl, "yy_meta", numecs + 1 );
879 
880  for ( i = 1; i <= numecs; ++i )
881  {
882  if ( trace )
883  fprintf( stderr, "%d = %d\n", i, abs( tecbck[i] ) );
884 
885  mkdata( abs( tecbck[i] ) );
886  }
887 
888  dataend();
889  }
890 
891  total_states = lastdfa + numtemps;
892 
893  printf( tblend > MAX_SHORT ? C_long_decl : C_short_decl,
894  "yy_base", total_states + 1 );
895 
896  for ( i = 1; i <= lastdfa; ++i )
897  {
898  int d = def[i];
899 
900  if ( base[i] == JAMSTATE )
901  base[i] = jambase;
902 
903  if ( d == JAMSTATE )
904  def[i] = jamstate;
905 
906  else if ( d < 0 )
907  {
908  /* template reference */
909  ++tmpuses;
910  def[i] = lastdfa - d + 1;
911  }
912 
913  mkdata( base[i] );
914  }
915 
916  /* generate jam state's base index */
917  mkdata( base[i] );
918 
919  for ( ++i /* skip jam state */; i <= total_states; ++i )
920  {
921  mkdata( base[i] );
922  def[i] = jamstate;
923  }
924 
925  dataend();
926 
927  printf( tblend > MAX_SHORT ? C_long_decl : C_short_decl,
928  "yy_def", total_states + 1 );
929 
930  for ( i = 1; i <= total_states; ++i )
931  mkdata( def[i] );
932 
933  dataend();
934 
935  printf( lastdfa > MAX_SHORT ? C_long_decl : C_short_decl,
936  "yy_nxt", tblend + 1 );
937 
938  for ( i = 1; i <= tblend; ++i )
939  {
940  if ( nxt[i] == 0 || chk[i] == 0 )
941  nxt[i] = jamstate; /* new state is the JAM state */
942 
943  mkdata( nxt[i] );
944  }
945 
946  dataend();
947 
948  printf( lastdfa > MAX_SHORT ? C_long_decl : C_short_decl,
949  "yy_chk", tblend + 1 );
950 
951  for ( i = 1; i <= tblend; ++i )
952  {
953  if ( chk[i] == 0 )
954  ++nummt;
955 
956  mkdata( chk[i] );
957  }
958 
959  dataend();
960  }
961 
962 
963 /* write out a formatted string (with a secondary string argument) at the
964  * current indentation level, adding a final newline
965  */
966 
967 void indent_put2s(char *fmt, char *arg)
968 {
969  do_indent();
970  printf( fmt, arg );
971  putchar( '\n' );
972  }
973 
974 
975 /* write out a string at the current indentation level, adding a final
976  * newline
977  */
978 
979 void indent_puts(char *str)
980 {
981  do_indent();
982  puts( str );
983  }
984 
985 
986 /* make_tables - generate transition tables
987  *
988  * synopsis
989  * make_tables();
990  *
991  * Generates transition tables and finishes generating output file
992  */
993 
994 void make_tables(void)
995 {
996  int i;
997  int did_eof_rule = false;
998 
999  skelout();
1000 
1001  /* first, take care of YY_DO_BEFORE_ACTION depending on yymore being used */
1002  set_indent( 2 );
1003 
1004  if ( yymore_used )
1005  {
1006  indent_puts( "yytext -= yy_more_len; \\" );
1007  indent_puts( "yyleng = yy_cp - yytext; \\" );
1008  }
1009 
1010  else
1011  indent_puts( "yyleng = yy_cp - yy_bp; \\" );
1012 
1013  set_indent( 0 );
1014 
1015  skelout();
1016 
1017 
1018  printf( "#define YY_END_OF_BUFFER %d\n", num_rules + 1 );
1019 
1020  if ( fullspd )
1021  { /* need to define the transet type as a size large
1022  * enough to hold the biggest offset
1023  */
1024  int total_table_size = tblend + numecs + 1;
1025  char *trans_offset_type =
1026  total_table_size > MAX_SHORT ? "long" : "short";
1027 
1028  set_indent( 0 );
1029  indent_puts( "struct yy_trans_info" );
1030  indent_up();
1031  indent_puts( "{" );
1032  indent_puts( "short yy_verify;" );
1033 
1034  /* in cases where its sister yy_verify *is* a "yes, there is a
1035  * transition", yy_nxt is the offset (in records) to the next state.
1036  * In most cases where there is no transition, the value of yy_nxt
1037  * is irrelevant. If yy_nxt is the -1th record of a state, though,
1038  * then yy_nxt is the action number for that state
1039  */
1040 
1041  indent_put2s( "%s yy_nxt;", trans_offset_type );
1042  indent_puts( "};" );
1043  indent_down();
1044 
1045  indent_puts( "typedef const struct yy_trans_info *yy_state_type;" );
1046  }
1047 
1048  else
1049  indent_puts( "typedef int yy_state_type;" );
1050 
1051  if ( fullspd )
1052  genctbl();
1053 
1054  else if ( fulltbl )
1055  genftbl();
1056 
1057  else
1058  gentabs();
1059 
1060  if ( num_backtracking > 0 )
1061  {
1062  indent_puts( "static yy_state_type yy_last_accepting_state;" );
1063  indent_puts( "static YY_CHAR *yy_last_accepting_cpos;\n" );
1064  }
1065 
1066  if ( nultrans )
1067  {
1068  printf( C_state_decl, "yy_NUL_trans", lastdfa + 1 );
1069 
1070  for ( i = 1; i <= lastdfa; ++i )
1071  {
1072  if ( fullspd )
1073  {
1074  if ( nultrans )
1075  printf( " &yy_transition[%d],\n", base[i] );
1076  else
1077  printf( " 0,\n" );
1078  }
1079 
1080  else
1081  mkdata( nultrans[i] );
1082  }
1083 
1084  dataend();
1085  }
1086 
1087  if ( ddebug )
1088  { /* spit out table mapping rules to line numbers */
1089  indent_puts( "extern int yy_flex_debug;" );
1090  indent_puts( "int yy_flex_debug = 1;\n" );
1091 
1092  printf( C_short_decl, "yy_rule_linenum", num_rules );
1093  for ( i = 1; i < num_rules; ++i )
1094  mkdata( rule_linenum[i] );
1095  dataend();
1096  }
1097 
1098  if ( reject )
1099  {
1100  /* declare state buffer variables */
1101  puts(
1102  "static yy_state_type yy_state_buf[YY_BUF_SIZE + 2], *yy_state_ptr;" );
1103  puts( "static YY_CHAR *yy_full_match;" );
1104  puts( "static int yy_lp;" );
1105 
1107  {
1108  puts( "static int yy_looking_for_trail_begin = 0;" );
1109  puts( "static int yy_full_lp;" );
1110  puts( "static int *yy_full_state;" );
1111  printf( "#define YY_TRAILING_MASK 0x%x\n", YY_TRAILING_MASK );
1112  printf( "#define YY_TRAILING_HEAD_MASK 0x%x\n",
1114  }
1115 
1116  puts( "#define REJECT \\" );
1117  puts( "{ \\" );
1118  puts(
1119  "*yy_cp = yy_hold_char; /* undo effects of setting up yytext */ \\" );
1120  puts(
1121  "yy_cp = yy_full_match; /* restore poss. backed-over text */ \\" );
1122 
1124  {
1125  puts( "yy_lp = yy_full_lp; /* restore orig. accepting pos. */ \\" );
1126  puts(
1127  "yy_state_ptr = yy_full_state; /* restore orig. state */ \\" );
1128  puts(
1129  "yy_current_state = *yy_state_ptr; /* restore curr. state */ \\" );
1130  }
1131 
1132  puts( "++yy_lp; \\" );
1133  puts( "goto find_rule; \\" );
1134  puts( "}" );
1135  }
1136 
1137  else
1138  {
1139  puts( "/* the intent behind this definition is that it'll catch" );
1140  puts( " * any uses of REJECT which flex missed" );
1141  puts( " */" );
1142  puts( "#define REJECT reject_used_but_not_detected" );
1143  }
1144 
1145  if ( yymore_used )
1146  {
1147  indent_puts( "static int yy_more_flag = 0;" );
1148  indent_puts( "static int yy_doing_yy_more = 0;" );
1149  indent_puts( "static int yy_more_len = 0;" );
1150  indent_puts(
1151  "#define yymore() { yy_more_flag = 1; }" );
1152  indent_puts(
1153  "#define YY_MORE_ADJ (yy_doing_yy_more ? yy_more_len : 0)" );
1154  }
1155 
1156  else
1157  {
1158  indent_puts( "#define yymore() yymore_used_but_not_detected" );
1159  indent_puts( "#define YY_MORE_ADJ 0" );
1160  }
1161 
1162  skelout();
1163 
1164  if ( ferror( temp_action_file ) )
1165  flexfatal( "error occurred when writing temporary action file" );
1166 
1167  else if ( fseek( temp_action_file, 0L, SEEK_SET) != 0 )
1168  flexfatal( "error occurred when rewinding temporary action file" );
1169 
1170  /* copy prolog from action_file to output file */
1171  action_out();
1172 
1173  skelout();
1174 
1175  set_indent( 2 );
1176 
1177  if ( yymore_used )
1178  {
1179  indent_puts( "yy_more_len = 0;" );
1180  indent_puts( "yy_doing_yy_more = yy_more_flag;" );
1181  indent_puts( "if ( yy_doing_yy_more )" );
1182  indent_up();
1183  indent_puts( "{" );
1184  indent_puts( "yy_more_len = yyleng;" );
1185  indent_puts( "yy_more_flag = 0;" );
1186  indent_puts( "}" );
1187  indent_down();
1188  }
1189 
1190  skelout();
1191 
1192  gen_start_state();
1193 
1194  /* note, don't use any indentation */
1195  puts( "yy_match:" );
1196  gen_next_match();
1197 
1198  skelout();
1199  set_indent( 2 );
1200  gen_find_action();
1201 
1202  skelout();
1203  if ( ddebug )
1204  {
1205  indent_puts( "if ( yy_flex_debug )" );
1206  indent_up();
1207 
1208  indent_puts( "{" );
1209  indent_puts( "if ( yy_act == 0 )" );
1210  indent_up();
1211  indent_puts( "fprintf( stderr, \"--scanner backtracking\\n\" );" );
1212  indent_down();
1213 
1214  do_indent();
1215  printf( "else if ( yy_act < %d )\n", num_rules );
1216  indent_up();
1217  indent_puts(
1218  "fprintf( stderr, \"--accepting rule at line %d (\\\"%s\\\")\\n\"," );
1219  indent_puts( " yy_rule_linenum[yy_act], yytext );" );
1220  indent_down();
1221 
1222  do_indent();
1223  printf( "else if ( yy_act == %d )\n", num_rules );
1224  indent_up();
1225  indent_puts(
1226  "fprintf( stderr, \"--accepting default rule (\\\"%s\\\")\\n\"," );
1227  indent_puts( " yytext );" );
1228  indent_down();
1229 
1230  do_indent();
1231  printf( "else if ( yy_act == %d )\n", num_rules + 1 );
1232  indent_up();
1233  indent_puts( "fprintf( stderr, \"--(end of buffer or a NUL)\\n\" );" );
1234  indent_down();
1235 
1236  do_indent();
1237  printf( "else\n" );
1238  indent_up();
1239  indent_puts( "fprintf( stderr, \"--EOF\\n\" );" );
1240  indent_down();
1241 
1242  indent_puts( "}" );
1243  indent_down();
1244  }
1245 
1246  /* copy actions from action_file to output file */
1247  skelout();
1248  indent_up();
1249  gen_bt_action();
1250  action_out();
1251 
1252  /* generate cases for any missing EOF rules */
1253  for ( i = 1; i <= lastsc; ++i )
1254  if ( ! sceof[i] )
1255  {
1256  do_indent();
1257  printf( "case YY_STATE_EOF(%s):\n", scname[i] );
1258  did_eof_rule = true;
1259  }
1260 
1261  if ( did_eof_rule )
1262  {
1263  indent_up();
1264  indent_puts( "yyterminate();" );
1265  indent_down();
1266  }
1267 
1268 
1269  /* generate code for handling NUL's, if needed */
1270 
1271  /* first, deal with backtracking and setting up yy_cp if the scanner
1272  * finds that it should JAM on the NUL
1273  */
1274  skelout();
1275  set_indent( 7 );
1276 
1277  if ( fullspd || fulltbl )
1278  indent_puts( "yy_cp = yy_c_buf_p;" );
1279 
1280  else
1281  { /* compressed table */
1282  if ( ! reject && ! interactive )
1283  {
1284  /* do the guaranteed-needed backtrack to figure out the match */
1285  indent_puts( "yy_cp = yy_last_accepting_cpos;" );
1286  indent_puts( "yy_current_state = yy_last_accepting_state;" );
1287  }
1288  }
1289 
1290 
1291  /* generate code for yy_get_previous_state() */
1292  set_indent( 1 );
1293  skelout();
1294 
1295  if ( bol_needed )
1296  indent_puts( "YY_CHAR *yy_bp = yytext;\n" );
1297 
1298  gen_start_state();
1299 
1300  set_indent( 2 );
1301  skelout();
1302  gen_next_state( true );
1303 
1304  set_indent( 1 );
1305  skelout();
1306  gen_NUL_trans();
1307 
1308  skelout();
1309 
1310  /* copy remainder of input to output */
1311 
1313  (void) flexscan(); /* copy remainder of input to output */
1314  }
int tmpuses
Definition: flex.c:101
#define max(x, y)
Definition: flexdef.h:81
void indent_put2s(char[], char[])
#define indent_up()
Definition: gen.c:50
int real_reject
Definition: flex.c:69
#define RULE_VARIABLE
Definition: flexdef.h:422
int * rule_type
Definition: flex.c:78
void gen_find_action(void)
Definition: gen.c:271
char ** scname
Definition: flex.c:88
int jambase
Definition: flex.c:95
int tecbck[CSIZE+1]
Definition: flex.c:84
int ecgroup[CSIZE+1]
Definition: flex.c:83
int i
Definition: scan.c:967
int fullspd
Definition: flex.c:68
int variable_trailing_context_rules
Definition: flex.c:80
#define YY_TRAILING_HEAD_MASK
Definition: flexdef.h:144
int interactive
Definition: flex.c:67
int tblend
Definition: flex.c:92
void gen_backtracking(void)
Definition: gen.c:85
int nummt
Definition: flex.c:100
int ddebug
Definition: flex.c:66
#define printf
Definition: epicsStdio.h:41
Char clower(int c)
Definition: misc.c:175
union dfaacc_union * dfaacc
Definition: flex.c:93
void action_out(void)
Definition: misc.c:62
int * dfaacc_set
Definition: flexdef.h:527
void skelout(void)
Definition: misc.c:734
#define str(v)
int * rule_linenum
Definition: flex.c:78
char * readable_form(int c)
Definition: misc.c:672
#define ACTION_POSITION
Definition: flexdef.h:96
void make_tables(void)
Definition: gen.c:994
int csize
Definition: flex.c:68
int * accsiz
Definition: flex.c:94
int caseins
Definition: flex.c:67
void indent_puts(char[])
int num_rules
Definition: flex.c:76
#define allocate_integer_array(size)
Definition: flexdef.h:581
int useecs
Definition: flex.c:67
void genecs(void)
Definition: gen.c:223
#define YY_TRAILING_MASK
Definition: flexdef.h:141
int lastdfa
Definition: flex.c:91
void dataend(void)
Definition: misc.c:292
#define puts
Definition: epicsStdio.h:46
int numtemps
Definition: flex.c:81
void gentabs(void)
Definition: gen.c:729
int usemecs
Definition: flex.c:67
#define putchar
Definition: epicsStdio.h:51
#define Char
Definition: flexdef.h:59
int NUL_ec
Definition: flex.c:92
void genftbl(void)
Definition: gen.c:391
void gen_next_compressed_state(char *char_map)
Definition: gen.c:424
void gen_next_state(int)
Definition: gen.c:563
void mkdata(int)
Definition: misc.c:524
int end_of_buffer_state
Definition: flex.c:105
#define stdout
Definition: epicsStdio.h:30
int jamstate
Definition: flex.c:95
#define MAX_SHORT
Definition: flexdef.h:273
int bol_needed
Definition: flex.c:102
void line_directive_out(FILE *)
Definition: misc.c:479
int flexscan()
int reject
Definition: flex.c:69
void flexfatal(char[])
int trace
Definition: flex.c:66
int lastsc
Definition: flex.c:87
void gen_start_state(void)
Definition: gen.c:695
int * nultrans
Definition: flex.c:92
int current_max_dfas
Definition: flex.c:90
int * sceof
Definition: flex.c:87
int num_backtracking
Definition: flex.c:102
FILE * temp_action_file
Definition: flex.c:103
int * nxt
Definition: flex.c:91
int * base
Definition: flex.c:92
int * def
Definition: flex.c:92
#define JAMSTATE
Definition: flexdef.h:176
void gen_bt_action(void)
Definition: gen.c:106
#define EOB_POSITION
Definition: flexdef.h:95
void gen_next_match(void)
Definition: gen.c:468
#define stderr
Definition: epicsStdio.h:32
int yymore_used
Definition: flex.c:69
void do_indent(void)
Definition: gen.c:65
int dfaacc_state
Definition: flexdef.h:528
#define set_indent(indent_val)
Definition: gen.c:52
int numecs
Definition: flex.c:83
int fulltbl
Definition: flex.c:67
void gen_NUL_trans(void)
Definition: gen.c:620
int numas
Definition: flex.c:94
void genctbl(void)
Definition: gen.c:139
#define indent_down()
Definition: gen.c:51
void transition_struct_out(int, int)
Definition: misc.c:756
int * chk
Definition: flex.c:91