home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 5 / FreshFish_July-August1994.bin / bbs / gnu / flex-2.4.6-src.lha / src / amiga / flex-2.4.6 / main.c < prev    next >
C/C++ Source or Header  |  1994-01-04  |  25KB  |  990 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: main.c,v 1.2 94/01/04 14:33:11 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.  
  51. /* these globals are all defined and commented in flexdef.h */
  52. int printstats, syntaxerror, eofseen, ddebug, trace, nowarn, spprdflt;
  53. int interactive, caseins, lex_compat, useecs, fulltbl, usemecs;
  54. int fullspd, gen_line_dirs, performance_report, backing_up_report;
  55. int C_plus_plus, long_align, use_read, yytext_is_array, csize;
  56. int yymore_used, reject, real_reject, continued_action;
  57. int yymore_really_used, reject_really_used;
  58. int datapos, dataline, linenum;
  59. FILE *skelfile = NULL;
  60. int skel_ind = 0;
  61. char *action_array;
  62. int action_size, defs1_offset, prolog_offset, action_offset, action_index;
  63. char *infilename = NULL;
  64. int onestate[ONE_STACK_SIZE], onesym[ONE_STACK_SIZE];
  65. int onenext[ONE_STACK_SIZE], onedef[ONE_STACK_SIZE], onesp;
  66. int current_mns, num_rules, num_eof_rules, default_rule;
  67. int current_max_rules, lastnfa;
  68. int *firstst, *lastst, *finalst, *transchar, *trans1, *trans2;
  69. int *accptnum, *assoc_rule, *state_type;
  70. int *rule_type, *rule_linenum, *rule_useful;
  71. int current_state_type;
  72. int variable_trailing_context_rules;
  73. int numtemps, numprots, protprev[MSP], protnext[MSP], prottbl[MSP];
  74. int protcomst[MSP], firstprot, lastprot, protsave[PROT_SAVE_SIZE];
  75. int numecs, nextecm[CSIZE + 1], ecgroup[CSIZE + 1], nummecs, tecfwd[CSIZE + 1];
  76. int tecbck[CSIZE + 1];
  77. int lastsc, current_max_scs, *scset, *scbol, *scxclu, *sceof, *actvsc;
  78. char **scname;
  79. int current_max_dfa_size, current_max_xpairs;
  80. int current_max_template_xpairs, current_max_dfas;
  81. int lastdfa, *nxt, *chk, *tnxt;
  82. int *base, *def, *nultrans, NUL_ec, tblend, firstfree, **dss, *dfasiz;
  83. union dfaacc_union *dfaacc;
  84. int *accsiz, *dhash, numas;
  85. int numsnpairs, jambase, jamstate;
  86. int lastccl, current_maxccls, *cclmap, *ccllen, *cclng, cclreuse;
  87. int current_max_ccl_tbl_size;
  88. Char *ccltbl;
  89. char nmstr[MAXLINE];
  90. int sectnum, nummt, hshcol, dfaeql, numeps, eps2, num_reallocs;
  91. int tmpuses, totnst, peakpairs, numuniq, numdup, hshsave;
  92. int num_backing_up, bol_needed;
  93. FILE *backing_up_file;
  94. int end_of_buffer_state;
  95. char **input_files;
  96. int num_input_files;
  97. char *program_name;
  98.  
  99. #ifndef SHORT_FILE_NAMES
  100. static char *outfile_template = "lex.%s.%s";
  101. #else
  102. static char *outfile_template = "lex%s.%s";
  103. #endif
  104. static char outfile_path[64];
  105.  
  106. static int outfile_created = 0;
  107. static int use_stdout;
  108. static char *skelname = NULL;
  109. static char *prefix = "yy";
  110.  
  111.  
  112. int main( argc, argv )
  113. int argc;
  114. char **argv;
  115.     {
  116.     int i;
  117.  
  118.     flexinit( argc, argv );
  119.  
  120.     readin();
  121.  
  122.     ntod();
  123.  
  124.     for ( i = 1; i <= num_rules; ++i )
  125.         if ( ! rule_useful[i] && i != default_rule )
  126.             line_warning( "rule cannot be matched",
  127.                     rule_linenum[i] );
  128.  
  129.     if ( spprdflt && ! reject && rule_useful[default_rule] )
  130.         line_warning( "-s option given but default rule can be matched",
  131.             rule_linenum[default_rule] );
  132.  
  133.     /* Generate the C state transition tables from the DFA. */
  134.     make_tables();
  135.  
  136.     /* Note, flexend does not return.  It exits with its argument
  137.      * as status.
  138.      */
  139.     flexend( 0 );
  140.  
  141.     return 0;    /* keep compilers/lint happy */
  142.     }
  143.  
  144.  
  145. /* flexend - terminate flex
  146.  *
  147.  * note
  148.  *    This routine does not return.
  149.  */
  150.  
  151. void flexend( exit_status )
  152. int exit_status;
  153.  
  154.     {
  155.     int tblsiz;
  156.     int unlink();
  157.  
  158.     if ( skelfile != NULL )
  159.         {
  160.         if ( ferror( skelfile ) )
  161.             flexfatal(
  162.                 "error occurred when reading skeleton file" );
  163.  
  164.         else if ( fclose( skelfile ) )
  165.             flexfatal(
  166.                 "error occurred when closing skeleton file" );
  167.         }
  168.  
  169.     if ( exit_status != 0 && outfile_created )
  170.         {
  171.         if ( ferror( stdout ) )
  172.             flexfatal( "error occurred when writing output file" );
  173.  
  174.         else if ( fclose( stdout ) )
  175.             flexfatal( "error occurred when closing output file" );
  176.  
  177.         else if ( unlink( outfile_path ) )
  178.             flexfatal( "error occurred when deleting output file" );
  179.         }
  180.  
  181.     if ( backing_up_report && backing_up_file )
  182.         {
  183.         if ( num_backing_up == 0 )
  184.             fprintf( backing_up_file, "No backing up.\n" );
  185.         else if ( fullspd || fulltbl )
  186.             fprintf( backing_up_file,
  187.                 "%d backing up (non-accepting) states.\n",
  188.                 num_backing_up );
  189.         else
  190.             fprintf( backing_up_file,
  191.                 "Compressed tables always back up.\n" );
  192.  
  193.         if ( ferror( backing_up_file ) )
  194.             flexfatal( "error occurred when writing backup file" );
  195.  
  196.         else if ( fclose( backing_up_file ) )
  197.             flexfatal( "error occurred when closing backup file" );
  198.         }
  199.  
  200.     if ( printstats )
  201.         {
  202.         fprintf( stderr, "%s version %s usage statistics:\n",
  203.             program_name, flex_version );
  204.  
  205.         fprintf( stderr, "  scanner options: -" );
  206.  
  207.         if ( C_plus_plus )
  208.             putc( '+', stderr );
  209.         if ( backing_up_report )
  210.             putc( 'b', stderr );
  211.         if ( ddebug )
  212.             putc( 'd', stderr );
  213.         if ( caseins )
  214.             putc( 'i', stderr );
  215.         if ( lex_compat )
  216.             putc( 'l', stderr );
  217.         if ( performance_report > 0 )
  218.             putc( 'p', stderr );
  219.         if ( performance_report > 1 )
  220.             putc( 'p', stderr );
  221.         if ( spprdflt )
  222.             putc( 's', stderr );
  223.         if ( use_stdout )
  224.             putc( 't', stderr );
  225.         if ( printstats )
  226.             putc( 'v', stderr );    /* always true! */
  227.         if ( nowarn )
  228.             putc( 'w', stderr );
  229.         if ( ! interactive )
  230.             putc( 'B', stderr );
  231.         if ( interactive )
  232.             putc( 'I', stderr );
  233.         if ( ! gen_line_dirs )
  234.             putc( 'L', stderr );
  235.         if ( trace )
  236.             putc( 'T', stderr );
  237.         if ( csize == 128 )
  238.             putc( '7', stderr );
  239.         else
  240.             putc( '8', stderr );
  241.  
  242.         fprintf( stderr, " -C" );
  243.  
  244.         if ( long_align )
  245.             putc( 'a', stderr );
  246.         if ( fulltbl )
  247.             putc( 'f', stderr );
  248.         if ( fullspd )
  249.             putc( 'F', stderr );
  250.         if ( useecs )
  251.             putc( 'e', stderr );
  252.         if ( usemecs )
  253.             putc( 'm', stderr );
  254.         if ( use_read )
  255.             putc( 'r', stderr );
  256.  
  257.         if ( skelname )
  258.             fprintf( stderr, " -S%s", skelname );
  259.  
  260.         if ( strcmp( prefix, "yy" ) )
  261.             fprintf( stderr, " -P%s", prefix );
  262.  
  263.         putc( '\n', stderr );
  264.  
  265.         fprintf( stderr, "  %d/%d NFA states\n", lastnfa, current_mns );
  266.         fprintf( stderr, "  %d/%d DFA states (%d words)\n", lastdfa,
  267.             current_max_dfas, totnst );
  268.         fprintf( stderr, "  %d rules\n",
  269.         num_rules + num_eof_rules - 1 /* - 1 for def. rule */ );
  270.  
  271.         if ( num_backing_up == 0 )
  272.             fprintf( stderr, "  No backing up\n" );
  273.         else if ( fullspd || fulltbl )
  274.             fprintf( stderr,
  275.                 "  %d backing-up (non-accepting) states\n",
  276.                 num_backing_up );
  277.         else
  278.             fprintf( stderr,
  279.                 "  Compressed tables always back-up\n" );
  280.  
  281.         if ( bol_needed )
  282.             fprintf( stderr,
  283.                 "  Beginning-of-line patterns used\n" );
  284.  
  285.         fprintf( stderr, "  %d/%d start conditions\n", lastsc,
  286.             current_max_scs );
  287.         fprintf( stderr,
  288.             "  %d epsilon states, %d double epsilon states\n",
  289.             numeps, eps2 );
  290.  
  291.         if ( lastccl == 0 )