style: cex: prefer the array notation

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.
This commit is contained in:
Akim Demaille
2020-07-11 17:59:30 +02:00
parent 5b2b7b1ffb
commit a2ad33dca6
4 changed files with 41 additions and 43 deletions

View File

@@ -196,7 +196,7 @@ expand_to_conflict (state_item_number start, symbol_number conflict_sym)
while (gl_list_size (queue) > 0) while (gl_list_size (queue) > 0)
{ {
node = (si_bfs_node *) gl_list_get_at (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); symbol_number sym = item_number_as_symbol_number (*silast->item);
if (sym == conflict_sym) if (sym == conflict_sym)
break; 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) 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; item_number *pos = si->item;
if (SI_PRODUCTION (si)) if (SI_PRODUCTION (si))
{ {
@@ -438,7 +438,7 @@ nonunifying_shift_path (gl_list_t reduce_path, state_item *shift_conflict)
if (sis->si == 0) if (sis->si == 0)
break; 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, // if the current state-item is a production item,
// its reverse production items get added to the queue. // its reverse production items get added to the queue.
// Otherwise, look for a reverse transition to the target state. // 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; state_item_number sin;
BITSET_FOR_EACH (biter, rsi, sin, 0) BITSET_FOR_EACH (biter, rsi, sin, 0)
{ {
prevsi = state_items + sin; prevsi = &state_items[sin];
if (SI_TRANSITION (search_si)) if (SI_TRANSITION (search_si))
{ {
if (prevsi->state == refsi->state) 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 // prepend path to shift we found
if (sis) 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) 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; si = prevsi;
@@ -1057,8 +1057,8 @@ unifying_example (state_item_number itm1,
bool shift_reduce, bool shift_reduce,
gl_list_t reduce_path, symbol_number next_sym) gl_list_t reduce_path, symbol_number next_sym)
{ {
state_item *conflict1 = state_items + itm1; state_item *conflict1 = &state_items[itm1];
state_item *conflict2 = state_items + itm2; state_item *conflict2 = &state_items[itm2];
search_state *initial = initial_search_state (conflict1, conflict2); search_state *initial = initial_search_state (conflict1, conflict2);
ssb_queue = gl_list_create_empty (GL_RBTREEHASH_LIST, ssb_queue = gl_list_create_empty (GL_RBTREEHASH_LIST,
(gl_listelement_equals_fn) ssb_equals, (gl_listelement_equals_fn) ssb_equals,

View File

@@ -100,7 +100,7 @@ append_lssi (lssi *sn, Hash_table *visited, gl_list_t queue)
static void static void
lssi_print (lssi *l) lssi_print (lssi *l)
{ {
print_state_item (state_items + l->si, stdout); print_state_item (&state_items[l->si], stdout);
if (l->lookahead) if (l->lookahead)
{ {
printf ("FOLLOWL = { "); printf ("FOLLOWL = { ");
@@ -175,7 +175,7 @@ shortest_path_from_start (state_item_number target, symbol_number next_sym)
finished = true; finished = true;
break; break;
} }
state_item *si = state_items + last; state_item *si = &state_items[last];
// Transitions don't change follow_L // Transitions don't change follow_L
if (si->trans >= 0) 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_t res =
gl_list_create_empty (GL_LINKED_LIST, NULL, NULL, NULL, true); gl_list_create_empty (GL_LINKED_LIST, NULL, NULL, NULL, true);
for (lssi *sn = n; sn != NULL; sn = sn->parent) 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); hash_free (visited);
gl_list_free (queue); gl_list_free (queue);
@@ -320,7 +320,7 @@ lssi_reverse_production (const state_item *si, bitset lookahead)
state_item_number sin; state_item_number sin;
BITSET_FOR_EACH (biter, si->revs, sin, 0) 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)) if (!production_allowed (prevsi, si))
continue; continue;
bitset prev_lookahead = prevsi->lookahead; bitset prev_lookahead = prevsi->lookahead;

View File

@@ -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; for (state_item_number sin = si->trans; sin != -1;
prev_sin = sin, sin = state_items[sin].trans) 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); symbol_number sp = item_number_as_symbol_number (*psi->item);
if (ISTOKEN (sp) || !nullable[sp - ntokens]) if (ISTOKEN (sp) || !nullable[sp - ntokens])
break; break;
state_item *nsi = state_items + sin; state_item *nsi = &state_items[sin];
current_ps = copy_parse_state (false, current_ps); current_ps = copy_parse_state (false, current_ps);
ps_si_append (current_ps, nsi); ps_si_append (current_ps, nsi);
ps_derivs_append (current_ps, derivation_new (sp, derivation_list_new ())); ps_derivs_append (current_ps, derivation_new (sp, derivation_list_new ()));
@@ -446,11 +446,11 @@ simulate_transition (parse_state *ps)
if (si_next < 0) if (si_next < 0)
return result; return result;
parse_state *next_ps = copy_parse_state (false, ps); 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)); ps_derivs_append (next_ps, derivation_new_leaf (sym));
parse_state_list_append (result, next_ps); 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; 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 // Take production step only if lhs is not nullable and
// if first rhs symbol is compatible with compat_sym // 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; item_number *itm1 = next->item;
if (!compatible (*itm1, compat_sym) || !production_allowed (si, next)) if (!compatible (*itm1, compat_sym) || !production_allowed (si, next))
continue; continue;
@@ -529,7 +529,7 @@ simulate_reduction (parse_state *ps, int rule_len, bitset symbol_set)
if (s_size != rule_len + 1) if (s_size != rule_len + 1)
{ {
state_item *tail = (state_item *) new_root->state_items.tail_elt; 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); parse_state_list_append (result, new_root);
} }
else else
@@ -560,7 +560,7 @@ simulate_reduction (parse_state *ps, int rule_len, bitset symbol_set)
copy = copy_parse_state (false, copy); copy = copy_parse_state (false, copy);
struct si_chunk *sis = &copy->state_items; struct si_chunk *sis = &copy->state_items;
const state_item *tail = sis->tail_elt; 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); parse_state_list_append (result, copy);
nullable_closure (copy, (state_item *) sis->tail_elt, result); 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) BITSET_FOR_EACH (biter, head->revs, sin, 0)
{ {
parse_state *copy = copy_parse_state (true, ps); 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)) if (SI_TRANSITION (head))
ps_derivs_prepend (copy, derivation_new_leaf (prepend_sym)); ps_derivs_prepend (copy, derivation_new_leaf (prepend_sym));
parse_state_list_append (res, copy); parse_state_list_append (res, copy);

View File

@@ -144,7 +144,7 @@ init_state_items (void)
for (int j = 0; j < s->nitems; ++j) for (int j = 0; j < s->nitems; ++j)
{ {
state_item_set (sidx, s, s->items[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); const rule *r = item_rule (si->item);
if (rule_search_idx < red->num && red->rules[rule_search_idx] < r) if (rule_search_idx < red->num && red->rules[rule_search_idx] < r)
++rule_search_idx; ++rule_search_idx;
@@ -222,16 +222,14 @@ init_trans (void)
// find the item in the destination state that corresponds // find the item in the destination state that corresponds
// to the transition of item // to the transition of item
for (int k = 0; k < dst->nitems; ++k) for (int k = 0; k < dst->nitems; ++k)
{ if (item + 1 == ritem + dst->items[k])
if (item + 1 == ritem + dst->items[k]) {
{ state_item_number dstSI =
state_item_number dstSI = state_item_index_lookup (dst->number, k);
state_item_index_lookup (dst->number, k);
state_items[j].trans = dstSI; state_items[j].trans = dstSI;
bitset_set (state_items[dstSI].revs, j); bitset_set (state_items[dstSI].revs, j);
break; break;
}
} }
} }
hash_free (transition_set); hash_free (transition_set);
@@ -253,7 +251,7 @@ init_prods (void)
for (int j = state_item_map[i] + s->nitems; for (int j = state_item_map[i] + s->nitems;
j < state_item_map[i + 1]; ++j) j < state_item_map[i + 1]; ++j)
{ {
state_item *src = state_items + j; state_item *src = &state_items[j];
item_number *item = src->item; item_number *item = src->item;
symbol_number lhs = item_rule (item)->lhs->number; symbol_number lhs = item_rule (item)->lhs->number;
bitset itms = hash_pair_lookup (closure_map, lhs); bitset itms = hash_pair_lookup (closure_map, lhs);
@@ -268,7 +266,7 @@ init_prods (void)
// try to create a production edge. // try to create a production edge.
for (int j = state_item_map[i]; j < state_item_map[i + 1]; ++j) 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); item_number item = *(src->item);
// Skip reduce items and items with terminals after the dot // Skip reduce items and items with terminals after the dot
if (item_number_is_rule_number (item) || ISTOKEN (item)) 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) 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) if (item_number_is_symbol_number (*(si->item)) || !si->lookahead)
continue; continue;
@@ -339,7 +337,7 @@ init_firsts (void)
firsts = bitsetv_create (nnterms, nsyms, BITSET_FIXED); firsts = bitsetv_create (nnterms, nsyms, BITSET_FIXED);
for (rule_number i = 0; i < nrules; ++i) for (rule_number i = 0; i < nrules; ++i)
{ {
rule *r = rules + i; rule *r = &rules[i];
item_number *n = r->rhs; item_number *n = r->rhs;
// Iterate through nullable nonterminals to try to find a terminal. // Iterate through nullable nonterminals to try to find a terminal.
while (item_number_is_symbol_number (*n) && ISVAR (*n) while (item_number_is_symbol_number (*n) && ISVAR (*n)
@@ -357,7 +355,7 @@ init_firsts (void)
change = false; change = false;
for (rule_number i = 0; i < nrules; ++i) for (rule_number i = 0; i < nrules; ++i)
{ {
rule *r = rules + i; rule *r = &rules[i];
symbol_number lhs = r->lhs->number; symbol_number lhs = r->lhs->number;
bitset f_lhs = FIRSTS (lhs); bitset f_lhs = FIRSTS (lhs);
for (item_number *n = r->rhs; 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); state_item *dsi = (state_item *) gl_list_get_at (queue, 0);
gl_list_remove_at (queue, 0); gl_list_remove_at (queue, 0);
if (dsi->trans >= 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) if (dsi->prods)
{ {
@@ -409,7 +407,7 @@ prune_forward (const state_item *si)
state_item_number sin; state_item_number sin;
BITSET_FOR_EACH (biter, dsi->prods, sin, 0) 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); bitset_reset (prod->revs, dsi - state_items);
if (bitset_empty_p (prod->revs)) if (bitset_empty_p (prod->revs))
gl_list_add_last (queue, prod); gl_list_add_last (queue, prod);
@@ -441,7 +439,7 @@ prune_backward (const state_item *si)
{ {
if (SI_DISABLED (sin)) if (SI_DISABLED (sin))
continue; continue;
state_item *rev = state_items + sin; state_item *rev = &state_items[sin];
if (rev->prods) if (rev->prods)
{ {
bitset_reset (rev->prods, dsi - state_items); bitset_reset (rev->prods, dsi - state_items);
@@ -466,7 +464,7 @@ prune_disabled_paths (void)
{ {
for (int i = nstate_items - 1; i >= 0; --i) 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)) if (si->trans == -1 && item_number_is_symbol_number (*si->item))
{ {
prune_forward (si); prune_forward (si);
@@ -496,7 +494,7 @@ state_items_report (void)
printf ("State %d:\n", i); printf ("State %d:\n", i);
for (int j = state_item_map[i]; j < state_item_map[i + 1]; ++j) 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); item_print (si->item, NULL, stdout);
if (SI_DISABLED (j)) if (SI_DISABLED (j))
{ {
@@ -508,7 +506,7 @@ state_items_report (void)
if (si->trans >= 0) if (si->trans >= 0)
{ {
fputs (" -> ", stdout); 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 }; bitset sets[2] = { si->prods, si->revs };
@@ -523,7 +521,7 @@ state_items_report (void)
BITSET_FOR_EACH (biter, b, sin, 0) BITSET_FOR_EACH (biter, b, sin, 0)
{ {
fputs (txt[seti], stdout); 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) for (int i = 0; i < nstate_items; ++i)
if (!SI_DISABLED (i)) if (!SI_DISABLED (i))
{ {
state_item *si = state_items + i; state_item *si = &state_items[i];
if (si->prods) if (si->prods)
bitset_free (si->prods); bitset_free (si->prods);
bitset_free (si->revs); bitset_free (si->revs);