]> git.saurik.com Git - bison.git/blobdiff - src/reduce.c
Get rid of the ad hoc handling of token_buffer in the scanner: use
[bison.git] / src / reduce.c
index d160f9d6f134322439cc7b85dd54a15430d23a6e..c5be01feab1f43c23c3dfdbade3fff8decb8cf6c 100644 (file)
@@ -1,5 +1,5 @@
 /* Grammar reduction for Bison.
-   Copyright (C) 1988, 1989, 2000 Free Software Foundation, Inc.
+   Copyright 1988, 1989, 2000 Free Software Foundation, Inc.
 
    This file is part of Bison, the GNU Compiler Compiler.
 
 #include "getargs.h"
 #include "files.h"
 #include "gram.h"
-#include "alloc.h"
+#include "xalloc.h"
 #include "complain.h"
 #include "reduce.h"
-
-extern char **tags;            /* reader.c */
-static int statisticsflag;     /* XXXXXXX */
-extern int fixed_outfiles;
+#include "reader.h"
+#include "getargs.h"
 
 typedef unsigned *BSet;
 typedef short *rule;
@@ -124,7 +122,7 @@ useless_nonterminals (void)
   /* N is set as built.  Np is set being built this iteration. P is
      set of all productions which have a RHS all in N.  */
 
-  Np = NEW2 (WORDSIZE (nvars), unsigned);
+  Np = XCALLOC (unsigned, WORDSIZE (nvars));
 
   /* The set being computed is a set of nonterminals which can derive
      the empty string or strings consisting of all terminals. At each
@@ -164,7 +162,7 @@ useless_nonterminals (void)
       Np = N;
       N = Ns;
     }
-  FREE (N);
+  XFREE (N);
   N = Np;
 }
 
@@ -200,8 +198,8 @@ inaccessable_symbols (void)
      terminals are printed (if running in verbose mode) so that the
      user can know.  */
 
-  Vp = NEW2 (WORDSIZE (nsyms), unsigned);
-  Pp = NEW2 (WORDSIZE (nrules + 1), unsigned);
+  Vp = XCALLOC (unsigned, WORDSIZE (nsyms));
+  Pp = XCALLOC (unsigned, WORDSIZE (nrules + 1));
 
   /* If the start symbol isn't useful, then nothing will be useful. */
   if (!BITISSET (N, start_symbol - ntokens))
@@ -238,7 +236,7 @@ inaccessable_symbols (void)
     }
 end_iteration:
 
-  FREE (V);
+  XFREE (V);
   V = Vp;
 
   /* Tokens 0, 1, and 2 are internal to Bison.  Consider them useful. */
@@ -246,7 +244,7 @@ end_iteration:
   SETBIT (V, 1);               /* error token */
   SETBIT (V, 2);               /* some undefined token */
 
-  FREE (P);
+  XFREE (P);
   P = Pp;
 
   nuseful_productions = bits_size (P, WORDSIZE (nrules + 1));
@@ -342,7 +340,7 @@ reduce_grammar_tables (void)
         number. -1 in the map means it was useless and is being
         eliminated.  */
 
-      nontermmap = NEW2 (nvars, short) - ntokens;
+      nontermmap = XCALLOC (short, nvars) - ntokens;
       for (i = ntokens; i < nsyms; i++)
        nontermmap[i] = -1;
 
@@ -403,10 +401,11 @@ print_results (void)
 
   if (nuseless_nonterminals > 0)
     {
-      fprintf (foutput, _("Useless nonterminals:\n\n"));
+      obstack_sgrow (&output_obstack, _("Useless nonterminals:"));
+      obstack_sgrow (&output_obstack, "\n\n");
       for (i = ntokens; i < nsyms; i++)
        if (!BITISSET (V, i))
-         fprintf (foutput, "   %s\n", tags[i]);
+         obstack_fgrow1 (&output_obstack, "   %s\n", tags[i]);
     }
   b = FALSE;
   for (i = 0; i < ntokens; i++)
@@ -415,32 +414,35 @@ print_results (void)
        {
          if (!b)
            {
-             fprintf (foutput, _("\n\nTerminals which are not used:\n\n"));
+             obstack_sgrow (&output_obstack, "\n\n");
+             obstack_sgrow (&output_obstack,
+                                  _("Terminals which are not used:"));
+             obstack_sgrow (&output_obstack, "\n\n");
              b = TRUE;
            }
-         fprintf (foutput, "   %s\n", tags[i]);
+         obstack_fgrow1 (&output_obstack, "   %s\n", tags[i]);
        }
     }
 
   if (nuseless_productions > 0)
     {
-      fprintf (foutput, _("\n\nUseless rules:\n\n"));
+      obstack_sgrow (&output_obstack, "\n\n");
+      obstack_sgrow (&output_obstack, _("Useless rules:"));
+      obstack_sgrow (&output_obstack, "\n\n");
       for (i = 1; i <= nrules; i++)
        {
          if (!BITISSET (P, i))
            {
-             fprintf (foutput, "#%-4d  ", i);
-             fprintf (foutput, "%s :\t", tags[rlhs[i]]);
+             obstack_fgrow1 (&output_obstack, "#%-4d  ", i);
+             obstack_fgrow1 (&output_obstack, "%s :\t", tags[rlhs[i]]);
              for (r = &ritem[rrhs[i]]; *r >= 0; r++)
-               {
-                 fprintf (foutput, " %s", tags[*r]);
-               }
-             fprintf (foutput, ";\n");
+               obstack_fgrow1 (&output_obstack, " %s", tags[*r]);
+             obstack_sgrow (&output_obstack, ";\n");
            }
        }
     }
   if (nuseless_nonterminals > 0 || nuseless_productions > 0 || b)
