home *** CD-ROM | disk | FTP | other *** search
/ APDL Public Domain 1 / APDL_PD1A.iso / program / language / bison / Old_Bison / C / Output < prev    next >
Encoding:
Text File  |  1990-04-04  |  22.0 KB  |  1,196 lines

  1. /* Output the generated parsing program for bison,
  2.    Copyright (C) 1984, 1986, 1989 Free Software Foundation, Inc.
  3.  
  4. This file is part of Bison, the GNU Compiler Compiler.
  5.  
  6. Bison is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. Bison is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with Bison; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20.  
  21. /* functions to output parsing data to various files.  Entries are:
  22.  
  23.   output_headers ()
  24.  
  25. Output constant strings to the beginning of certain files.
  26.  
  27.   output_trailers()
  28.  
  29. Output constant strings to the ends of certain files.
  30.  
  31.   output ()
  32.  
  33. Output the parsing tables and the parser code to ftable.
  34.  
  35. The parser tables consist of:  (starred ones needed only for the semantic parser)
  36.  
  37. yytranslate = vector mapping yylex's token numbers into bison's token numbers.
  38.  
  39. yytname = vector of string-names indexed by bison token number
  40.  
  41. yyrline = vector of line-numbers of all rules.  For yydebug printouts.
  42.  
  43. * yyrhs = vector of items of all rules.
  44.         This is exactly what ritems contains.
  45.  
  46. * yyprhs[r] = index in yyrhs of first item for rule r.
  47.  
  48. yyr1[r] = symbol number of symbol that rule r derives.
  49.  
  50. yyr2[r] = number of symbols composing right hand side of rule r.
  51.  
  52. * yystos[s] = the symbol number of the symbol that leads to state s.
  53.  
  54. yydefact[s] = default rule to reduce with in state s,
  55.           when yytable doesn't specify something else to do.
  56.           Zero means the default is an error.
  57.  
  58. yydefgoto[i] = default state to go to after a reduction of a rule that
  59.            generates variable ntokens + i, except when yytable
  60.            specifies something else to do.
  61.  
  62. yypact[s] = index in yytable of the portion describing state s.
  63.             The lookahed token's type is used to index that portion
  64.             to find out what to do.
  65.  
  66.         If the value in yytable is positive,
  67.         we shift the token and go to that state.
  68.  
  69.         If the value is negative, it is minus a rule number to reduce by.
  70.  
  71.         If the value is zero, the default action from yydefact[s] is used.
  72.  
  73. yypgoto[i] = the index in yytable of the portion describing 
  74.              what to do after reducing a rule that derives variable i + ntokens.
  75.              This portion is indexed by the parser state number
  76.          as of before the text for this nonterminal was read.
  77.          The value from yytable is the state to go to.
  78.  
  79. yytable = a vector filled with portions for different uses,
  80.           found via yypact and yypgoto.
  81.  
  82. yycheck = a vector indexed in parallel with yytable.
  83.       It indicates, in a roundabout way, the bounds of the
  84.       portion you are trying to examine.
  85.  
  86.       Suppose that the portion of yytable starts at index p
  87.       and the index to be examined within the portion is i.
  88.       Then if yycheck[p+i] != i, i is outside the bounds
  89.       of what is actually allocated, and the default
  90.       (from yydefact or yydefgoto) should be used.
  91.       Otherwise, yytable[p+i] should be used.
  92.  
  93. YYFINAL = the state number of the termination state.
  94. YYFLAG = most negative short int.  Used to flag ??
  95. YYNTBASE = ntokens.
  96.  
  97. */
  98.  
  99. #include <stdio.h>
  100. #include "system.h"
  101. #include "machine.h"
  102. #include "new.h"
  103. #include "files.h"
  104. #include "gram.h"
  105. #include "state.h"
  106.  
  107.  
  108. extern int debugflag;
  109. extern int nolinesflag;
  110.  
  111. extern char **tags;
  112. extern int tokensetsize;
  113. extern int final_state;
  114. extern core **state_table;
  115. extern shifts **shift_table;
  116. extern errs **err_table;
  117. extern reductions **reduction_table;
  118. extern short *accessing_symbol;
  119. extern unsigned *LA;
  120. extern short *LAruleno;
  121. extern short *lookaheads;
  122. extern char *consistent;
  123. extern short *goto_map;
  124. extern short *from_state;
  125. extern short *to_state;
  126.  
  127. static int nvectors;
  128. static int nentries;
  129. static short **froms;
  130. static short **tos;
  131. static short *tally;
  132. static short *width;
  133. static short *actrow;
  134. static short *state_count;
  135. static short *order;
  136. static short *base;
  137. static short *pos;
  138. static short *table;
  139. static short *check;
  140. static int lowzero;
  141. static int high;
  142.  
  143.  
  144. #define    ACTSTR    "\n  switch (yyn) {\n"
  145.  
  146. void output_headers(void)
  147. {
  148.   fprintf(faction, ACTSTR);
  149. }
  150.  
  151.  
  152. void output_trailers(void)
  153. {
  154.   fprintf(faction, "\n}\n");
  155. }
  156.  
  157.  
  158. void output(void)
  159. {
  160.   int c;
  161.  
  162.   rewind(fattrs);
  163.   while ((c=getc(fattrs))!=EOF)
  164.     putc(c,ftable);
  165.  
  166.   fprintf(ftable, "\n#ifndef YYDEBUG\n#define YYDEBUG %d\n#endif\n\n",
  167.       !!debugflag);
  168.  
  169.   fprintf(ftable, "#include <stdio.h>\n");
  170.   fprintf(ftable, "#ifdef __STDC__\n#include <stdlib.h>\n#endif\n\n");
  171.  
  172.   /* Make "const" do nothing if not in ANSI C.  */
  173.   fprintf (ftable, "#ifndef __STDC__\n#define const\n#endif\n\n");
  174.  
  175.   free_itemsets();
  176.   output_defines();
  177.   output_token_translations();
  178.   FREE(ritem);
  179.   output_rule_data();
  180.   output_actions();
  181.   output_parser();
  182.   output_program();
  183. }
  184.  
  185.  
  186. void output_token_translations(void)
  187. {
  188.   register int i, j;
  189.  
  190.   if (translations)
  191.     {
  192.       fprintf(ftable,
  193.           "\n#define YYTRANSLATE(x) ((unsigned)(x) <= %d ? yytranslate[x] : %d)\n",
  194.           max_user_token_number, nsyms);
  195.     
  196.       if (ntokens < 127)  /* play it very safe; check maximum element value.  */
  197.         fprintf(ftable, "\nstatic const char yytranslate[] = {     0");
  198.       else
  199.     fprintf(ftable, "\nstatic const short yytranslate[] = {     0");
  200.     
  201.       j = 10;
  202.       for (i = 1; i <= max_user_token_number; i++)
  203.     {
  204.       putc(',', ftable);
  205.     
  206.       if (j >= 10)
  207.         {
  208.           putc('\n', ftable);
  209.           j = 1;
  210.         }
  211.       else
  212.         {
  213.           j++;
  214.         }
  215.     
  216.       fprintf(ftable, "%6d", token_translations[i]);
  217.     }
  218.     
  219.       fprintf(ftable, "\n};\n");
  220.     }
  221.   else
  222.     {
  223.       fprintf(ftable, "\n#define YYTRANSLATE(x) (x)\n");
  224.     } 
  225. }
  226.  
  227.  
  228. void output_rule_data(void)
  229. {
  230.   register int i;
  231.   register int j;
  232.  
  233.   fprintf(ftable, "\n#if YYDEBUG != 0");
  234.   fprintf(ftable, "\nstatic const short yyrline[] = {     0");
  235.  
  236.   j = 10;
  237.   for (i = 1; i <= nrules; i++)
  238.     {
  239.       putc(',', ftable);
  240.  
  241.       if (j >= 10)
  242.     {
  243.       putc('\n', ftable);
  244.       j = 1;
  245.     }
  246.       else
  247.     {
  248.       j++;
  249.     }
  250.  
  251.       fprintf(ftable, "%6d", rline[i]);
  252.     }
  253.  
  254.   /* Output the table of token names.  */
  255.  
  256.   fprintf(ftable, "\n};\n");
  257.   fprintf(ftable, "\nstatic const char * const yytname[] = {     0");
  258.  
  259.   j = 10;
  260.   for (i = 1; i <= ntokens; i++)
  261.     {
  262.       register char *p;
  263.       putc(',', ftable);
  264.  
  265.       if (j >= 10)
  266.     {
  267.       putc('\n', ftable);
  268.       j = 1;
  269.     }
  270.       else
  271.     {
  272.       j++;
  273.     }
  274.  
  275.       putc ('\"', ftable);
  276.  
  277.       for (p = tags[i]; *p; p++)
  278.     if (*p == '"' || *p == '\\')
  279.       fprintf(ftable, "\\%c", *p);
  280.     else if (*p == '\n')
  281.       fprintf(ftable, "\\n");
  282.     else if (*p == '\t')
  283.       fprintf(ftable, "\\t");
  284.     else if (*p == '\b')
  285.       fprintf(ftable, "\\b");
  286.     else if (*p < 040 || *p >= 0177)
  287.       fprintf(ftable, "\\%03o", *p);
  288.     else
  289.       putc(*p, ftable);
  290.  
  291.       putc ('\"', ftable);
  292.     }
  293.  
  294.   fprintf(ftable, "\n};\n");
  295.   fprintf(ftable, "#endif\n\n");
  296.   fprintf(ftable, "static const short yyr1[] = {     0");
  297.  
  298.   j = 10;
  299.   for (i = 1; i <= nrules; i++)
  300.     {
  301.       putc(',', ftable);
  302.  
  303.       if (j >= 10)
  304.     {
  305.       putc('\n', ftable);
  306.       j = 1;
  307.     }
  308.       else
  309.     {
  310.       j++;
  311.     }
  312.  
  313.       fprintf(ftable, "%6d", rlhs[i]);
  314.     }
  315.  
  316.   FREE(rlhs + 1);
  317.  
  318.   fprintf(ftable, "\n};\n\nstatic const short yyr2[] = {     0");
  319.  
  320.   j = 10;
  321.   for (i = 1; i < nrules; i++)
  322.     {
  323.       putc(',', ftable);
  324.  
  325.       if (j >= 10)
  326.     {
  327.       putc('\n', ftable);
  328.       j = 1;
  329.     }
  330.       else
  331.     {
  332.       j++;
  333.     }
  334.  
  335.       fprintf(ftable, "%6d", rrhs[i + 1] - rrhs[i] - 1);
  336.     }
  337.  
  338.   putc(',', ftable);
  339.   if (j >= 10)
  340.     putc('\n', ftable);
  341.  
  342.   fprintf(ftable, "%6d\n};\n", nitems - rrhs[nrules] - 1);
  343.   FREE(rrhs + 1);
  344. }
  345.  
  346.  
  347. void output_defines(void)
  348. {
  349.   fprintf(ftable, "#define\tYYFINAL\t\t%d\n", final_state);
  350.   fprintf(ftable, "#define\tYYFLAG\t\t%d\n", MINSHORT);
  351.   fprintf(ftable, "#define\tYYNTBASE\t%d\n", ntokens);
  352. }
  353.  
  354.  
  355.  
  356. /* compute and output yydefact, yydefgoto, yypact, yypgoto, yytable and yycheck.  */
  357.  
  358. void output_actions(void)
  359. {
  360.   nvectors = nstates + nvars;
  361.  
  362.   froms = NEW2(nvectors, short *);
  363.   tos = NEW2(nvectors, short *);
  364.   tally = NEW2(nvectors, short);
  365.   width = NEW2(nvectors, short);
  366.  
  367.   token_actions();
  368.   free_shifts();
  369.   free_reductions();
  370.   FREE(lookaheads);
  371.   FREE(LA);
  372.   FREE(LAruleno);
  373.   FREE(accessing_symbol);
  374.  
  375.   goto_actions();
  376.   FREE(goto_map + ntokens);
  377.   FREE(from_state);
  378.   FREE(to_state);
  379.  
  380.   sort_actions();
  381.   pack_table();
  382.   output_base();
  383.   output_table();
  384.   output_check();
  385. }
  386.  
  387.  
  388.  
  389. /* figure out the actions for the specified state, indexed by lookahead token type.
  390.  
  391.    The yydefact table is output now.  The detailed info
  392.    is saved for putting into yytable later.  */
  393.  
  394. void token_actions(void)
  395. {
  396.   register int i;
  397.   register int j;
  398.   register int k;
  399.  
  400.   actrow = NEW2(ntokens, short);
  401.  
  402.   k = action_row(0);
  403.   fprintf(ftable, "\nstatic const short yydefact[] = {%6d", k);
  404.   save_row(0);
  405.  
  406.   j = 10;
  407.   for (i = 1; i < nstates; i++)
  408.     {
  409.       putc(',', ftable);
  410.  
  411.       if (j >= 10)
  412.     {
  413.       putc('\n', ftable);
  414.       j = 1;
  415.     }
  416.       else
  417.     {
  418.       j++;
  419.     }
  420.  
  421.       k = action_row(i);
  422.       fprintf(ftable, "%6d", k);
  423.       save_row(i);
  424.     }
  425.  
  426.   fprintf(ftable, "\n};\n");
  427.   FREE(actrow);
  428. }
  429.  
  430.  
  431.  
  432. /* Decide what to do for each type of token if seen as the lookahead token in specified state.
  433.    The value returned is used as the default action (yydefact) for the state.
  434.    In addition, actrow is filled with what to do for each kind of token,
  435.    index by symbol number, with zero meaning do the default action.
  436.    The value MINSHORT, a very negative number, means this situation
  437.    is an error.  The parser recognizes this value specially.
  438.  
  439.    This is where conflicts are resolved.  The loop over lookahead rules
  440.    considered lower-numbered rules last, and the last rule considered that likes
  441.    a token gets to handle it.  */
  442.  
  443. int action_row(int state)
  444. {
  445.   register int i;
  446.   register int j;
  447.   register int k;
  448.   register int m;
  449.   register int n;
  450.   register int count;
  451.   register int default_rule;
  452.   register int nreds;
  453.   register int max;
  454.   register int rule;
  455.   register int shift_state;
  456.   register int symbol;
  457.   register unsigned mask;
  458.   register unsigned *wordp;
  459.   register reductions *redp;
  460.   register shifts *shiftp;
  461.   register errs *errp;
  462.   int nodefault = 0;  /* set nonzero to inhibit having any default reduction */
  463.  
  464.   for (i = 0; i < ntokens; i++)
  465.     actrow[i] = 0;
  466.  
  467.   default_rule = 0;
  468.   nreds = 0;
  469.   redp = reduction_table[state];
  470.  
  471.   if (redp)
  472.     {
  473.       nreds = redp->nreds;
  474.  
  475.       if (nreds >= 1)
  476.     {
  477.       /* loop over all the rules available here which require lookahead */
  478.       m = lookaheads[state];
  479.       n = lookaheads[state + 1];
  480.  
  481.       for (i = n - 1; i >= m; i--)
  482.         {
  483.           rule = - LAruleno[i];
  484.           wordp = LA + i * tokensetsize;
  485.           mask = 1;
  486.  
  487.           /* and find each token which the rule finds acceptable to come next */
  488.           for (j = 0; j < ntokens; j++)
  489.         {
  490.           /* and record this rule as the rule to use if that token follows.  */
  491.           if (mask & *wordp)
  492.             actrow[j] = rule;
  493.  
  494.           mask <<= 1;
  495.           if (mask == 0)
  496.             {
  497.               mask = 1;
  498.               wordp++;
  499.             }
  500.         }
  501.         }
  502.     }
  503.     }
  504.  
  505.   shiftp = shift_table[state];
  506.  
  507.   /* now see which tokens are allowed for shifts in this state.
  508.      For them, record the shift as the thing to do.  So shift is preferred to reduce.  */
  509.  
  510.   if (shiftp)
  511.     {
  512.       k = shiftp->nshifts;
  513.  
  514.       for (i = 0; i < k; i++)
  515.     {
  516.       shift_state = shiftp->shifts[i];
  517.       if (! shift_state) continue;
  518.  
  519.       symbol = accessing_symbol[shift_state];
  520.  
  521.       if (ISVAR(symbol))
  522.         break;
  523.  
  524.       actrow[symbol] = shift_state;
  525.  
  526.       /* do not use any default reduction if there is a shift for error */
  527.  
  528.       if (symbol == error_token_number) nodefault = 1;
  529.     }
  530.     }
  531.  
  532.   errp = err_table[state];
  533.  
  534.   /* See which tokens are an explicit error in this state
  535.      (due to %nonassoc).  For them, record MINSHORT as the action.  */
  536.  
  537.   if (errp)
  538.     {
  539.       k = errp->nerrs;
  540.  
  541.       for (i = 0; i < k; i++)
  542.     {
  543.       symbol = errp->errs[i];
  544.       actrow[symbol] = MINSHORT;
  545.     }
  546.     }
  547.  
  548.   /* now find the most common reduction and make it the default action for this state.  */
  549.  
  550.   if (nreds >= 1 && ! nodefault)
  551.     {
  552.       if (consistent[state])
  553.     default_rule = redp->rules[0];
  554.       else
  555.     {
  556.       max = 0;
  557.       for (i = m; i < n; i++)
  558.         {
  559.           count = 0;
  560.           rule = - LAruleno[i];
  561.     
  562.           for (j = 0; j < ntokens; j++)
  563.         {
  564.           if (actrow[j] == rule)
  565.             count++;
  566.         }
  567.     
  568.           if (count > max)
  569.         {
  570.           max = count;
  571.           default_rule = rule;
  572.         }
  573.         }
  574.     
  575.       /* actions which match the default are replaced with zero,
  576.          which means "use the default" */
  577.     
  578.       if (max > 0)
  579.         {
  580.           for (j = 0; j < ntokens; j++)
  581.         {
  582.           if (actrow[j] == default_rule)
  583.             actrow[j] = 0;
  584.         }
  585.     
  586.           default_rule = - default_rule;
  587.         }
  588.     }
  589.     }
  590.  
  591.   /* If have no default rule, the default is an error.
  592.      So replace any action which says "error" with "use default".  */
  593.  
  594.   if (default_rule == 0)
  595.     for (j = 0; j < ntokens; j++)
  596.       {
  597.     if (actrow[j] == MINSHORT)
  598.       actrow[j] = 0;
  599.       }
  600.  
  601.   return (default_rule);
  602. }
  603.  
  604.  
  605. void save_row(int state)
  606. {
  607.   register int i;
  608.   register int count;
  609.   register short *sp;
  610.   register short *sp1;
  611.   register short *sp2;
  612.  
  613.   count = 0;
  614.   for (i = 0; i < ntokens; i++)
  615.     {
  616.       if (actrow[i] != 0)
  617.     count++;
  618.     }
  619.  
  620.   if (count == 0)
  621.     return;
  622.  
  623.   froms[state] = sp1 = sp = NEW2(count, short);
  624.   tos[state] = sp2 = NEW2(count, short);
  625.  
  626.   for (i = 0; i < ntokens; i++)
  627.     {
  628.       if (actrow[i] != 0)
  629.     {
  630.       *sp1++ = i;
  631.       *sp2++ = actrow[i];
  632.     }
  633.     }
  634.  
  635.   tally[state] = count;
  636.   width[state] = sp1[-1] - sp[0] + 1;
  637. }
  638.  
  639.  
  640.  
  641. /* figure out what to do after reducing with each rule,
  642.    depending on the saved state from before the beginning
  643.    of parsing the data that matched this rule.
  644.  
  645.    The yydefgoto table is output now.  The detailed info
  646.    is saved for putting into yytable later.  */
  647.  
  648. void goto_actions(void)
  649. {
  650.   register int i;
  651.   register int j;
  652.   register int k;
  653.  
  654.   state_count = NEW2(nstates, short);
  655.  
  656.   k = default_goto(ntokens);
  657.   fprintf(ftable, "\nstatic const short yydefgoto[] = {%6d", k);
  658.   save_column(ntokens, k);
  659.  
  660.   j = 10;
  661.   for (i = ntokens + 1; i < nsyms; i++)
  662.     {
  663.       putc(',', ftable);
  664.  
  665.       if (j >= 10)
  666.     {
  667.       putc('\n', ftable);
  668.       j = 1;
  669.     }
  670.       else
  671.     {
  672.       j++;
  673.     }
  674.  
  675.       k = default_goto(i);
  676.       fprintf(ftable, "%6d", k);
  677.       save_column(i, k);
  678.     }
  679.  
  680.   fprintf(ftable, "\n};\n");
  681.   FREE(state_count);
  682. }
  683.  
  684.  
  685.  
  686. int default_goto(int symbol)
  687. {
  688.   register int i;
  689.   register int m;
  690.   register int n;
  691.   register int default_state;
  692.   register int max;
  693.  
  694.   m = goto_map[symbol];
  695.   n = goto_map[symbol + 1];
  696.  
  697.   if (m == n)
  698.     return (-1);
  699.  
  700.   for (i = 0; i < nstates; i++)
  701.     state_count[i] = 0;
  702.  
  703.   for (i = m; i < n; i++)
  704.     state_count[to_state[i]]++;
  705.  
  706.   max = 0;
  707.   default_state = -1;
  708.  
  709.   for (i = 0; i < nstates; i++)
  710.     {
  711.       if (state_count[i] > max)
  712.     {
  713.       max = state_count[i];
  714.       default_state = i;
  715.     }
  716.     }
  717.  
  718.   return (default_state);
  719. }
  720.  
  721.  
  722. void save_column(int symbol, int default_state)
  723. {
  724.   register int i;
  725.   register int m;
  726.   register int n;
  727.   register short *sp;
  728.   register short *sp1;
  729.   register short *sp2;
  730.   register int count;
  731.   register int symno;
  732.  
  733.   m = goto_map[symbol];
  734.   n = goto_map[symbol + 1];
  735.  
  736.   count = 0;
  737.   for (i = m; i < n; i++)
  738.     {
  739.       if (to_state[i] != default_state)
  740.     count++;
  741.     }
  742.  
  743.   if (count == 0)
  744.     return;
  745.  
  746.   symno = symbol - ntokens + nstates;
  747.  
  748.   froms[symno] = sp1 = sp = NEW2(count, short);
  749.   tos[symno] = sp2 = NEW2(count, short);
  750.  
  751.   for (i = m; i < n; i++)
  752.     {
  753.       if (to_state[i] != default_state)
  754.     {
  755.       *sp1++ = from_state[i];
  756.       *sp2++ = to_state[i];
  757.     }
  758.     }
  759.  
  760.   tally[symno] = count;
  761.   width[symno] = sp1[-1] - sp[0] + 1;
  762. }
  763.  
  764.  
  765.  
  766. /* the next few functions decide how to pack 
  767.    the actions and gotos information into yytable. */
  768.  
  769. void sort_actions(void)
  770. {
  771.   register int i;
  772.   register int j;
  773.   register int k;
  774.   register int t;
  775.   register int w;
  776.  
  777.   order = NEW2(nvectors, short);
  778.   nentries = 0;
  779.  
  780.   for (i = 0; i < nvectors; i++)
  781.     {
  782.       if (tally[i] > 0)
  783.     {
  784.       t = tally[i];
  785.       w = width[i];
  786.       j = nentries - 1;
  787.  
  788.       while (j >= 0 && (width[order[j]] < w))
  789.         j--;
  790.  
  791.       while (j >= 0 && (width[order[j]] == w) && (tally[order[j]] < t))
  792.         j--;
  793.  
  794.       for (k = nentries - 1; k > j; k--)
  795.         order[k + 1] = order[k];
  796.  
  797.       order[j + 1] = i;
  798.       nentries++;
  799.     }
  800.     }
  801. }
  802.  
  803.  
  804. void pack_table(void)
  805. {
  806.   register int i;
  807.   register int place;
  808.   register int state;
  809.  
  810.   base = NEW2(nvectors, short);
  811.   pos = NEW2(nentries, short);
  812.   table = NEW2(MAXTABLE, short);
  813.   check = NEW2(MAXTABLE, short);
  814.  
  815.   lowzero = 0;
  816.   high = 0;
  817.  
  818.   for (i = 0; i < nvectors; i++)
  819.     base[i] = MINSHORT;
  820.  
  821.   for (i = 0; i < MAXTABLE; i++)
  822.     check[i] = -1;
  823.  
  824.   for (i = 0; i < nentries; i++)
  825.     {
  826.       state = matching_state(i);
  827.  
  828.       if (state < 0)
  829.     place = pack_vector(i);
  830.       else
  831.     place = base[state];
  832.  
  833.       pos[i] = place;
  834.       base[order[i]] = place;
  835.     }
  836.  
  837.   for (i = 0; i < nvectors; i++)
  838.     {
  839.       if (froms[i])
  840.     FREE(froms[i]);
  841.       if (tos[i])
  842.     FREE(tos[i]);
  843.     }
  844.  
  845.   FREE(froms);
  846.   FREE(tos);
  847.   FREE(pos);
  848. }
  849.  
  850.  
  851.  
  852. int matching_state(int vector)
  853. {
  854.   register int i;
  855.   register int j;
  856.   register int k;
  857.   register int t;
  858.   register int w;
  859.   register int match;
  860.   register int prev;
  861.  
  862.   i = order[vector];
  863.   if (i >= nstates)
  864.     return (-1);
  865.  
  866.   t = tally[i];
  867.   w = width[i];
  868.  
  869.   for (prev = vector - 1; prev >= 0; prev--)
  870.     {
  871.       j = order[prev];
  872.       if (width[j] != w || tally[j] != t)
  873.     return (-1);
  874.  
  875.       match = 1;
  876.       for (k = 0; match && k < t; k++)
  877.     {
  878.       if (tos[j][k] != tos[i][k] || froms[j][k] != froms[i][k])
  879.         match = 0;
  880.     }
  881.  
  882.       if (match)
  883.     return (j);
  884.     }
  885.  
  886.   return (-1);
  887. }
  888.  
  889.  
  890.  
  891. int pack_vector(int vector)
  892. {
  893.   register int i;
  894.   register int j;
  895.   register int k;
  896.   register int t;
  897.   register int loc;
  898.   register int ok;
  899.   register short *from;
  900.   register short *to;
  901.  
  902.   i = order[vector];
  903.   t = tally[i];
  904.  
  905.   if (t == 0)
  906.     berror("pack_vector");
  907.  
  908.   from = froms[i];
  909.   to = tos[i];
  910.  
  911.   for (j = lowzero - from[0]; j < MAXTABLE; j++)
  912.     {
  913.       ok = 1;
  914.  
  915.       for (k = 0; ok && k < t; k++)
  916.     {
  917.       loc = j + from[k];
  918.       if (loc > MAXTABLE)
  919.         fatals("maximum table size (%d) exceeded",MAXTABLE);
  920.  
  921.       if (table[loc] != 0)
  922.         ok = 0;
  923.     }
  924.  
  925.       for (k = 0; ok && k < vector; k++)
  926.     {
  927.       if (pos[k] == j)
  928.         ok = 0;
  929.     }
  930.  
  931.       if (ok)
  932.     {
  933.       for (k = 0; k < t; k++)
  934.         {
  935.           loc = j + from[k];
  936.           table[loc] = to[k];
  937.           check[loc] = from[k];
  938.         }
  939.  
  940.       while (table[lowzero] != 0)
  941.         lowzero++;
  942.  
  943.       if (loc > high)
  944.         high = loc;
  945.  
  946.       return (j);
  947.     }
  948.     }
  949.  
  950.   berror("pack_vector");
  951.   return 0;    /* JF keep lint happy */
  952. }
  953.  
  954.  
  955.  
  956. /* the following functions output yytable, yycheck
  957.    and the vectors whose elements index the portion starts */
  958.  
  959. void output_base(void)
  960. {
  961.   register int i;
  962.   register int j;
  963.  
  964.   fprintf(ftable, "\nstatic const short yypact[] = {%6d", base[0]);
  965.  
  966.   j = 10;
  967.   for (i = 1; i < nstates; i++)
  968.     {
  969.       putc(',', ftable);
  970.  
  971.       if (j >= 10)
  972.     {
  973.       putc('\n', ftable);
  974.       j = 1;
  975.     }
  976.       else
  977.     {
  978.       j++;
  979.     }
  980.  
  981.       fprintf(ftable, "%6d", base[i]);
  982.     }
  983.  
  984.   fprintf(ftable, "\n};\n\nstatic const short yypgoto[] = {%6d", base[nstates]);
  985.  
  986.   j = 10;
  987.   for (i = nstates + 1; i < nvectors; i++)
  988.     {
  989.       putc(',', ftable);
  990.  
  991.       if (j >= 10)
  992.     {
  993.       putc('\n', ftable);
  994.       j = 1;
  995.     }
  996.       else
  997.     {
  998.       j++;
  999.     }
  1000.  
  1001.       fprintf(ftable, "%6d", base[i]);
  1002.     }
  1003.  
  1004.   fprintf(ftable, "\n};\n");
  1005.   FREE(base);
  1006. }
  1007.  
  1008.  
  1009. void output_table(void)
  1010. {
  1011.   register int i;
  1012.   register int j;
  1013.  
  1014.   fprintf(ftable, "\n\n#define\tYYLAST\t\t%d\n\n", high);
  1015.   fprintf(ftable, "\nstatic const short yytable[] = {%6d", table[0]);
  1016.  
  1017.   j = 10;
  1018.   for (i = 1; i <= high; i++)
  1019.     {
  1020.       putc(',', ftable);
  1021.  
  1022.       if (j >= 10)
  1023.     {
  1024.       putc('\n', ftable);
  1025.       j = 1;
  1026.     }
  1027.       else
  1028.     {
  1029.       j++;
  1030.     }
  1031.  
  1032.       fprintf(ftable, "%6d", table[i]);
  1033.     }
  1034.  
  1035.   fprintf(ftable, "\n};\n");
  1036.   FREE(table);
  1037. }
  1038.  
  1039.  
  1040. void output_check(void)
  1041. {
  1042.   register int i;
  1043.   register int j;
  1044.  
  1045.   fprintf(ftable, "\nstatic const short yycheck[] = {%6d", check[0]);
  1046.  
  1047.   j = 10;
  1048.   for (i = 1; i <= high; i++)
  1049.     {
  1050.       putc(',', ftable);
  1051.  
  1052.       if (j >= 10)
  1053.     {
  1054.       putc('\n', ftable);
  1055.       j = 1;
  1056.     }
  1057.       else
  1058.     {
  1059.       j++;
  1060.     }
  1061.  
  1062.       fprintf(ftable, "%6d", check[i]);
  1063.     }
  1064.  
  1065.   fprintf(ftable, "\n};\n");
  1066.   FREE(check);
  1067. }
  1068.  
  1069.  
  1070.  
  1071. /* copy the parser code into the ftable file at the end.  */
  1072.  
  1073. void output_parser(void)
  1074. {
  1075.   register int c;
  1076.  
  1077.   if (pure_parser)
  1078.     fprintf(ftable, "#define YYIMPURE 1\n\n");
  1079.   else
  1080.     fprintf(ftable, "#define YYPURE 1\n\n");
  1081.  
  1082.   /* Loop over lines in the standard parser file.  */
  1083.  
  1084.   while (1)
  1085.     {
  1086.       int write_line = 1;
  1087.  
  1088.       c = getc(fparser);
  1089.  
  1090.       /* See if the line starts with `#line.
  1091.      If so, set write_line to 0.  */
  1092.       if (nolinesflag)
  1093.     if (c == '#') 
  1094.       {
  1095.         c = getc(fparser);
  1096.         if (c == 'l')
  1097.           {
  1098.         c = getc(fparser);
  1099.         if (c == 'i')
  1100.           {
  1101.             c = getc(fparser);
  1102.             if (c == 'n')
  1103.               {
  1104.             c = getc(fparser);
  1105.             if (c == 'e')
  1106.               write_line = 0;
  1107.             else
  1108.               fprintf(ftable, "#lin");
  1109.               }
  1110.             else
  1111.               fprintf(ftable, "#li");
  1112.           }
  1113.         else
  1114.           fprintf(ftable, "#l");
  1115.           }
  1116.         else
  1117.           fprintf(ftable, "#");
  1118.       }
  1119.  
  1120.       /* now write out the line... */
  1121.       for ( ; c != '\n' && c != EOF; c = getc(fparser))
  1122.     if (write_line)
  1123.       {
  1124.         if (c == '$')
  1125.           {
  1126.             /* `$' in the parser file indicates where to put the actions.
  1127.            Copy them in at this point.  */
  1128.             rewind(faction);
  1129.             for(c=getc(faction);c!=EOF;c=getc(faction))
  1130.           putc(c,ftable);
  1131.           }
  1132.         else
  1133.           putc(c, ftable);
  1134.       }
  1135.       if (c == EOF)
  1136.     break;
  1137.       putc(c, ftable);
  1138.     }
  1139. }
  1140.  
  1141.  
  1142. void output_program(void)
  1143. {
  1144.   register int c;
  1145.   extern int lineno;
  1146.  
  1147.   if (!nolinesflag)
  1148.     fprintf(ftable, "#line %d \"%s\"\n", lineno, infile);
  1149.  
  1150.   c = getc(finput);
  1151.   while (c != EOF)
  1152.     {
  1153.       putc(c, ftable);
  1154.       c = getc(finput);
  1155.     }
  1156. }
  1157.  
  1158.  
  1159. void free_itemsets(void)
  1160. {
  1161.   register core *cp,*cptmp;
  1162.  
  1163.   FREE(state_table);
  1164.  
  1165.   for (cp = first_state; cp; cp = cptmp) {
  1166.     cptmp=cp->next;
  1167.     FREE(cp);
  1168.   }
  1169. }
  1170.  
  1171.  
  1172. void free_shifts(void)
  1173. {
  1174.   register shifts *sp,*sptmp;/* JF derefrenced freed ptr */
  1175.  
  1176.   FREE(shift_table);
  1177.  
  1178.   for (sp = first_shift; sp; sp = sptmp) {
  1179.     sptmp=sp->next;
  1180.     FREE(sp);
  1181.   }
  1182. }
  1183.  
  1184.  
  1185. void free_reductions(void)
  1186. {
  1187.   register reductions *rp,*rptmp;/* JF fixed freed ptr */
  1188.  
  1189.   FREE(reduction_table);
  1190.  
  1191.   for (rp = first_reduction; rp; rp = rptmp) {
  1192.     rptmp=rp->next;
  1193.     FREE(rp);
  1194.   }
  1195. }
  1196.