Re: [PATCH] style: decouple different uses of item_number

Akim Demaille <[email protected]>
Newsgroups gmane.comp.parsers.bison.patches
Message-ID <[email protected]>

> Le 25 mai 2020 à 07:27, Akim Demaille <[email protected]> a écrit :
> 
>> * src/gram.h: introduce item_index for ritem indices.
>> * src/closure.h, src/closure.c, src/ielr.c, src/lr0.c, src/print-graph.c, src/state.h, src/state.h: Replace uses of item_number with item_index where appropriate.
> 
> Installed.  Thanks!

On second thought, we can have the compiler help us on this.  I've installed the following changes on top of yours.

Cheers!


commit 518dc6ca26d6ff9db850f5905a5b8795597f9601
Author: Akim Demaille <[email protected]>
Date:   Mon May 25 07:34:11 2020 +0200

    style: make item_index a truly different type from item_number
    
    See previous commit.
    
    * src/gram.h (item_index): Make it unsigned.
    Fix remaiming issues.

diff --git a/src/closure.c b/src/closure.c
index 94439b13..ee0c9d4a 100644
--- a/src/closure.c
+++ b/src/closure.c
@@ -60,10 +60,10 @@ closure_print (char const *title, item_index const *array, size_t size)
   for (size_t i = 0; i < size; ++i)
     {
       fprintf (stderr, "  %2d: .", array[i]);
-      item_index *rp;
-      for (rp = &ritem[array[i]]; *rp >= 0; ++rp)
+      item_number *rp;
+      for (rp = &ritem[array[i]]; 0 <= *rp; ++rp)
         fprintf (stderr, " %s", symbols[*rp]->tag);
-      fprintf (stderr, "  (rule %d)\n", -*rp - 1);
+      fprintf (stderr, "  (rule %d)\n", item_number_as_rule_number (*rp));
     }
   fputs ("\n\n", stderr);
 }
diff --git a/src/gram.h b/src/gram.h
index a4b58f0a..77fc7699 100644
--- a/src/gram.h
+++ b/src/gram.h
@@ -111,14 +111,15 @@ extern int nsyms;
 extern int ntokens;
 extern int nvars;
 
-/* elements of ritem */
+/* Elements of ritem. */
 typedef int item_number;
-/* indices into ritem */
-typedef int item_index;
 # define ITEM_NUMBER_MAX INT_MAX
 extern item_number *ritem;
 extern int nritems;
 
+/* Indices into ritem. */
+typedef unsigned int item_index;
+
 /* There is weird relationship between OT1H item_number and OTOH
    symbol_number and rule_number: we store the latter in
    item_number.  symbol_number values are stored as-is, while
diff --git a/src/ielr.c b/src/ielr.c
index f86cd461..47ac714a 100644
--- a/src/ielr.c
+++ b/src/ielr.c
@@ -1030,7 +1030,7 @@ ielr_split_states (bitsetv follow_kernel_items, bitsetv always_follows,
         if (!node->state->consistent)
           {
             size_t i = 0;
-            item_number *itemset = node->state->items;
+            item_index *itemset = node->state->items;
             for (size_t r = 0; r < node->state->reductions->num; ++r)
               {
                 rule *this_rule = node->state->reductions->rules[r];
diff --git a/src/print-xml.c b/src/print-xml.c
index a44af211..168d6bd5 100644
--- a/src/print-xml.c
+++ b/src/print-xml.c
@@ -57,7 +57,7 @@ static struct escape_buf escape_bufs[num_escape_bufs];
 static void
 print_core (FILE *out, int level, state *s)
 {
-  item_number *sitems = s->items;
+  item_index *sitems = s->items;
   size_t snritems = s->nitems;
 
   /* Output all the items of a state, not only its kernel.  */
diff --git a/src/print.c b/src/print.c
index 720740ee..1da0f9dd 100644
--- a/src/print.c
+++ b/src/print.c
@@ -61,7 +61,7 @@ max_length (size_t *width, const char *str)
 static void
 print_core (FILE *out, state *s)
 {
-  item_number *sitems = s->items;
+  item_index *sitems = s->items;
   size_t snritems = s->nitems;
   /* Output all the items of a state, not only its kernel.  */
   if (report_flag & report_itemsets)
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.