+#include "print-xml.h"
+#include "reader.h"
+#include "reduce.h"
+#include "symtab.h"
+
+/* Comments for these variables are in gram.h. */
+
+item_number *ritem = NULL;
+unsigned int nritems = 0;
+
+rule *rules = NULL;
+rule_number nrules = 0;
+
+symbol **symbols = NULL;
+int nsyms = 0;
+int ntokens = 1;
+int nvars = 0;
+
+symbol_number *token_translations = NULL;
+
+int max_user_token_number = 256;
+
+bool
+rule_useful_in_grammar_p (rule const *r)
+{
+ return r->number < nrules;
+}
+
+bool
+rule_useless_in_grammar_p (rule const *r)
+{
+ return !rule_useful_in_grammar_p (r);
+}
+
+bool
+rule_useless_in_parser_p (rule const *r)
+{
+ return !r->useful && rule_useful_in_grammar_p (r);
+}
+
+void
+rule_lhs_print (rule const *r, symbol const *previous_lhs, FILE *out)
+{
+ fprintf (out, " %3d ", r->number);
+ if (previous_lhs != r->lhs)
+ fprintf (out, "%s:", r->lhs->tag);
+ else
+ fprintf (out, "%*s|", (int) strlen (previous_lhs->tag), "");
+}
+
+void
+rule_lhs_print_xml (rule const *r, FILE *out, int level)
+{
+ xml_printf (out, level, "<lhs>%s</lhs>", r->lhs->tag);
+}
+
+size_t
+rule_rhs_length (rule const *r)
+{
+ size_t res = 0;
+ item_number *rhsp;
+ for (rhsp = r->rhs; *rhsp >= 0; ++rhsp)
+ ++res;
+ return res;
+}
+
+void
+rule_rhs_print (rule const *r, FILE *out)
+{
+ if (0 <= *r->rhs)
+ {
+ item_number *rp;
+ for (rp = r->rhs; *rp >= 0; rp++)
+ fprintf (out, " %s", symbols[*rp]->tag);
+ }
+ else
+ fputs (" %empty", out);
+}
+
+static void
+rule_rhs_print_xml (rule const *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>%s</symbol>",
+ 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>");
+ }
+}
+
+void
+ritem_print (FILE *out)
+{
+ unsigned int i;
+ fputs ("RITEM\n", out);
+ for (i = 0; i < nritems; ++i)
+ if (ritem[i] >= 0)
+ fprintf (out, " %s", symbols[ritem[i]]->tag);
+ else
+ fprintf (out, " (rule %d)\n", item_number_as_rule_number (ritem[i]));
+ fputs ("\n\n", out);
+}
+
+size_t
+ritem_longest_rhs (void)
+{
+ int max = 0;
+ rule_number r;
+
+ for (r = 0; r < nrules; ++r)
+ {
+ int length = rule_rhs_length (&rules[r]);
+ if (length > max)
+ max = length;
+ }
+
+ return max;
+}
+
+void
+grammar_rules_partial_print (FILE *out, const char *title,
+ rule_filter filter)
+{
+ rule_number r;
+ bool first = true;
+ symbol *previous_lhs = NULL;