mirror of
https://git.savannah.gnu.org/git/bison.git
synced 2026-03-15 15:23:02 +00:00
Use hash.h for the state hash table.
* src/LR0.c (STATE_HASH_SIZE, state_hash): Remove. (allocate_storage): Use state_hash_new. (free_storage): Use state_hash_free. (new_state, get_state): Adjust. * src/lalr.h, src/lalr.c (states): Move to... * src/states.h (state_t): Remove the `link' member, no longer used. * src/states.h, src/states.c: here. (state_hash_new, state_hash_free, state_hash_lookup) (state_hash_insert, states_free): New. * src/states.c (state_table, state_compare, state_hash): New. * src/output.c (output_actions): Do not free states now, since we still need to know the final_state number in `prepare', called afterwards. Do it... * src/main.c (main): here: call states_free after `output'.
This commit is contained in:
55
src/LR0.c
55
src/LR0.c
@@ -52,11 +52,6 @@ static item_number_t **kernel_base = NULL;
|
||||
static int *kernel_size = NULL;
|
||||
static item_number_t *kernel_items = NULL;
|
||||
|
||||
/* hash table for states, to recognize equivalent ones. */
|
||||
|
||||
#define STATE_HASH_SIZE 1009
|
||||
static state_t **state_hash = NULL;
|
||||
|
||||
|
||||
static void
|
||||
allocate_itemsets (void)
|
||||
@@ -107,7 +102,7 @@ allocate_storage (void)
|
||||
|
||||
shiftset = XCALLOC (state_number_t, nsyms);
|
||||
redset = XCALLOC (short, nrules + 1);
|
||||
state_hash = XCALLOC (state_t *, STATE_HASH_SIZE);
|
||||
state_hash_new ();
|
||||
shift_symbol = XCALLOC (symbol_number_t, nsyms);
|
||||
}
|
||||
|
||||
@@ -121,7 +116,7 @@ free_storage (void)
|
||||
free (kernel_base);
|
||||
free (kernel_size);
|
||||
XFREE (kernel_items);
|
||||
free (state_hash);
|
||||
state_hash_free ();
|
||||
}
|
||||
|
||||
|
||||
@@ -185,6 +180,7 @@ new_state (symbol_number_t symbol, size_t core_size, item_number_t *core)
|
||||
nstates, symbol, symbol_tag_get (symbols[symbol]));
|
||||
|
||||
res = state_new (symbol, core_size, core);
|
||||
state_hash_insert (res);
|
||||
|
||||
/* If this is the eoftoken, and this is not the initial state, then
|
||||
this is the final state. */
|
||||
@@ -210,8 +206,6 @@ new_state (symbol_number_t symbol, size_t core_size, item_number_t *core)
|
||||
static state_number_t
|
||||
get_state (symbol_number_t symbol, size_t core_size, item_number_t *core)
|
||||
{
|
||||
int key;
|
||||
size_t i;
|
||||
state_t *sp;
|
||||
|
||||
if (trace_flag)
|
||||
@@ -219,45 +213,9 @@ get_state (symbol_number_t symbol, size_t core_size, item_number_t *core)
|
||||
this_state->number, symbol,
|
||||
symbol_tag_get (symbols[symbol]));
|
||||
|
||||
/* Add up the target state's active item numbers to get a hash key.
|
||||
*/
|
||||
key = 0;
|
||||
for (i = 0; i < core_size; ++i)
|
||||
key += core[i];
|
||||
key = key % STATE_HASH_SIZE;
|
||||
sp = state_hash[key];
|
||||
|
||||
if (sp)
|
||||
{
|
||||
int found = 0;
|
||||
while (!found)
|
||||
{
|
||||
if (sp->nitems == core_size)
|
||||
{
|
||||
found = 1;
|
||||
for (i = 0; i < core_size; ++i)
|
||||
if (core[i] != sp->items[i])
|
||||
found = 0;
|
||||
}
|
||||
|
||||
if (!found)
|
||||
{
|
||||
if (sp->link)
|
||||
{
|
||||
sp = sp->link;
|
||||
}
|
||||
else /* bucket exhausted and no match */
|
||||
{
|
||||
sp = sp->link = new_state (symbol, core_size, core);
|
||||
found = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else /* bucket is empty */
|
||||
{
|
||||
state_hash[key] = sp = new_state (symbol, core_size, core);
|
||||
}
|
||||
sp = state_hash_lookup (core_size, core);
|
||||
if (!sp)
|
||||
sp = new_state (symbol, core_size, core);
|
||||
|
||||
if (trace_flag)
|
||||
fprintf (stderr, "Exiting get_state => %d\n", sp->number);
|
||||
@@ -386,6 +344,7 @@ set_states (void)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*-------------------------------------------------------------------.
|
||||
| Compute the nondeterministic finite state machine (see state.h for |
|
||||
| details) from the grammar. |
|
||||
|
||||
Reference in New Issue
Block a user