]> git.saurik.com Git - bison.git/blobdiff - src/gram.c
(xsl:template name="escape"): Write xsl:param "required" attribute as comment.
[bison.git] / src / gram.c
index cf5a946db983e698269f6f2fa1aaa1800ecbc3ad..84f7924458fb404ebce80d1017f8e8787e016eb8 100644 (file)
@@ -1,25 +1,24 @@
 /* Allocate input grammar variables for Bison.
 
-   Copyright (C) 1984, 1986, 1989, 2001, 2002 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., 59 Temple Place - Suite 330,
-   Boston, MA 02111-1307, USA.  */
-
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 
+#include <config.h>
 #include "system.h"
 
 #include <quotearg.h>
@@ -28,6 +27,7 @@
 #include "reader.h"
 #include "reduce.h"
 #include "symtab.h"
+#include "print-xml.h"
 
 /* Comments for these variables are in gram.h.  */
 
@@ -46,10 +46,6 @@ symbol_number *token_translations = NULL;
 
 int max_user_token_number = 256;
 
-int glr_parser = 0;
-int pure_parser = 0;
-
-
 /*--------------------------------------------------------------.
 | Return true IFF the rule has a `number' smaller than NRULES.  |
 `--------------------------------------------------------------*/
@@ -68,7 +64,7 @@ rule_useful_p (rule *r)
 bool
 rule_useless_p (rule *r)
 {
-  return r->number >= nrules;
+  return !rule_useful_p (r);
 }
 
 
@@ -80,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);
 }
 
 
@@ -107,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.  |
@@ -143,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.  |
@@ -203,7 +225,7 @@ void
 grammar_rules_partial_print (FILE *out, const char *title,
                             rule_filter filter)
 {
-  int r;
+  rule_number r;
   bool first = true;
   symbol *previous_lhs = NULL;
 
@@ -226,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.  |
 `------------------------------------------*/
@@ -236,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.  |
@@ -270,11 +332,11 @@ grammar_dump (FILE *out, const char *title)
     for (i = 0; i < nrules + nuseless_productions; i++)
       {
        rule *rule_i = &rules[i];
-       item_number *r = NULL;
+       item_number *rp = NULL;
        unsigned int rhs_itemno = rule_i->rhs - ritem;
-       unsigned int rhs_count = 0;
+       unsigned int rhs_count = 0;
        /* Find the last RHS index in ritems. */
-       for (r = rule_i->rhs; *r >= 0; ++r)
+       for (rp = rule_i->rhs; *rp >= 0; ++rp)
          ++rhs_count;
        fprintf (out, "%3d (%2d, %2d, %2d, %2u-%2u)   %2d ->",
                 i,
@@ -285,9 +347,9 @@ grammar_dump (FILE *out, const char *title)
                 rhs_itemno + rhs_count - 1,
                 rule_i->lhs->number);
        /* Dumped the RHS. */
-       for (r = rule_i->rhs; *r >= 0; r++)
-         fprintf (out, " %3d", *r);
-       fprintf (out, "  [%d]\n", item_number_as_rule_number (*r));
+       for (rp = rule_i->rhs; *rp >= 0; rp++)
+         fprintf (out, " %3d", *rp);
+       fprintf (out, "  [%d]\n", item_number_as_rule_number (*rp));
       }
   }
   fprintf (out, "\n\n");
@@ -320,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);
       }
 }
@@ -329,9 +390,10 @@ grammar_rules_never_reduced_report (const char *message)
 void
 grammar_free (void)
 {
-  XFREE (ritem);
+  if (ritem)
+    free (ritem - 1);
   free (rules);
-  XFREE (token_translations);
+  free (token_translations);
   /* Free the symbol table data structure.  */
   symbols_free ();
   free_merger_functions ();