bison-patches
[Top][All Lists]
Advanced

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

[PATCH] style: decouple different uses of item_number


From: Vincent Imbimbo
Subject: [PATCH] style: decouple different uses of item_number
Date: Sun, 24 May 2020 11:09:49 -0400

* 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.

Hey Akim,
This is something that gave me a lot of trouble when I started working on 
Bison. item_number is used for elements of ritem as well as indices into ritem 
which is fairly confusing. I've introduced item_index to represent indices into 
ritem.

However, another solution would be to replace all of the instances of 
item_index with *item_number, which is how some bison code (including cex) 
already works. Basically, all incidents of ritem[i] would be replaced by *i.  
This is a little risky as it would be really easy to miss one of these uses.
---
 src/closure.c     | 10 +++++-----
 src/closure.h     |  8 ++++----
 src/gram.h        |  3 +++
 src/ielr.c        |  2 +-
 src/lr0.c         | 14 +++++++-------
 src/print-graph.c |  2 +-
 src/state.c       |  4 ++--
 src/state.h       |  6 +++---
 8 files changed, 26 insertions(+), 23 deletions(-)

diff --git a/src/closure.c b/src/closure.c
index 5a93ded7..94439b13 100644
--- a/src/closure.c
+++ b/src/closure.c
@@ -32,7 +32,7 @@
 #include "symtab.h"
 
 /* NITEMSET is the size of the array ITEMSET.  */
-item_number *itemset;
+item_index *itemset;
 size_t nitemset;
 
 /* RULESET contains a bit for each rule.  CLOSURE sets the bits for
@@ -54,13 +54,13 @@ static bitsetv firsts = NULL;
 `-----------------*/
 
 static void
