home *** CD-ROM | disk | FTP | other *** search
/ RISC DISC 2 / RISC_DISC_2.iso / pd_share / program / gnu / flex / c / main < prev    next >
Encoding:
Text File  |  1995-06-03  |  28.3 KB  |  1,187 lines

  1. /* flex - tool to generate fast lexical analyzers */
  2.  
  3. /*-
  4.  * Copyright (c) 1990 The Regents of the University of California.
  5.  * All rights reserved.
  6.  *
  7.  * This code is derived from software contributed to Berkeley by
  8.  * Vern Paxson.
  9.  *
  10.  * The United States Government has rights in this work pursuant
  11.  * to contract no. DE-AC03-76SF00098 between the United States
  12.  * Department of Energy and the University of California.
  13.  *
  14.  * Redistribution and use in source and binary forms are permitted provided
  15.  * that: (1) source distributions retain this entire copyright notice and
  16.  * comment, and (2) distributions including binaries display the following
  17.  * acknowledgement:  ``This product includes software developed by the
  18.  * University of California, Berkeley and its contributors'' in the
  19.  * documentation or other materials provided with the distribution and in
  20.  * all advertising materials mentioning features or use of this software.
  21.  * Neither the name of the University nor the names of its contributors may
  22.  * be used to endorse or promote products derived from this software without
  23.  * specific prior written permission.
  24.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  25.  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  26.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  27.  */
  28.  
  29. #ifndef lint
  30. char copyright[] =
  31. "@(#) Copyright (c) 1990 The Regents of the University of California.\n\
  32.  All rights reserved.\n";
  33. #endif /* not lint */
  34.  
  35. /* $Header: /home/daffy/u0/vern/flex/RCS/main.c,v 2.63 95/04/20 13:53:23 vern Exp $ */
  36.  
  37.  
  38. #include "flexdef.h"
  39. #include "version.h"
  40.  
  41. static char flex_version[] = FLEX_VERSION;
  42.  
  43.  
  44. /* declare functions that have forward references */
  45.  
  46. void flexinit PROTO((int, char**));
  47. void readin PROTO((void));
  48. void set_up_initial_allocations PROTO((void));
  49.  
  50. #ifdef NEED_ARGV_FIXUP
  51. extern void argv_fixup PROTO((int *, char ***));
  52. #endif
  53.  
  54.  
  55. /* these globals are all defined and commented in flexdef.h */
  56. int printstats, syntaxerror, eofseen, ddebug, trace, nowarn, spprdflt;
  57. int interactive, caseins, lex_compat, do_yylineno, useecs, fulltbl, usemecs;
  58. int fullspd, gen_line_dirs, performance_report, backing_up_report;
  59. int C_plus_plus, long_align, use_read, yytext_is_array, do_yywrap, csize;
  60. int yymore_used, reject, real_reject, continued_action, in_rule;
  61. int yymore_really_used, reject_really_used;
  62. int datapos, dataline, linenum, out_linenum;
  63. FILE *skelfile = NULL;
  64. int skel_ind = 0;
  65. char *action_array;
  66. int action_size, defs1_offset, prolog_offset, action_offset, action_index;
  67. char *infilename = NULL, *outfilename = NULL;
  68. int did_outfilename;
  69. char *prefix, *yyclass;
  70. int do_stdinit, use_stdout;
  71. int onestate[ONE_STACK_SIZE], onesym[ONE_STACK_SIZE];
  72. int onenext[ONE_STACK_SIZE], onedef[ONE_STACK_SIZE], onesp;
  73. int current_mns, current_max_rules;
  74. int num_rules, num_eof_rules, default_rule, lastnfa;
  75. int *firstst, *lastst, *finalst, *transchar, *trans1, *trans2;
  76. int *accptnum, *assoc_rule, *state_type;
  77. int *rule_type, *rule_linenum, *rule_useful;
  78. int current_state_type;
  79. int variable_trailing_context_rules;
  80. int numtemps, numprots, protprev[MSP], protnext[MSP], prottbl[MSP];
  81. int protcomst[MSP], firstprot, lastprot, protsave[PROT_SAVE_SIZE];
  82. int numecs, nextecm[CSIZE + 1], ecgroup[CSIZE + 1], nummecs, tecfwd[CSIZE + 1];
  83. int tecbck[CSIZE + 1];
  84. int lastsc, *scset, *scbol, *scxclu, *sceof;
  85. int current_max_scs;
  86. char **scname;
  87. int current_max_dfa_size, current_max_xpairs;
  88. int current_max_template_xpairs, current_max_dfas;
  89. int lastdfa, *nxt, *chk, *tnxt;
  90. int *base, *def, *nultrans, NUL_ec, tblend, firstfree, **dss, *dfasiz;
  91. union dfaacc_union *dfaacc;
  92. int *accsiz, *dhash, numas;
  93. int numsnpairs, jambase, jamstate;
  94. int lastccl, *cclmap, *ccllen, *cclng, cclreuse;
  95. int current_maxccls, current_max_ccl_tbl_size;
  96. Char *ccltbl;
  97. char nmstr[MAXLINE];
  98. int sectnum, nummt, hshcol, dfaeql, numeps, eps2, num_reallocs;
  99. int tmpuses, totnst, peakpairs, numuniq, numdup, hshsave;
  100. int num_backing_up, bol_needed;
  101. FILE *backing_up_file;
  102. int end_of_buffer_state;
  103. char **input_files;
  104. int num_input_files;
  105.  
  106. /* Make sure program_name is initialized so we don't crash if writing
  107.  * out an error message before getting the program name from argv[0].
  108.  */
  109. char *program_name = "flex";
  110.  
  111. #ifndef SHORT_FILE_NAMES
  112. static char *outfile_template = "lex.%s.%s";
  113. static char *backing_name = "lex.backup";
  114. #else
  115. #ifdef __riscos
  116. static char *outfile_template = "%s.lex%s";
  117. static char *backing_name = "bck.lex";
  118. #else
  119. static char *outfile_template = "lex%s.%s";
  120. static char *backing_name = "lex.bck";
  121. #endif
  122. #endif
  123.  
  124. #ifdef THINK_C
  125. #include <console.h>
  126. #endif
  127.  
  128. #ifdef MS_DOS
  129. extern unsigned _stklen = 16384;
  130. #endif
  131.  
  132. static char outfile_path[MAXLINE];
  133. static int outfile_created = 0;
  134. static char *skelname = NULL;
  135.  
  136.  
  137. int main( argc, argv )
  138. int argc;
  139. char **argv;
  140.     {
  141.     int i;
  142.  
  143. #ifdef THINK_C
  144.     argc = ccommand( &argv );
  145. #endif
  146. #ifdef NEED_ARGV_FIXUP
  147.     argv_fixup( &argc, &argv );
  148. #endif
  149.  
  150.     flexinit( argc, argv );
  151.  
  152.     readin();
  153.  
  154.     ntod();
  155.  
  156.     for ( i = 1; i <= num_rules; ++i )
  157.         if ( ! rule_useful[i] && i != default_rule )
  158.             line_warning( _( "rule cannot be matched" ),
  159.                     rule_linenum[i] );
  160.  
  161.     if ( spprdflt && ! reject && rule_useful[default_rule] )
  162.         line_warning(
  163.             _( "-s option given but default rule can be matched" ),
  164.             rule_linenum[default_rule] );
  165.  
  166.     /* Generate the C state transition tables from the DFA. */
  167.     make_tables();
  168.  
  169.     /* Note, flexend does not return.  It exits with its argument
  170.      * as status.
  171.      */
  172.     flexend( 0 );
  173.  
  174.     return 0;    /* keep compilers/lint happy */
  175.     }
  176.  
  177.  
  178. /* check_options - check user-specified options */
  179.  
  180. void check_options()
  181.     {
  182.     int i;
  183.  
  184.     if ( lex_compat )
  185.         {
  186.         if ( C_plus_plus )
  187.             flexerror( _( "Can't use -+ with -l option" ) );
  188.  
  189.         if ( fulltbl || fullspd )
  190.             flexerror( _( "Can't use -f or -F with -l option" ) );
  191.  
  192.         /* Don't rely on detecting use of yymore() and REJECT,
  193.          * just assume they'll be used.
  194.          */
  195.         yymore_really_used = reject_really_used = true;
  196.  
  197.         yytext_is_array = true;
  198.         do_yylineno = true;
  199.         use_read = false;
  200.         }
  201.  
  202.     if ( do_yylineno )
  203.         /* This should really be "maintain_backup_tables = true" */
  204.         reject_really_used = true;
  205.  
  206.     if ( csize == unspecified )
  207.         {
  208.         if ( (fulltbl || fullspd) && ! useecs )
  209.             csize = DEFAULT_CSIZE;
  210.         else
  211.             csize = CSIZE;
  212.         }
  213.  
  214.     if ( interactive == unspecified )
  215.         {
  216.         if ( fulltbl || fullspd )
  217.             interactive = false;
  218.         else
  219.             interactive = true;
  220.         }
  221.  
  222.     if ( fulltbl || fullspd )
  223.         {
  224.         if ( usemecs )
  225.             flexerror(
  226.             _( "-Cf/-CF and -Cm don't make sense together" ) );
  227.  
  228.         if ( interactive )
  229.             flexerror( _( "-Cf/-CF and -I are incompatible" ) );
  230.  
  231.         if ( lex_compat )
  232.             flexerror(
  233.         _( "-Cf/-CF are incompatible with lex-compatibility mode" ) );
  234.  
  235.         if ( do_yylineno )
  236.             flexerror(
  237.             _( "-Cf/-CF and %option yylineno are incompatible" ) );
  238.  
  239.         if ( fulltbl && fullspd )
  240.             flexerror( _( "-Cf and -CF are mutually exclusive" ) );
  241.         }
  242.  
  243.     if ( C_plus_plus && fullspd )
  244.         flexerror( _( "Can't use -+ with -CF option" ) );
  245.  
  246.     if ( C_plus_plus && yytext_is_array )
  247.         {
  248.         warn( _( "%array incompatible with -+ option" ) );
  249.         yytext_is_array = false;
  250.         }
  251.  
  252.     if ( useecs )
  253.         { /* Set up doubly-linked equivalence classes. */
  254.  
  255.         /* We loop all the way up to csize, since ecgroup[csize] is
  256.          * the position used for NUL characters.
  257.          */
  258.         ecgroup[1] = NIL;
  259.  
  260.         for ( i = 2; i <= csize; ++i )
  261.             {
  262.             ecgroup[i] = i - 1;
  263.             nextecm[i - 1] = i;
  264.             }
  265.  
  266.         nextecm[csize] = NIL;
  267.         }
  268.  
  269.     else
  270.         {
  271.         /* Put everything in its own equivalence class. */
  272.         for ( i = 1; i <= csize; ++i )
  273.             {
  274.             ecgroup[i] = i;
  275.             nextecm[i] = BAD_SUBSCRIPT;    /* to catch errors */
  276.             }
  277.         }
  278.  
  279.     if ( ! use_stdout )
  280.         {
  281.         FILE *prev_stdout;
  282.  
  283.         if ( ! did_outfilename )
  284.             {
  285.             char *suffix;
  286.  
  287.             if ( C_plus_plus )
  288.                 suffix = "cc";
  289.             else
  290.                 suffix = "c";
  291.  
  292. #ifdef __riscos
  293.                 sprintf( outfile_path, outfile_template,
  294.                         suffix, prefix );
  295. #else
  296.             sprintf( outfile_path, outfile_template,
  297.                 prefix, suffix );
  298. #endif
  299.             outfilename = outfile_path;
  300.             }
  301.  
  302.         prev_stdout = freopen( outfilename, "w", stdout );
  303.  
  304.         if ( prev_stdout == NULL )
  305.             lerrsf( _( "could not create %s" ), outfilename );
  306.  
  307.         outfile_created = 1;
  308.         }
  309.  
  310.     if ( skelname && (skelfile = fopen( skelname, "r" )) == NULL )
  311.         lerrsf( _( "can't open skeleton file %s" ), skelname );
  312.  
  313.     if ( strcmp( prefix, "yy" ) )
  314.         {
  315. #define GEN_PREFIX(name) out_str3( "#define yy%s %s%s\n", name, prefix, name )
  316.         if ( C_plus_plus )
  317.             GEN_PREFIX( "FlexLexer" );
  318.         else
  319.             {
  320.             GEN_PREFIX( "_create_buffer" );
  321.             GEN_PREFIX( "_delete_buffer" );
  322.             GEN_PREFIX( "_scan_buffer" );
  323.             GEN_PREFIX( "_scan_string" );
  324.             GEN_PREFIX( "_scan_bytes" );
  325.             GEN_PREFIX( "_flex_debug" );
  326.             GEN_PREFIX( "_init_buffer" );
  327.             GEN_PREFIX( "_flush_buffer" );
  328.             GEN_PREFIX( "_load_buffer_state" );
  329.             GEN_PREFIX( "_switch_to_buffer" );
  330.             GEN_PREFIX( "in" );
  331.             GEN_PREFIX( "leng" );
  332.             GEN_PREFIX( "lex" );
  333.             GEN_PREFIX( "out" );
  334.             GEN_PREFIX( "restart" );
  335.             GEN_PREFIX( "text" );
  336.  
  337.             if ( do_yylineno )
  338.                 GEN_PREFIX( "lineno" );
  339.             }
  340.  
  341.         if ( do_yywrap )
  342.             GEN_PREFIX( "wrap" );
  343.  
  344.         outn( "" );
  345.         }
  346.  
  347.     if ( did_outfilename )
  348.         line_directive_out( stdout, 0 );
  349.  
  350.     skelout();
  351.     }
  352.  
  353.  
  354. /* flexend - terminate flex
  355.  *
  356.  * note
  357.  *    This routine does not return.
  358.  */
  359.  
  360. void flexend( exit_status )
  361. int exit_status;
  362.  
  363.     {
  364.     int tblsiz;
  365.     int unlink();
  366.  
  367.     if ( skelfile != NULL )
  368.         {
  369.         if ( ferror( skelfile ) )
  370.             lerrsf( _( "input error reading skeleton file %s" ),
  371.                 skelname );
  372.  
  373.         else if ( fclose( skelfile ) )
  374.             lerrsf( _( "error closing skeleton file %s" ),
  375.                 skelname );
  376.         }
  377.  
  378.     if ( exit_status != 0 && outfile_created )
  379.         {
  380.         if ( ferror( stdout ) )
  381.             lerrsf( _( "error writing output file %s" ),
  382.                 outfilename );
  383.  
  384.         else if ( fclose( stdout ) )
  385.             lerrsf( _( "error closing output file %s" ),
  386.                 outfilename );
  387.  
  388.         else if ( unlink( outfilename ) )
  389.             lerrsf( _( "error deleting output file %s" ),
  390.                 outfilename );
  391.         }
  392.  
  393.     if ( backing_up_report && backing_up_file )
  394.         {
  395.         if ( num_backing_up == 0 )
  396.             fprintf( backing_up_file, _( "No backing up.\n" ) );
  397.         else if ( fullspd || fulltbl )
  398.             fprintf( backing_up_file,
  399.                 _( "%d backing up (non-accepting) states.\n" ),
  400.                 num_backing_up );
  401.         else
  402.             fprintf( backing_up_file,
  403.                 _( "Compressed tables always back up.\n" ) );
  404.  
  405.         if ( ferror( backing_up_file ) )
  406.             lerrsf( _( "error writing backup file %s" ),
  407.                 backing_name );
  408.  
  409.         else if ( fclose( backing_up_file ) )
  410.             lerrsf( _( "error closing backup file %s" ),
  411.                 backing_name );
  412.         }
  413.  
  414.     if ( printstats )
  415.         {
  416.         fprintf( stderr, _( "%s version %s usage statistics:\n" ),
  417.             program_name, flex_version );
  418.  
  419.         fprintf( stderr, _( "  scanner options: -" ) );
  420.  
  421.         if ( C_plus_plus )
  422.             putc( '+', stderr );
  423.         if ( backing_up_report )
  424.             putc( 'b', stderr );
  425.         if ( ddebug )
  426.             putc( 'd', stderr );
  427.         if ( caseins )
  428.             putc( 'i', stderr );
  429.         if ( lex_compat )
  430.             putc( 'l', stderr );
  431.         if ( performance_report > 0 )
  432.             putc( 'p', stderr );
  433.         if ( performance_report > 1 )
  434.             putc( 'p', stderr );
  435.         if ( spprdflt )
  436.             putc( 's', stderr );
  437.         if ( use_stdout )
  438.             putc( 't', stderr );
  439.         if ( printstats )
  440.             putc( 'v', stderr );    /* always true! */
  441.         if ( nowarn )
  442.             putc( 'w', stderr );
  443.         if ( interactive == false )
  444.             putc( 'B', stderr );
  445.         if ( interactive == true )
  446.             putc( 'I', stderr );
  447.         if ( ! gen_line_dirs )
  448.             putc( 'L', stderr );
  449.         if ( trace )
  450.             putc( 'T', stderr );
  451.  
  452.         if ( csize == unspecified )
  453.             /* We encountered an error fairly early on, so csize
  454.              * never got specified.  Define it now, to prevent
  455.              * bogus table sizes being written out below.
  456.              */
  457.             csize = 256;
  458.  
  459.         if ( csize == 128 )
  460.             putc( '7', stderr );
  461.         else
  462.             putc( '8', stderr );
  463.  
  464.         fprintf( stderr, " -C" );
  465.  
  466.         if ( long_align )
  467.             putc( 'a', stderr );
  468.         if ( fulltbl )
  469.             putc( 'f', stderr );
  470.         if ( fullspd )
  471.             putc( 'F', stderr );
  472.         if ( useecs )
  473.             putc( 'e', stderr );
  474.         if ( usemecs )
  475.             putc( 'm', stderr );
  476.         if ( use_read )
  477.             putc( 'r', stderr );
  478.  
  479.         if ( did_outfilename )
  480.             fprintf( stderr, " -o%s", outfilename );
  481.  
  482.         if ( skelname )
  483.             fprintf( stderr, " -S%s", skelname );
  484.  
  485.         if ( strcmp( prefix, "yy" ) )
  486.             fprintf( stderr, " -P%s", prefix );
  487.  
  488.         putc( '\n', stderr );
  489.  
  490.         fprintf( stderr, _( "  %d/%d NFA states\n" ),
  491.             lastnfa, current_mns );
  492.         fprintf( stderr, _( "  %d/%d DFA states (%d words)\n" ),
  493.             lastdfa, current_max_dfas, totnst );
  494.         fprintf( stderr, _( "  %d rules\n" ),
  495.         num_rules + num_eof_rules - 1 /* - 1 for def. rule */ );
  496.  
  497.         if ( num_backing_up == 0 )
  498.             fprintf( stderr, _( "  No backing up\n" ) );
  499.         else if ( fullspd || fulltbl )
  500.             fprintf( stderr,
  501.             _( "  %d backing-up (non-accepting) states\n" ),
  502.                 num_backing_up );
  503.         else
  504.             fprintf( stderr,
  505.                 _( "  Compressed tables always back-up\n" ) );
  506.  
  507.         if ( bol_needed )
  508.             fprintf( stderr,
  509.                 _( "  Beginning-of-line patterns used\n" ) );
  510.  
  511.         fprintf( stderr, _( "  %d/%d start conditions\n" ), lastsc,
  512.             current_max_scs );
  513.         fprintf( stderr,
  514.             _( "  %d epsilon states, %d double epsilon states\n" ),
  515.             numeps, eps2 );
  516.  
  517.         if ( lastccl == 0 )
  518.             fprintf( stderr, _( "  no character classes\n" ) );
  519.         else
  520.             fprintf( stderr,
  521. _( "  %d/%d character classes needed %d/%d words of storage, %d reused\n" ),
  522.                 lastccl, current_maxccls,
  523.                 cclmap[lastccl] + ccllen[lastccl],
  524.                 current_max_ccl_tbl_size, cclreuse );
  525.  
  526.         fprintf( stderr, _( "  %d state/nextstate pairs created\n" ),
  527.             numsnpairs );
  528.         fprintf( stderr, _( "  %d/%d unique/duplicate transitions\n" ),
  529.             numuniq, numdup );
  530.  
  531.         if ( fulltbl )
  532.             {
  533.             tblsiz = lastdfa * numecs;
  534.             fprintf( stderr, _( "  %d table entries\n" ), tblsiz );
  535.             }
  536.  
  537.         else
  538.             {
  539.             tblsiz = 2 * (lastdfa + numtemps) + 2 * tblend;
  540.  
  541.             fprintf( stderr,
  542.                 _( "  %d/%d base-def entries created\n" ),
  543.                 lastdfa + numtemps, current_max_dfas );
  544.             fprintf( stderr,
  545.             _( "  %d/%d (peak %d) nxt-chk entries created\n" ),
  546.                 tblend, current_max_xpairs, peakpairs );
  547.             fprintf( stderr,
  548.         _( "  %d/%d (peak %d) template nxt-chk entries created\n" ),
  549.                 numtemps * nummecs,
  550.                 current_max_template_xpairs,
  551.                 numtemps * numecs );
  552.             fprintf( stderr, _( "  %d empty table entries\n" ),
  553.                 nummt );
  554.             fprintf( stderr, _( "  %d protos created\n" ),
  555.                 numprots );
  556.             fprintf( stderr,
  557.                 _( "  %d templates created, %d uses\n" ),
  558.                 numtemps, tmpuses );
  559.             }
  560.  
  561.         if ( useecs )
  562.             {
  563.             tblsiz = tblsiz + csize;
  564.             fprintf( stderr,
  565.                 _( "  %d/%d equivalence classes created\n" ),
  566.                 numecs, csize );
  567.             }
  568.  
  569.         if ( usemecs )
  570.             {
  571.             tblsiz = tblsiz + numecs;
  572.             fprintf( stderr,
  573.             _( "  %d/%d meta-equivalence classes created\n" ),
  574.                 nummecs, csize );
  575.             }
  576.  
  577.         fprintf( stderr,
  578.         _( "  %d (%d saved) hash collisions, %d DFAs equal\n" ),
  579.             hshcol, hshsave, dfaeql );
  580.         fprintf( stderr, _( "  %d sets of reallocations needed\n" ),
  581.             num_reallocs );
  582.         fprintf( stderr, _( "  %d total table entries needed\n" ),
  583.             tblsiz );
  584.         }
  585.  
  586.     exit( exit_status );
  587.     }
  588.  
  589.  
  590. /* flexinit - initialize flex */
  591.  
  592. void flexinit( argc, argv )
  593. int argc;
  594. char **argv;
  595.     {
  596.     int i, sawcmpflag;
  597.     char *arg, *mktemp();
  598.  
  599.     printstats = syntaxerror = trace = spprdflt = caseins = false;
  600.     lex_compat = C_plus_plus = backing_up_report = ddebug = fulltbl = false;
  601.     fullspd = long_align = nowarn = yymore_used = continued_action = false;
  602.     do_yylineno = yytext_is_array = in_rule = reject = do_stdinit = false;
  603.     yymore_really_used = reject_really_used = unspecified;
  604.     interactive = csize = unspecified;
  605.     do_yywrap = gen_line_dirs = usemecs = useecs = true;
  606.     performance_report = 0;
  607.     did_outfilename = 0;
  608.     prefix = "yy";
  609.     yyclass = 0;
  610.     use_read = use_stdout = false;
  611.  
  612.     sawcmpflag = false;
  613.  
  614.     /* Initialize dynamic array for holding the rule actions. */
  615.     action_size = 2048;    /* default size of action array in bytes */
  616.     action_array = allocate_character_array( action_size );
  617.     defs1_offset = prolog_offset = action_offset = action_index = 0;
  618.     action_array[0] = '\0';
  619.  
  620.     program_name = argv[0];
  621.  
  622.     if ( program_name[0] != '\0' &&
  623.          program_name[strlen( program_name ) - 1] == '+' )
  624.         C_plus_plus = true;
  625.  
  626.     /* read flags */
  627.     for ( --argc, ++argv; argc ; --argc, ++argv )
  628.         {
  629.         arg = argv[0];
  630.  
  631.         if ( arg[0] != '-' || arg[1] == '\0' )
  632.             break;
  633.  
  634.         if ( arg[1] == '-' )
  635.             { /* --option */
  636.             if ( ! strcmp( arg, "--help" ) )
  637.                 arg = "-h";
  638.  
  639.             else if ( ! strcmp( arg, "--version" ) )
  640.                 arg = "-V";
  641.  
  642.             else if ( ! strcmp( arg, "--" ) )
  643.                 { /* end of options */
  644.                 --argc;
  645.                 ++argv;
  646.                 break;
  647.                 }
  648.             }
  649.  
  650.         for ( i = 1; arg[i] != '\0'; ++i )
  651.             switch ( arg[i] )
  652.                 {
  653.                 case '+':
  654.                     C_plus_plus = true;
  655.                     break;
  656.  
  657.                 case 'B':
  658.                     interactive = false;
  659.                     break;
  660.  
  661.                 case 'b':
  662.                     backing_up_report = true;
  663.                     break;
  664.  
  665.                 case 'c':
  666.                     break;
  667.  
  668.                 case 'C':
  669.                     if ( i != 1 )
  670.                         flexerror(
  671.                 _( "-C flag must be given separately" ) );
  672.  
  673.                     if ( ! sawcmpflag )
  674.                         {
  675.                         useecs = false;
  676.                         usemecs = false;
  677.                         fulltbl = false;
  678.                         sawcmpflag = true;
  679.                         }
  680.  
  681.                     for ( ++i; arg[i] != '\0'; ++i )
  682.                         switch ( arg[i] )
  683.                             {
  684.                             case 'a':
  685.                                 long_align =
  686.                                     true;
  687.                                 break;
  688.  
  689.                             case 'e':
  690.                                 useecs = true;
  691.                                 break;
  692.  
  693.                             case 'F':
  694.                                 fullspd = true;
  695.                                 break;
  696.  
  697.                             case 'f':
  698.                                 fulltbl = true;
  699.                                 break;
  700.  
  701.                             case 'm':
  702.                                 usemecs = true;
  703.                                 break;
  704.  
  705.                             case 'r':
  706.                                 use_read = true;
  707.                                 break;
  708.  
  709.                             default:
  710.                                 lerrif(
  711.                         _( "unknown -C option '%c'" ),
  712.                                 (int) arg[i] );
  713.                                 break;
  714.                             }
  715.  
  716.                     goto get_next_arg;
  717.  
  718.                 case 'd':
  719.                     ddebug = true;
  720.                     break;
  721.  
  722.                 case 'f':
  723.                     useecs = usemecs = false;
  724.                     use_read = fulltbl = true;
  725.                     break;
  726.  
  727.                 case 'F':
  728.                     useecs = usemecs = false;
  729.                     use_read = fullspd = true;
  730.                     break;
  731.  
  732.                 case '?':
  733.                 case 'h':
  734.                     usage();
  735.                     exit( 0 );
  736.  
  737.                 case 'I':
  738.                     interactive = true;
  739.                     break;
  740.  
  741.                 case 'i':
  742.                     caseins = true;
  743.                     break;
  744.  
  745.                 case 'l':
  746.                     lex_compat = true;
  747.                     break;
  748.  
  749.                 case 'L':
  750.                     gen_line_dirs = false;
  751.                     break;
  752.  
  753.                 case 'n':
  754.                     /* Stupid do-nothing deprecated
  755.                      * option.
  756.                      */
  757.                     break;
  758.  
  759.                 case 'o':
  760.                     if ( i != 1 )
  761.                         flexerror(
  762.                 _( "-o flag must be given separately" ) );
  763.  
  764.                     outfilename = arg + i + 1;
  765.                     did_outfilename = 1;
  766.                     goto get_next_arg;
  767.  
  768.                 case 'P':
  769.                     if ( i != 1 )
  770.                         flexerror(
  771.                 _( "-P flag must be given separately" ) );
  772.  
  773.                     prefix = arg + i + 1;
  774.                     goto get_next_arg;
  775.  
  776.                 case 'p':
  777.                     ++performance_report;
  778.                     break;
  779.  
  780.                 case 'S':
  781.                     if ( i != 1 )
  782.                         flexerror(
  783.                 _( "-S flag must be given separately" ) );
  784.  
  785.                     skelname = arg + i + 1;
  786.                     goto get_next_arg;
  787.  
  788.                 case 's':
  789.                     spprdflt = true;
  790.                     break;
  791.  
  792.                 case 't':
  793.                     use_stdout = true;
  794.                     break;
  795.  
  796.                 case 'T':
  797.                     trace = true;
  798.                     break;
  799.  
  800.                 case 'v':
  801.                     printstats = true;
  802.                     break;
  803.  
  804.                 case 'V':
  805.                     printf( _( "%s version %s\n" ),
  806.                         program_name, flex_version );
  807.                     exit( 0 );
  808.  
  809.                 case 'w':
  810.                     nowarn = true;
  811.                     break;
  812.  
  813.                 case '7':
  814.                     csize = 128;
  815.                     break;
  816.  
  817.                 case '8':
  818.                     csize = CSIZE;
  819.                     break;
  820.  
  821.                 default:
  822.                     fprintf( stderr,
  823.         _( "%s: unknown flag '%c'.  For usage, try\n\t%s --help\n" ),
  824.                         program_name, (int) arg[i],
  825.                         program_name );
  826.                     exit( 1 );
  827.                 }
  828.  
  829.         /* Used by -C, -S, -o, and -P flags in lieu of a "continue 2"
  830.          * control.
  831.          */
  832.         get_next_arg: ;
  833.         }
  834.  
  835.     num_input_files = argc;
  836.     input_files = argv;
  837.     set_input_file( num_input_files > 0 ? input_files[0] : NULL );
  838.  
  839.     lastccl = lastsc = lastdfa = lastnfa = 0;
  840.     num_rules = num_eof_rules = default_rule = 0;
  841.     numas = numsnpairs = tmpuses = 0;
  842.     numecs = numeps = eps2 = num_reallocs = hshcol = dfaeql = totnst = 0;
  843.     numuniq = numdup = hshsave = eofseen = datapos = dataline = 0;
  844.     num_backing_up = onesp = numprots = 0;
  845.     variable_trailing_context_rules = bol_needed = false;
  846.  
  847.     out_linenum = linenum = sectnum = 1;
  848.     firstprot = NIL;
  849.  
  850.     /* Used in mkprot() so that the first proto goes in slot 1
  851.      * of the proto queue.
  852.      */
  853.     lastprot = 1;
  854.  
  855.     set_up_initial_allocations();
  856.     }
  857.  
  858.  
  859. /* readin - read in the rules section of the input file(s) */
  860.  
  861. void readin()
  862.     {
  863.     static char yy_stdinit[] = "FILE *yyin = stdin, *yyout = stdout;";
  864.     static char yy_nostdinit[] =
  865.         "FILE *yyin = (FILE *) 0, *yyout = (FILE *) 0;";
  866.  
  867.     line_directive_out( (FILE *) 0, 1 );
  868.  
  869.     if ( yyparse() )
  870.         {
  871.         pinpoint_message( _( "fatal parse error" ) );
  872.         flexend( 1 );
  873.         }
  874.  
  875.     if ( syntaxerror )
  876.         flexend( 1 );
  877.  
  878.     if ( backing_up_report )
  879.         {
  880.         backing_up_file = fopen( backing_name, "w" );
  881.         if ( backing_up_file == NULL )
  882.             lerrsf(
  883.             _( "could not create backing-up info file %s" ),
  884.                 backing_name );
  885.         }
  886.  
  887.     else
  888.         backing_up_file = NULL;
  889.  
  890.     if ( yymore_really_used == true )
  891.         yymore_used = true;
  892.     else if ( yymore_really_used == false )
  893.         yymore_used = false;
  894.  
  895.     if ( reject_really_used == true )
  896.         reject = true;
  897.     else if ( reject_really_used == false )
  898.         reject = false;
  899.  
  900.     if ( performance_report > 0 )
  901.         {
  902.         if ( lex_compat )
  903.             {
  904.             fprintf( stderr,
  905. _( "-l AT&T lex compatibility option entails a large performance penalty\n" ) );
  906.             fprintf( stderr,
  907. _( " and may be the actual source of other reported performance penalties\n" ) );
  908.             }
  909.  
  910.         else if ( do_yylineno )
  911.             {
  912.             fprintf( stderr,
  913.     _( "%%option yylineno entails a large performance penalty\n" ) );
  914.             }
  915.  
  916.         if ( performance_report > 1 )
  917.             {
  918.             if ( interactive )
  919.                 fprintf( stderr,
  920.     _( "-I (interactive) entails a minor performance penalty\n" ) );
  921.  
  922.             if ( yymore_used )
  923.                 fprintf( stderr,
  924.         _( "yymore() entails a minor performance penalty\n" ) );
  925.             }
  926.  
  927.         if ( reject )
  928.             fprintf( stderr,
  929.             _( "REJECT entails a large performance penalty\n" ) );
  930.  
  931.         if ( variable_trailing_context_rules )
  932.             fprintf( stderr,
  933. _( "Variable trailing context rules entail a large performance penalty\n" ) );
  934.         }
  935.  
  936.     if ( reject )
  937.         real_reject = true;
  938.  
  939.     if ( variable_trailing_context_rules )
  940.         reject = true;
  941.  
  942.     if ( (fulltbl || fullspd) && reject )
  943.         {
  944.         if ( real_reject )
  945.             flexerror(
  946.                 _( "REJECT cannot be used with -f or -F" ) );
  947.         else if ( do_yylineno )
  948.             flexerror(
  949.             _( "%option yylineno cannot be used with -f or -F" ) );
  950.         else
  951.             flexerror(
  952.     _( "variable trailing context rules cannot be used with -f or -F" ) );
  953.         }
  954.  
  955.     if ( reject )
  956.         outn( "\n#define YY_USES_REJECT" );
  957.  
  958.     if ( ! do_yywrap )
  959.         {
  960.         outn( "\n#define yywrap() 1" );
  961.         outn( "#define YY_SKIP_YYWRAP" );
  962.         }
  963.  
  964.     if ( ddebug )
  965.         outn( "\n#define FLEX_DEBUG" );
  966.  
  967.     if ( csize == 256 )
  968.         outn( "typedef unsigned char YY_CHAR;" );
  969.     else
  970.         outn( "typedef char YY_CHAR;" );
  971.  
  972.     if ( C_plus_plus )
  973.         {
  974.         outn( "#define yytext_ptr yytext" );
  975.  
  976.         if ( interactive )
  977.             outn( "#define YY_INTERACTIVE" );
  978.         }
  979.  
  980.     else
  981.         {
  982.         if ( do_stdinit )
  983.             {
  984.             outn( "#ifdef VMS" );
  985.             outn( "#ifndef __VMS_POSIX" );
  986.             outn( yy_nostdinit );
  987.             outn( "#else" );
  988.             outn( yy_stdinit );
  989.             outn( "#endif" );
  990.             outn( "#else" );
  991.             outn( yy_stdinit );
  992.             outn( "#endif" );
  993.             }
  994.  
  995.         else
  996.             outn( yy_nostdinit );
  997.         }
  998.  
  999.     if ( fullspd )
  1000.         outn( "typedef yyconst struct yy_trans_info *yy_state_type;" );
  1001.     else if ( ! C_plus_plus )
  1002.         outn( "typedef int yy_state_type;" );
  1003.  
  1004.     if ( ddebug )
  1005.         outn( "\n#define FLEX_DEBUG" );
  1006.  
  1007.     if ( lex_compat )
  1008.         outn( "#define YY_FLEX_LEX_COMPAT" );
  1009.  
  1010.     if ( do_yylineno && ! C_plus_plus )
  1011.         {
  1012.         outn( "extern int yylineno;" );
  1013.         outn( "int yylineno = 1;" );
  1014.         }
  1015.  
  1016.     if ( C_plus_plus )
  1017.         {
  1018.         outn( "\n#include <FlexLexer.h>" );
  1019.  
  1020.         if ( yyclass )
  1021.             {
  1022.             outn( "int yyFlexLexer::yylex()" );
  1023.             outn( "\t{" );
  1024.             outn(
  1025. "\tLexerError( \"yyFlexLexer::yylex invoked but %option yyclass used\" );" );
  1026.             outn( "\treturn 0;" );
  1027.             outn( "\t}" );
  1028.  
  1029.             out_str( "\n#define YY_DECL int %s::yylex()\n",
  1030.                 yyclass );
  1031.             }
  1032.         }
  1033.  
  1034.     else
  1035.         {
  1036.         if ( yytext_is_array )
  1037.             outn( "extern char yytext[];\n" );
  1038.  
  1039.         else
  1040.             {
  1041.             outn( "extern char *yytext;" );
  1042.             outn( "#define yytext_ptr yytext" );
  1043.             }
  1044.  
  1045.         if ( yyclass )
  1046.             flexerror(
  1047.         _( "%option yyclass only meaningful for C++ scanners" ) );
  1048.         }
  1049.  
  1050.     if ( useecs )
  1051.         numecs = cre8ecs( nextecm, ecgroup, csize );
  1052.     else
  1053.         numecs = csize;
  1054.  
  1055.     /* Now map the equivalence class for NUL to its expected place. */
  1056.     ecgroup[0] = ecgroup[csize];
  1057.     NUL_ec = ABS( ecgroup[0] );
  1058.  
  1059.     if ( useecs )
  1060.         ccl2ecl();
  1061.     }
  1062.  
  1063.  
  1064. /* set_up_initial_allocations - allocate memory for internal tables */
  1065.  
  1066. void set_up_initial_allocations()
  1067.     {
  1068.     current_mns = INITIAL_MNS;
  1069.     firstst = allocate_integer_array( current_mns );
  1070.     lastst = allocate_integer_array( current_mns );
  1071.     finalst = allocate_integer_array( current_mns );
  1072.     transchar = allocate_integer_array( current_mns );
  1073.     trans1 = allocate_integer_array( current_mns );
  1074.     trans2 = allocate_integer_array( current_mns );
  1075.     accptnum = allocate_integer_array( current_mns );
  1076.     assoc_rule = allocate_integer_array( current_mns );
  1077.     state_type = allocate_integer_array( current_mns );
  1078.  
  1079.     current_max_rules = INITIAL_MAX_RULES;
  1080.     rule_type = allocate_integer_array( current_max_rules );
  1081.     rule_linenum = allocate_integer_array( current_max_rules );
  1082.     rule_useful = allocate_integer_array( current_max_rules );
  1083.  
  1084.     current_max_scs = INITIAL_MAX_SCS;
  1085.     scset = allocate_integer_array( current_max_scs );
  1086.     scbol = allocate_integer_array( current_max_scs );
  1087.     scxclu = allocate_integer_array( current_max_scs );
  1088.     sceof = allocate_integer_array( current_max_scs );
  1089.     scname = allocate_char_ptr_array( current_max_scs );
  1090.  
  1091.     current_maxccls = INITIAL_MAX_CCLS;
  1092.     cclmap = allocate_integer_array( current_maxccls );
  1093.     ccllen = allocate_integer_array( current_maxccls );
  1094.     cclng = allocate_integer_array( current_maxccls );
  1095.  
  1096.     current_max_ccl_tbl_size = INITIAL_MAX_CCL_TBL_SIZE;
  1097.     ccltbl = allocate_Character_array( current_max_ccl_tbl_size );
  1098.  
  1099.     current_max_dfa_size = INITIAL_MAX_DFA_SIZE;
  1100.  
  1101.     current_max_xpairs = INITIAL_MAX_XPAIRS;
  1102.     nxt = allocate_integer_array( current_max_xpairs );
  1103.     chk = allocate_integer_array( current_max_xpairs );
  1104.  
  1105.     current_max_template_xpairs = INITIAL_MAX_TEMPLATE_XPAIRS;
  1106.     tnxt = allocate_integer_array( current_max_template_xpairs );
  1107.  
  1108.     current_max_dfas = INITIAL_MAX_DFAS;
  1109.     base = allocate_integer_array( current_max_dfas );
  1110.     def = allocate_integer_array( current_max_dfas );
  1111.     dfasiz = allocate_integer_array( current_max_dfas );
  1112.     accsiz = allocate_integer_array( current_max_dfas );
  1113.     dhash = allocate_integer_array( current_max_dfas );
  1114.     dss = allocate_int_ptr_array( current_max_dfas );
  1115.     dfaacc = allocate_dfaacc_union( current_max_dfas );
  1116.  
  1117.     nultrans = (int *) 0;
  1118.     }
  1119.  
  1120.  
  1121. void usage()
  1122.     {
  1123.     FILE *f = stdout;
  1124.  
  1125.     fprintf( f,
  1126. _( "%s [-bcdfhilnpstvwBFILTV78+? -C[aefFmr] -ooutput -Pprefix -Sskeleton]\n" ),
  1127.         program_name );
  1128.     fprintf( f, _( "\t[--help --version] [file ...]\n" ) );
  1129.  
  1130.     fprintf( f, _( "\t-b  generate backing-up information to %s\n" ),
  1131.         backing_name );
  1132.     fprintf( f, _( "\t-c  do-nothing POSIX option\n" ) );
  1133.     fprintf( f, _( "\t-d  turn on debug mode in generated scanner\n" ) );
  1134.     fprintf( f, _( "\t-f  generate fast, large scanner\n" ) );
  1135.     fprintf( f, _( "\t-h  produce this help message\n" ) );
  1136.     fprintf( f, _( "\t-i  generate case-insensitive scanner\n" ) );
  1137.     fprintf( f, _( "\t-l  maximal compatibility with original lex\n" ) );
  1138.     fprintf( f, _( "\t-n  do-nothing POSIX option\n" ) );
  1139.     fprintf( f, _( "\t-p  generate performance report to stderr\n" ) );
  1140.     fprintf( f,
  1141.         _( "\t-s  suppress default rule to ECHO unmatched text\n" ) );
  1142.  
  1143.     if ( ! did_outfilename )
  1144.         {
  1145.         sprintf( outfile_path, outfile_template,
  1146.             prefix, C_plus_plus ? "cc" : "c" );
  1147.         outfilename = outfile_path;
  1148.         }
  1149.  
  1150.     fprintf( f,
  1151.         _( "\t-t  write generated scanner on stdout instead of %s\n" ),
  1152.         outfilename );
  1153.  
  1154.     fprintf( f,
  1155.         _( "\t-v  write summary of scanner statistics to f\n" ) );
  1156.     fprintf( f, _( "\t-w  do not generate warnings\n" ) );
  1157.     fprintf( f, _( "\t-B  generate batch scanner (opposite of -I)\n" ) );
  1158.     fprintf( f,
  1159.         _( "\t-F  use alternative fast scanner representation\n" ) );
  1160.     fprintf( f,
  1161.         _( "\t-I  generate interactive scanner (opposite of -B)\n" ) );
  1162.     fprintf( f, _( "\t-L  suppress #line directives in scanner\n" ) );
  1163.     fprintf( f, _( "\t-T  %s should run in trace mode\n" ), program_name );
  1164.     fprintf( f, _( "\t-V  report %s version\n" ), program_name );
  1165.     fprintf( f, _( "\t-7  generate 7-bit scanner\n" ) );
  1166.     fprintf( f, _( "\t-8  generate 8-bit scanner\n" ) );
  1167.     fprintf( f, _( "\t-+  generate C++ scanner class\n" ) );
  1168.     fprintf( f, _( "\t-?  produce this help message\n" ) );
  1169.     fprintf( f,
  1170. _( "\t-C  specify degree of table compression (default is -Cem):\n" ) );
  1171.     fprintf( f,
  1172. _( "\t\t-Ca  trade off larger tables for better memory alignment\n" ) );
  1173.     fprintf( f, _( "\t\t-Ce  construct equivalence classes\n" ) );
  1174.     fprintf( f,
  1175. _( "\t\t-Cf  do not compress scanner tables; use -f representation\n" ) );
  1176.     fprintf( f,
  1177. _( "\t\t-CF  do not compress scanner tables; use -F representation\n" ) );
  1178.     fprintf( f, _( "\t\t-Cm  construct meta-equivalence classes\n" ) );
  1179.     fprintf( f,
  1180.     _( "\t\t-Cr  use read() instead of stdio for scanner input\n" ) );
  1181.     fprintf( f, _( "\t-o  specify output filename\n" ) );
  1182.     fprintf( f, _( "\t-P  specify scanner prefix other than \"yy\"\n" ) );
  1183.     fprintf( f, _( "\t-S  specify skeleton file\n" ) );
  1184.     fprintf( f, _( "\t--help     produce this help message\n" ) );
  1185.     fprintf( f, _( "\t--version  report %s version\n" ), program_name );
  1186.     }
  1187.