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

  1. /* Grammar reduction for Bison.
  2.    Copyright (C) 1988, 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. /*
  22.  * Reduce the grammar:  Find and eliminate unreachable terminals,
  23.  * nonterminals, and productions.  David S. Bakin.
  24.  */
  25.  
  26. /*
  27.  * Don't eliminate unreachable terminals:  They may be used by the user's
  28.  * parser.
  29.  */
  30.  
  31. #include <stdio.h>
  32. #include "system.h"
  33. #include "files.h"
  34. #include "gram.h"
  35. #include "machine.h"
  36. #include "new.h"
  37.  
  38.  
  39. extern char **tags;        /* reader.c */
  40. extern int verboseflag;        /* getargs.c */
  41. static int statisticsflag;
  42.  
  43. #define TRUE    (1)
  44. #define FALSE    (0)
  45.  
  46. typedef int bool;
  47. typedef unsigned *BSet;
  48. typedef short  *rule;
  49.  
  50. /*
  51.  * N is set of all nonterminals which are not useless.  P is set of all rules
  52.  * which have no useless nonterminals in their RHS.  V is the set of all
  53.  * accessible symbols.
  54.  */
  55.  
  56. static BSet     N, P, V, V1;
  57.  
  58. static int      nuseful_productions, nuseless_productions,
  59.                 nuseful_nonterminals, nuseless_nonterminals;
  60.  
  61.  
  62. static bool useful_production (int, BSet);
  63. static void useless_nonterminals (void);
  64. static void inaccessable_symbols (void);
  65. static void reduce_grammar_tables (void);
  66. static void print_results (void);
  67. static void print_notices (void);
  68. static bool bits_equal(BSet, BSet, int);
  69. static int nbits (unsigned);
  70. static int bits_size (BSet, int);
  71.  
  72.  
  73. static bool bits_equal(BSet L, BSet R, int n)
  74. {
  75.   int i;
  76.  
  77.   for (i = n - 1; i >= 0; i--)
  78.     if (L[i] != R[i])
  79.       return FALSE;
  80.   return TRUE;
  81. }
  82.  
  83.  
  84. static int nbits (unsigned i)
  85. {
  86.   int count = 0;
  87.  
  88.   while (i != 0) {
  89.     i ^= (i & -i);
  90.     ++count;
  91.   }
  92.   return count;
  93. }
  94.  
  95.  
  96. static int bits_size (BSet S, int n)
  97. {
  98.   int i, count = 0;
  99.  
  100.   for (i = n - 1; i >= 0; i--)
  101.     count += nbits(S[i]);
  102.   return count;
  103. }
  104.  
  105. void reduce_grammar (void)
  106. {
  107.   bool reduced;
  108.  
  109.   /* Allocate the global sets used to compute the reduced grammar */
  110.  
  111.   N = NEW2(WORDSIZE(nvars), unsigned);
  112.   P = NEW2(WORDSIZE(nrules + 1), unsigned);
  113.   V = NEW2(WORDSIZE(nsyms), unsigned);
  114.   V1 = NEW2(WORDSIZE(nsyms), unsigned);
  115.  
  116.   useless_nonterminals();
  117.   inaccessable_symbols();
  118.  
  119.   reduced = (bool) (nuseless_nonterminals + nuseless_productions > 0);
  120.  
  121.   if (verboseflag)
  122.     print_results();
  123.  
  124.   if (reduced == FALSE)
  125.     goto done_reducing;
  126.  
  127.   print_notices();
  128.  
  129.   if (!BITISSET(N, start_symbol - ntokens))
  130.     fatals("Start symbol %s does not derive any sentence.",
  131.        tags[start_symbol]);
  132.  
  133.   reduce_grammar_tables();
  134.   /* if (verboseflag) {
  135.      fprintf(foutput, "REDUCED GRAMMAR\n\n");
  136.      dump_grammar();
  137.      }
  138.      */
  139.  
  140.   /**/ statisticsflag = FALSE; /* someday getopts should handle this */
  141.   if (statisticsflag == TRUE)
  142.     fprintf(stderr,
  143.         "reduced %s defines %d terminal%s, %d nonterminal%s\
  144. , and %d production%s.\n", infile,
  145.         ntokens, (ntokens == 1 ? "" : "s"),
  146.         nvars,   (nvars   == 1 ? "" : "s"),
  147.         nrules,  (nrules  == 1 ? "" : "s"));
  148.  
  149.  done_reducing:
  150.  
  151.   /* Free the global sets used to compute the reduced grammar */
  152.  
  153.   FREE(N);
  154.   FREE(V);
  155.   FREE(P);
  156.  
  157. }
  158.  
  159. /*
  160.  * Another way to do this would be with a set for each production and then do
  161.  * subset tests against N, but even for the C grammar the whole reducing
  162.  * process takes only 2 seconds on my 8Mhz AT.
  163.  */
  164.  
  165. static bool useful_production (int i, BSet N)
  166. {
  167.   rule  r;
  168.   short n;
  169.  
  170.   /*
  171.    * A production is useful if all of the nonterminals in its RHS
  172.    * appear in the set of useful nonterminals.
  173.    */
  174.  
  175.   for (r = &ritem[rrhs[i]]; *r > 0; r++)
  176.     if (ISVAR(n = *r))
  177.       if (!BITISSET(N, n - ntokens))
  178.     return FALSE;
  179.   return TRUE;
  180. }
  181.  
  182.  
  183. /* Remember that rules are 1-origin, symbols are 0-origin. */
  184.  
  185. static void useless_nonterminals (void)
  186. {
  187.   BSet Np, Ns;
  188.   int  i, n;
  189.  
  190.   /*
  191.    * N is set as built.  Np is set being built this iteration. P is set
  192.    * of all productions which have a RHS all in N.
  193.    */
  194.  
  195.   Np = NEW2(WORDSIZE(nvars), unsigned);
  196.  
  197.   /*
  198.    * The set being computed is a set of nonterminals which can derive
  199.    * the empty string or strings consisting of all terminals. At each
  200.    * iteration a nonterminal is added to the set if there is a
  201.    * production with that nonterminal as its LHS for which all the
  202.    * nonterminals in its RHS are already in the set.  Iterate until the
  203.    * set being computed remains unchanged.  Any nonterminals not in the
  204.    * set at that point are useless in that they will never be used in
  205.    * deriving a sentence of the language.
  206.    * 
  207.    * This iteration doesn't use any special traversal over the
  208.    * productions.  A set is kept of all productions for which all the
  209.    * nonterminals in the RHS are in useful.  Only productions not in
  210.    * this set are scanned on each iteration.  At the end, this set is
  211.    * saved to be used when finding useful productions: only productions
  212.    * in this set will appear in the final grammar.
  213.    */
  214.  
  215.   n = 0;
  216.   while (1)
  217.     {
  218.       for (i = WORDSIZE(nvars) - 1; i >= 0; i--)
  219.     Np[i] = N[i];
  220.       for (i = 1; i <= nrules; i++)
  221.     {
  222.       if (!BITISSET(P, i))
  223.         {
  224.           if (useful_production(i, N))
  225.         {
  226.           SETBIT(Np, rlhs[i] - ntokens);
  227.           SETBIT(P, i);
  228.         }
  229.         }
  230.     }
  231.       if (bits_equal(N, Np, WORDSIZE(nvars)))
  232.     break;
  233.       Ns = Np;
  234.       Np = N;
  235.       N = Ns;
  236.     }
  237.   FREE(N);
  238.   N = Np;
  239. }
  240.  
  241. static void inaccessable_symbols (void)
  242. {
  243.   BSet  Vp, Vs, Pp;
  244.   int   i, n;
  245.   short t;
  246.   rule  r;
  247.  
  248.   /*
  249.    * Find out which productions are reachable and which symbols are
  250.    * used.  Starting with an empty set of productions and a set of
  251.    * symbols which only has the start symbol in it, iterate over all
  252.    * productions until the set of productions remains unchanged for an
  253.    * iteration.  For each production which has a LHS in the set of
  254.    * reachable symbols, add the production to the set of reachable
  255.    * productions, and add all of the nonterminals in the RHS of the
  256.    * production to the set of reachable symbols.
  257.    * 
  258.    * Consider only the (partially) reduced grammar which has only
  259.    * nonterminals in N and productions in P.
  260.    * 
  261.    * The result is the set P of productions in the reduced grammar, and
  262.    * the set V of symbols in the reduced grammar.
  263.    * 
  264.    * Although this algorithm also computes the set of terminals which are
  265.    * reachable, no terminal will be deleted from the grammar. Some
  266.    * terminals might not be in the grammar but might be generated by
  267.    * semantic routines, and so the user might want them available with
  268.    * specified numbers.  (Is this true?)  However, the nonreachable
  269.    * terminals are printed (if running in verbose mode) so that the user
  270.    * can know.
  271.    */
  272.  
  273.   Vp = NEW2(WORDSIZE(nsyms), unsigned);
  274.   Pp = NEW2(WORDSIZE(nrules + 1), unsigned);
  275.  
  276.   /* If the start symbol isn't useful, then nothing will be useful. */
  277.   if (!BITISSET(N, start_symbol - ntokens))
  278.     goto end_iteration;
  279.  
  280.   SETBIT(V, start_symbol);
  281.  
  282.   n = 0;
  283.   while (1)
  284.     {
  285.       for (i = WORDSIZE(nsyms) - 1; i >= 0; i--)
  286.     Vp[i] = V[i];
  287.       for (i = 1; i <= nrules; i++)
  288.     {
  289.       if (!BITISSET(Pp, i) && BITISSET(P, i) && 
  290.           BITISSET(V, rlhs[i]))
  291.         {
  292.           for (r = &ritem[rrhs[i]]; *r >= 0; r++)
  293.         {
  294.           if (ISTOKEN(t = *r)
  295.               || BITISSET(N, t - ntokens))
  296.             {
  297.               SETBIT(Vp, t);
  298.             }
  299.         }
  300.           SETBIT(Pp, i);
  301.         }
  302.     }
  303.       if (bits_equal(V, Vp, WORDSIZE(nsyms)))
  304.     {
  305.       break;
  306.     }
  307.       Vs = Vp;
  308.       Vp = V;
  309.       V = Vs;
  310.     }
  311.  end_iteration:
  312.  
  313.   FREE(V);
  314.   V = Vp;
  315.  
  316.   /* Tokens 0, 1, and 2 are internal to Bison.  Consider them useful. */
  317.   SETBIT(V, 0);            /* end-of-input token */
  318.   SETBIT(V, 1);            /* error token */
  319.   SETBIT(V, 2);            /* illegal token */
  320.  
  321.   FREE(P);
  322.   P = Pp;
  323.  
  324.   nuseful_productions = bits_size(P, WORDSIZE(nrules + 1));
  325.   nuseless_productions = nrules - nuseful_productions;
  326.  
  327.   nuseful_nonterminals = 0;
  328.   for (i = ntokens; i < nsyms; i++)
  329.     if (BITISSET(V, i))
  330.       nuseful_nonterminals++;
  331.   nuseless_nonterminals = nvars - nuseful_nonterminals;
  332.  
  333.   /* A token that was used in %prec should not be warned about.  */
  334.   for (i = 1; i < nrules; i++)
  335.     if (rprecsym[i] != 0)
  336.       SETBIT(V1, rprecsym[i]);
  337. }
  338.  
  339. static void reduce_grammar_tables (void)
  340. {
  341.  
  342.   /* remove useless productions */
  343.   if (nuseless_productions > 0)
  344.     {
  345.       short np, pn, ni, pi;
  346.  
  347.       np = 0;
  348.       ni = 0;
  349.       for (pn = 1; pn <= nrules; pn++)
  350.     {
  351.       if (BITISSET(P, pn))
  352.         {
  353.           np++;
  354.           if (pn != np)
  355.         {
  356.           rlhs[np] = rlhs[pn];
  357.           rprec[np] = rprec[pn];
  358.           rassoc[np] = rassoc[pn];
  359.           rrhs[np] = rrhs[pn];
  360.           if (rrhs[np] != ni)
  361.             {
  362.               pi = rrhs[np];
  363.               rrhs[np] = ni;
  364.               while (ritem[pi] >= 0)
  365.             ritem[ni++] = ritem[pi++];
  366.               ritem[ni++] = -np;
  367.             }
  368.         } else {
  369.           while (ritem[ni++] >= 0);
  370.         }
  371.         }
  372.     }
  373.       ritem[ni] = 0;
  374.       nrules -= nuseless_productions;
  375.       nitems = ni;
  376.  
  377.       /*
  378.        * Is it worth it to reduce the amount of memory for the
  379.        * grammar? Probably not.
  380.        */
  381.  
  382.     }
  383.   /* remove useless symbols */
  384.   if (nuseless_nonterminals > 0)
  385.     {
  386.  
  387.       int    i, n;
  388.       short *nontermmap;
  389.       rule   r;
  390.  
  391.       /*
  392.        * create a map of nonterminal number to new nonterminal
  393.        * number. -1 in the map means it was useless and is being
  394.        * eliminated.
  395.        */
  396.  
  397.       nontermmap = NEW2(nvars, short) - ntokens;
  398.       for (i = ntokens; i < nsyms; i++)
  399.     nontermmap[i] = -1;
  400.  
  401.       n = ntokens;
  402.       for (i = ntokens; i < nsyms; i++)
  403.     if (BITISSET(V, i))
  404.       nontermmap[i] = n++;
  405.  
  406.       /* Shuffle elements of tables indexed by symbol number.  */
  407.  
  408.       for (i = ntokens; i < nsyms; i++)
  409.     {
  410.       n = nontermmap[i];
  411.       if (n >= 0)
  412.         {
  413.           sassoc[n] = sassoc[i];
  414.           sprec[n] = sprec[i];
  415.           tags[n] = tags[i];
  416.         } else {
  417.           free(tags[i]);
  418.         }
  419.     }
  420.  
  421.       /* Replace all symbol numbers in valid data structures.  */
  422.  
  423.       for (i = 1; i <= nrules; i++)
  424.     rlhs[i] = nontermmap[rlhs[i]];
  425.  
  426.       for (r = ritem; *r; r++)
  427.     if (ISVAR(*r))
  428.       *r = nontermmap[*r];
  429.  
  430.       start_symbol = nontermmap[start_symbol];
  431.  
  432.       nsyms -= nuseless_nonterminals;
  433.       nvars -= nuseless_nonterminals;
  434.  
  435.       free(&nontermmap[ntokens]);
  436.     }
  437. }
  438.  
  439. static void print_results (void)
  440. {
  441.   int   i;
  442.   rule  r;
  443.   bool  b;
  444.  
  445.   if (nuseless_nonterminals > 0)
  446.     {
  447.       fprintf(foutput, "Useless nonterminals:\n\n");
  448.       for (i = ntokens; i < nsyms; i++)
  449.     if (!BITISSET(V, i))
  450.       fprintf(foutput, "   %s\n", tags[i]);
  451.     }
  452.   b = FALSE;
  453.   for (i = 0; i < ntokens; i++)
  454.     {
  455.       if (!BITISSET(V, i) && !BITISSET(V1, i))
  456.     {
  457.       if (!b)
  458.         {
  459.           fprintf(foutput, "\n\nTerminals which are not used:\n\n");
  460.           b = TRUE;
  461.         }
  462.       fprintf(foutput, "   %s\n", tags[i]);
  463.     }
  464.     }
  465.  
  466.   if (nuseless_productions > 0)
  467.     {
  468.       fprintf(foutput, "\n\nUseless rules:\n\n");
  469.       for (i = 1; i <= nrules; i++)
  470.     {
  471.       if (!BITISSET(P, i))
  472.         {
  473.           fprintf(foutput, "#%-4d  ", i);
  474.           fprintf(foutput, "%s :\t", tags[rlhs[i]]);
  475.           for (r = &ritem[rrhs[i]]; *r >= 0; r++)
  476.         {
  477.           fprintf(foutput, " %s", tags[*r]);
  478.         }
  479.           fprintf(foutput, ";\n");
  480.         }
  481.     }
  482.     }
  483.   if (nuseless_nonterminals > 0 || nuseless_productions > 0 || b)
  484.     fprintf(foutput, "\n\n");
  485. }
  486.  
  487. void dump_grammar (void)
  488. {
  489.   int i;
  490.   rule r;
  491.  
  492.   fprintf(foutput,
  493.       "ntokens = %d, nvars = %d, nsyms = %d, nrules = %d, nitems = %d\n\n",
  494.       ntokens, nvars, nsyms, nrules, nitems);
  495.   fprintf(foutput, "Variables\n---------\n\n");
  496.   fprintf(foutput, "Value  Sprec    Sassoc    Tag\n");
  497.   for (i = ntokens; i < nsyms; i++)
  498.     fprintf(foutput, "%5d  %5d  %5d  %s\n",
  499.         i, sprec[i], sassoc[i], tags[i]);
  500.   fprintf(foutput, "\n\n");
  501.   fprintf(foutput, "Rules\n-----\n\n");
  502.   for (i = 1; i <= nrules; i++)
  503.     {
  504.       fprintf(foutput, "%-5d(%5d%5d)%5d : (@%-5d)", 
  505.           i, rprec[i], rassoc[i], rlhs[i], rrhs[i]);
  506.       for (r = &ritem[rrhs[i]]; *r > 0; r++)
  507.     fprintf(foutput, "%5d", *r);
  508.       fprintf(foutput, " [%d]\n", -(*r));
  509.     }
  510.   fprintf(foutput, "\n\n");
  511.   fprintf(foutput, "Rules interpreted\n-----------------\n\n");
  512.   for (i = 1; i <= nrules; i++)
  513.     {
  514.       fprintf(foutput, "%-5d  %s :", i, tags[rlhs[i]]);
  515.       for (r = &ritem[rrhs[i]]; *r > 0; r++)
  516.     fprintf(foutput, " %s", tags[*r]);
  517.       fprintf(foutput, "\n");
  518.     }
  519.   fprintf(foutput, "\n\n");
  520. }
  521.  
  522.  
  523. static void print_notices (void)
  524. {
  525.   extern int fixed_outfiles;
  526.  
  527.   if (fixed_outfiles && nuseless_productions)
  528.     fprintf(stderr, "%d rules never reduced\n", nuseless_productions);
  529.  
  530.   fprintf(stderr, "%s contains ", infile);
  531.  
  532.   if (nuseless_nonterminals > 0)
  533.     {
  534.       fprintf(stderr, "%d useless nonterminal%s",
  535.           nuseless_nonterminals,
  536.           (nuseless_nonterminals == 1 ? "" : "s"));
  537.     }
  538.   if (nuseless_nonterminals > 0 && nuseless_productions > 0)
  539.     fprintf(stderr, " and ");
  540.  
  541.   if (nuseless_productions > 0)
  542.     {
  543.       fprintf(stderr, "%d useless rule%s",
  544.           nuseless_productions,
  545.           (nuseless_productions == 1 ? "" : "s"));
  546.     }
  547.   fprintf(stderr, ".\n");
  548.   fflush(stderr);
  549. }
  550.