home *** CD-ROM | disk | FTP | other *** search
/ ftp.mactech.com 2010 / ftp.mactech.com.tar / ftp.mactech.com / online / source / c / compilers / Bison.sit.hqx / Bison / Source / output.c < prev    next >
Text File  |  1992-08-21  |  28KB  |  1,510 lines

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