-closure_print (char const *title, item_number const *array, size_t size)
+closure_print (char const *title, item_index const *array, size_t size)
 {
   fprintf (stderr, "Closure: %s\n", title);
   for (size_t i = 0; i < size; ++i)
     {
       fprintf (stderr, "  %2d: .", array[i]);
-      item_number *rp;
+      item_index *rp;
       for (rp = &ritem[array[i]]; *rp >= 0; ++rp)
         fprintf (stderr, " %s", symbols[*rp]->tag);
       fprintf (stderr, "  (rule %d)\n", -*rp - 1);
@@ -182,7 +182,7 @@ closure_new (int n)
 
 
 void
-closure (item_number const *core, size_t n)
+closure (item_index const *core, size_t n)
 {
   if (trace_flag & trace_closure)
     closure_print ("input", core, n);
@@ -203,7 +203,7 @@ closure (item_number const *core, size_t n)
   bitset_iterator iter;
   BITSET_FOR_EACH (iter, ruleset, ruleno, 0)
     {
-      item_number itemno = rules[ruleno].rhs - ritem;
+      item_index itemno = rules[ruleno].rhs - ritem;
       while (c < n && core[c] < itemno)
         {
           itemset[nitemset] = core[c];
diff --git a/src/closure.h b/src/closure.h
index 21d6ba19..8fa1ed60 100644
--- a/src/closure.h
+++ b/src/closure.h
@@ -30,12 +30,12 @@
 void closure_new (int n);
 
 
-/* Given the kernel (aka core) of a state (a sorted vector of item numbers
+/* Given the kernel (aka core) of a state (a sorted vector of item indices
    ITEMS, of length N), set up RULESET and ITEMSET to indicate what
    rules could be run and which items could be accepted when those
    items are the active ones.  */
 
-void closure (item_number const *items, size_t n);
+void closure (item_index const *items, size_t n);
 
 
 /* Free ITEMSET, RULESET and internal data.  */
@@ -43,12 +43,12 @@ void closure (item_number const *items, size_t n);
 void closure_free (void);
 
 
-/* ITEMSET is a sorted vector of item numbers; NITEMSET is its size
+/* ITEMSET is a sorted vector of item indices; NITEMSET is its size
    (actually, points to just beyond the end of the part of it that is
    significant).  CLOSURE places there the indices of all items which
    represent units of input that could arrive next.  */
 
-extern item_number *itemset;
+extern item_index *itemset;
 extern size_t nitemset;
 
 #endif /* !CLOSURE_H_ */
diff --git a/src/gram.h b/src/gram.h
index 22a4e2e9..a4b58f0a 100644
--- a/src/gram.h
+++ b/src/gram.h
@@ -111,7 +111,10 @@ extern int nsyms;
 extern int ntokens;
 extern int nvars;
 
+/* 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;
diff --git a/src/ielr.c b/src/ielr.c
index 8e34b2cc..f86cd461 100644
--- a/src/ielr.c
+++ b/src/ielr.c
@@ -230,7 +230,7 @@ ielr_compute_follow_kernel_items (bitset 
ritem_sees_lookahead_set,
   for (goto_number i = 0; i < ngotos; ++i)
     {
       size_t nitems = states[from_state[i]]->nitems;
-      item_number *items = states[from_state[i]]->items;
+      item_index *items = states[from_state[i]]->items;
       for (size_t j = 0; j < nitems; ++j)
         /* If this item has this goto and if all subsequent symbols in this
            RHS (if any) are nullable nonterminals, then record this item as
diff --git a/src/lr0.c b/src/lr0.c
index 2ebdcf8a..fb06edae 100644
--- a/src/lr0.c
+++ b/src/lr0.c
@@ -49,7 +49,7 @@ static state_list *last_state = NULL;
 
 /* Print CORE for debugging. */
 static void
-core_print (size_t core_size, item_number *core, FILE *out)
+core_print (size_t core_size, item_index *core, FILE *out)
 {
   for (int i = 0; i < core_size; ++i)
     {
@@ -65,7 +65,7 @@ core_print (size_t core_size, item_number *core, FILE *out)
 `-----------------------------------------------------------------*/
 
 static state *
-state_list_append (symbol_number sym, size_t core_size, item_number *core)
+state_list_append (symbol_number sym, size_t core_size, item_index *core)
 {
   state_list *node = xmalloc (sizeof *node);
   state *res = state_new (sym, core_size, core);
@@ -98,14 +98,14 @@ static rule **redset;
 static state **shiftset;
 
 
-/* KERNEL_BASE[symbol-number] -> list of item numbers (offsets inside
+/* KERNEL_BASE[symbol-number] -> list of item indices (offsets inside
    RITEM) of length KERNEL_SIZE[symbol-number]. */
-static item_number **kernel_base;
+static item_index **kernel_base;
 static int *kernel_size;
 
 /* A single dimension array that serves as storage for
    KERNEL_BASE.  */
-static item_number *kernel_items;
+static item_index *kernel_items;
 
 
 static void
@@ -257,7 +257,7 @@ new_itemsets (state *s)
 `--------------------------------------------------------------*/
 
 static state *
-get_state (symbol_number sym, size_t core_size, item_number *core)
+get_state (symbol_number sym, size_t core_size, item_index *core)
 {
   if (trace_flag & trace_automaton)
     {
@@ -394,7 +394,7 @@ generate_states (void)
 
   /* Create the initial state.  The 0 at the lhs is the index of the
      item of this initial rule.  */
-  item_number initial_core = 0;
+  item_index initial_core = 0;
   state_list_append (0, 1, &initial_core);
 
   /* States are queued when they are created; process them all.  */
diff --git a/src/print-graph.c b/src/print-graph.c
index 1dd6b15e..5691c6d8 100644
--- a/src/print-graph.c
+++ b/src/print-graph.c
@@ -47,7 +47,7 @@
 static void
 print_core (struct obstack *oout, state *s)
 {
-  item_number const *sitems = s->items;
+  item_index const *sitems = s->items;
   sym_content *previous_lhs = NULL;
   size_t snritems = s->nitems;
 
diff --git a/src/state.c b/src/state.c
index 6118445a..8f17a00c 100644
--- a/src/state.c
+++ b/src/state.c
@@ -126,7 +126,7 @@ state *final_state = NULL;
 
 state *
 state_new (symbol_number accessing_symbol,
-           size_t nitems, item_number *core)
+           size_t nitems, item_index *core)
 {
   aver (nstates < STATE_NUMBER_MAXIMUM);
 
@@ -395,7 +395,7 @@ state_hash_insert (state *s)
 `------------------------------------------------------------------*/
 
 state *
-state_hash_lookup (size_t nitems, item_number *core)
+state_hash_lookup (size_t nitems, item_index *core)
 {
   size_t items_size = nitems * sizeof *core;
   state *probe = xmalloc (offsetof (state, items) + items_size);
diff --git a/src/state.h b/src/state.h
index a087dc83..4f0cfd10 100644
--- a/src/state.h
+++ b/src/state.h
@@ -225,7 +225,7 @@ struct state
   /* Its items.  Must be last, since ITEMS can be arbitrarily large.  Sorted
      ascendingly on item index in RITEM, which is sorted on rule number.  */
   size_t nitems;
-  item_number items[1];
+  item_index items[1];
 };
 
 extern state_number nstates;
@@ -233,7 +233,7 @@ extern state *final_state;
 
 /* Create a new state with ACCESSING_SYMBOL for those items.  */
 state *state_new (symbol_number accessing_symbol,
-                  size_t core_size, item_number *core);
+                  size_t core_size, item_index *core);
 state *state_new_isocore (state const *s);
 
 /* Record that from S we can reach all the DST states (NUM of them).  */
@@ -264,7 +264,7 @@ void state_hash_free (void);
 
 /* Find the state associated to the CORE, and return it.  If it does
    not exist yet, return NULL.  */
-state *state_hash_lookup (size_t core_size, item_number *core);
+state *state_hash_lookup (size_t core_size, item_index *core);
 
 /* Insert STATE in the state hash table.  */
 void state_hash_insert (state *s);
-- 
2.20.1 (Apple Git-117)




reply via email to

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