static int nuseful_nonterminals;
int nuseless_nonterminals;
\f
-static int
-bits_size (bitset S)
-{
- int i, count = 0;
-
- BITSET_EXECUTE (S, 0, i, { ++count; });
- return count;
-}
-\f
/*-------------------------------------------------------------------.
| Another way to do this would be with a set for each production and |
| then do subset tests against N0, but even for the C grammar the |
/* A production is useful if all of the nonterminals in its appear
in the set of useful nonterminals. */
- for (r = &ritem[rules[i].rhs]; *r >= 0; r++)
- if (ISVAR (n = *r))
- if (!bitset_test (N0, n - ntokens))
- return FALSE;
+ for (r = rules[i].rhs; *r >= 0; r++)
+ if (ISVAR (n = *r) && !bitset_test (N0, n - ntokens))
+ return FALSE;
return TRUE;
}
set of all productions which have a RHS all in N. */
Np = bitset_create (nvars, BITSET_FIXED);
- bitset_zero (Np);
/* The set being computed is a set of nonterminals which can derive
while (1)
{
bitset_copy (Np, N);
- for (i = 1; i <= nrules; i++)
- {
- if (!bitset_test (P, i))
- {
- if (useful_production (i, N))
- {
- bitset_set (Np, rules[i].lhs - ntokens);
- bitset_set (P, i);
- }
- }
- }
+ for (i = 1; i < nrules + 1; i++)
+ if (!bitset_test (P, i)
+ && useful_production (i, N))
+ {
+ bitset_set (Np, rules[i].lhs - ntokens);
+ bitset_set (P, i);
+ }
if (bitset_equal_p (N, Np))
break;
Ns = Np;
user can know. */
Vp = bitset_create (nsyms, BITSET_FIXED);
- bitset_zero (Vp);
Pp = bitset_create (nrules + 1, BITSET_FIXED);
- bitset_zero (Pp);
/* If the start symbol isn't useful, then nothing will be useful. */
if (bitset_test (N, start_symbol - ntokens))
while (1)
{
bitset_copy (Vp, V);
- for (i = 1; i <= nrules; i++)
+ for (i = 1; i < nrules + 1; i++)
{
if (!bitset_test (Pp, i)
&& bitset_test (P, i)
&& bitset_test (V, rules[i].lhs))
{
- for (r = &ritem[rules[i].rhs]; *r >= 0; r++)
+ for (r = rules[i].rhs; *r >= 0; r++)
if (ISTOKEN (t = *r) || bitset_test (N, t - ntokens))
bitset_set (Vp, t);
bitset_set (Pp, i);
bitset_free (P);
P = Pp;
- nuseful_productions = bits_size (P);
+ nuseful_productions = bitset_count (P);
nuseless_productions = nrules - nuseful_productions;
nuseful_nonterminals = 0;
nuseless_nonterminals = nvars - nuseful_nonterminals;
/* A token that was used in %prec should not be warned about. */
- for (i = 1; i < nrules; i++)
+ for (i = 1; i < nrules + 1; i++)
if (rules[i].precsym != 0)
bitset_set (V1, rules[i].precsym);
}
np = 0;
ni = 0;
- for (pn = 1; pn <= nrules; pn++)
+ for (pn = 1; pn < nrules + 1; pn++)
if (bitset_test (P, pn))
{
np++;
rules[np].prec = rules[pn].prec;
rules[np].assoc = rules[pn].assoc;
rules[np].rhs = rules[pn].rhs;
- if (rules[np].rhs != ni)
+ if (rules[np].rhs - ritem != ni)
{
- pi = rules[np].rhs;
- rules[np].rhs = ni;
+ pi = rules[np].rhs - ritem;
+ rules[np].rhs = ritem + ni;
while (ritem[pi] >= 0)
ritem[ni++] = ritem[pi++];
ritem[ni++] = -np;
if (nuseless_productions > 0)
{
int pn;
- for (pn = 1; pn <= nrules; pn++)
+ for (pn = 1; pn < nrules + 1; pn++)
rules[pn].useful = bitset_test (P, pn);
}
}
/* Replace all symbol numbers in valid data structures. */
- for (i = 1; i <= nrules; i++)
+ for (i = 1; i < nrules + 1; i++)
{
rules[i].lhs = nontermmap[rules[i].lhs];
if (ISVAR (rules[i].precsym))
{
int i;
fprintf (out, "%s\n\n", _("Useless rules:"));
- for (i = 1; i <= nrules; i++)
+ for (i = 1; i < nrules + 1; i++)
if (!rules[i].useful)
{
rule r;
fprintf (out, "#%-4d ", i - 1);
fprintf (out, "%s:", symbols[rules[i].lhs]->tag);
- for (r = &ritem[rules[i].rhs]; *r >= 0; r++)
+ for (r = rules[i].rhs; *r >= 0; r++)
fprintf (out, " %s", symbols[*r]->tag);
fputs (";\n", out);
}
fprintf (out, "\n\n");
fprintf (out, "Rules\n-----\n\n");
fprintf (out, "Num (Prec, Assoc, Useful, Ritem Range) Lhs -> Rhs (Ritem range) [Num]\n");
- for (i = 1; i <= nrules; i++)
+ for (i = 1; i < nrules + 1; i++)
{
int rhs_count = 0;
/* Find the last RHS index in ritems. */
- for (r = &ritem[rules[i].rhs]; *r >= 0; ++r)
+ for (r = rules[i].rhs; *r >= 0; ++r)
++rhs_count;
fprintf (out, "%3d (%2d, %2d, %2d, %2d-%2d) %2d ->",
i - 1,
rules[i].prec, rules[i].assoc, rules[i].useful,
- rules[i].rhs, rules[i].rhs + rhs_count - 1,
+ rules[i].rhs - ritem, rules[i].rhs - ritem + rhs_count - 1,
rules[i].lhs);
/* Dumped the RHS. */
- for (r = &ritem[rules[i].rhs]; *r >= 0; r++)
+ for (r = rules[i].rhs; *r >= 0; r++)
fprintf (out, "%3d", *r);
fprintf (out, " [%d]\n", -(*r) - 1);
}
fprintf (out, "\n\n");
fprintf (out, "Rules interpreted\n-----------------\n\n");
- for (i = 1; i <= nrules; i++)
+ for (i = 1; i < nrules + 1; i++)
{
fprintf (out, "%-5d %s :", i, symbols[rules[i].lhs]->tag);
- for (r = &ritem[rules[i].rhs]; *r >= 0; r++)
+ for (r = rules[i].rhs; *r >= 0; r++)
fprintf (out, " %s", symbols[*r]->tag);
fputc ('\n', out);
}
/* Allocate the global sets used to compute the reduced grammar */
N = bitset_create (nvars, BITSET_FIXED);
- bitset_zero (N);
P = bitset_create (nrules + 1, BITSET_FIXED);
- bitset_zero (P);
V = bitset_create (nsyms, BITSET_FIXED);
- bitset_zero (V);
V1 = bitset_create (nsyms, BITSET_FIXED);
- bitset_zero (V1);
useless_nonterminals ();
inaccessable_symbols ();
reduced = (bool) (nuseless_nonterminals + nuseless_productions > 0);
-
if (!reduced)
return;