[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
FYI: master: style: reduce scopes
From: |
Akim Demaille |
Subject: |
FYI: master: style: reduce scopes |
Date: |
Sat, 11 Aug 2018 18:27:22 +0200 |
commit e19e088e66acf77877ac0773eb3a75caf92a0ba6
Author: Akim Demaille <address@hidden>
Date: Mon Aug 6 09:09:32 2018 +0200
style: reduce scopes
* src/closure.c, src/conflicts.c: here.
diff --git a/src/closure.c b/src/closure.c
index db17e15f..235391cd 100644
--- a/src/closure.c
+++ b/src/closure.c
@@ -54,12 +54,11 @@ static bitsetv firsts = NULL;
static void
print_closure (char const *title, item_number const *array, size_t size)
{
- size_t i;
fprintf (stderr, "Closure: %s\n", title);
- for (i = 0; i < size; ++i)
+ for (size_t i = 0; i < size; ++i)
{
- item_number *rp;
fprintf (stderr, " %2d: .", array[i]);
+ item_number *rp;
for (rp = &ritem[array[i]]; *rp >= 0; ++rp)
fprintf (stderr, " %s", symbols[*rp]->tag);
fprintf (stderr, " (rule %d)\n", -*rp - 1);
@@ -71,13 +70,12 @@ print_closure (char const *title, item_number const *array,
size_t size)
static void
print_firsts (void)
{
- symbol_number i, j;
-
fprintf (stderr, "FIRSTS\n");
- for (i = ntokens; i < nsyms; i++)
+ for (symbol_number i = ntokens; i < nsyms; ++i)
{
- bitset_iterator iter;
fprintf (stderr, " %s firsts\n", symbols[i]->tag);
+ bitset_iterator iter;
+ symbol_number j;
BITSET_FOR_EACH (iter, FIRSTS (i), j, 0)
fprintf (stderr, " %s\n", symbols[j + ntokens]->tag);
}
@@ -88,14 +86,12 @@ print_firsts (void)
static void
print_fderives (void)
{
- int i;
- rule_number r;
-
fprintf (stderr, "FDERIVES\n");
- for (i = ntokens; i < nsyms; i++)
+ for (symbol_number i = ntokens; i < nsyms; ++i)
{
- bitset_iterator iter;
fprintf (stderr, " %s derives\n", symbols[i]->tag);
+ bitset_iterator iter;
+ rule_number r;
BITSET_FOR_EACH (iter, FDERIVES (i), r, 0)
{
fprintf (stderr, " %3d ", r);
@@ -120,12 +116,10 @@ print_fderives (void)
static void
set_firsts (void)
{
- symbol_number i, j;
-
firsts = bitsetv_create (nvars, nvars, BITSET_FIXED);
- for (i = ntokens; i < nsyms; i++)
- for (j = 0; derives[i - ntokens][j]; ++j)
+ for (symbol_number i = ntokens; i < nsyms; ++i)
+ for (symbol_number j = 0; derives[i - ntokens][j]; ++j)
{
item_number sym = derives[i - ntokens][j]->rhs[0];
if (ISVAR (sym))
@@ -155,17 +149,14 @@ set_firsts (void)
static void
set_fderives (void)
{
- symbol_number i, j;
- rule_number k;
-
fderives = bitsetv_create (nvars, nrules, BITSET_FIXED);
set_firsts ();
- for (i = ntokens; i < nsyms; ++i)
- for (j = ntokens; j < nsyms; ++j)
+ for (symbol_number i = ntokens; i < nsyms; ++i)
+ for (symbol_number j = ntokens; j < nsyms; ++j)
if (bitset_test (FIRSTS (i), j - ntokens))
- for (k = 0; derives[j - ntokens][k]; ++k)
+ for (rule_number k = 0; derives[j - ntokens][k]; ++k)
bitset_set (FDERIVES (i), derives[j - ntokens][k]->number);
if (trace_flag & trace_sets)
@@ -191,27 +182,23 @@ new_closure (unsigned int n)
void
closure (item_number const *core, size_t n)
{
- /* Index over CORE. */
- size_t c;
-
- /* A bit index over RULESET. */
- rule_number ruleno;
-
- bitset_iterator iter;
-
if (trace_flag & trace_sets)
print_closure ("input", core, n);
bitset_zero (ruleset);
- for (c = 0; c < n; ++c)
+ for (size_t c = 0; c < n; ++c)
if (ISVAR (ritem[core[c]]))
bitset_or (ruleset, ruleset, FDERIVES (ritem[core[c]]));
/* core is sorted on item index in ritem, which is sorted on rule number.
Compute itemset with the same sort. */
+ /* A bit index over RULESET. */
+ rule_number ruleno;
+ bitset_iterator iter;
+
nitemset = 0;
- c = 0;
+ size_t c = 0;
BITSET_FOR_EACH (iter, ruleset, ruleno, 0)
{
item_number itemno = rules[ruleno].rhs - ritem;
diff --git a/src/conflicts.c b/src/conflicts.c
index 2138e3bb..6a6f88d9 100644
--- a/src/conflicts.c
+++ b/src/conflicts.c
@@ -221,10 +221,9 @@ static void
flush_shift (state *s, int token)
{
transitions *trans = s->transitions;
- int i;
bitset_reset (lookahead_set, token);
- for (i = 0; i < trans->num; i++)
+ for (int i = 0; i < trans->num; ++i)
if (!TRANSITION_IS_DISABLED (trans, i)
&& TRANSITION_SYMBOL (trans, i) == token)
TRANSITION_DISABLE (trans, i);
@@ -259,14 +258,13 @@ flush_reduce (bitset lookahead_tokens, int token)
static void
resolve_sr_conflict (state *s, int ruleno, symbol **errors, int *nerrs)
{
- symbol_number i;
reductions *reds = s->reductions;
/* Find the rule to reduce by to get precedence of reduction. */
rule *redrule = reds->rules[ruleno];
int redprec = redrule->prec->prec;
bitset lookahead_tokens = reds->lookahead_tokens[ruleno];
- for (i = 0; i < ntokens; i++)
+ for (symbol_number i = 0; i < ntokens; ++i)
if (bitset_test (lookahead_tokens, i)
&& bitset_test (lookahead_set, i)
&& symbols[i]->content->prec)
@@ -337,33 +335,34 @@ resolve_sr_conflict (state *s, int ruleno, symbol
**errors, int *nerrs)
static void
set_conflicts (state *s, symbol **errors)
{
- int i;
- transitions *trans = s->transitions;
- reductions *reds = s->reductions;
- int nerrs = 0;
-
if (s->consistent)
return;
+ reductions *reds = s->reductions;
+ int nerrs = 0;
+
bitset_zero (lookahead_set);
- FOR_EACH_SHIFT (trans, i)
- bitset_set (lookahead_set, TRANSITION_SYMBOL (trans, i));
+ {
+ transitions *trans = s->transitions;
+ int i;
+ FOR_EACH_SHIFT (trans, i)
+ bitset_set (lookahead_set, TRANSITION_SYMBOL (trans, i));
+ }
/* Loop over all rules which require lookahead in this state. First
check for shift-reduce conflict, and try to resolve using
precedence. */
- for (i = 0; i < reds->num; ++i)
+ for (int i = 0; i < reds->num; ++i)
if (reds->rules[i]->prec && reds->rules[i]->prec->prec
&& !bitset_disjoint_p (reds->lookahead_tokens[i], lookahead_set))
resolve_sr_conflict (s, i, errors, &nerrs);
if (nerrs)
- {
- /* Some tokens have been explicitly made errors. Allocate a
- permanent errs structure for this state, to record them. */
- state_errs_set (s, nerrs, errors);
- }
+ /* Some tokens have been explicitly made errors. Allocate a
+ permanent errs structure for this state, to record them. */
+ state_errs_set (s, nerrs, errors);
+
if (obstack_object_size (&solved_conflicts_obstack))
s->solved_conflicts = obstack_finish0 (&solved_conflicts_obstack);
if (obstack_object_size (&solved_conflicts_xml_obstack))
@@ -371,7 +370,7 @@ set_conflicts (state *s, symbol **errors)
/* Loop over all rules which require lookahead in this state. Check
for conflicts not resolved above. */
- for (i = 0; i < reds->num; ++i)
+ for (int i = 0; i < reds->num; ++i)
{
if (!bitset_disjoint_p (reds->lookahead_tokens[i], lookahead_set))
conflicts[s->number] = 1;
@@ -388,7 +387,6 @@ set_conflicts (state *s, symbol **errors)
void
conflicts_solve (void)
{
- state_number i;
/* List of lookahead tokens on which we explicitly raise a syntax error. */
symbol **errors = xnmalloc (ntokens + 1, sizeof *errors);
@@ -398,7 +396,7 @@ conflicts_solve (void)
obstack_init (&solved_conflicts_obstack);
obstack_init (&solved_conflicts_xml_obstack);
- for (i = 0; i < nstates; i++)
+ for (state_number i = 0; i < nstates; ++i)
{
set_conflicts (states[i], errors);
@@ -416,8 +414,7 @@ void
conflicts_update_state_numbers (state_number old_to_new[],
state_number nstates_old)
{
- state_number i;
- for (i = 0; i < nstates_old; ++i)
+ for (state_number i = 0; i < nstates_old; ++i)
if (old_to_new[i] != nstates_old)
conflicts[old_to_new[i]] = conflicts[i];
}
@@ -430,7 +427,6 @@ conflicts_update_state_numbers (state_number old_to_new[],
static size_t
count_state_sr_conflicts (state *s)
{
- int i;
transitions *trans = s->transitions;
reductions *reds = s->reductions;
@@ -440,10 +436,13 @@ count_state_sr_conflicts (state *s)
bitset_zero (lookahead_set);
bitset_zero (shift_set);
- FOR_EACH_SHIFT (trans, i)
- bitset_set (shift_set, TRANSITION_SYMBOL (trans, i));
+ {
+ int i;
+ FOR_EACH_SHIFT (trans, i)
+ bitset_set (shift_set, TRANSITION_SYMBOL (trans, i));
+ }
- for (i = 0; i < reds->num; ++i)
+ for (int i = 0; i < reds->num; ++i)
bitset_or (lookahead_set, lookahead_set, reds->lookahead_tokens[i]);
bitset_and (lookahead_set, lookahead_set, shift_set);
@@ -459,10 +458,8 @@ static size_t
count_sr_conflicts (void)
{
size_t res = 0;
- state_number i;
-
/* Conflicts by state. */
- for (i = 0; i < nstates; i++)
+ for (state_number i = 0; i < nstates; ++i)
if (conflicts[i])
res += count_state_sr_conflicts (states[i]);
return res;
@@ -480,15 +477,13 @@ count_sr_conflicts (void)
static size_t
count_state_rr_conflicts (state *s, bool one_per_token)
{
- int i;
reductions *reds = s->reductions;
size_t res = 0;
- for (i = 0; i < ntokens; i++)
+ for (symbol_number i = 0; i < ntokens; ++i)
{
int count = 0;
- int j;
- for (j = 0; j < reds->num; ++j)
+ for (int j = 0; j < reds->num; ++j)
count += bitset_test (reds->lookahead_tokens[j], i);
if (count >= 2)
res += one_per_token ? 1 : count-1;
@@ -501,10 +496,8 @@ static size_t
count_rr_conflicts (bool one_per_token)
{
size_t res = 0;
- state_number i;
-
/* Conflicts by state. */
- for (i = 0; i < nstates; i++)
+ for (state_number i = 0; i < nstates; ++i)
if (conflicts[i])
res += count_state_rr_conflicts (states[i], one_per_token);
return res;
@@ -519,8 +512,7 @@ void
conflicts_output (FILE *out)
{
bool printed_sth = false;
- state_number i;
- for (i = 0; i < nstates; i++)
+ for (state_number i = 0; i < nstates; ++i)
{
state *s = states[i];
if (conflicts[i])
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- FYI: master: style: reduce scopes,
Akim Demaille <=