]> git.saurik.com Git - bison.git/blobdiff - src/gram.c
2007-11-08 Paolo Bonzini <bonzini@gnu.org>
[bison.git] / src / gram.c
index dd77a766078be0b54cc795ff5c736cdd824de0b9..84f7924458fb404ebce80d1017f8e8787e016eb8 100644 (file)
@@ -1,26 +1,24 @@
 /* Allocate input grammar variables for Bison.
 
-   Copyright (C) 1984, 1986, 1989, 2001, 2002, 2003 Free Software
-   Foundation, Inc.
+   Copyright (C) 1984, 1986, 1989, 2001, 2002, 2003, 2005, 2006
+   2007 Free Software Foundation, Inc.
 
    This file is part of Bison, the GNU Compiler Compiler.
 
-   Bison is free software; you can redistribute it and/or modify
+   This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 2, or (at your option)
-   any later version.
+   the Free Software Foundation, either version 3 of the License, or
+   (at your option) any later version.
 
-   Bison is distributed in the hope that it will be useful,
+   This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with Bison; see the file COPYING.  If not, write to
-   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
-   Boston, MA 02110-1301, USA.  */
-
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 
+#include <config.h>
 #include "system.h"
 
 #include <quotearg.h>
@@ -29,6 +27,7 @@
 #include "reader.h"
 #include "reduce.h"
 #include "symtab.h"
+#include "print-xml.h"
 
 /* Comments for these variables are in gram.h.  */
 
@@ -65,7 +64,7 @@ rule_useful_p (rule *r)
 bool
 rule_useless_p (rule *r)
 {
-  return r->number >= nrules;
+  return !rule_useful_p (r);
 }
 
 
@@ -77,7 +76,7 @@ rule_useless_p (rule *r)
 bool
 rule_never_reduced_p (rule *r)
 {
-  return !r->useful && r->number < nrules;
+  return !r->useful && rule_useful_p (r);
 }
 
 
@@ -104,6 +103,12 @@ rule_lhs_print (rule *r, symbol *previous_lhs, FILE *out)
     }
 }
 
+void
+rule_lhs_print_xml (rule *r, FILE *out, int level)
+{
+  xml_printf (out, level, "<lhs>%s</lhs>", r->lhs->tag);
+}
+
 
 /*--------------------------------------.
 | Return the number of symbols in RHS.  |
@@ -140,6 +145,26 @@ rule_rhs_print (rule *r, FILE *out)
     }
 }
 
+static void
+rule_rhs_print_xml (rule *r, FILE *out, int level)
+{
+  if (*r->rhs >= 0)
+    {
+      item_number *rp;
+      xml_puts (out, level, "<rhs>");
+      for (rp = r->rhs; *rp >= 0; rp++)
+       xml_printf (out, level + 1, "<symbol class=\"%s\">%s</symbol>",
+                   symbol_class_get_string (symbols[*rp]),
+                   xml_escape (symbols[*rp]->tag));
+      xml_puts (out, level, "</rhs>");
+    }
+  else
+    {
+      xml_puts (out, level, "<rhs>");
+      xml_puts (out, level + 1, "<empty/>");
+      xml_puts (out, level, "</rhs>");
+    }
+}
 
 /*-------------------------.
 | Print this rule on OUT.  |
@@ -223,6 +248,40 @@ grammar_rules_partial_print (FILE *out, const char *title,
 }
 
 
+/*----------------------------------------------------------.
+| Print the grammar's rules that match FILTER on OUT (XML). |
+`-----------------------------------------------------------*/
+
+void
+grammar_rules_partial_print_xml (FILE *out, int level, bool rtag,
+                                rule_filter filter)
+{
+  rule_number r;
+  bool first = true;
+
+  for (r = 0; r < nrules + nuseless_productions; r++)
+    {
+      if (filter && !filter (&rules[r]))
+       continue;
+      if (rtag && first)
+       xml_puts (out, level + 1, "<rules>");
+      first = false;
+
+      xml_printf (out, level + 2, "<rule number=\"%d\">",
+                 rules[r].number);
+      rule_lhs_print_xml (&rules[r], out, level + 3);
+      rule_rhs_print_xml (&rules[r], out, level + 3);
+      xml_puts (out, level + 2, "</rule>");
+    }
+  if (rtag)
+    {
+      if (!first)
+       xml_puts (out, level + 1, "</rules>");
+      else
+       xml_puts (out, level + 1, "<rules/>");
+    }
+}
+
 /*------------------------------------------.
 | Print the grammar's useful rules on OUT.  |
 `------------------------------------------*/
@@ -233,6 +292,12 @@ grammar_rules_print (FILE *out)
   grammar_rules_partial_print (out, _("Grammar"), rule_useful_p);
 }
 
+void
+grammar_rules_print_xml (FILE *out, int level)
+{
+  grammar_rules_partial_print_xml (out, level, true, rule_useful_p);
+}
+
 
 /*-------------------.
 | Dump the grammar.  |
@@ -247,24 +312,23 @@ grammar_dump (FILE *out, const char *title)
           ntokens, nvars, nsyms, nrules, nritems);
 
 
-  fputs (("Variables\n---------\n\n"
-         "Value  Sprec  Sassoc  Tag\n"),
-        out);
+  fprintf (out, "Variables\n---------\n\n");
   {
     symbol_number i;
+    fprintf (out, "Value  Sprec  Sassoc  Tag\n");
+
     for (i = ntokens; i < nsyms; i++)
       fprintf (out, "%5d  %5d   %5d  %s\n",
               i,
               symbols[i]->prec, symbols[i]->assoc,
               symbols[i]->tag);
+    fprintf (out, "\n\n");
   }
-  fputs ("\n\n", out);
 
-  fputs (("Rules\n-----\n\n"
-         "Num (Prec, Assoc, Useful, Ritem Range) Lhs -> Rhs (Ritem range) [Num]\n"),
-        out);
+  fprintf (out, "Rules\n-----\n\n");
   {
     rule_number i;
+    fprintf (out, "Num (Prec, Assoc, Useful, Ritem Range) Lhs -> Rhs (Ritem range) [Num]\n");
     for (i = 0; i < nrules + nuseless_productions; i++)
       {
        rule *rule_i = &rules[i];
@@ -288,9 +352,9 @@ grammar_dump (FILE *out, const char *title)
        fprintf (out, "  [%d]\n", item_number_as_rule_number (*rp));
       }
   }
-  fputs ("\n\n", out);
+  fprintf (out, "\n\n");
 
-  fputs ("Rules interpreted\n-----------------\n\n", out);
+  fprintf (out, "Rules interpreted\n-----------------\n\n");
   {
     rule_number r;
     for (r = 0; r < nrules + nuseless_productions; r++)
@@ -299,7 +363,7 @@ grammar_dump (FILE *out, const char *title)
        rule_print (&rules[r], out);
       }
   }
-  fputs ("\n\n", out);
+  fprintf (out, "\n\n");
 }
 
 
@@ -318,8 +382,7 @@ grammar_rules_never_reduced_report (const char *message)
     if (!rules[r].useful)
       {
        location_print (stderr, rules[r].location);
-       fprintf (stderr, ": %s: %s: ",
-                _("warning"), message);
+       fprintf (stderr, ": %s: %s: ", _("warning"), message);
        rule_print (&rules[r], stderr);
       }
 }
@@ -327,7 +390,8 @@ grammar_rules_never_reduced_report (const char *message)
 void
 grammar_free (void)
 {
-  free (ritem);
+  if (ritem)
+    free (ritem - 1);
   free (rules);
   free (token_translations);
   /* Free the symbol table data structure.  */