home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 290_02 / tblcmp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-05-12  |  45.9 KB  |  1,607 lines

  1. /* tblcmp - table compression routines */
  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. #include "flexdef.h"
  16.  
  17. #include "main.h"
  18. #include "misc.h"
  19. #include "tblcmp.h"
  20. #include "dfa.h"
  21. #include "nfa.h"
  22. #include "ecs.h"
  23.  
  24. extern int flexscan( void) ;        /* from scan.c */
  25.  
  26. /* bldtbl - build table entries for dfa state
  27.  *
  28.  * synopsis
  29.  *   int state[numecs], statenum, totaltrans, comstate, comfreq;
  30.  *   bldtbl( state, statenum, totaltrans, comstate, comfreq );
  31.  *
  32.  * State is the statenum'th dfa state.  It is indexed by equivalence class and
  33.  * gives the number of the state to enter for a given equivalence class.
  34.  * totaltrans is the total number of transitions out of the state.  Comstate
  35.  * is that state which is the destination of the most transitions out of State.
  36.  * Comfreq is how many transitions there are out of State to Comstate.
  37.  *
  38.  * A note on terminology:
  39.  *    "protos" are transition tables which have a high probability of
  40.  * either being redundant (a state processed later will have an identical
  41.  * transition table) or nearly redundant (a state processed later will have
  42.  * many of the same out-transitions).  A "most recently used" queue of
  43.  * protos is kept around with the hope that most states will find a proto
  44.  * which is similar enough to be usable, and therefore compacting the
  45.  * output tables.
  46.  *    "templates" are a special type of proto.  If a transition table is
  47.  * homogeneous or nearly homogeneous (all transitions go to the same
  48.  * destination) then the odds are good that future states will also go
  49.  * to the same destination state on basically the same character set.
  50.  * These homogeneous states are so common when dealing with large rule
  51.  * sets that they merit special attention.  If the transition table were
  52.  * simply made into a proto, then (typically) each subsequent, similar
  53.  * state will differ from the proto for two out-transitions.  One of these
  54.  * out-transitions will be that character on which the proto does not go
  55.  * to the common destination, and one will be that character on which the
  56.  * state does not go to the common destination.  Templates, on the other
  57.  * hand, go to the common state on EVERY transition character, and therefore
  58.  * cost only one difference.
  59.  */
  60.  
  61. void bldtbl( state, statenum, totaltrans, comstate, comfreq )
  62. int state[], statenum, totaltrans, comstate, comfreq;
  63.  
  64. {
  65.     int extptr, extrct[2][CSIZE + 1];
  66.     int mindiff, minprot, i, d;
  67.     int checkcom;
  68.  
  69.     /* If extptr is 0 then the first array of extrct holds the result of the
  70.      * "best difference" to date, which is those transitions which occur in
  71.      * "state" but not in the proto which, to date, has the fewest differences
  72.      * between itself and "state".  If extptr is 1 then the second array of
  73.      * extrct hold the best difference.  The two arrays are toggled
  74.      * between so that the best difference to date can be kept around and
  75.      * also a difference just created by checking against a candidate "best"
  76.      * proto.
  77.      */
  78.  
  79.     extptr = 0;
  80.  
  81.     /* if the state has too few out-transitions, don't bother trying to
  82.      * compact its tables
  83.      */
  84.  
  85.     if ( (totaltrans * 100) < (numecs * PROTO_SIZE_PERCENTAGE) )
  86.         mkentry( state, numecs, statenum, JAMSTATE, totaltrans );
  87.  
  88.     else
  89.     {
  90.         /* checkcom is true if we should only check "state" against
  91.          * protos which have the same "comstate" value
  92.          */
  93.  
  94.         checkcom = comfreq * 100 > totaltrans * CHECK_COM_PERCENTAGE;
  95.  
  96.         minprot = firstprot;
  97.         mindiff = totaltrans;
  98.  
  99.         if ( checkcom )
  100.         {
  101.             /* find first proto which has the same "comstate" */
  102.             for ( i = firstprot; i != NIL; i = protnext[i] )
  103.                 if ( protcomst[i] == comstate )
  104.                 {
  105.                     minprot = i;
  106.                     mindiff = tbldiff( state, minprot, extrct[extptr] );
  107.                     break;
  108.                 }
  109.         }
  110.  
  111.         else
  112.         {
  113.             /* since we've decided that the most common destination out
  114.              * of "state" does not occur with a high enough frequency,
  115.              * we set the "comstate" to zero, assuring that if this state
  116.              * is entered into the proto list, it will not be considered
  117.              * a template.
  118.              */
  119.             comstate = 0;
  120.  
  121.             if ( firstprot != NIL )
  122.             {
  123.                 minprot = firstprot;
  124.                 mindiff = tbldiff( state, minprot, extrct[extptr] );
  125.             }
  126.         }
  127.  
  128.         /* we now have the first interesting proto in "minprot".  If
  129.          * it matches within the tolerances set for the first proto,
  130.          * we don't want to bother scanning the rest of the proto list
  131.          * to see if we have any other reasonable matches.
  132.          */
  133.  
  134.         if ( mindiff * 100 > totaltrans * FIRST_MATCH_DIFF_PERCENTAGE )
  135.         { /* not a good enough match.  Scan the rest of the protos */
  136.             for ( i = minprot; i != NIL; i = protnext[i] )
  137.             {
  138.                 d = tbldiff( state, i, extrct[1 - extptr] );
  139.                 if ( d < mindiff )
  140.                 {
  141.                     extptr = 1 - extptr;
  142.                     mindiff = d;
  143.                     minprot = i;
  144.                 }
  145.             }
  146.         }
  147.  
  148.         /* check if the proto we've decided on as our best bet is close
  149.          * enough to the state we want to match to be usable
  150.          */
  151.  
  152.         if ( mindiff * 100 > totaltrans * ACCEPTABLE_DIFF_PERCENTAGE )
  153.         {
  154.             /* no good.  If the state is homogeneous enough, we make a
  155.              * template out of it.  Otherwise, we make a proto.
  156.              */
  157.  
  158.             if ( comfreq * 100 >= totaltrans * TEMPLATE_SAME_PERCENTAGE )
  159.                 mktemplate( state, statenum, comstate );
  160.  
  161.             else
  162.             {
  163.                 mkprot( state, statenum, comstate );
  164.                 mkentry( state, numecs, statenum, JAMSTATE, totaltrans );
  165.             }
  166.         }
  167.  
  168.         else
  169.         { /* use the proto */
  170.             mkentry( extrct[extptr], numecs, statenum,
  171.               prottbl[minprot], mindiff );
  172.  
  173.             /* if this state was sufficiently different from the proto
  174.              * we built it from, make it, too, a proto
  175.              */
  176.  
  177.             if ( mindiff * 100 >= totaltrans * NEW_PROTO_DIFF_PERCENTAGE )
  178.                 mkprot( state, statenum, comstate );
  179.  
  180.             /* since mkprot added a new proto to the proto queue, it's possible
  181.              * that "minprot" is no longer on the proto queue (if it happened
  182.              * to have been the last entry, it would have been bumped off).
  183.              * If it's not there, then the new proto took its physical place
  184.              * (though logically the new proto is at the beginning of the
  185.              * queue), so in that case the following call will do nothing.
  186.              */
  187.  
  188.             mv2front( minprot );
  189.         }
  190.     }
  191. }
  192.  
  193.  
  194. /* cmptmps - compress template table entries
  195.  *
  196.  * synopsis
  197.  *    cmptmps();
  198.  *
  199.  *  template tables are compressed by using the 'template equivalence
  200.  *  classes', which are collections of transition character equivalence
  201.  *  classes which always appear together in templates - really meta-equivalence
  202.  *  classes.  until this point, the tables for templates have been stored
  203.  *  up at the top end of the nxt array; they will now be compressed and have
  204.  *  table entries made for them.
  205.  */
  206.  
  207. void cmptmps()
  208.  
  209. {
  210.     int tmpstorage[CSIZE + 1];
  211.     register int *tmp = tmpstorage, i, j;
  212.     int totaltrans, trans;
  213.  
  214.     peakpairs = numtemps * numecs + tblend;
  215.  
  216.     if ( usemecs )
  217.     {
  218.         /* create equivalence classes base on data gathered on template
  219.          * transitions
  220.          */
  221.  
  222.         nummecs = cre8ecs( tecfwd, tecbck, numecs );
  223.     }
  224.  
  225.     else
  226.         nummecs = numecs;
  227.  
  228.     if ( lastdfa + numtemps + 1 >= current_max_dfas )
  229.         increase_max_dfas();
  230.  
  231.     /* loop through each template */
  232.  
  233.     for ( i = 1; i <= numtemps; ++i )
  234.     {
  235.         totaltrans = 0; /* number of non-jam transitions out of this template */
  236.  
  237.         for ( j = 1; j <= numecs; ++j )
  238.         {
  239.             trans = tnxt[numecs * i + j];
  240.  
  241.             if ( usemecs )
  242.             {
  243.                 /* the absolute value of tecbck is the meta-equivalence class
  244.                  * of a given equivalence class, as set up by cre8ecs
  245.                  */
  246.                 if ( tecbck[j] > 0 )
  247.                 {
  248.                     tmp[tecbck[j]] = trans;
  249.  
  250.                     if ( trans > 0 )
  251.                         ++totaltrans;
  252.                 }
  253.             }
  254.  
  255.             else
  256.             {
  257.                 tmp[j] = trans;
  258.  
  259.                 if ( trans > 0 )
  260.                     ++totaltrans;
  261.             }
  262.         }
  263.  
  264.         /* it is assumed (in a rather subtle way) in the skeleton that
  265.          * if we're using meta-equivalence classes, the def[] entry for
  266.          * all templates is the jam template, i.e., templates never default
  267.          * to other non-jam table entries (e.g., another template)
  268.          */
  269.  
  270.         /* leave room for the jam-state after the last real state */
  271.         mkentry( tmp, nummecs, lastdfa + i + 1, JAMSTATE, totaltrans );
  272.     }
  273. }
  274.  
  275.  
  276.  
  277. /* expand_nxt_chk - expand the next check arrays */
  278.  
  279. void expand_nxt_chk()
  280.  
  281. {
  282.     register int old_max = current_max_xpairs;
  283.  
  284.     current_max_xpairs += MAX_XPAIRS_INCREMENT;
  285.  
  286.     ++num_reallocs;
  287.  
  288.     nxt = reallocate_integer_array( nxt, current_max_xpairs );
  289.     chk = reallocate_integer_array( chk, current_max_xpairs );
  290.  
  291.     bzero( (char *) (chk + old_max),
  292.       MAX_XPAIRS_INCREMENT * sizeof( int ) / sizeof( char ) );
  293. }
  294.  
  295.  
  296. /* find_table_space - finds a space in the table for a state to be placed
  297.  *
  298.  * synopsis
  299.  *     int *state, numtrans, block_start;
  300.  *     int find_table_space();
  301.  *
  302.  *     block_start = find_table_space( state, numtrans );
  303.  *
  304.  * State is the state to be added to the full speed transition table.
  305.  * Numtrans is the number of out-transitions for the state.
  306.  *
  307.  * find_table_space() returns the position of the start of the first block (in
  308.  * chk) able to accommodate the state
  309.  *
  310.  * In determining if a state will or will not fit, find_table_space() must take
  311.  * into account the fact that an end-of-buffer state will be added at [0],
  312.  * and an action number will be added in [-1].
  313.  */
  314.  
  315. int find_table_space( state, numtrans )
  316. int *state, numtrans;
  317.  
  318. {
  319.     /* firstfree is the position of the first possible occurrence of two
  320.      * consecutive unused records in the chk and nxt arrays
  321.      */
  322.     register int i;
  323.     register int *state_ptr, *chk_ptr;
  324.     register int *ptr_to_last_entry_in_state;
  325.  
  326.     /* if there are too many out-transitions, put the state at the end of
  327.      * nxt and chk
  328.      */
  329.     if ( numtrans > MAX_XTIONS_FOR_FULL_TABLE )
  330.     {
  331.         /* if table is empty, return the first available spot in chk/nxt,
  332.          * which should be 1
  333.          */
  334.         if ( tblend < 2 )
  335.             return ( 1 );
  336.  
  337.         i = tblend - numecs;    /* start searching for table space near the
  338.                                  * end of chk/nxt arrays
  339.                                          */
  340.     }
  341.  
  342.     else
  343.         i = firstfree;          /* start searching for table space from the
  344.                                  * beginning (skipping only the elements
  345.                                  * which will definitely not hold the new
  346.                                  * state)
  347.                                      */
  348.  
  349.     while ( 1 )         /* loops until a space is found */
  350.     {
  351.         if ( i + numecs > current_max_xpairs )
  352.             expand_nxt_chk();
  353.  
  354.         /* loops until space for end-of-buffer and action number are found */
  355.         while ( 1 )
  356.         {
  357.             if ( chk[i - 1] == 0 )      /* check for action number space */
  358.             {
  359.                 if ( chk[i] == 0 )      /* check for end-of-buffer space */
  360.                     break;
  361.  
  362.                 else
  363.                     i += 2;     /* since i != 0, there is no use checking to
  364.                                  * see if (++i) - 1 == 0, because that's the
  365.                                  * same as i == 0, so we skip a space
  366.                                                  */
  367.             }
  368.  
  369.             else
  370.                 ++i;
  371.  
  372.             if ( i + numecs > current_max_xpairs )
  373.                 expand_nxt_chk();
  374.         }
  375.  
  376.         /* if we started search from the beginning, store the new firstfree for
  377.          * the next call of find_table_space()
  378.          */
  379.         if ( numtrans <= MAX_XTIONS_FOR_FULL_TABLE )
  380.             firstfree = i + 1;
  381.  
  382.         /* check to see if all elements in chk (and therefore nxt) that are
  383.          * needed for the new state have not yet been taken
  384.          */
  385.  
  386.         state_ptr = &state[1];
  387.         ptr_to_last_entry_in_state = &chk[i + numecs + 1];
  388.  
  389.         for ( chk_ptr = &chk[i + 1]; chk_ptr != ptr_to_last_entry_in_state;
  390.           ++chk_ptr )
  391.             if ( *(state_ptr++) != 0 && *chk_ptr != 0 )
  392.                 break;
  393.  
  394.         if ( chk_ptr == ptr_to_last_entry_in_state )
  395.             return ( i );
  396.  
  397.         else
  398.             ++i;
  399.     }
  400. }
  401.  
  402.  
  403. /* genctbl - generates full speed compressed transition table
  404.  *
  405.  * synopsis
  406.  *     genctbl();
  407.  */
  408.  
  409. void genctbl()
  410.  
  411. {
  412.     register int i;
  413.  
  414.     /* table of verify for transition and offset to next state */
  415.     printf( "static struct yy_trans_info yy_transition[%d] =\n",
  416.       tblend + numecs + 1 );
  417.     printf( "    {\n" );
  418.  
  419.     /* We want the transition to be represented as the offset to the
  420.      * next state, not the actual state number, which is what it currently is.
  421.      * The offset is base[nxt[i]] - base[chk[i]].  That's just the
  422.      * difference between the starting points of the two involved states
  423.      * (to - from).
  424.      *
  425.      * first, though, we need to find some way to put in our end-of-buffer
  426.      * flags and states.  We do this by making a state with absolutely no
  427.      * transitions.  We put it at the end of the table.
  428.      */
  429.     /* at this point, we're guaranteed that there's enough room in nxt[]
  430.      * and chk[] to hold tblend + numecs entries.  We need just two slots.
  431.      * One for the action and one for the end-of-buffer transition.  We
  432.      * now *assume* that we're guaranteed the only character we'll try to
  433.      * index this nxt/chk pair with is EOB, i.e., 0, so we don't have to
  434.      * make sure there's room for jam entries for other characters.
  435.      */
  436.  
  437.     base[lastdfa + 1] = tblend + 2;
  438.     nxt[tblend + 1] = END_OF_BUFFER_ACTION;
  439.     chk[tblend + 1] = numecs + 1;
  440.     chk[tblend + 2] = 1; /* anything but EOB */
  441.     nxt[tblend + 2] = 0; /* so that "make test" won't show arb. differences */
  442.  
  443.     /* make sure every state has a end-of-buffer transition and an action # */
  444.     for ( i = 0; i <= lastdfa; ++i )
  445.     {
  446.         chk[base[i]] = EOB_POSITION;
  447.         chk[base[i] - 1] = ACTION_POSITION;
  448.         nxt[base[i] - 1] = dfaacc[i].dfaacc_state;      /* action number */
  449.     }
  450.  
  451.     for ( i = 0; i <= lastsc * 2; ++i )
  452.         nxt[base[i] - 1] = DEFAULT_ACTION;
  453.  
  454.     dataline = 0;
  455.     datapos = 0;
  456.  
  457.     for ( i = 0; i <= tblend; ++i )
  458.     {
  459.         if ( chk[i] == EOB_POSITION )
  460.             transition_struct_out( 0, base[lastdfa + 1] - i );
  461.  
  462.         else if ( chk[i] == ACTION_POSITION )
  463.             transition_struct_out( 0, nxt[i] );
  464.  
  465.         else if ( chk[i] > numecs || chk[i] == 0 )
  466.             transition_struct_out( 0, 0 );              /* unused slot */
  467.  
  468.         else /* verify, transition */
  469.             transition_struct_out( chk[i], base[nxt[i]] - (i - chk[i]) );
  470.     }
  471.  
  472.  
  473.     /* here's the final, end-of-buffer state */
  474.     transition_struct_out( chk[tblend + 1], nxt[tblend + 1] );
  475.     transition_struct_out( chk[tblend + 2], nxt[tblend + 2] );
  476.  
  477.     printf( "    };\n" );
  478.     printf( "\n" );
  479.  
  480.     /* table of pointers to start states */
  481.     printf( "static struct yy_trans_info *yy_state_ptr[%d] =\n",
  482.       lastsc * 2 + 1 );
  483.     printf( "    {\n" );
  484.  
  485.     for ( i = 0; i <= lastsc * 2; ++i )
  486.         printf( "    &yy_transition[%d],\n", base[i] );
  487.  
  488.     printf( "    };\n" );
  489.  
  490.     if ( useecs )
  491.         genecs();
  492. }
  493.  
  494.  
  495. /* gentabs - generate data statements for the transition tables
  496.  *
  497.  * synopsis
  498.  *    gentabs();
  499.  */
  500.  
  501. void gentabs()
  502.  
  503. {
  504.     int i, j, k, *accset, nacc, *acc_array;
  505.     char clower();
  506.  
  507.     /* *everything* is done in terms of arrays starting at 1, so provide
  508.      * a null entry for the zero element of all FTL arrays
  509.      */
  510.     static char ftl_long_decl[] = "static long int %c[%d] =\n    {   0,\n";
  511.     static char ftl_short_decl[] = "static short int %c[%d] =\n    {   0,\n";
  512.     static char ftl_char_decl[] = "static char %c[%d] =\n    {   0,\n";
  513.  
  514.     acc_array = allocate_integer_array( current_max_dfas );
  515.     nummt = 0;
  516.  
  517.     if ( fulltbl )
  518.         jambase = lastdfa + 1;  /* home of "jam" pseudo-state */
  519.  
  520.     printf( "#define YY_JAM %d\n", jamstate );
  521.     printf( "#define YY_JAM_BASE %d\n", jambase );
  522.  
  523.     if ( usemecs )
  524.         printf( "#define YY_TEMPLATE %d\n", lastdfa + 2 );
  525.  
  526.     if ( reject )
  527.     {
  528.         /* write out accepting list and pointer list
  529.          * first we generate the ACCEPT array.  In the process, we compute
  530.          * the indices that will go into the ALIST array, and save the
  531.          * indices in the dfaacc array
  532.          */
  533.  
  534.         printf( accnum > 127 ? ftl_short_decl : ftl_char_decl,
  535.           ACCEPT, max( numas, 1 ) + 1 );
  536.  
  537.         j = 1;  /* index into ACCEPT array */
  538.  
  539.         for ( i = 1; i <= lastdfa; ++i )
  540.         {
  541.             acc_array[i] = j;
  542.  
  543.             if ( accsiz[i] != 0 )
  544.             {
  545.                 accset = dfaacc[i].dfaacc_set;
  546.                 nacc = accsiz[i];
  547.  
  548.                 if ( trace )
  549.                     fprintf( stderr, "state # %d accepts: ", i );
  550.  
  551.                 for ( k = 1; k <= nacc; ++k )
  552.                 {
  553.                     ++j;
  554.                     mkdata( accset[k] );
  555.  
  556.                     if ( trace )
  557.                     {
  558.                         fprintf( stderr, "[%d]", accset[k] );
  559.  
  560.                         if ( k < nacc )
  561.                             fputs( ", ", stderr );
  562.                         else
  563.                             putc( '\n', stderr );
  564.                     }
  565.                 }
  566.             }
  567.         }
  568.  
  569.         /* add accepting number for the "jam" state */
  570.         acc_array[i] = j;
  571.  
  572.         dataend();
  573.     }
  574.  
  575.     else
  576.     {
  577.         for ( i = 1; i <= lastdfa; ++i )
  578.             acc_array[i] = dfaacc[i].dfaacc_state;
  579.  
  580.         acc_array[i] = 0; /* add (null) accepting number for jam state */
  581.     }
  582.  
  583.     /* spit out ALIST array.  If we're doing "reject", it'll be pointers
  584.      * into the ACCEPT array.  Otherwise it's actual accepting numbers.
  585.      * In either case, we just dump the numbers.
  586.      */
  587.  
  588.     /* "lastdfa + 2" is the size of ALIST; includes room for FTL arrays
  589.      * beginning at 0 and for "jam" state
  590.      */
  591.     k = lastdfa + 2;
  592.  
  593.     if ( reject )
  594.         /* we put a "cap" on the table associating lists of accepting
  595.          * numbers with state numbers.  This is needed because we tell
  596.          * where the end of an accepting list is by looking at where
  597.          * the list for the next state starts.
  598.          */
  599.         ++k;
  600.  
  601. #ifdef UNSIGNED_CHAR
  602.     printf( ftl_short_decl, ALIST, k );
  603. #else
  604.     printf( ((reject && numas > 126) || accnum > 127) ?
  605.       ftl_short_decl : ftl_char_decl, ALIST, k );
  606. #endif
  607.  
  608.     /* set up default actions */
  609.     for ( i = 1; i <= lastsc * 2; ++i )
  610.         acc_array[i] = DEFAULT_ACTION;
  611.  
  612.     acc_array[end_of_buffer_state] = END_OF_BUFFER_ACTION;
  613.  
  614.     for ( i = 1; i <= lastdfa; ++i )
  615.     {
  616.         mkdata( acc_array[i] );
  617.  
  618.         if ( ! reject && trace && acc_array[i] )
  619.             fprintf( stderr, "state # %d accepts: [%d]\n", i, acc_array[i] );
  620.     }
  621.  
  622.     /* add entry for "jam" state */
  623.     mkdata( acc_array[i] );
  624.  
  625.     if ( reject )
  626.         /* add "cap" for the list */
  627.         mkdata( acc_array[i] );
  628.  
  629.     dataend();
  630.  
  631.     if ( useecs )
  632.         genecs();
  633.  
  634.     if ( usemecs )
  635.     {
  636.         /* write out meta-equivalence classes (used to index templates with) */
  637.  
  638.         if ( trace )
  639.             fputs( "\n\nMeta-Equivalence Classes:\n", stderr );
  640.  
  641.         printf( ftl_char_decl, MATCHARRAY, numecs + 1 );
  642.  
  643.         for ( i = 1; i <= numecs; ++i )
  644.         {
  645.             if ( trace )
  646.                 fprintf( stderr, "%d = %d\n", i, abs( tecbck[i] ) );
  647.  
  648.             mkdata( abs( tecbck[i] ) );
  649.         }
  650.  
  651.         dataend();
  652.     }
  653.  
  654.     if ( ! fulltbl )
  655.     {
  656.         int total_states = lastdfa + numtemps;
  657.  
  658.         printf( tblend > MAX_SHORT ? ftl_long_decl : ftl_short_decl,
  659.           BASEARRAY, total_states + 1 );
  660.  
  661.         for ( i = 1; i <= lastdfa; ++i )
  662.         {
  663.             register int d = def[i];
  664.  
  665.             if ( base[i] == JAMSTATE )
  666.                 base[i] = jambase;
  667.  
  668.             if ( d == JAMSTATE )
  669.                 def[i] = jamstate;
  670.  
  671.             else if ( d < 0 )
  672.             {
  673.                 /* template reference */
  674.                 ++tmpuses;
  675.                 def[i] = lastdfa - d + 1;
  676.             }
  677.  
  678.             mkdata( base[i] );
  679.         }
  680.  
  681.         /* generate jam state's base index */
  682.         mkdata( base[i] );
  683.  
  684.         for ( ++i /* skip jam state */; i <= total_states; ++i )
  685.         {
  686.             mkdata( base[i] );
  687.             def[i] = jamstate;
  688.         }
  689.  
  690.         dataend();
  691.  
  692.         printf( tblend > MAX_SHORT ? ftl_long_decl : ftl_short_decl,
  693.           DEFARRAY, total_states + 1 );
  694.  
  695.         for ( i = 1; i <= total_states; ++i )
  696.             mkdata( def[i] );
  697.  
  698.         dataend();
  699.  
  700.         printf( lastdfa > MAX_SHORT ? ftl_long_decl : ftl_short_decl,
  701.           NEXTARRAY, tblend + 1 );
  702.  
  703.         for ( i = 1; i <= tblend; ++i )
  704.         {
  705.             if ( nxt[i] == 0 || chk[i] == 0 )
  706.                 nxt[i] = jamstate;      /* new state is the JAM state */
  707.  
  708.             mkdata( nxt[i] );
  709.         }
  710.  
  711.         dataend();
  712.  
  713.         printf( lastdfa > MAX_SHORT ? ftl_long_decl : ftl_short_decl,
  714.           CHECKARRAY, tblend + 1 );
  715.  
  716.         for ( i = 1; i <= tblend; ++i )
  717.         {
  718.             if ( chk[i] == 0 )
  719.                 ++nummt;
  720.  
  721.             mkdata( chk[i] );
  722.         }
  723.  
  724.         dataend();
  725.     }
  726. }
  727.  
  728.  
  729. /* generate equivalence-class tables */
  730.  
  731. void genecs()
  732. {
  733.     register int i, j;
  734.     static char ftl_char_decl[] = "static char %c[%d] =\n    {   0,\n";
  735.     int numrows;
  736.  
  737.     printf( ftl_char_decl, ECARRAY, CSIZE + 1 );
  738.  
  739.     for ( i = 1; i <= CSIZE; ++i )
  740.     {
  741.         /*      if ( caseins && (i >= 'A') && (i <= 'Z') ) */
  742.         if ( caseins &&  isupper(i) )
  743.             ecgroup[i] = ecgroup[clower( (unsigned char) i )];
  744.  
  745.         ecgroup[i] = abs( ecgroup[i] );
  746.         mkdata( ecgroup[i] );
  747.     }
  748.  
  749.     dataend();
  750.  
  751.     if ( trace )
  752.     {
  753.         fputs( "\n\nEquivalence Classes:\n\n", stderr );
  754.  
  755.         numrows = (CSIZE + 1) / 8;
  756.  
  757.         for ( j = 1; j <= numrows; ++j )
  758.         {
  759.             for ( i = j; i <= CSIZE; i = i + numrows )
  760.             {
  761.                 if ( i >= 1 && i <= 31 )
  762.                     fprintf( stderr, "^%c = %-2d",
  763.                       'A' + i - 1, ecgroup[i] );
  764.  
  765.                 else if ( i >= 32 && i <= 126 )
  766.                     fprintf( stderr, " %c = %-2d", i, ecgroup[i] );
  767.  
  768.                 else if ( i == 127 )
  769.                     fprintf( stderr, "^@ = %-2d", ecgroup[i] );
  770.  
  771.                 else
  772.                     fprintf( stderr, "\nSomething Weird: %d = %d\n", i,
  773.                       ecgroup[i] );
  774.  
  775.                 putc( '\t', stderr );
  776.             }
  777.  
  778.             putc( '\n', stderr );
  779.         }
  780.     }
  781. }
  782.  
  783.  
  784. /* inittbl - initialize transition tables
  785.  *
  786.  * synopsis
  787.  *   inittbl();
  788.  *
  789.  * Initializes "firstfree" to be one beyond the end of the table.  Initializes
  790.  * all "chk" entries to be zero.  Note that templates are built in their
  791.  * own tbase/tdef tables.  They are shifted down to be contiguous
  792.  * with the non-template entries during table generation.
  793.  */
  794. void inittbl()
  795.  
  796. {
  797.     register int i;
  798.  
  799.     bzero( (char *) chk, current_max_xpairs * sizeof( int ) / sizeof( char ) );
  800.  
  801.     tblend = 0;
  802.     firstfree = tblend + 1;
  803.     numtemps = 0;
  804.  
  805.     if ( usemecs )
  806.     {
  807.         /* set up doubly-linked meta-equivalence classes
  808.          * these are sets of equivalence classes which all have identical
  809.          * transitions out of TEMPLATES
  810.          */
  811.  
  812.         tecbck[1] = NIL;
  813.  
  814.         for ( i = 2; i <= numecs; ++i )
  815.         {
  816.             tecbck[i] = i - 1;
  817.             tecfwd[i - 1] = i;
  818.         }
  819.  
  820.         tecfwd[numecs] = NIL;
  821.     }
  822. }
  823.  
  824.  
  825. /* make_tables - generate transition tables
  826.  *
  827.  * synopsis
  828.  *     make_tables();
  829.  *
  830.  * Generates transition tables and finishes generating output file
  831.  */
  832.  
  833. void make_tables()
  834.  
  835. {
  836.     if ( fullspd )
  837. { /* need to define YY_TRANS_OFFSET_TYPE as a size large
  838.            * enough to hold the biggest offset
  839.                */
  840.         int total_table_size = tblend + numecs + 1;
  841.  
  842.         printf( "#define YY_TRANS_OFFSET_TYPE %s\n",
  843.           total_table_size > MAX_SHORT ? "long" : "short" );
  844.     }
  845.  
  846.     if ( fullspd || fulltbl )
  847.         skelout();
  848.  
  849.     /* compute the tables and copy them to output file */
  850.     if ( fullspd )
  851.         genctbl();
  852.  
  853.     else
  854.         gentabs();
  855.  
  856.     skelout();
  857.  
  858.     (void) fclose( temp_action_file );
  859.     temp_action_file = fopen( action_file_name, "r" );
  860.  
  861.     /* copy prolog from action_file to output file */
  862.     action_out();
  863.  
  864.     skelout();
  865.  
  866.     /* copy actions from action_file to output file */
  867.     action_out();
  868.  
  869.     skelout();
  870.  
  871.     /* copy remainder of input to output */
  872.  
  873.     line_directive_out( stdout );
  874.     (void) flexscan(); /* copy remainder of input to output */
  875. }
  876.  
  877.  
  878. /* mkdeftbl - make the default, "jam" table entries
  879.  *
  880.  * synopsis
  881.  *   mkdeftbl();
  882.  */
  883.  
  884. void mkdeftbl()
  885.  
  886. {
  887.     int i;
  888.  
  889.     jamstate = lastdfa + 1;
  890.  
  891.     if ( tblend + numecs > current_max_xpairs )
  892.         expand_nxt_chk();
  893.  
  894.     for ( i = 1; i <= numecs; ++i )
  895.     {
  896.         nxt[tblend + i] = 0;
  897.         chk[tblend + i] = jamstate;
  898.     }
  899.  
  900.     jambase = tblend;
  901.  
  902.     base[jamstate] = jambase;
  903.  
  904.     /* should generate a run-time array bounds check if
  905.      * ever used as a default
  906.      */
  907.     def[jamstate] = BAD_SUBSCRIPT;
  908.  
  909.     tblend += numecs;
  910.     ++numtemps;
  911. }
  912.  
  913.  
  914. /* mkentry - create base/def and nxt/chk entries for transition array
  915.  *
  916.  * synopsis
  917.  *   int state[numchars + 1], numchars, statenum, deflink, totaltrans;
  918.  *   mkentry( state, numchars, statenum, deflink, totaltrans );
  919.  *
  920.  * "state" is a transition array "numchars" characters in size, "statenum"
  921.  * is the offset to be used into the base/def tables, and "deflink" is the
  922.  * entry to put in the "def" table entry.  If "deflink" is equal to
  923.  * "JAMSTATE", then no attempt will be made to fit zero entries of "state"
  924.  * (i.e., jam entries) into the table.  It is assumed that by linking to
  925.  * "JAMSTATE" they will be taken care of.  In any case, entries in "state"
  926.  * marking transitions to "SAME_TRANS" are treated as though they will be
  927.  * taken care of by whereever "deflink" points.  "totaltrans" is the total
  928.  * number of transitions out of the state.  If it is below a certain threshold,
  929.  * the tables are searched for an interior spot that will accommodate the
  930.  * state array.
  931.  */
  932.  
  933. void mkentry( state, numchars, statenum, deflink, totaltrans )
  934. register int *state;
  935. int numchars, statenum, deflink, totaltrans;
  936.  
  937. {
  938.     register int minec, maxec, i, baseaddr;
  939.     int tblbase, tbllast;
  940.  
  941.     if ( totaltrans == 0 )
  942.     { /* there are no out-transitions */
  943.         if ( deflink == JAMSTATE )
  944.             base[statenum] = JAMSTATE;
  945.         else
  946.             base[statenum] = 0;
  947.  
  948.         def[statenum] = deflink;
  949.         return;
  950.     }
  951.  
  952.     for ( minec = 1; minec <= numchars; ++minec )
  953.     {
  954.         if ( state[minec] != SAME_TRANS )
  955.             if ( state[minec] != 0 || deflink != JAMSTATE )
  956.                 break;
  957.     }
  958.  
  959.     if ( totaltrans == 1 )
  960.     {
  961.         /* there's only one out-transition.  Save it for later to fill
  962.          * in holes in the tables.
  963.          */
  964.         stack1( statenum, minec, state[minec], deflink );
  965.         return;
  966.     }
  967.  
  968.     for ( maxec = numchars; maxec > 0; --maxec )
  969.     {
  970.         if ( state[maxec] != SAME_TRANS )
  971.             if ( state[maxec] != 0 || deflink != JAMSTATE )
  972.                 break;
  973.     }
  974.  
  975.     /* Whether we try to fit the state table in the middle of the table
  976.      * entries we have already generated, or if we just take the state
  977.      * table at the end of the nxt/chk tables, we must make sure that we
  978.      * have a valid base address (i.e., non-negative).  Note that not only are
  979.      * negative base addresses dangerous at run-time (because indexing the
  980.      * next array with one and a low-valued character might generate an
  981.      * array-out-of-bounds error message), but at compile-time negative
  982.      * base addresses denote TEMPLATES.
  983.      */
  984.  
  985.     /* find the first transition of state that we need to worry about. */
  986.     if ( totaltrans * 100 <= numchars * INTERIOR_FIT_PERCENTAGE )
  987.     { /* attempt to squeeze it into the middle of the tabls */
  988.         baseaddr = firstfree;
  989.  
  990.         while ( baseaddr < minec )
  991.         {
  992.             /* using baseaddr would result in a negative base address below
  993.              * find the next free slot
  994.              */
  995.             for ( ++baseaddr; chk[baseaddr] != 0; ++baseaddr )
  996.                 ;
  997.         }
  998.  
  999.         if ( baseaddr + maxec - minec >= current_max_xpairs )
  1000.             expand_nxt_chk();
  1001.  
  1002.         for ( i = minec; i <= maxec; ++i )
  1003.             if ( state[i] != SAME_TRANS )
  1004.                 if ( state[i] != 0 || deflink != JAMSTATE )
  1005.                     if ( chk[baseaddr + i - minec] != 0 )
  1006.                     { /* baseaddr unsuitable - find another */
  1007.                         for ( ++baseaddr;
  1008.                           baseaddr < current_max_xpairs &&
  1009.                           chk[baseaddr] != 0;
  1010.                           ++baseaddr )
  1011.                             ;
  1012.  
  1013.                         if ( baseaddr + maxec - minec >= current_max_xpairs )
  1014.                             expand_nxt_chk();
  1015.  
  1016.                         /* reset the loop counter so we'll start all
  1017.                          * over again next time it's incremented
  1018.                          */
  1019.  
  1020.                         i = minec - 1;
  1021.                     }
  1022.     }
  1023.  
  1024.     else
  1025.     {
  1026.         /* ensure that the base address we eventually generate is
  1027.          * non-negative
  1028.          */
  1029.         baseaddr = max( tblend + 1, minec );
  1030.     }
  1031.  
  1032.     tblbase = baseaddr - minec;
  1033.     tbllast = tblbase + maxec;
  1034.  
  1035.     if ( tbllast >= current_max_xpairs )
  1036.         expand_nxt_chk();
  1037.  
  1038.     base[statenum] = tblbase;
  1039.     def[statenum] = deflink;
  1040.  
  1041.     for ( i = minec; i <= maxec; ++i )
  1042.         if ( state[i] != SAME_TRANS )
  1043.             if ( state[i] != 0 || deflink != JAMSTATE )
  1044.             {
  1045.                 nxt[tblbase + i] = state[i];
  1046.                 chk[tblbase + i] = statenum;
  1047.             }
  1048.  
  1049.     if ( baseaddr == firstfree )
  1050.         /* find next free slot in tables */
  1051.         for ( ++firstfree; chk[firstfree] != 0; ++firstfree )
  1052.             ;
  1053.  
  1054.     tblend = max( tblend, tbllast );
  1055. }
  1056.  
  1057.  
  1058. /* mk1tbl - create table entries for a state (or state fragment) which
  1059.  *            has only one out-transition
  1060.  *
  1061.  * synopsis
  1062.  *   int state, sym, onenxt, onedef;
  1063.  *   mk1tbl( state, sym, onenxt, onedef );
  1064.  */
  1065.  
  1066. void mk1tbl( state, sym, onenxt, onedef )
  1067. int state, sym, onenxt, onedef;
  1068.  
  1069. {
  1070.     if ( firstfree < sym )
  1071.         firstfree = sym;
  1072.  
  1073.     while ( chk[firstfree] != 0 )
  1074.         if ( ++firstfree >= current_max_xpairs )
  1075.             expand_nxt_chk();
  1076.  
  1077.     base[state] = firstfree - sym;
  1078.     def[state] = onedef;
  1079.     chk[firstfree] = state;
  1080.     nxt[firstfree] = onenxt;
  1081.  
  1082.     if ( firstfree > tblend )
  1083.     {
  1084.         tblend = firstfree++;
  1085.  
  1086.         if ( firstfree >= current_max_xpairs )
  1087.             expand_nxt_chk();
  1088.     }
  1089. }
  1090.  
  1091.  
  1092. /* mkprot - create new proto entry
  1093.  *
  1094.  * synopsis
  1095.  *   int state[], statenum, comstate;
  1096.  *   mkprot( state, statenum, comstate );
  1097.  */
  1098.  
  1099. void mkprot( state, statenum, comstate )
  1100. int state[], statenum, comstate;
  1101.  
  1102. {
  1103.     int i, slot, tblbase;
  1104.  
  1105.     if ( ++numprots >= MSP || numecs * numprots >= PROT_SAVE_SIZE )
  1106.     {
  1107.         /* gotta make room for the new proto by dropping last entry in
  1108.          * the queue
  1109.          */
  1110.         slot = lastprot;
  1111.         lastprot = protprev[lastprot];
  1112.         protnext[lastprot] = NIL;
  1113.     }
  1114.  
  1115.     else
  1116.         slot = numprots;
  1117.  
  1118.     protnext[slot] = firstprot;
  1119.  
  1120.     if ( firstprot != NIL )
  1121.         protprev[firstprot] = slot;
  1122.  
  1123.     firstprot = slot;
  1124.     prottbl[slot] = statenum;
  1125.     protcomst[slot] = comstate;
  1126.  
  1127.     /* copy state into save area so it can be compared with rapidly */
  1128.     tblbase = numecs * (slot - 1);
  1129.  
  1130.     for ( i = 1; i <= numecs; ++i )
  1131.         protsave[tblbase + i] = state[i];
  1132. }
  1133.  
  1134.  
  1135. /* mktemplate - create a template entry based on a state, and connect the state
  1136.  *              to it
  1137.  *
  1138.  * synopsis
  1139.  *   int state[], statenum, comstate, totaltrans;
  1140.  *   mktemplate( state, statenum, comstate, totaltrans );
  1141.  */
  1142.  
  1143. void mktemplate( state, statenum, comstate )
  1144. int state[], statenum, comstate;
  1145.  
  1146. {
  1147.     int i, numdiff, tmpbase, tmp[CSIZE + 1];
  1148.     char transset[CSIZE + 1];
  1149.     int tsptr;
  1150.  
  1151.     ++numtemps;
  1152.  
  1153.     tsptr = 0;
  1154.  
  1155.     /* calculate where we will temporarily store the transition table
  1156.      * of the template in the tnxt[] array.  The final transition table
  1157.      * gets created by cmptmps()
  1158.      */
  1159.  
  1160.     tmpbase = numtemps * numecs;
  1161.  
  1162.     if ( tmpbase + numecs >= current_max_template_xpairs )
  1163.     {
  1164.         current_max_template_xpairs += MAX_TEMPLATE_XPAIRS_INCREMENT;
  1165.  
  1166.         ++num_reallocs;
  1167.  
  1168.         tnxt = reallocate_integer_array( tnxt, current_max_template_xpairs );
  1169.     }
  1170.  
  1171.     for ( i = 1; i <= numecs; ++i )
  1172.         if ( state[i] == 0 )
  1173.             tnxt[tmpbase + i] = 0;
  1174.         else
  1175.         {
  1176.             transset[tsptr++] = (unsigned char) i;
  1177.             tnxt[tmpbase + i] = comstate;
  1178.         }
  1179.  
  1180.     if ( usemecs )
  1181.         mkeccl( transset, tsptr, tecfwd, tecbck, numecs );
  1182.  
  1183.     mkprot( tnxt + tmpbase, -numtemps, comstate );
  1184.  
  1185.     /* we rely on the fact that mkprot adds things to the beginning
  1186.      * of the proto queue
  1187.      */
  1188.  
  1189.     numdiff = tbldiff( state, firstprot, tmp );
  1190.     mkentry( tmp, numecs, statenum, -numtemps, numdiff );
  1191. }
  1192.  
  1193.  
  1194. /* mv2front - move proto queue element to front of queue
  1195.  *
  1196.  * synopsis
  1197.  *   int qelm;
  1198.  *   mv2front( qelm );
  1199.  */
  1200.  
  1201. void mv2front( qelm )
  1202. int qelm;
  1203.  
  1204. {
  1205.     if ( firstprot != qelm )
  1206.     {
  1207.         if ( qelm == lastprot )
  1208.             lastprot = protprev[lastprot];
  1209.  
  1210.         protnext[protprev[qelm]] = protnext[qelm];
  1211.  
  1212.         if ( protnext[qelm] != NIL )
  1213.             protprev[protnext[qelm]] = protprev[qelm];
  1214.  
  1215.         protprev[qelm] = NIL;
  1216.         protnext[qelm] = firstprot;
  1217.         protprev[firstprot] = qelm;
  1218.         firstprot = qelm;
  1219.     }
  1220. }
  1221.  
  1222.  
  1223. /* ntod - convert an ndfa to a dfa
  1224.  *
  1225.  * synopsis
  1226.  *    ntod();
  1227.  *
  1228.  *  creates the dfa corresponding to the ndfa we've constructed.  the
  1229.  *  dfa starts out in state #1.
  1230.  */
  1231. void ntod()
  1232.  
  1233. {
  1234.     int *accset, ds, nacc, newds;
  1235.     int duplist[CSIZE + 1], sym, hashval, numstates, dsize;
  1236.     int targfreq[CSIZE + 1], targstate[CSIZE + 1], state[CSIZE + 1];
  1237.     int *nset, *dset;
  1238.     int targptr, totaltrans, i, comstate, comfreq, targ;
  1239.     int *epsclosure(), snstods(), symlist[CSIZE + 1];
  1240.  
  1241.     /* this is so find_table_space(...) will know where to start looking in
  1242.      * chk/nxt for unused records for space to put in the state
  1243.      */
  1244.     if ( fullspd )
  1245.         firstfree = 0;
  1246.  
  1247.     accset = allocate_integer_array( accnum + 1 );
  1248.     nset = allocate_integer_array( current_max_dfa_size );
  1249.  
  1250.     todo_head = todo_next = 0;
  1251.  
  1252.     #define ADD_QUEUE_ELEMENT(element) \
  1253.         if ( ++element >= current_max_dfas ) \
  1254.             { /* check for queue overflowing */ \
  1255.             if ( todo_head == 0 ) \
  1256.                 increase_max_dfas(); \
  1257.             else \
  1258.                 element = 0; \
  1259.             }
  1260.  
  1261. #define NEXT_QUEUE_ELEMENT(element) ((element + 1) % (current_max_dfas + 1))
  1262.  
  1263.     for ( i = 0; i <= CSIZE; ++i )
  1264.     {
  1265.         duplist[i] = NIL;
  1266.         symlist[i] = false;
  1267.     }
  1268.  
  1269.     for ( i = 0; i <= accnum; ++i )
  1270.         accset[i] = NIL;
  1271.  
  1272.     if ( trace )
  1273.     {
  1274.         dumpnfa( scset[1] );
  1275.         fputs( "\n\nDFA Dump:\n\n", stderr );
  1276.     }
  1277.  
  1278.     inittbl();
  1279.  
  1280.     if ( fullspd )
  1281.     {
  1282.         for ( i = 0; i <= numecs; ++i )
  1283.             state[i] = 0;
  1284.         place_state( state, 0, 0 );
  1285.     }
  1286.  
  1287.     if ( fulltbl )
  1288.     {
  1289.         /* declare it "short" because it's a real long-shot that that
  1290.          * won't be large enough
  1291.          */
  1292.         printf( "static short int %c[][%d] =\n    {\n", NEXTARRAY,
  1293.           numecs + 1 ); /* '}' so vi doesn't get too confused */
  1294.  
  1295.         /* generate 0 entries for state #0 */
  1296.         for ( i = 0; i <= numecs; ++i )
  1297.             mk2data( 0 );
  1298.  
  1299.         /* force ',' and dataflush() next call to mk2data */
  1300.         datapos = NUMDATAITEMS;
  1301.  
  1302.         /* force extra blank line next dataflush() */
  1303.         dataline = NUMDATALINES;
  1304.     }
  1305.  
  1306.     /* create the first states */
  1307.  
  1308.     for ( i = 1; i <= lastsc * 2; ++i )
  1309.     {
  1310.         numstates = 1;
  1311.  
  1312.         /* for each start condition, make one state for the case when
  1313.          * we're at the beginning of the line (the '%' operator) and
  1314.          * one for the case when we're not
  1315.          */
  1316.         if ( i % 2 == 1 )
  1317.             nset[numstates] = scset[(i / 2) + 1];
  1318.         else
  1319.             nset[numstates] = mkbranch( scbol[i / 2], scset[i / 2] );
  1320.  
  1321.         nset = epsclosure( nset, &numstates, accset, &nacc, &hashval );
  1322.  
  1323.         if ( snstods( nset, numstates, accset, nacc, hashval, &ds ) )
  1324.         {
  1325.             numas = numas + nacc;
  1326.             totnst = totnst + numstates;
  1327.  
  1328.             todo[todo_next] = ds;
  1329.             ADD_QUEUE_ELEMENT(todo_next);
  1330.         }
  1331.     }
  1332.  
  1333.     if ( fulltbl )
  1334.     {
  1335.         if ( ! snstods( nset, 0, accset, 0, 0, &end_of_buffer_state ) )
  1336.             flexfatal( "could not create unique end-of-buffer state" );
  1337.  
  1338.         numas += 1;
  1339.  
  1340.         todo[todo_next] = end_of_buffer_state;
  1341.         ADD_QUEUE_ELEMENT(todo_next);
  1342.     }
  1343.  
  1344.     while ( todo_head != todo_next )
  1345.     {
  1346.         targptr = 0;
  1347.         totaltrans = 0;
  1348.  
  1349.         for ( i = 1; i <= numecs; ++i )
  1350.             state[i] = 0;
  1351.  
  1352.         ds = todo[todo_head];
  1353.         todo_head = NEXT_QUEUE_ELEMENT(todo_head);
  1354.  
  1355.         dset = dss[ds];
  1356.         dsize = dfasiz[ds];
  1357.  
  1358.         if ( trace )
  1359.             fprintf( stderr, "state # %d:\n", ds );
  1360.  
  1361.         sympartition( dset, dsize, symlist, duplist );
  1362.  
  1363.         for ( sym = 1; sym <= numecs; ++sym )
  1364.         {
  1365.             if ( symlist[sym] )
  1366.             {
  1367.                 symlist[sym] = 0;
  1368.  
  1369.                 if ( duplist[sym] == NIL )
  1370.                 { /* symbol has unique out-transitions */
  1371.                     numstates = symfollowset( dset, dsize, sym, nset );
  1372.                     nset = epsclosure( nset, &numstates, accset,
  1373.                       &nacc, &hashval );
  1374.  
  1375.                     if ( snstods( nset, numstates, accset,
  1376.                       nacc, hashval, &newds ) )
  1377.                     {
  1378.                         totnst = totnst + numstates;
  1379.                         todo[todo_next] = newds;
  1380.                         ADD_QUEUE_ELEMENT(todo_next);
  1381.                         numas = numas + nacc;
  1382.                     }
  1383.  
  1384.                     state[sym] = newds;
  1385.  
  1386.                     if ( trace )
  1387.                         fprintf( stderr, "\t%d\t%d\n", sym, newds );
  1388.  
  1389.                     targfreq[++targptr] = 1;
  1390.                     targstate[targptr] = newds;
  1391.                     ++numuniq;
  1392.                 }
  1393.  
  1394.                 else
  1395.                 {
  1396.                     /* sym's equivalence class has the same transitions
  1397.                      * as duplist(sym)'s equivalence class
  1398.                      */
  1399.                     targ = state[duplist[sym]];
  1400.                     state[sym] = targ;
  1401.  
  1402.                     if ( trace )
  1403.                         fprintf( stderr, "\t%d\t%d\n", sym, targ );
  1404.  
  1405.                     /* update frequency count for destination state */
  1406.  
  1407.                     i = 0;
  1408.                     while ( targstate[++i] != targ )
  1409.                         ;
  1410.  
  1411.                     ++targfreq[i];
  1412.                     ++numdup;
  1413.                 }
  1414.  
  1415.                 ++totaltrans;
  1416.                 duplist[sym] = NIL;
  1417.             }
  1418.         }
  1419.  
  1420.         numsnpairs = numsnpairs + totaltrans;
  1421.  
  1422.         if ( caseins && ! useecs )
  1423.         {
  1424.             register int j;
  1425.  
  1426.             for ( i = 'A', j = 'a'; i <= 'Z'; ++i, ++j )
  1427.                 state[i] = state[j];
  1428.         }
  1429.  
  1430.         if ( fulltbl )
  1431.         {
  1432.             /* supply array's 0-element */
  1433.             if ( ds == end_of_buffer_state )
  1434.                 mk2data( 0 );
  1435.             else
  1436.                 mk2data( end_of_buffer_state );
  1437.  
  1438.             for ( i = 1; i <= numecs; ++i )
  1439.                 mk2data( state[i] );
  1440.  
  1441.             /* force ',' and dataflush() next call to mk2data */
  1442.             datapos = NUMDATAITEMS;
  1443.  
  1444.             /* force extra blank line next dataflush() */
  1445.             dataline = NUMDATALINES;
  1446.         }
  1447.  
  1448.         else if ( fullspd )
  1449.             place_state( state, ds, totaltrans );
  1450.  
  1451.         else
  1452.         {
  1453.             /* determine which destination state is the most common, and
  1454.              * how many transitions to it there are
  1455.              */
  1456.  
  1457.             comfreq = 0;
  1458.             comstate = 0;
  1459.  
  1460.             for ( i = 1; i <= targptr; ++i )
  1461.                 if ( targfreq[i] > comfreq )
  1462.                 {
  1463.                     comfreq = targfreq[i];
  1464.                     comstate = targstate[i];
  1465.                 }
  1466.  
  1467.             bldtbl( state, ds, totaltrans, comstate, comfreq );
  1468.         }
  1469.     }
  1470.  
  1471.     if ( fulltbl )
  1472.         dataend();
  1473.  
  1474.     else
  1475.     {
  1476.         cmptmps();  /* create compressed template entries */
  1477.  
  1478.         /* create tables for all the states with only one out-transition */
  1479.         while ( onesp > 0 )
  1480.         {
  1481.             mk1tbl( onestate[onesp], onesym[onesp], onenext[onesp],
  1482.               onedef[onesp] );
  1483.             --onesp;
  1484.         }
  1485.  
  1486.         mkdeftbl();
  1487.     }
  1488.  
  1489. }
  1490.  
  1491.  
  1492. /* place_state - place a state into full speed transition table
  1493.  *
  1494.  * synopsis
  1495.  *     int *state, statenum, transnum;
  1496.  *     place_state( state, statenum, transnum );
  1497.  *
  1498.  * State is the statenum'th state.  It is indexed by equivalence class and
  1499.  * gives the number of the state to enter for a given equivalence class.
  1500.  * Transnum is the number of out-transitions for the state.
  1501.  */
  1502.  
  1503. void place_state( state, statenum, transnum )
  1504. int *state, statenum, transnum;
  1505.  
  1506. {
  1507.     register int i;
  1508.     register int *state_ptr;
  1509.     int position = find_table_space( state, transnum );
  1510.  
  1511.     /* base is the table of start positions */
  1512.     base[statenum] = position;
  1513.  
  1514.     /* put in action number marker; this non-zero number makes sure that
  1515.      * find_table_space() knows that this position in chk/nxt is taken
  1516.      * and should not be used for another accepting number in another state
  1517.      */
  1518.     chk[position - 1] = 1;
  1519.  
  1520.     /* put in end-of-buffer marker; this is for the same purposes as above */
  1521.     chk[position] = 1;
  1522.  
  1523.     /* place the state into chk and nxt */
  1524.     state_ptr = &state[1];
  1525.  
  1526.     for ( i = 1; i <= numecs; ++i, ++state_ptr )
  1527.         if ( *state_ptr != 0 )
  1528.         {
  1529.             chk[position + i] = i;
  1530.             nxt[position + i] = *state_ptr;
  1531.         }
  1532.  
  1533.     if ( position + numecs > tblend )
  1534.         tblend = position + numecs;
  1535. }
  1536.  
  1537.  
  1538. /* stack1 - save states with only one out-transition to be processed later
  1539.  *
  1540.  * synopsis
  1541.  *   int statenum, sym, nextstate, deflink;
  1542.  *   stack1( statenum, sym, nextstate, deflink );
  1543.  *
  1544.  * if there's room for another state one the "one-transition" stack, the
  1545.  * state is pushed onto it, to be processed later by mk1tbl.  If there's
  1546.  * no room, we process the sucker right now.
  1547.  */
  1548.  
  1549. void stack1( statenum, sym, nextstate, deflink )
  1550. int statenum, sym, nextstate, deflink;
  1551.  
  1552. {
  1553.     if ( onesp >= ONE_STACK_SIZE )
  1554.         mk1tbl( statenum, sym, nextstate, deflink );
  1555.  
  1556.     else
  1557.     {
  1558.         ++onesp;
  1559.         onestate[onesp] = statenum;
  1560.         onesym[onesp] = sym;
  1561.         onenext[onesp] = nextstate;
  1562.         onedef[onesp] = deflink;
  1563.     }
  1564. }
  1565.  
  1566.  
  1567. /* tbldiff - compute differences between two state tables
  1568.  *
  1569.  * synopsis
  1570.  *   int state[], pr, ext[];
  1571.  *   int tbldiff, numdifferences;
  1572.  *   numdifferences = tbldiff( state, pr, ext )
  1573.  *
  1574.  * "state" is the state array which is to be extracted from the pr'th
  1575.  * proto.  "pr" is both the number of the proto we are extracting from
  1576.  * and an index into the save area where we can find the proto's complete
  1577.  * state table.  Each entry in "state" which differs from the corresponding
  1578.  * entry of "pr" will appear in "ext".
  1579.  * Entries which are the same in both "state" and "pr" will be marked
  1580.  * as transitions to "SAME_TRANS" in "ext".  The total number of differences
  1581.  * between "state" and "pr" is returned as function value.  Note that this
  1582.  * number is "numecs" minus the number of "SAME_TRANS" entries in "ext".
  1583.  */
  1584.  
  1585. int tbldiff( state, pr, ext )
  1586. int state[], pr, ext[];
  1587.  
  1588. {
  1589.     register int i, *sp = state, *ep = ext, *protp;
  1590.     register int numdiff = 0;
  1591.  
  1592.     protp = &protsave[numecs * (pr - 1)];
  1593.  
  1594.     for ( i = numecs; i > 0; --i )
  1595.     {
  1596.         if ( *++protp == *++sp )
  1597.             *++ep = SAME_TRANS;
  1598.         else
  1599.         {
  1600.             *++ep = *sp;
  1601.             ++numdiff;
  1602.         }
  1603.     }
  1604.  
  1605.     return ( numdiff );
  1606. }
  1607.