home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c100 / 5.ddi / FLEX.ZIP / MAIN.C < prev    next >
Encoding:
C/C++ Source or Header  |  1988-05-08  |  13.4 KB  |  538 lines

  1. /* flex - tool to generate fast lexical analyzers
  2.  *
  3.  *
  4.  * Copyright (c) 1987, the University of California
  5.  * 
  6.  * The United States Government has rights in this work pursuant to
  7.  * contract no. DE-AC03-76SF00098 between the United States Department of
  8.  * Energy and the University of California.
  9.  * 
  10.  * This program may be redistributed.  Enhancements and derivative works
  11.  * may be created provided the new works, if made available to the general
  12.  * public, are made available for use by anyone.
  13.  *
  14.  *
  15.  * ver   date  who    remarks
  16.  * ---   ----  ------ -------------------------------------------------------
  17.  * 04b 30sep87 kg, vp .implemented (part of) Van Jacobson's fast scanner design
  18.  * 04a 27jun86 vp     .translated from Ratfor into C
  19.  * 01a 22aug83 vp     .written.  Original version by Jef Poskanzer.
  20.  */
  21.  
  22. #include "flexdef.h"
  23.  
  24.  
  25. /* these globals are all defined and commented in flexdef.h */
  26. int printstats, syntaxerror, eofseen, ddebug, trace, spprdflt;
  27. int interactive, caseins, useecs, fulltbl, usemecs, reject;
  28. int fullspd, gen_line_dirs;
  29. int datapos, dataline, linenum;
  30. FILE *skelfile = NULL;
  31. char *infilename = NULL;
  32. int onestate[ONE_STACK_SIZE], onesym[ONE_STACK_SIZE];
  33. int onenext[ONE_STACK_SIZE], onedef[ONE_STACK_SIZE], onesp;
  34. int current_mns;
  35. int accnum, *firstst, *lastst, *finalst, *transchar;
  36. int *trans1, *trans2, *accptnum, lastnfa;
  37. int numtemps, numprots, protprev[MSP], protnext[MSP], prottbl[MSP];
  38. int protcomst[MSP], firstprot, lastprot, protsave[PROT_SAVE_SIZE];
  39. int numecs, nextecm[CSIZE + 1], ecgroup[CSIZE + 1], nummecs, tecfwd[CSIZE + 1];
  40. int tecbck[CSIZE + 1];
  41. int lastsc, current_max_scs, *scset, *scbol, *scxclu, *actvsc;
  42. int current_max_dfa_size, current_max_xpairs;
  43. int current_max_template_xpairs, current_max_dfas;
  44. int lastdfa, *nxt, *chk, *tnxt;
  45. int *base, *def, tblend, firstfree, numtemps, **dss, *dfasiz;
  46. union dfaacc_union *dfaacc;
  47. int *accsiz, *dhash, *todo, todo_head, todo_next, numas;
  48. int numsnpairs, jambase, jamstate;
  49. int lastccl, current_maxccls, *cclmap, *ccllen, *cclng, cclreuse;
  50. int current_max_ccl_tbl_size;
  51. char *ccltbl;
  52. char *starttime, *endtime, nmstr[MAXLINE];
  53. int sectnum, nummt, hshcol, dfaeql, numeps, eps2, num_reallocs;
  54. int tmpuses, totnst, peakpairs, numuniq, numdup, hshsave;
  55. FILE *temp_action_file;
  56. int end_of_buffer_state;
  57. char *action_file_name = "/tmp/flexXXXXXX";
  58.  
  59.  
  60. /* flex - main program
  61.  *
  62.  * synopsis (from the shell)
  63.  *    flex [-v] [file ...]
  64.  */
  65.  
  66. main( argc, argv )
  67. int argc;
  68. char **argv;
  69.  
  70.     {
  71.     flexinit( argc, argv );
  72.  
  73.     readin();
  74.  
  75.     if ( ! syntaxerror )
  76.     {
  77.     /* convert the ndfa to a dfa */
  78.     ntod();
  79.  
  80.     /* generate the C state transition tables from the DFA */
  81.     make_tables();
  82.     }
  83.  
  84.     /* note, flexend does not return.  It exits with its argument as status. */
  85.     flexend( 0 );
  86.     }
  87.  
  88.  
  89. /* flexend - terminate flex
  90.  *
  91.  * synopsis
  92.  *    int status;
  93.  *    flexend( status );
  94.  *
  95.  *    status is exit status.
  96.  *
  97.  * note
  98.  *    This routine does not return.
  99.  */
  100.  
  101. flexend( status )
  102. int status;
  103.  
  104.     {
  105.     int tblsiz;
  106.     char *gettime();
  107.  
  108.     if ( skelfile != NULL )
  109.     (void) fclose( skelfile );
  110.  
  111.     if ( temp_action_file )
  112.     {
  113.     (void) fclose( temp_action_file );
  114.     (void) unlink( action_file_name );
  115.     }
  116.  
  117.     if ( printstats )
  118.     {
  119.     endtime = gettime();
  120.  
  121.     fprintf( stderr, "flex usage statistics:\n" );
  122.     fprintf( stderr, "  started at %s, finished at %s\n",
  123.          starttime, endtime );
  124.  
  125.     fprintf( stderr, "  %d/%d NFA states\n", lastnfa, current_mns );
  126.     fprintf( stderr, "  %d/%d DFA states (%d words)\n", lastdfa,
  127.              current_max_dfas, totnst );
  128.     fprintf( stderr, "  %d rules\n", accnum );
  129.     fprintf( stderr, "  %d/%d start conditions\n", lastsc,
  130.              current_max_scs );
  131.     fprintf( stderr, "  %d epsilon states, %d double epsilon states\n",
  132.          numeps, eps2 );
  133.  
  134.     if ( lastccl == 0 )
  135.         fprintf( stderr, "  no character classes\n" );
  136.     else
  137.         fprintf( stderr,
  138.     "  %d/%d character classes needed %d/%d words of storage, %d reused\n",
  139.              lastccl, current_maxccls,
  140.              cclmap[lastccl] + ccllen[lastccl] - 1,
  141.              current_max_ccl_tbl_size, cclreuse );
  142.  
  143.     fprintf( stderr, "  %d state/nextstate pairs created\n", numsnpairs );
  144.     fprintf( stderr, "  %d/%d unique/duplicate transitions\n",
  145.          numuniq, numdup );
  146.  
  147.     if ( fulltbl )
  148.         {
  149.         tblsiz = lastdfa * numecs;
  150.         fprintf( stderr, "  %d table entries\n", tblsiz );
  151.         }
  152.  
  153.     else
  154.         {
  155.         tblsiz = 2 * (lastdfa + numtemps) + 2 * tblend;
  156.  
  157.         fprintf( stderr, "  %d/%d base/def entries created\n",
  158.              lastdfa + numtemps, current_max_dfas );
  159.         fprintf( stderr, "  %d/%d (peak %d) nxt/chk entries created\n",
  160.              tblend, current_max_xpairs, peakpairs );
  161.         fprintf( stderr,
  162.              "  %d/%d (peak %d) template nxt/chk entries created\n",
  163.              numtemps * nummecs, current_max_template_xpairs,
  164.              numtemps * numecs );
  165.         fprintf( stderr, "  %d empty table entries\n", nummt );
  166.         fprintf( stderr, "  %d protos created\n", numprots );
  167.         fprintf( stderr, "  %d templates created, %d uses\n",
  168.              numtemps, tmpuses );
  169.         }
  170.  
  171.     if ( useecs )
  172.         {
  173.         tblsiz = tblsiz + CSIZE;
  174.         fprintf( stderr, "  %d/%d equivalence classes created\n",
  175.              numecs, CSIZE );
  176.         }
  177.  
  178.     if ( usemecs )
  179.         {
  180.         tblsiz = tblsiz + numecs;
  181.         fprintf( stderr, "  %d/%d meta-equivalence classes created\n",
  182.              nummecs, CSIZE );
  183.         }
  184.  
  185.     fprintf( stderr, "  %d (%d saved) hash collisions, %d DFAs equal\n",
  186.          hshcol, hshsave, dfaeql );
  187.     fprintf( stderr, "  %d sets of reallocations needed\n", num_reallocs );
  188.     fprintf( stderr, "  %d total table entries needed\n", tblsiz );
  189.     }
  190.  
  191.     exit( status );
  192.     }
  193.  
  194.  
  195. /* flexinit - initialize flex
  196.  *
  197.  * synopsis
  198.  *    int argc;
  199.  *    char **argv;
  200.  *    flexinit( argc, argv );
  201.  */
  202.  
  203. flexinit( argc, argv )
  204. int argc;
  205. char **argv;
  206.  
  207.     {
  208.     int i, sawcmpflag, use_stdout;
  209.     char *arg, *skelname = NULL, *gettime(), clower(), *mktemp();
  210.  
  211.     printstats = syntaxerror = trace = spprdflt = interactive = caseins = false;
  212.     ddebug = fulltbl = reject = fullspd = false;
  213.     gen_line_dirs = usemecs = useecs = true;
  214.  
  215.     sawcmpflag = false;
  216.     use_stdout = false;
  217.  
  218.     /* read flags */
  219.     for ( --argc, ++argv; argc ; --argc, ++argv )
  220.     {
  221.     if ( argv[0][0] != '-' || argv[0][1] == '\0' )
  222.         break;
  223.  
  224.     arg = argv[0];
  225.  
  226.     for ( i = 1; arg[i] != '\0'; ++i )
  227.         switch ( arg[i] )
  228.         {
  229.         case 'c':
  230.             if ( i != 1 )
  231.             flexerror( "-c flag must be given separately" );
  232.  
  233.             if ( ! sawcmpflag )
  234.             {
  235.             useecs = false;
  236.             usemecs = false;
  237.             fulltbl = false;
  238.             sawcmpflag = true;
  239.             }
  240.  
  241.             for ( ++i; arg[i] != '\0'; ++i )
  242.             switch ( clower( arg[i] ) )
  243.                 {
  244.                 case 'e':
  245.                 useecs = true;
  246.                 break;
  247.  
  248.                 case 'F':
  249.                 fullspd = true;
  250.                 break;
  251.  
  252.                 case 'f':
  253.                 fulltbl = true;
  254.                 break;
  255.  
  256.                 case 'm':
  257.                 usemecs = true;
  258.                 break;
  259.  
  260.                 default:
  261.                 lerrif( "unknown -c option %c",
  262.                     (int) arg[i] );
  263.                 break;
  264.                 }
  265.             
  266.             goto get_next_arg;
  267.  
  268.         case 'd':
  269.             ddebug = true;
  270.             break;
  271.  
  272.         case 'f':
  273.             useecs = usemecs = false;
  274.             fulltbl = true;
  275.             break;
  276.  
  277.         case 'I':
  278.             interactive = true;
  279.             break;
  280.  
  281.         case 'i':
  282.             caseins = true;
  283.             break;
  284.  
  285.         case 'L':
  286.             gen_line_dirs = false;
  287.             break;
  288.  
  289.         case 'r':
  290.             reject = true;
  291.             break;
  292.  
  293.         case 'F':
  294.             useecs = usemecs = false;
  295.             fullspd = true;
  296.             break;
  297.  
  298.         case 'S':
  299.             if ( i != 1 )
  300.             flexerror( "-S flag must be given separately" );
  301.  
  302.             skelname = arg + i + 1;
  303.             goto get_next_arg;
  304.  
  305.         case 's':
  306.             spprdflt = true;
  307.             break;
  308.  
  309.         case 't':
  310.             use_stdout = true;
  311.             break;
  312.  
  313.         case 'T':
  314.             trace = true;
  315.             break;
  316.  
  317.         case 'v':
  318.             printstats = true;
  319.             break;
  320.  
  321.         default:
  322.             lerrif( "unknown flag %c", (int) arg[i] );
  323.             break;
  324.         }
  325.  
  326. get_next_arg: /* used by -c and -S flags in lieu of a "continue 2" control */
  327.     ;
  328.     }
  329.  
  330.     if ( (fulltbl || fullspd) && usemecs )
  331.     flexerror( "full table and -cm don't make sense together" );
  332.  
  333.     if ( (fulltbl || fullspd) && interactive )
  334.     flexerror( "full table and -I are (currently) incompatible" );
  335.  
  336.     if ( (fulltbl || fullspd) && reject )
  337.     flexerror( "reject (-r) cannot be used with -f or -F" );
  338.  
  339.     if ( fulltbl && fullspd )
  340.     flexerror( "full table and -F are mutually exclusive" );
  341.  
  342.     if ( ! skelname )
  343.     {
  344.     static char skeleton_name_storage[400];
  345.  
  346.     skelname = skeleton_name_storage;
  347.  
  348.     if ( fullspd || fulltbl )
  349.         (void) strcpy( skelname, FAST_SKELETON_FILE );
  350.     else
  351.         (void) strcpy( skelname, DEFAULT_SKELETON_FILE );
  352.     }
  353.  
  354.     if ( ! use_stdout )
  355.     {
  356. #ifndef MSDOS
  357.     FILE *prev_stdout = freopen( "lex.yy.c", "w", stdout );
  358. #else
  359.     FILE *prev_stdout = freopen( "lexyy.c", "w", stdout );
  360. #endif
  361.     if ( prev_stdout == NULL )
  362. #ifndef MSDOS
  363.         flexerror( "could not create lex.yy.c" );
  364. #else
  365.         flexerror( "could not create lexyy.c" );
  366. #endif
  367.  
  368.     }
  369.  
  370.     if ( argc )
  371.     {
  372.     if ( argc > 1 )
  373.         flexerror( "extraneous argument(s) given" );
  374.  
  375.     yyin = fopen( infilename = argv[0], "r" );
  376.  
  377.     if ( yyin == NULL )
  378.         lerrsf( "can't open %s", argv[0] );
  379.     }
  380.  
  381.     else
  382.     yyin = stdin;
  383.  
  384.     lastccl = 0;
  385.     lastsc = 0;
  386.  
  387.     /* initialize the statistics */
  388.     starttime = gettime();
  389.  
  390.     if ( (skelfile = fopen( skelname, "r" )) == NULL )
  391.     lerrsf( "can't open skeleton file %s", skelname );
  392.  
  393.     (void) mktemp( action_file_name );
  394.  
  395.     if ( (temp_action_file = fopen( action_file_name, "w" )) == NULL )
  396.     lerrsf( "can't open temporary action file %s", action_file_name );
  397.  
  398.     lastdfa = lastnfa = accnum = numas = numsnpairs = tmpuses = 0;
  399.     numecs = numeps = eps2 = num_reallocs = hshcol = dfaeql = totnst = 0;
  400.     numuniq = numdup = hshsave = eofseen = datapos = dataline = 0;
  401.     onesp = numprots = 0;
  402.  
  403.     linenum = sectnum = 1;
  404.     firstprot = NIL;
  405.  
  406.     /* used in mkprot() so that the first proto goes in slot 1
  407.      * of the proto queue
  408.      */
  409.     lastprot = 1;
  410.  
  411.     if ( useecs )
  412.     {
  413.     /* set up doubly-linked equivalence classes */
  414.     ecgroup[1] = NIL;
  415.  
  416.     for ( i = 2; i <= CSIZE; ++i )
  417.         {
  418.         ecgroup[i] = i - 1;
  419.         nextecm[i - 1] = i;
  420.         }
  421.  
  422.     nextecm[CSIZE] = NIL;
  423.     }
  424.  
  425.     else
  426.     { /* put everything in its own equivalence class */
  427.     for ( i = 1; i <= CSIZE; ++i )
  428.         {
  429.         ecgroup[i] = i;
  430.         nextecm[i] = BAD_SUBSCRIPT;    /* to catch errors */
  431.         }
  432.     }
  433.  
  434.     set_up_initial_allocations();
  435.     }
  436.  
  437.  
  438. /* readin - read in the rules section of the input file(s)
  439.  *
  440.  * synopsis
  441.  *    readin();
  442.  */
  443.  
  444. readin()
  445.  
  446.     {
  447.     fputs( "#define YY_DEFAULT_ACTION ", stdout );
  448.  
  449.     if ( spprdflt )
  450.     fputs( "YY_FATAL_ERROR( \"flex scanner jammed\" )", stdout );
  451.     else
  452.     fputs( "ECHO", stdout );
  453.  
  454.     fputs( ";\n", stdout );
  455.  
  456.     if ( ddebug )
  457.     puts( "#define FLEX_DEBUG" );
  458.     if ( useecs )
  459.     puts( "#define FLEX_USE_ECS" );
  460.     if ( usemecs )
  461.     puts( "#define FLEX_USE_MECS" );
  462.     if ( interactive )
  463.     puts( "#define FLEX_INTERACTIVE_SCANNER" );
  464.     if ( reject )
  465.     puts( "#define FLEX_REJECT_ENABLED" );
  466.     if ( fulltbl )
  467.     puts( "#define FLEX_FULL_TABLE" );
  468.  
  469.     skelout();
  470.  
  471.     line_directive_out( stdout );
  472.  
  473.     if ( yyparse() )
  474.     lerrif( "fatal parse error at line %d", linenum );
  475.  
  476.     if ( useecs )
  477.     {
  478.     numecs = cre8ecs( nextecm, ecgroup, CSIZE );
  479.     ccl2ecl();
  480.     }
  481.  
  482.     else
  483.     numecs = CSIZE;
  484.  
  485.     }
  486.  
  487.  
  488.  
  489. /* set_up_initial_allocations - allocate memory for internal tables */
  490.  
  491. set_up_initial_allocations()
  492.  
  493.     {
  494.     current_mns = INITIAL_MNS;
  495.     firstst = allocate_integer_array( current_mns );
  496.     lastst = allocate_integer_array( current_mns );
  497.     finalst = allocate_integer_array( current_mns );
  498.     transchar = allocate_integer_array( current_mns );
  499.     trans1 = allocate_integer_array( current_mns );
  500.     trans2 = allocate_integer_array( current_mns );
  501.     accptnum = allocate_integer_array( current_mns );
  502.  
  503.     current_max_scs = INITIAL_MAX_SCS;
  504.     scset = allocate_integer_array( current_max_scs );
  505.     scbol = allocate_integer_array( current_max_scs );
  506.     scxclu = allocate_integer_array( current_max_scs );
  507.     actvsc = allocate_integer_array( current_max_scs );
  508.  
  509.     current_maxccls = INITIAL_MAXCCLS;
  510.     cclmap = allocate_integer_array( current_maxccls );
  511.     ccllen = allocate_integer_array( current_maxccls );
  512.     cclng = allocate_integer_array( current_maxccls );
  513.  
  514.     current_max_ccl_tbl_size = INITIAL_MAX_CCL_TBL_SIZE;
  515.     ccltbl = allocate_character_array( current_max_ccl_tbl_size );
  516.  
  517.     current_max_dfa_size = INITIAL_MAX_DFA_SIZE;
  518.  
  519.     current_max_xpairs = INITIAL_MAX_XPAIRS;
  520.     nxt = allocate_integer_array( current_max_xpairs );
  521.     chk = allocate_integer_array( current_max_xpairs );
  522.  
  523.     current_max_template_xpairs = INITIAL_MAX_TEMPLATE_XPAIRS;
  524.     tnxt = allocate_integer_array( current_max_template_xpairs );
  525.  
  526.     current_max_dfas = INITIAL_MAX_DFAS;
  527.     base = allocate_integer_array( current_max_dfas );
  528.     def = allocate_integer_array( current_max_dfas );
  529.     dfasiz = allocate_integer_array( current_max_dfas );
  530.     accsiz = allocate_integer_array( current_max_dfas );
  531.     dhash = allocate_integer_array( current_max_dfas );
  532.     todo = allocate_integer_array( current_max_dfas );
  533.     dss = allocate_integer_pointer_array( current_max_dfas );
  534.     dfaacc = allocate_dfaacc_union( current_max_dfas );
  535.  
  536.     }
  537. /***/
  538.