style: reduce scopes in state.c and ielr.c

This commit is contained in:
Akim Demaille
2019-01-27 17:53:59 +01:00
parent 0d472b29ec
commit f82f7eb1d8
2 changed files with 51 additions and 63 deletions

View File

@@ -1051,27 +1051,33 @@ ielr_split_states (bitsetv follow_kernel_items, bitsetv always_follows,
} }
} }
/* The user's requested LR type. */
static LrType
lr_type_get (void)
{
char *type = muscle_percent_define_get ("lr.type");
LrType res;
if (STREQ (type, "lalr"))
res = LR_TYPE__LALR;
else if (STREQ (type, "ielr"))
res = LR_TYPE__IELR;
else if (STREQ (type, "canonical-lr"))
res = LR_TYPE__CANONICAL_LR;
else
{
aver (false);
abort ();
}
free (type);
return res;
}
void void
ielr (void) ielr (void)
{ {
LrType lr_type; LrType lr_type = lr_type_get ();
/* Examine user options. */
{
char *type = muscle_percent_define_get ("lr.type");
if (STREQ (type, "lalr"))
lr_type = LR_TYPE__LALR;
else if (STREQ (type, "ielr"))
lr_type = LR_TYPE__IELR;
else if (STREQ (type, "canonical-lr"))
lr_type = LR_TYPE__CANONICAL_LR;
else
{
aver (false);
abort ();
}
free (type);
}
/* Phase 0: LALR(1). */ /* Phase 0: LALR(1). */
timevar_push (tv_lalr); timevar_push (tv_lalr);

View File

@@ -57,8 +57,7 @@ transitions_new (int num, state **the_states)
state * state *
transitions_to (transitions *shifts, symbol_number sym) transitions_to (transitions *shifts, symbol_number sym)
{ {
int j; for (int j = 0; ; j++)
for (j = 0; ; j++)
{ {
aver (j < shifts->num); aver (j < shifts->num);
if (TRANSITION_SYMBOL (shifts, j) == sym) if (TRANSITION_SYMBOL (shifts, j) == sym)
@@ -131,12 +130,10 @@ state *
state_new (symbol_number accessing_symbol, state_new (symbol_number accessing_symbol,
size_t nitems, item_number *core) size_t nitems, item_number *core)
{ {
state *res;
size_t items_size = nitems * sizeof *core;
aver (nstates < STATE_NUMBER_MAXIMUM); aver (nstates < STATE_NUMBER_MAXIMUM);
res = xmalloc (offsetof (state, items) + items_size); size_t items_size = nitems * sizeof *core;
state *res = xmalloc (offsetof (state, items) + items_size);
res->number = nstates++; res->number = nstates++;
res->accessing_symbol = accessing_symbol; res->accessing_symbol = accessing_symbol;
res->transitions = NULL; res->transitions = NULL;
@@ -158,12 +155,10 @@ state_new (symbol_number accessing_symbol,
state * state *
state_new_isocore (state const *s) state_new_isocore (state const *s)
{ {
state *res;
size_t items_size = s->nitems * sizeof *s->items;
aver (nstates < STATE_NUMBER_MAXIMUM); aver (nstates < STATE_NUMBER_MAXIMUM);
res = xmalloc (offsetof (state, items) + items_size); size_t items_size = s->nitems * sizeof *s->items;
state *res = xmalloc (offsetof (state, items) + items_size);
res->number = nstates++; res->number = nstates++;
res->accessing_symbol = s->accessing_symbol; res->accessing_symbol = s->accessing_symbol;
res->transitions = res->transitions =
@@ -223,9 +218,8 @@ state_reductions_set (state *s, int num, rule **reds)
int int
state_reduction_find (state *s, rule *r) state_reduction_find (state *s, rule *r)
{ {
int i;
reductions *reds = s->reductions; reductions *reds = s->reductions;
for (i = 0; i < reds->num; ++i) for (int i = 0; i < reds->num; ++i)
if (reds->rules[i] == r) if (reds->rules[i] == r)
return i; return i;
return -1; return -1;
@@ -310,12 +304,10 @@ static struct hash_table *state_table = NULL;
static inline bool static inline bool
state_compare (state const *s1, state const *s2) state_compare (state const *s1, state const *s2)
{ {
size_t i;
if (s1->nitems != s2->nitems) if (s1->nitems != s2->nitems)
return false; return false;
for (i = 0; i < s1->nitems; ++i) for (size_t i = 0; i < s1->nitems; ++i)
if (s1->items[i] != s2->items[i]) if (s1->items[i] != s2->items[i])
return false; return false;
@@ -333,8 +325,7 @@ state_hash (state const *s, size_t tablesize)
{ {
/* Add up the state's item numbers to get a hash key. */ /* Add up the state's item numbers to get a hash key. */
size_t key = 0; size_t key = 0;
size_t i; for (size_t i = 0; i < s->nitems; ++i)
for (i = 0; i < s->nitems; ++i)
key += s->items[i]; key += s->items[i];
return key % tablesize; return key % tablesize;
} }
@@ -394,11 +385,9 @@ state_hash_lookup (size_t nitems, item_number *core)
{ {
size_t items_size = nitems * sizeof *core; size_t items_size = nitems * sizeof *core;
state *probe = xmalloc (offsetof (state, items) + items_size); state *probe = xmalloc (offsetof (state, items) + items_size);
state *entry;
probe->nitems = nitems; probe->nitems = nitems;
memcpy (probe->items, core, items_size); memcpy (probe->items, core, items_size);
entry = hash_lookup (state_table, probe); state *entry = hash_lookup (state_table, probe);
free (probe); free (probe);
return entry; return entry;
} }
@@ -414,12 +403,9 @@ state_record_reachable_states (state *s, bitset reachable)
if (bitset_test (reachable, s->number)) if (bitset_test (reachable, s->number))
return; return;
bitset_set (reachable, s->number); bitset_set (reachable, s->number);
{ for (int i = 0; i < s->transitions->num; ++i)
int i; if (!TRANSITION_IS_DISABLED (s->transitions, i))
for (i = 0; i < s->transitions->num; ++i) state_record_reachable_states (s->transitions->states[i], reachable);
if (!TRANSITION_IS_DISABLED (s->transitions, i))
state_record_reachable_states (s->transitions->states[i], reachable);
}
} }
void void
@@ -428,23 +414,20 @@ state_remove_unreachable_states (state_number old_to_new[])
state_number nstates_reachable = 0; state_number nstates_reachable = 0;
bitset reachable = bitset_create (nstates, BITSET_FIXED); bitset reachable = bitset_create (nstates, BITSET_FIXED);
state_record_reachable_states (states[0], reachable); state_record_reachable_states (states[0], reachable);
{ for (state_number i = 0; i < nstates; ++i)
state_number i; {
for (i = 0; i < nstates; ++i) if (bitset_test (reachable, states[i]->number))
{ {
if (bitset_test (reachable, states[i]->number)) states[nstates_reachable] = states[i];
{ states[nstates_reachable]->number = nstates_reachable;
states[nstates_reachable] = states[i]; old_to_new[i] = nstates_reachable++;
states[nstates_reachable]->number = nstates_reachable; }
old_to_new[i] = nstates_reachable++; else
} {
else state_free (states[i]);
{ old_to_new[i] = nstates;
state_free (states[i]); }
old_to_new[i] = nstates; }
}
}
}
nstates = nstates_reachable; nstates = nstates_reachable;
bitset_free (reachable); bitset_free (reachable);
} }
@@ -460,8 +443,7 @@ state **states = NULL;
void void
states_free (void) states_free (void)
{ {
state_number i; for (state_number i = 0; i < nstates; ++i)
for (i = 0; i < nstates; ++i)
state_free (states[i]); state_free (states[i]);
free (states); free (states);
} }