-    fprintf (foutput, "\n\n");
+    obstack_sgrow (&output_obstack, "\n\n");
 }
 \f
 #if 0                          /* XXX currently unused.  */
@@ -450,42 +452,45 @@ dump_grammar (void)
   int i;
   rule r;
 
-  fprintf (foutput,
-          "ntokens = %d, nvars = %d, nsyms = %d, nrules = %d, nitems = %d\n\n",
-          ntokens, nvars, nsyms, nrules, nitems);
-  fprintf (foutput, _("Variables\n---------\n\n"));
-  fprintf (foutput, _("Value  Sprec    Sassoc    Tag\n"));
+  obstack_fgrow5 (&output_obstack,
+        "ntokens = %d, nvars = %d, nsyms = %d, nrules = %d, nitems = %d\n\n",
+        ntokens, nvars, nsyms, nrules, nitems);
+  obstack_sgrow (&output_obstack,
+                      _("Variables\n---------\n\n"));
+  obstack_sgrow (&output_obstack,
+                      _("Value  Sprec    Sassoc    Tag\n"));
   for (i = ntokens; i < nsyms; i++)
-    fprintf (foutput, "%5d  %5d  %5d  %s\n", i, sprec[i], sassoc[i], tags[i]);
-  fprintf (foutput, "\n\n");
-  fprintf (foutput, _("Rules\n-----\n\n"));
+    obstack_fgrow4 (&output_obstack,
+                   "%5d  %5d  %5d  %s\n", i, sprec[i], sassoc[i], tags[i]);
+  obstack_sgrow (&output_obstack, "\n\n");
+  obstack_sgrow (&output_obstack, _("Rules\n-----\n\n"));
   for (i = 1; i <= nrules; i++)
     {
-      fprintf (foutput, "%-5d(%5d%5d)%5d : (@%-5d)",
-              i, rprec[i], rassoc[i], rlhs[i], rrhs[i]);
+      obstack_fgrow5 (&output_obstack, "%-5d(%5d%5d)%5d : (@%-5d)",
+                     i, rprec[i], rassoc[i], rlhs[i], rrhs[i]);
       for (r = &ritem[rrhs[i]]; *r > 0; r++)
-       fprintf (foutput, "%5d", *r);
-      fprintf (foutput, " [%d]\n", -(*r));
+       obstack_fgrow1 (&output_obstack, "%5d", *r);
+      obstack_fgrow1 (&output_obstack, " [%d]\n", -(*r));
     }
-  fprintf (foutput, "\n\n");
-  fprintf (foutput, _("Rules interpreted\n-----------------\n\n"));
+  obstack_sgrow (&output_obstack, "\n\n");
+  obstack_sgrow (&output_obstack,
+                      _("Rules interpreted\n-----------------\n\n"));
   for (i = 1; i <= nrules; i++)
     {
-      fprintf (foutput, "%-5d  %s :", i, tags[rlhs[i]]);
+      obstack_fgrow2 (&output_obstack, "%-5d  %s :", i, tags[rlhs[i]]);
       for (r = &ritem[rrhs[i]]; *r > 0; r++)
-       fprintf (foutput, " %s", tags[*r]);
-      fprintf (foutput, "\n");
+       obstack_fgrow1 (&output_obstack, " %s", tags[*r]);
+      obstack_grow1 (&output_obstack, '\n');
     }
-  fprintf (foutput, "\n\n");
+  obstack_sgrow (&output_obstack, "\n\n");
 }
-
 #endif
 
 
 static void
 print_notices (void)
 {
-  if (fixed_outfiles && nuseless_productions)
+  if (yacc_flag && nuseless_productions)
     fprintf (stderr, _("%d rules never reduced\n"), nuseless_productions);
 
   fprintf (stderr, _("%s contains "), infile);
@@ -515,17 +520,17 @@ reduce_grammar (void)
 
   /* Allocate the global sets used to compute the reduced grammar */
 
-  N = NEW2 (WORDSIZE (nvars), unsigned);
-  P = NEW2 (WORDSIZE (nrules + 1), unsigned);
-  V = NEW2 (WORDSIZE (nsyms), unsigned);
-  V1 = NEW2 (WORDSIZE (nsyms), unsigned);
+  N = XCALLOC (unsigned, WORDSIZE (nvars));
+  P = XCALLOC (unsigned, WORDSIZE (nrules + 1));
+  V = XCALLOC (unsigned, WORDSIZE (nsyms));
+  V1 = XCALLOC (unsigned, WORDSIZE (nsyms));
 
   useless_nonterminals ();
   inaccessable_symbols ();
 
   reduced = (bool) (nuseless_nonterminals + nuseless_productions > 0);
 
-  if (verboseflag)
+  if (verbose_flag)
     print_results ();
 
   if (reduced == FALSE)
@@ -539,14 +544,14 @@ reduce_grammar (void)
 
   reduce_grammar_tables ();
 #if 0
-  if (verboseflag)
+  if (verbose_flag)
     {
-      fprintf (foutput, "REDUCED GRAMMAR\n\n");
+      obstack_fgrow1 (&output_obstack, "REDUCED GRAMMAR\n\n");
       dump_grammar ();
     }
 #endif
-  statisticsflag = FALSE;      /* someday getopts should handle this */
-  if (statisticsflag == TRUE)
+
+  if (statistics_flag)
     fprintf (stderr, _("reduced %s defines %d terminal%s, %d nonterminal%s\
 , and %d production%s.\n"),
             infile,
@@ -560,7 +565,7 @@ reduce_grammar (void)
 done_reducing:
   /* Free the global sets used to compute the reduced grammar */
 
-  FREE (N);
-  FREE (V);
-  FREE (P);
+  XFREE (N);
+  XFREE (V);
+  XFREE (P);
 }