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