-/* This is turned off because we would need to change the numbers
- in the case statements in the actions file. */
-#if 0
- /* remove useless productions */
- if (nuseless_productions > 0)
- {
- short np, pn, ni, pi;
-
- np = 0;
- ni = 0;
- for (pn = 1; pn <= nrules; pn++)
- {
- if (BITISSET (P, pn))
- {
- np++;
- if (pn != np)
- {
- rlhs[np] = rlhs[pn];
- rline[np] = rline[pn];
- rprec[np] = rprec[pn];
- rassoc[np] = rassoc[pn];
- rrhs[np] = rrhs[pn];
- if (rrhs[np] != ni)
- {
- pi = rrhs[np];
- rrhs[np] = ni;
- while (ritem[pi] >= 0)
- ritem[ni++] = ritem[pi++];
- ritem[ni++] = -np;
- }
- }
- else
- {
- while (ritem[ni++] >= 0);
- }
- }
- }
- ritem[ni] = 0;
- nrules -= nuseless_productions;
- nitems = ni;
-
- /* Is it worth it to reduce the amount of memory for the
- grammar? Probably not. */
-
- }
-#endif /* 0 */
- /* Disable useless productions,
- since they may contain useless nonterms
- that would get mapped below to -1 and confuse everyone. */
- if (nuseless_productions > 0)
- {
- int pn;
-
- for (pn = 1; pn <= nrules; pn++)
- {
- if (!BITISSET (P, pn))
- {
- rlhs[pn] = -1;
- }
- }
- }
-
- /* remove useless symbols */
- if (nuseless_nonterminals > 0)
- {
+ /* Flag useless productions. */
+ {
+ int pn;
+ for (pn = 1; pn < nrules + 1; pn++)
+ rules[pn].useful = bitset_test (P, pn);
+ }
+
+ /* Map the nonterminals to their new index: useful first, useless
+ afterwards. Kept for later report. */
+ {
+ int useful = 1;
+ int useless = nrules + 1 - nuseless_productions;
+ rule_t *rules_sorted = XMALLOC (rule_t, nrules + 1) - 1;
+ int i;
+ for (i = 1; i < nrules + 1; ++i)
+ rules_sorted[rules[i].useful ? useful++ : useless++] = rules[i];
+ free (rules + 1);
+ rules = rules_sorted;
+
+ /* Renumber the rules markers in RITEMS. */
+ for (i = 1; i < nrules + 1; ++i)
+ {
+ item_number_t *rhsp = rules[i].rhs;
+ for (/* Nothing. */; *rhsp >= 0; ++rhsp)
+ /* Nothing. */;
+ *rhsp = -i;
+ rules[i].number = i;
+ }
+ nrules -= nuseless_productions;
+ }
+
+ /* Adjust NRITEMS and NITEMS. */
+ {
+ int r;
+ int length;
+ for (r = nrules + 1; r < nrules + 1 + nuseless_productions; ++r)
+ {
+ length = rule_rhs_length (&rules[r]);
+ nritems -= length + 1;
+ }
+ }
+}