style: reduce scopes in LR0.c

This commit is contained in:
Akim Demaille
2018-09-15 07:18:26 +02:00
parent 5603f0bdf2
commit 46532a9d3b

View File

@@ -89,10 +89,6 @@ static item_number *kernel_items;
static void static void
allocate_itemsets (void) allocate_itemsets (void)
{ {
symbol_number i;
rule_number r;
item_number *rhsp;
/* Count the number of occurrences of all the symbols in RITEMS. /* Count the number of occurrences of all the symbols in RITEMS.
Note that useless productions (hence useless nonterminals) are Note that useless productions (hence useless nonterminals) are
browsed too, hence we need to allocate room for _all_ the browsed too, hence we need to allocate room for _all_ the
@@ -101,8 +97,8 @@ allocate_itemsets (void)
size_t *symbol_count = xcalloc (nsyms + nuseless_nonterminals, size_t *symbol_count = xcalloc (nsyms + nuseless_nonterminals,
sizeof *symbol_count); sizeof *symbol_count);
for (r = 0; r < nrules; ++r) for (rule_number r = 0; r < nrules; ++r)
for (rhsp = rules[r].rhs; *rhsp >= 0; ++rhsp) for (item_number *rhsp = rules[r].rhs; *rhsp >= 0; ++rhsp)
{ {
count++; count++;
symbol_count[*rhsp]++; symbol_count[*rhsp]++;
@@ -118,7 +114,7 @@ allocate_itemsets (void)
kernel_items = xnmalloc (count, sizeof *kernel_items); kernel_items = xnmalloc (count, sizeof *kernel_items);
count = 0; count = 0;
for (i = 0; i < nsyms; i++) for (symbol_number i = 0; i < nsyms; i++)
{ {
kernel_base[i] = kernel_items + count; kernel_base[i] = kernel_items + count;
count += symbol_count[i]; count += symbol_count[i];
@@ -174,8 +170,6 @@ free_storage (void)
static void static void
new_itemsets (state *s) new_itemsets (state *s)
{ {
size_t i;
if (trace_flag & trace_automaton) if (trace_flag & trace_automaton)
fprintf (stderr, "Entering new_itemsets, state = %d\n", s->number); fprintf (stderr, "Entering new_itemsets, state = %d\n", s->number);
@@ -183,7 +177,7 @@ new_itemsets (state *s)
nshifts = 0; nshifts = 0;
for (i = 0; i < nitemset; ++i) for (size_t i = 0; i < nitemset; ++i)
if (item_number_is_symbol_number (ritem[itemset[i]])) if (item_number_is_symbol_number (ritem[itemset[i]]))
{ {
symbol_number sym = item_number_as_symbol_number (ritem[itemset[i]]); symbol_number sym = item_number_as_symbol_number (ritem[itemset[i]]);
@@ -209,13 +203,11 @@ new_itemsets (state *s)
static state * static state *
get_state (symbol_number sym, size_t core_size, item_number *core) get_state (symbol_number sym, size_t core_size, item_number *core)
{ {
state *s;
if (trace_flag & trace_automaton) if (trace_flag & trace_automaton)
fprintf (stderr, "Entering get_state, symbol = %d (%s)\n", fprintf (stderr, "Entering get_state, symbol = %d (%s)\n",
sym, symbols[sym]->tag); sym, symbols[sym]->tag);
s = state_hash_lookup (core_size, core); state *s = state_hash_lookup (core_size, core);
if (!s) if (!s)
s = state_list_append (sym, core_size, core); s = state_list_append (sym, core_size, core);
@@ -235,25 +227,23 @@ get_state (symbol_number sym, size_t core_size, item_number *core)
static void static void
append_states (state *s) append_states (state *s)
{ {
int i;
if (trace_flag & trace_automaton) if (trace_flag & trace_automaton)
fprintf (stderr, "Entering append_states, state = %d\n", s->number); fprintf (stderr, "Entering append_states, state = %d\n", s->number);
/* First sort shift_symbol into increasing order. */ /* First sort shift_symbol into increasing order. */
for (i = 1; i < nshifts; i++) for (int i = 1; i < nshifts; i++)
{ {
symbol_number sym = shift_symbol[i]; const symbol_number sym = shift_symbol[i];
int j; int j = i;
for (j = i; 0 < j && sym < shift_symbol[j - 1]; j--) for (; 0 < j && sym < shift_symbol[j - 1]; j--)
shift_symbol[j] = shift_symbol[j - 1]; shift_symbol[j] = shift_symbol[j - 1];
shift_symbol[j] = sym; shift_symbol[j] = sym;
} }
for (i = 0; i < nshifts; i++) for (int i = 0; i < nshifts; i++)
{ {
symbol_number sym = shift_symbol[i]; const symbol_number sym = shift_symbol[i];
shiftset[i] = get_state (sym, kernel_size[sym], kernel_base[sym]); shiftset[i] = get_state (sym, kernel_size[sym], kernel_base[sym]);
} }
} }
@@ -269,10 +259,9 @@ static void
save_reductions (state *s) save_reductions (state *s)
{ {
int count = 0; int count = 0;
size_t i;
/* Find and count the active items that represent ends of rules. */ /* Find and count the active items that represent ends of rules. */
for (i = 0; i < nitemset; ++i) for (size_t i = 0; i < nitemset; ++i)
{ {
item_number item = ritem[itemset[i]]; item_number item = ritem[itemset[i]];
if (item_number_is_rule_number (item)) if (item_number_is_rule_number (item))
@@ -334,17 +323,16 @@ set_states (void)
void void
generate_states (void) generate_states (void)
{ {
item_number initial_core = 0;
state_list *list = NULL;
allocate_storage (); allocate_storage ();
new_closure (nritems); new_closure (nritems);
/* Create the initial state. The 0 at the lhs is the index of the /* Create the initial state. The 0 at the lhs is the index of the
item of this initial rule. */ item of this initial rule. */
item_number initial_core = 0;
state_list_append (0, 1, &initial_core); state_list_append (0, 1, &initial_core);
/* States are queued when they are created; process them all. */ /* States are queued when they are created; process them all. */
for (list = first_state; list; list = list->next) for (state_list *list = first_state; list; list = list->next)
{ {
state *s = list->state; state *s = list->state;
if (trace_flag & trace_automaton) if (trace_flag & trace_automaton)