-/* 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)
- {
- rule_table[np].lhs = rule_table[pn].lhs;
- rline[np] = rline[pn];
- rule_table[np].prec = rule_table[pn].prec;
- rule_table[np].assoc = rule_table[pn].assoc;
- rule_table[np].rhs = rule_table[pn].rhs;
- if (rule_table[np].rhs != ni)
- {
- pi = rule_table[np].rhs;
- rule_table[np].rhs = 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))
- {
- rule_table[pn].lhs = -1;
- }
- }
- }
-
- /* remove useless symbols */
- if (nuseless_nonterminals > 0)
- {
-
- int i, n;
-/* short j; JF unused */
- short *nontermmap;
- rule r;
-
- /* Create a map of nonterminal number to new nonterminal
- number. -1 in the map means it was useless and is being
- eliminated. */
-
- nontermmap = XCALLOC (short, nvars) - ntokens;
- for (i = ntokens; i < nsyms; i++)
- nontermmap[i] = -1;
-
- n = ntokens;
- for (i = ntokens; i < nsyms; i++)
- if (BITISSET (V, i))
- nontermmap[i] = n++;
-
- /* Shuffle elements of tables indexed by symbol number. */
-
- for (i = ntokens; i < nsyms; i++)
- {
- n = nontermmap[i];
- if (n >= 0)
- {
- sassoc[n] = sassoc[i];
- sprec[n] = sprec[i];
- tags[n] = tags[i];
- }
- }
-
- /* Replace all symbol numbers in valid data structures. */
+ /* Report and flag useless productions. */
+ {
+ rule_number r;
+ for (r = 0; r < nrules; r++)
+ rules[r].useful = bitset_test (P, r);
+ grammar_rules_useless_report (_("rule useless in grammar"));
+ }
+
+ /* Map the nonterminals to their new index: useful first, useless
+ afterwards. Kept for later report. */
+ {
+ int useful = 0;
+ int useless = nrules - nuseless_productions;
+ rule *rules_sorted = xnmalloc (nrules, sizeof *rules_sorted);
+ rule_number r;
+ for (r = 0; r < nrules; ++r)
+ rules_sorted[rules[r].useful ? useful++ : useless++] = rules[r];
+ free (rules);
+ rules = rules_sorted;
+
+ /* Renumber the rules markers in RITEMS. */
+ for (r = 0; r < nrules; ++r)
+ {
+ item_number *rhsp = rules[r].rhs;
+ for (/* Nothing. */; *rhsp >= 0; ++rhsp)
+ /* Nothing. */;
+ *rhsp = rule_number_as_item_number (r);
+ rules[r].number = r;
+ }
+ nrules -= nuseless_productions;
+ }
+
+ /* Adjust NRITEMS. */
+ {
+ rule_number r;
+ int length;
+ for (r = nrules; r < nrules + nuseless_productions; ++r)
+ {
+ length = rule_rhs_length (&rules[r]);
+ nritems -= length + 1;
+ }
+ }
+}