home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / bison-1.25-src.tgz / tar.out / fsf / bison / output.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  29KB  |  1,486 lines

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