bison-patches
[Top][All Lists]
Advanced

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

[PATCH 2/3] style: cex: prefer the array notation


From: Akim Demaille
Subject: [PATCH 2/3] style: cex: prefer the array notation
Date: Sat, 11 Jul 2020 18:53:54 +0200

Prefer `&foos[i]` to `foos + i` when `foos` is an array.  IMHO, it
makes the semantics clearer.

* src/counterexample.c, src/lssi.c, src/parse-simulation.c,
* src/state-item.c: With arrays, prefer the array notation rather than
the pointer one.
---
 src/counterexample.c   | 16 +++++++--------
 src/lssi.c             |  8 ++++----
 src/parse-simulation.c | 16 +++++++--------
 src/state-item.c       | 44 ++++++++++++++++++++----------------------
 4 files changed, 41 insertions(+), 43 deletions(-)

diff --git a/src/counterexample.c b/src/counterexample.c
index 40f8b831..ca0f4a0d 100644
--- a/src/counterexample.c
+++ b/src/counterexample.c
@@ -196,7 +196,7 @@ expand_to_conflict (state_item_number start, symbol_number 
conflict_sym)
   while (gl_list_size (queue) > 0)
     {
       node = (si_bfs_node *) gl_list_get_at (queue, 0);
-      state_item *silast = state_items + node->si;
+      state_item *silast = &state_items[node->si];
       symbol_number sym = item_number_as_symbol_number (*silast->item);
       if (sym == conflict_sym)
         break;
@@ -238,7 +238,7 @@ expand_to_conflict (state_item_number start, symbol_number 
conflict_sym)
 
   for (si_bfs_node *n = node; n != NULL; n = n->parent)
     {
-      state_item *si = state_items + n->si;
+      state_item *si = &state_items[n->si];
       item_number *pos = si->item;
       if (SI_PRODUCTION (si))
         {
@@ -438,7 +438,7 @@ nonunifying_shift_path (gl_list_t reduce_path, state_item 
*shift_conflict)
           if (sis->si == 0)
             break;
 
-          state_item *search_si = state_items + sis->si;
+          state_item *search_si = &state_items[sis->si];
           // if the current state-item is a production item,
           // its reverse production items get added to the queue.
           // Otherwise, look for a reverse transition to the target state.
@@ -447,7 +447,7 @@ nonunifying_shift_path (gl_list_t reduce_path, state_item 
*shift_conflict)
           state_item_number sin;
           BITSET_FOR_EACH (biter, rsi, sin, 0)
             {
-              prevsi = state_items + sin;
+              prevsi = &state_items[sin];
               if (SI_TRANSITION (search_si))
                 {
                   if (prevsi->state == refsi->state)
@@ -465,9 +465,9 @@ nonunifying_shift_path (gl_list_t reduce_path, state_item 
*shift_conflict)
       // prepend path to shift we found
       if (sis)
         {
-          gl_list_node_t ln = gl_list_add_first (result, state_items + 
sis->si);
+          gl_list_node_t ln = gl_list_add_first (result, 
&state_items[sis->si]);
           for (si_bfs_node *n = sis->parent; n; n = n->parent)
-            ln = gl_list_add_after (result, ln, state_items + n->si);
+            ln = gl_list_add_after (result, ln, &state_items[n->si]);
 
         }
       si = prevsi;
@@ -1057,8 +1057,8 @@ unifying_example (state_item_number itm1,
                   bool shift_reduce,
                   gl_list_t reduce_path, symbol_number next_sym)
 {
-  state_item *conflict1 = state_items + itm1;
-  state_item *conflict2 = state_items + itm2;
+  state_item *conflict1 = &state_items[itm1];
+  state_item *conflict2 = &state_items[itm2];
   search_state *initial = initial_search_state (conflict1, conflict2);
   ssb_queue = gl_list_create_empty (GL_RBTREEHASH_LIST,
                                     (gl_listelement_equals_fn) ssb_equals,
diff --git a/src/lssi.c b/src/lssi.c
index 3020db0b..86516d58 100644
--- a/src/lssi.c
+++ b/src/lssi.c
@@ -100,7 +100,7 @@ append_lssi (lssi *sn, Hash_table *visited, gl_list_t queue)
 static void
 lssi_print (lssi *l)
 {
-  print_state_item (state_items + l->si, stdout);
+  print_state_item (&state_items[l->si], stdout);
   if (l->lookahead)
     {
       printf ("FOLLOWL = { ");
@@ -175,7 +175,7 @@ shortest_path_from_start (state_item_number target, 
symbol_number next_sym)
           finished = true;
           break;
         }
-      state_item *si = state_items + last;
+      state_item *si = &state_items[last];
       // Transitions don't change follow_L
       if (si->trans >= 0)
         {
@@ -243,7 +243,7 @@ shortest_path_from_start (state_item_number target, 
symbol_number next_sym)
   gl_list_t res =
     gl_list_create_empty (GL_LINKED_LIST, NULL, NULL, NULL, true);
   for (lssi *sn = n; sn != NULL; sn = sn->parent)
-    gl_list_add_first (res, state_items + sn->si);
+    gl_list_add_first (res, &state_items[sn->si]);
 
   hash_free (visited);
   gl_list_free (queue);
@@ -320,7 +320,7 @@ lssi_reverse_production (const state_item *si, bitset 
lookahead)
   state_item_number sin;
   BITSET_FOR_EACH (biter, si->revs, sin, 0)
   {
-    state_item *prevsi = state_items + sin;
+    state_item *prevsi = &state_items[sin];
     if (!production_allowed (prevsi, si))
       continue;
     bitset prev_lookahead = prevsi->lookahead;
diff --git a/src/parse-simulation.c b/src/parse-simulation.c
index aeff0539..ce6ece9a 100644
--- a/src/parse-simulation.c
+++ b/src/parse-simulation.c
@@ -418,12 +418,12 @@ nullable_closure (parse_state *ps, state_item *si, 
parse_state_list state_list)
   for (state_item_number sin = si->trans; sin != -1;
        prev_sin = sin, sin = state_items[sin].trans)
     {
-      state_item *psi = state_items + prev_sin;
+      state_item *psi = &state_items[prev_sin];
       symbol_number sp = item_number_as_symbol_number (*psi->item);
       if (ISTOKEN (sp) || !nullable[sp - ntokens])
         break;
 
-      state_item *nsi = state_items + sin;
+      state_item *nsi = &state_items[sin];
       current_ps = copy_parse_state (false, current_ps);
       ps_si_append (current_ps, nsi);
       ps_derivs_append (current_ps, derivation_new (sp, derivation_list_new 
()));
@@ -446,11 +446,11 @@ simulate_transition (parse_state *ps)
   if (si_next < 0)
     return result;
   parse_state *next_ps = copy_parse_state (false, ps);
-  ps_si_append (next_ps, state_items + si_next);
+  ps_si_append (next_ps, &state_items[si_next]);
   ps_derivs_append (next_ps, derivation_new_leaf (sym));
   parse_state_list_append (result, next_ps);
 
-  nullable_closure (next_ps, state_items + si_next, result);
+  nullable_closure (next_ps, &state_items[si_next], result);
   return result;
 }
 
@@ -486,7 +486,7 @@ simulate_production (parse_state *ps, symbol_number 
compat_sym)
         {
           // Take production step only if lhs is not nullable and
           // if first rhs symbol is compatible with compat_sym
-          state_item *next = state_items + sin;
+          state_item *next = &state_items[sin];
           item_number *itm1 = next->item;
           if (!compatible (*itm1, compat_sym) || !production_allowed (si, 
next))
             continue;
@@ -529,7 +529,7 @@ simulate_reduction (parse_state *ps, int rule_len, bitset 
symbol_set)
   if (s_size != rule_len + 1)
     {
       state_item *tail = (state_item *) new_root->state_items.tail_elt;
-      ps_si_append (new_root, state_items + tail->trans);
+      ps_si_append (new_root, &state_items[tail->trans]);
       parse_state_list_append (result, new_root);
     }
   else
@@ -560,7 +560,7 @@ simulate_reduction (parse_state *ps, int rule_len, bitset 
symbol_set)
               copy = copy_parse_state (false, copy);
               struct si_chunk *sis = &copy->state_items;
               const state_item *tail = sis->tail_elt;
-              ps_si_append (copy, state_items + tail->trans);
+              ps_si_append (copy, &state_items[tail->trans]);
               parse_state_list_append (result, copy);
               nullable_closure (copy, (state_item *) sis->tail_elt, result);
             }
@@ -582,7 +582,7 @@ parser_prepend (parse_state *ps)
   BITSET_FOR_EACH (biter, head->revs, sin, 0)
   {
     parse_state *copy = copy_parse_state (true, ps);
-    ps_si_prepend (copy, state_items + sin);
+    ps_si_prepend (copy, &state_items[sin]);
     if (SI_TRANSITION (head))
       ps_derivs_prepend (copy, derivation_new_leaf (prepend_sym));
     parse_state_list_append (res, copy);
diff --git a/src/state-item.c b/src/state-item.c
index d3035772..5542d26b 100644
--- a/src/state-item.c
+++ b/src/state-item.c
@@ -144,7 +144,7 @@ init_state_items (void)
       for (int j = 0; j < s->nitems; ++j)
         {
           state_item_set (sidx, s, s->items[j]);
-          state_item *si = state_items + sidx;
+          state_item *si = &state_items[sidx];
           const rule *r = item_rule (si->item);
           if (rule_search_idx < red->num && red->rules[rule_search_idx] < r)
             ++rule_search_idx;
@@ -222,16 +222,14 @@ init_trans (void)
           // find the item in the destination state that corresponds
           // to the transition of item
           for (int k = 0; k < dst->nitems; ++k)
-            {
-              if (item + 1 == ritem + dst->items[k])
-                {
-                  state_item_number dstSI =
-                    state_item_index_lookup (dst->number, k);
+            if (item + 1 == ritem + dst->items[k])
+              {
+                state_item_number dstSI =
+                  state_item_index_lookup (dst->number, k);
 
-                  state_items[j].trans = dstSI;
-                  bitset_set (state_items[dstSI].revs, j);
-                  break;
-                }
+                state_items[j].trans = dstSI;
+                bitset_set (state_items[dstSI].revs, j);
+                break;
             }
         }
       hash_free (transition_set);
@@ -253,7 +251,7 @@ init_prods (void)
       for (int j = state_item_map[i] + s->nitems;
            j < state_item_map[i + 1]; ++j)
         {
-          state_item *src = state_items + j;
+          state_item *src = &state_items[j];
           item_number *item = src->item;
           symbol_number lhs = item_rule (item)->lhs->number;
           bitset itms = hash_pair_lookup (closure_map, lhs);
@@ -268,7 +266,7 @@ init_prods (void)
       // try to create a production edge.
       for (int j = state_item_map[i]; j < state_item_map[i + 1]; ++j)
         {
-          state_item *src = state_items + j;
+          state_item *src = &state_items[j];
           item_number item = *(src->item);
           // Skip reduce items and items with terminals after the dot
           if (item_number_is_rule_number (item) || ISTOKEN (item))
@@ -301,7 +299,7 @@ gen_lookaheads (void)
 {
   for (state_item_number i = 0; i < nstate_items; ++i)
     {
-      state_item *si = state_items + i;
+      state_item *si = &state_items[i];
       if (item_number_is_symbol_number (*(si->item)) || !si->lookahead)
         continue;
 
@@ -339,7 +337,7 @@ init_firsts (void)
   firsts = bitsetv_create (nnterms, nsyms, BITSET_FIXED);
   for (rule_number i = 0; i < nrules; ++i)
     {
-      rule *r = rules + i;
+      rule *r = &rules[i];
       item_number *n = r->rhs;
       // Iterate through nullable nonterminals to try to find a terminal.
       while (item_number_is_symbol_number (*n) && ISVAR (*n)
@@ -357,7 +355,7 @@ init_firsts (void)
       change = false;
       for (rule_number i = 0; i < nrules; ++i)
         {
-          rule *r = rules + i;
+          rule *r = &rules[i];
           symbol_number lhs = r->lhs->number;
           bitset f_lhs = FIRSTS (lhs);
           for (item_number *n = r->rhs;
@@ -401,7 +399,7 @@ prune_forward (const state_item *si)
       state_item *dsi = (state_item *) gl_list_get_at (queue, 0);
       gl_list_remove_at (queue, 0);
       if (dsi->trans >= 0)
-        gl_list_add_last (queue, state_items + dsi->trans);
+        gl_list_add_last (queue, &state_items[dsi->trans]);
 
       if (dsi->prods)
         {
@@ -409,7 +407,7 @@ prune_forward (const state_item *si)
           state_item_number sin;
           BITSET_FOR_EACH (biter, dsi->prods, sin, 0)
             {
-              const state_item *prod = state_items + sin;
+              const state_item *prod = &state_items[sin];
               bitset_reset (prod->revs, dsi - state_items);
               if (bitset_empty_p (prod->revs))
                 gl_list_add_last (queue, prod);
@@ -441,7 +439,7 @@ prune_backward (const state_item *si)
         {
           if (SI_DISABLED (sin))
             continue;
-          state_item *rev = state_items + sin;
+          state_item *rev = &state_items[sin];
           if (rev->prods)
             {
               bitset_reset (rev->prods, dsi - state_items);
@@ -466,7 +464,7 @@ prune_disabled_paths (void)
 {
   for (int i = nstate_items - 1; i >= 0; --i)
     {
-      state_item *si = state_items + i;
+      state_item *si = &state_items[i];
       if (si->trans == -1 && item_number_is_symbol_number (*si->item))
         {
           prune_forward (si);
@@ -496,7 +494,7 @@ state_items_report (void)
       printf ("State %d:\n", i);
       for (int j = state_item_map[i]; j < state_item_map[i + 1]; ++j)
         {
-          state_item *si = state_items + j;
+          state_item *si = &state_items[j];
           item_print (si->item, NULL, stdout);
           if (SI_DISABLED (j))
             {
@@ -508,7 +506,7 @@ state_items_report (void)
           if (si->trans >= 0)
             {
               fputs ("    -> ", stdout);
-              print_state_item (state_items + si->trans, stdout, "");
+              print_state_item (&state_items[si->trans], stdout, "");
             }
 
           bitset sets[2] = { si->prods, si->revs };
@@ -523,7 +521,7 @@ state_items_report (void)
                   BITSET_FOR_EACH (biter, b, sin, 0)
                     {
                       fputs (txt[seti], stdout);
-                      print_state_item (state_items + sin, stdout, "");
+                      print_state_item (&state_items[sin], stdout, "");
                     }
                 }
             }
@@ -565,7 +563,7 @@ state_items_free (void)
   for (int i = 0; i < nstate_items; ++i)
     if (!SI_DISABLED (i))
       {
-        state_item *si = state_items + i;
+        state_item *si = &state_items[i];
         if (si->prods)
           bitset_free (si->prods);
         bitset_free (si->revs);
-- 
2.27.0




reply via email to

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