style: decouple different uses of item_number

item_number is used for elements of ritem as well as indices into
ritem which is fairly confusing.  Introduce item_index to represent
indices into ritem.

* src/gram.h (item_index): Introduce it 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.
This commit is contained in:
Vincent Imbimbo
2020-05-24 11:09:49 -04:00
committed by Akim Demaille
parent dd878d1851
commit 6a1a681fd2
8 changed files with 26 additions and 23 deletions

View File

@@ -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];

View File

@@ -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_ */

View File

@@ -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;

View File

@@ -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

View File

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

View File

@@ -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;

View File

@@ -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);

View File

@@ -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);