* src/LR0.c (new_itemsets, get_state): Use more arrays and less

pointers to clarify the code.
(save_reductions, save_shifts): Factor common parts of alternatives.
This commit is contained in:
Akim Demaille
2001-11-19 10:33:54 +00:00
parent 2c5f66eda7
commit 97db7bd4a1
2 changed files with 34 additions and 63 deletions

View File

@@ -1,3 +1,9 @@
2001-11-19 Akim Demaille <akim@epita.fr>
* src/LR0.c (new_itemsets, get_state): Use more arrays and less
pointers to clarify the code.
(save_reductions, save_shifts): Factor common parts of alternatives.
2001-11-19 Akim Demaille <akim@epita.fr> 2001-11-19 Akim Demaille <akim@epita.fr>
* src/LR0.c (new_state, get_state): Complete TRACE code. * src/LR0.c (new_state, get_state): Complete TRACE code.

View File

@@ -1,5 +1,5 @@
/* Generate the nondeterministic finite state machine for bison, /* Generate the nondeterministic finite state machine for bison,
Copyright 1984, 1986, 1989, 2000 Free Software Foundation, Inc. Copyright 1984, 1986, 1989, 2000, 2001 Free Software Foundation, Inc.
This file is part of Bison, the GNU Compiler Compiler. This file is part of Bison, the GNU Compiler Compiler.
@@ -146,9 +146,6 @@ new_itemsets (void)
{ {
int i; int i;
int shiftcount; int shiftcount;
short *isp;
short *ksp;
int symbol;
#if TRACE #if TRACE
fprintf (stderr, "Entering new_itemsets, state = %d\n", fprintf (stderr, "Entering new_itemsets, state = %d\n",
@@ -160,23 +157,21 @@ new_itemsets (void)
shiftcount = 0; shiftcount = 0;
isp = itemset; for (i = 0; i < itemsetend - itemset; ++i)
while (isp < itemsetend)
{ {
i = *isp++; int symbol = ritem[itemset[i]];
symbol = ritem[i];
if (symbol > 0) if (symbol > 0)
{ {
ksp = kernel_end[symbol]; short *ksp = kernel_end[symbol];
if (!ksp) if (!ksp)
{ {
shift_symbol[shiftcount++] = symbol; shift_symbol[shiftcount] = symbol;
ksp = kernel_base[symbol]; ksp = kernel_base[symbol];
shiftcount++;
} }
*ksp++ = i + 1; *ksp++ = itemset[i] + 1;
kernel_end[symbol] = ksp; kernel_end[symbol] = ksp;
} }
} }
@@ -216,7 +211,6 @@ new_state (int symbol)
last_state->next = p; last_state->next = p;
last_state = p; last_state = p;
nstates++; nstates++;
return p; return p;
@@ -233,49 +227,38 @@ static int
get_state (int symbol) get_state (int symbol)
{ {
int key; int key;
short *isp1;
short *isp2; short *isp2;
short *iend; int i;
core *sp; core *sp;
int found;
int n; int n = kernel_end[symbol] - kernel_base[symbol];
#if TRACE #if TRACE
fprintf (stderr, "Entering get_state, state = %d, symbol = %d\n", fprintf (stderr, "Entering get_state, state = %d, symbol = %d\n",
nstates, symbol); nstates, symbol);
#endif #endif
isp1 = kernel_base[symbol]; /* Add up the target state's active item numbers to get a hash key.
iend = kernel_end[symbol]; */
n = iend - isp1;
/* add up the target state's active item numbers to get a hash key */
key = 0; key = 0;
while (isp1 < iend) for (i = 0; i < n; ++i)
key += *isp1++; key += kernel_base[symbol][i];
key = key % STATE_TABLE_SIZE; key = key % STATE_TABLE_SIZE;
sp = state_table[key]; sp = state_table[key];
if (sp) if (sp)
{ {
found = 0; int found = 0;
while (!found) while (!found)
{ {
if (sp->nitems == n) if (sp->nitems == n)
{ {
int i;
found = 1; found = 1;
isp1 = kernel_base[symbol]; for (i = 0; i < n; ++i)
isp2 = sp->items; if (kernel_base[symbol][i] != sp->items[i])
while (found && isp1 < iend)
{
if (*isp1++ != *isp2++)
found = 0; found = 0;
} }
}
if (!found) if (!found)
{ {
@@ -332,20 +315,14 @@ append_states (void)
} }
for (i = 0; i < nshifts; i++) for (i = 0; i < nshifts; i++)
{ shiftset[i] = get_state (shift_symbol[i]);
symbol = shift_symbol[i];
shiftset[i] = get_state (symbol);
}
} }
static void static void
new_states (void) new_states (void)
{ {
core *p; first_state = last_state = this_state = CORE_ALLOC (0);
p = CORE_ALLOC (0);
first_state = last_state = this_state = p;
nstates = 1; nstates = 1;
} }
@@ -353,9 +330,7 @@ new_states (void)
static void static void
save_shifts (void) save_shifts (void)
{ {
shifts *p; shifts *p = SHIFTS_ALLOC (nshifts);
p = SHIFTS_ALLOC (nshifts);
p->number = this_state->number; p->number = this_state->number;
p->nshifts = nshifts; p->nshifts = nshifts;
@@ -363,15 +338,10 @@ save_shifts (void)
shortcpy (p->shifts, shiftset, nshifts); shortcpy (p->shifts, shiftset, nshifts);
if (last_shift) if (last_shift)
{
last_shift->next = p; last_shift->next = p;
last_shift = p;
}
else else
{
first_shift = p; first_shift = p;
last_shift = p; last_shift = p;
}
} }
@@ -617,16 +587,11 @@ save_reductions (void)
shortcpy (p->rules, redset, count); shortcpy (p->rules, redset, count);
if (last_reduction) if (last_reduction)
{
last_reduction->next = p; last_reduction->next = p;
last_reduction = p;
}
else else
{
first_reduction = p; first_reduction = p;
last_reduction = p; last_reduction = p;
} }
}
} }