bison-patches
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH 3/3] style: factor printing of rules


From: Akim Demaille
Subject: [PATCH 3/3] style: factor printing of rules
Date: Sat, 9 Feb 2019 15:52:16 +0100

* src/gram.h, src/gram.c (rule_print): New.
Use it.
---
 src/gram.c | 16 +++++++++++-----
 src/gram.h |  3 +++
 src/lalr.c |  1 -
 src/lr0.c  |  2 +-
 4 files changed, 15 insertions(+), 7 deletions(-)

diff --git a/src/gram.c b/src/gram.c
index 0a414423..82e3500b 100644
--- a/src/gram.c
+++ b/src/gram.c
@@ -155,6 +155,13 @@ rule_rhs_print_xml (rule const *r, FILE *out, int level)
     }
 }
 
+void
+rule_print (rule const *r, rule const *prev_rule, FILE *out)
+{
+  rule_lhs_print (r, prev_rule ? prev_rule->lhs : NULL, out);
+  rule_rhs_print (r, out);
+}
+
 void
 ritem_print (FILE *out)
 {
@@ -186,7 +193,7 @@ grammar_rules_partial_print (FILE *out, const char *title,
                              rule_filter filter)
 {
   bool first = true;
-  sym_content *previous_lhs = NULL;
+  rule *previous_rule = NULL;
 
   /* rule # : LHS -> RHS */
   for (rule_number r = 0; r < nrules + nuseless_productions; r++)
@@ -195,13 +202,12 @@ grammar_rules_partial_print (FILE *out, const char *title,
         continue;
       if (first)
         fprintf (out, "%s\n\n", title);
-      else if (previous_lhs && previous_lhs != rules[r].lhs)
+      else if (previous_rule && previous_rule->lhs != rules[r].lhs)
         fputc ('\n', out);
       first = false;
-      rule_lhs_print (&rules[r], previous_lhs, out);
-      rule_rhs_print (&rules[r], out);
+      rule_print (&rules[r], previous_rule, out);
       fputc ('\n', out);
-      previous_lhs = rules[r].lhs;
+      previous_rule = &rules[r];
     }
   if (!first)
     fputs ("\n\n", out);
diff --git a/src/gram.h b/src/gram.h
index 5be1abe9..24cee8ff 100644
--- a/src/gram.h
+++ b/src/gram.h
@@ -248,6 +248,9 @@ size_t rule_rhs_length (rule const *r);
 /* Print this rule's RHS on OUT.  */
 void rule_rhs_print (rule const *r, FILE *out);
 
+/* Print this rule on OUT.  If a PREVIOUS_RULE was already displayed,
+   avoid useless repetitions of their LHS. */
+void rule_print (rule const *r, rule const *prev_rule, FILE *out);
 
 
 
diff --git a/src/lalr.c b/src/lalr.c
index 5e78b229..03e807de 100644
--- a/src/lalr.c
+++ b/src/lalr.c
@@ -241,7 +241,6 @@ build_relations (void)
               rp--;
               if (ISVAR (*rp))
                 {
-                  /* Downcasting from item_number to symbol_number.  */
                   edge[nedges++] = map_goto (states1[--length],
                                              item_number_as_symbol_number 
(*rp));
                   if (nullable[*rp - ntokens])
diff --git a/src/lr0.c b/src/lr0.c
index 970fc1c8..b41cc46e 100644
--- a/src/lr0.c
+++ b/src/lr0.c
@@ -376,7 +376,7 @@ generate_states (void)
       closure (s->items, s->nitems);
       /* Record the reductions allowed out of this state.  */
       save_reductions (s);
-      /* Find the itemsets of the states that shifts can reach.  */
+      /* Find the itemsets of the states that shifts/gotos can reach.  */
       new_itemsets (s);
       /* Find or create the core structures for those states.  */
       append_states (s);
-- 
2.20.1




reply via email to

[Prev in Thread] Current Thread [Next in Thread]