| 1 | /* Grammar reduction for Bison. |
| 2 | Copyright (C) 1988, 1989, 2000, 2001, 2002 Free Software Foundation, Inc. |
| 3 | |
| 4 | This file is part of Bison, the GNU Compiler Compiler. |
| 5 | |
| 6 | Bison is free software; you can redistribute it and/or modify |
| 7 | it under the terms of the GNU General Public License as published by |
| 8 | the Free Software Foundation; either version 2, or (at your option) |
| 9 | any later version. |
| 10 | |
| 11 | Bison is distributed in the hope that it will be useful, |
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | GNU General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU General Public License |
| 17 | along with Bison; see the file COPYING. If not, write to |
| 18 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 19 | Boston, MA 02111-1307, USA. */ |
| 20 | |
| 21 | |
| 22 | /* Reduce the grammar: Find and eliminate unreachable terminals, |
| 23 | nonterminals, and productions. David S. Bakin. */ |
| 24 | |
| 25 | /* Don't eliminate unreachable terminals: They may be used by the |
| 26 | user's parser. */ |
| 27 | |
| 28 | #include "system.h" |
| 29 | #include "quotearg.h" |
| 30 | #include "getargs.h" |
| 31 | #include "files.h" |
| 32 | #include "symtab.h" |
| 33 | #include "gram.h" |
| 34 | #include "complain.h" |
| 35 | #include "reduce.h" |
| 36 | #include "reader.h" |
| 37 | #include "getargs.h" |
| 38 | #include "bitset.h" |
| 39 | |
| 40 | /* Set of all nonterminals which are not useless. */ |
| 41 | static bitset N; |
| 42 | |
| 43 | /* Set of all rules which have no useless nonterminals in their RHS. */ |
| 44 | static bitset P; |
| 45 | |
| 46 | /* Set of all accessible symbols. */ |
| 47 | static bitset V; |
| 48 | |
| 49 | /* Set of symbols used to define rule precedence (so they are |
| 50 | `useless', but no warning should be issued). */ |
| 51 | static bitset V1; |
| 52 | |
| 53 | static rule_number_t nuseful_productions; |
| 54 | rule_number_t nuseless_productions; |
| 55 | static int nuseful_nonterminals; |
| 56 | symbol_number_t nuseless_nonterminals; |
| 57 | \f |
| 58 | /*-------------------------------------------------------------------. |
| 59 | | Another way to do this would be with a set for each production and | |
| 60 | | then do subset tests against N0, but even for the C grammar the | |
| 61 | | whole reducing process takes only 2 seconds on my 8Mhz AT. | |
| 62 | `-------------------------------------------------------------------*/ |
| 63 | |
| 64 | static bool |
| 65 | useful_production (rule_number_t r, bitset N0) |
| 66 | { |
| 67 | item_number_t *rhsp; |
| 68 | |
| 69 | /* A production is useful if all of the nonterminals in its appear |
| 70 | in the set of useful nonterminals. */ |
| 71 | |
| 72 | for (rhsp = rules[r].rhs; *rhsp >= 0; ++rhsp) |
| 73 | if (ISVAR (*rhsp) && !bitset_test (N0, *rhsp - ntokens)) |
| 74 | return FALSE; |
| 75 | return TRUE; |
| 76 | } |
| 77 | |
| 78 | |
| 79 | /*---------------------------------------------------------. |
| 80 | | Remember that rules are 1-origin, symbols are 0-origin. | |
| 81 | `---------------------------------------------------------*/ |
| 82 | |
| 83 | static void |
| 84 | useless_nonterminals (void) |
| 85 | { |
| 86 | bitset Np, Ns; |
| 87 | rule_number_t r; |
| 88 | |
| 89 | /* N is set as built. Np is set being built this iteration. P is |
| 90 | set of all productions which have a RHS all in N. */ |
| 91 | |
| 92 | Np = bitset_create (nvars, BITSET_FIXED); |
| 93 | |
| 94 | |
| 95 | /* The set being computed is a set of nonterminals which can derive |
| 96 | the empty string or strings consisting of all terminals. At each |
| 97 | iteration a nonterminal is added to the set if there is a |
| 98 | production with that nonterminal as its LHS for which all the |
| 99 | nonterminals in its RHS are already in the set. Iterate until |
| 100 | the set being computed remains unchanged. Any nonterminals not |
| 101 | in the set at that point are useless in that they will never be |
| 102 | used in deriving a sentence of the language. |
| 103 | |
| 104 | This iteration doesn't use any special traversal over the |
| 105 | productions. A set is kept of all productions for which all the |
| 106 | nonterminals in the RHS are in useful. Only productions not in |
| 107 | this set are scanned on each iteration. At the end, this set is |
| 108 | saved to be used when finding useful productions: only |
| 109 | productions in this set will appear in the final grammar. */ |
| 110 | |
| 111 | while (1) |
| 112 | { |
| 113 | bitset_copy (Np, N); |
| 114 | for (r = 0; r < nrules; r++) |
| 115 | if (!bitset_test (P, r) |
| 116 | && useful_production (r, N)) |
| 117 | { |
| 118 | bitset_set (Np, rules[r].lhs->number - ntokens); |
| 119 | bitset_set (P, r); |
| 120 | } |
| 121 | if (bitset_equal_p (N, Np)) |
| 122 | break; |
| 123 | Ns = Np; |
| 124 | Np = N; |
| 125 | N = Ns; |
| 126 | } |
| 127 | bitset_free (N); |
| 128 | N = Np; |
| 129 | } |
| 130 | |
| 131 | |
| 132 | static void |
| 133 | inaccessable_symbols (void) |
| 134 | { |
| 135 | bitset Vp, Vs, Pp; |
| 136 | |
| 137 | /* Find out which productions are reachable and which symbols are |
| 138 | used. Starting with an empty set of productions and a set of |
| 139 | symbols which only has the start symbol in it, iterate over all |
| 140 | productions until the set of productions remains unchanged for an |
| 141 | iteration. For each production which has a LHS in the set of |
| 142 | reachable symbols, add the production to the set of reachable |
| 143 | productions, and add all of the nonterminals in the RHS of the |
| 144 | production to the set of reachable symbols. |
| 145 | |
| 146 | Consider only the (partially) reduced grammar which has only |
| 147 | nonterminals in N and productions in P. |
| 148 | |
| 149 | The result is the set P of productions in the reduced grammar, |
| 150 | and the set V of symbols in the reduced grammar. |
| 151 | |
| 152 | Although this algorithm also computes the set of terminals which |
| 153 | are reachable, no terminal will be deleted from the grammar. Some |
| 154 | terminals might not be in the grammar but might be generated by |
| 155 | semantic routines, and so the user might want them available with |
| 156 | specified numbers. (Is this true?) However, the nonreachable |
| 157 | terminals are printed (if running in verbose mode) so that the |
| 158 | user can know. */ |
| 159 | |
| 160 | Vp = bitset_create (nsyms, BITSET_FIXED); |
| 161 | Pp = bitset_create (nrules, BITSET_FIXED); |
| 162 | |
| 163 | /* If the start symbol isn't useful, then nothing will be useful. */ |
| 164 | if (bitset_test (N, accept->number - ntokens)) |
| 165 | { |
| 166 | bitset_set (V, accept->number); |
| 167 | |
| 168 | while (1) |
| 169 | { |
| 170 | rule_number_t r; |
| 171 | bitset_copy (Vp, V); |
| 172 | for (r = 0; r < nrules; r++) |
| 173 | { |
| 174 | if (!bitset_test (Pp, r) |
| 175 | && bitset_test (P, r) |
| 176 | && bitset_test (V, rules[r].lhs->number)) |
| 177 | { |
| 178 | item_number_t *rhsp; |
| 179 | for (rhsp = rules[r].rhs; *rhsp >= 0; rhsp++) |
| 180 | if (ISTOKEN (*rhsp) || bitset_test (N, *rhsp - ntokens)) |
| 181 | bitset_set (Vp, *rhsp); |
| 182 | bitset_set (Pp, r); |
| 183 | } |
| 184 | } |
| 185 | if (bitset_equal_p (V, Vp)) |
| 186 | break; |
| 187 | Vs = Vp; |
| 188 | Vp = V; |
| 189 | V = Vs; |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | bitset_free (V); |
| 194 | V = Vp; |
| 195 | |
| 196 | /* Tokens 0, 1, and 2 are internal to Bison. Consider them useful. */ |
| 197 | bitset_set (V, endtoken->number); /* end-of-input token */ |
| 198 | bitset_set (V, errtoken->number); /* error token */ |
| 199 | bitset_set (V, undeftoken->number); /* some undefined token */ |
| 200 | |
| 201 | bitset_free (P); |
| 202 | P = Pp; |
| 203 | |
| 204 | nuseful_productions = bitset_count (P); |
| 205 | nuseless_productions = nrules - nuseful_productions; |
| 206 | |
| 207 | nuseful_nonterminals = 0; |
| 208 | { |
| 209 | symbol_number_t i; |
| 210 | for (i = ntokens; i < nsyms; i++) |
| 211 | if (bitset_test (V, i)) |
| 212 | nuseful_nonterminals++; |
| 213 | } |
| 214 | nuseless_nonterminals = nvars - nuseful_nonterminals; |
| 215 | |
| 216 | /* A token that was used in %prec should not be warned about. */ |
| 217 | { |
| 218 | rule_number_t r; |
| 219 | for (r = 0; r < nrules; ++r) |
| 220 | if (rules[r].precsym != 0) |
| 221 | bitset_set (V1, rules[r].precsym->number); |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | |
| 226 | /*-------------------------------------------------------------------. |
| 227 | | Put the useless productions at the end of RULES, and adjust NRULES | |
| 228 | | accordingly. | |
| 229 | `-------------------------------------------------------------------*/ |
| 230 | |
| 231 | static void |
| 232 | reduce_grammar_tables (void) |
| 233 | { |
| 234 | /* Report and flag useless productions. */ |
| 235 | { |
| 236 | rule_number_t r; |
| 237 | for (r = 0; r < nrules; r++) |
| 238 | rules[r].useful = bitset_test (P, r); |
| 239 | grammar_rules_never_reduced_report (_("useless rule")); |
| 240 | } |
| 241 | |
| 242 | /* Map the nonterminals to their new index: useful first, useless |
| 243 | afterwards. Kept for later report. */ |
| 244 | { |
| 245 | int useful = 0; |
| 246 | int useless = nrules - nuseless_productions; |
| 247 | rule_t *rules_sorted = XMALLOC (rule_t, nrules); |
| 248 | rule_number_t r; |
| 249 | for (r = 0; r < nrules; ++r) |
| 250 | rules_sorted[rules[r].useful ? useful++ : useless++] = rules[r]; |
| 251 | free (rules); |
| 252 | rules = rules_sorted; |
| 253 | |
| 254 | /* Renumber the rules markers in RITEMS. */ |
| 255 | for (r = 0; r < nrules; ++r) |
| 256 | { |
| 257 | item_number_t *rhsp = rules[r].rhs; |
| 258 | for (/* Nothing. */; *rhsp >= 0; ++rhsp) |
| 259 | /* Nothing. */; |
| 260 | *rhsp = rule_number_as_item_number (r); |
| 261 | rules[r].number = r; |
| 262 | } |
| 263 | nrules -= nuseless_productions; |
| 264 | } |
| 265 | |
| 266 | /* Adjust NRITEMS. */ |
| 267 | { |
| 268 | int r; |
| 269 | int length; |
| 270 | for (r = nrules; r < nrules + nuseless_productions; ++r) |
| 271 | { |
| 272 | length = rule_rhs_length (&rules[r]); |
| 273 | nritems -= length + 1; |
| 274 | } |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | |
| 279 | /*------------------------------. |
| 280 | | Remove useless nonterminals. | |
| 281 | `------------------------------*/ |
| 282 | |
| 283 | static void |
| 284 | nonterminals_reduce (void) |
| 285 | { |
| 286 | symbol_number_t i, n; |
| 287 | |
| 288 | /* Map the nonterminals to their new index: useful first, useless |
| 289 | afterwards. Kept for later report. */ |
| 290 | |
| 291 | symbol_number_t *nontermmap = XCALLOC (symbol_number_t, nvars) - ntokens; |
| 292 | n = ntokens; |
| 293 | for (i = ntokens; i < nsyms; i++) |
| 294 | if (bitset_test (V, i)) |
| 295 | nontermmap[i] = n++; |
| 296 | for (i = ntokens; i < nsyms; i++) |
| 297 | if (!bitset_test (V, i)) |
| 298 | { |
| 299 | nontermmap[i] = n++; |
| 300 | LOCATION_PRINT (stderr, symbols[i]->location); |
| 301 | fprintf (stderr, ": %s: %s: %s\n", |
| 302 | _("warning"), _("useless nonterminal"), |
| 303 | symbols[i]->tag); |
| 304 | } |
| 305 | |
| 306 | |
| 307 | /* Shuffle elements of tables indexed by symbol number. */ |
| 308 | { |
| 309 | symbol_t **symbols_sorted = XMALLOC (symbol_t *, nvars) - ntokens; |
| 310 | |
| 311 | for (i = ntokens; i < nsyms; i++) |
| 312 | symbols[i]->number = nontermmap[i]; |
| 313 | for (i = ntokens; i < nsyms; i++) |
| 314 | symbols_sorted[nontermmap[i]] = symbols[i]; |
| 315 | for (i = ntokens; i < nsyms; i++) |
| 316 | symbols[i] = symbols_sorted[i]; |
| 317 | free (symbols_sorted + ntokens); |
| 318 | } |
| 319 | |
| 320 | { |
| 321 | rule_number_t r; |
| 322 | for (r = 0; r < nrules; ++r) |
| 323 | { |
| 324 | item_number_t *rhsp; |
| 325 | for (rhsp = rules[r].rhs; *rhsp >= 0; ++rhsp) |
| 326 | if (ISVAR (*rhsp)) |
| 327 | *rhsp = symbol_number_as_item_number (nontermmap[*rhsp]); |
| 328 | } |
| 329 | accept->number = nontermmap[accept->number]; |
| 330 | } |
| 331 | |
| 332 | nsyms -= nuseless_nonterminals; |
| 333 | nvars -= nuseless_nonterminals; |
| 334 | |
| 335 | free (nontermmap + ntokens); |
| 336 | } |
| 337 | |
| 338 | |
| 339 | /*------------------------------------------------------------------. |
| 340 | | Output the detailed results of the reductions. For FILE.output. | |
| 341 | `------------------------------------------------------------------*/ |
| 342 | |
| 343 | void |
| 344 | reduce_output (FILE *out) |
| 345 | { |
| 346 | if (nuseless_nonterminals > 0) |
| 347 | { |
| 348 | int i; |
| 349 | fprintf (out, "%s\n\n", _("Useless nonterminals")); |
| 350 | for (i = 0; i < nuseless_nonterminals; ++i) |
| 351 | fprintf (out, " %s\n", symbols[nsyms + i]->tag); |
| 352 | fputs ("\n\n", out); |
| 353 | } |
| 354 | |
| 355 | { |
| 356 | bool b = FALSE; |
| 357 | int i; |
| 358 | for (i = 0; i < ntokens; i++) |
| 359 | if (!bitset_test (V, i) && !bitset_test (V1, i)) |
| 360 | { |
| 361 | if (!b) |
| 362 | fprintf (out, "%s\n\n", _("Terminals which are not used")); |
| 363 | b = TRUE; |
| 364 | fprintf (out, " %s\n", symbols[i]->tag); |
| 365 | } |
| 366 | if (b) |
| 367 | fputs ("\n\n", out); |
| 368 | } |
| 369 | |
| 370 | if (nuseless_productions > 0) |
| 371 | grammar_rules_partial_print (out, _("Useless rules"), |
| 372 | rule_useless_p); |
| 373 | } |
| 374 | \f |
| 375 | |
| 376 | |
| 377 | |
| 378 | |
| 379 | /*-------------------------------. |
| 380 | | Report the results to STDERR. | |
| 381 | `-------------------------------*/ |
| 382 | |
| 383 | static void |
| 384 | reduce_print (void) |
| 385 | { |
| 386 | if (yacc_flag && nuseless_productions) |
| 387 | fprintf (stderr, ngettext ("%d rule never reduced\n", |
| 388 | "%d rules never reduced\n", |
| 389 | nuseless_productions), |
| 390 | nuseless_productions); |
| 391 | |
| 392 | fprintf (stderr, "%s: %s: ", infile, _("warning")); |
| 393 | |
| 394 | if (nuseless_nonterminals > 0) |
| 395 | fprintf (stderr, ngettext ("%d useless nonterminal", |
| 396 | "%d useless nonterminals", |
| 397 | nuseless_nonterminals), |
| 398 | nuseless_nonterminals); |
| 399 | |
| 400 | if (nuseless_nonterminals > 0 && nuseless_productions > 0) |
| 401 | fprintf (stderr, _(" and ")); |
| 402 | |
| 403 | if (nuseless_productions > 0) |
| 404 | fprintf (stderr, ngettext ("%d useless rule", |
| 405 | "%d useless rules", |
| 406 | nuseless_productions), |
| 407 | nuseless_productions); |
| 408 | fprintf (stderr, "\n"); |
| 409 | fflush (stderr); |
| 410 | } |
| 411 | \f |
| 412 | void |
| 413 | reduce_grammar (void) |
| 414 | { |
| 415 | bool reduced; |
| 416 | |
| 417 | /* Allocate the global sets used to compute the reduced grammar */ |
| 418 | |
| 419 | N = bitset_create (nvars, BITSET_FIXED); |
| 420 | P = bitset_create (nrules, BITSET_FIXED); |
| 421 | V = bitset_create (nsyms, BITSET_FIXED); |
| 422 | V1 = bitset_create (nsyms, BITSET_FIXED); |
| 423 | |
| 424 | useless_nonterminals (); |
| 425 | inaccessable_symbols (); |
| 426 | |
| 427 | reduced = (bool) (nuseless_nonterminals + nuseless_productions > 0); |
| 428 | if (!reduced) |
| 429 | return; |
| 430 | |
| 431 | reduce_print (); |
| 432 | |
| 433 | if (!bitset_test (N, accept->number - ntokens)) |
| 434 | fatal_at (startsymbol_location, |
| 435 | _("start symbol %s does not derive any sentence"), |
| 436 | startsymbol->tag); |
| 437 | |
| 438 | /* First reduce the nonterminals, as they renumber themselves in the |
| 439 | whole grammar. If you change the order, nonterms would be |
| 440 | renumbered only in the reduced grammar. */ |
| 441 | if (nuseless_nonterminals > 0) |
| 442 | nonterminals_reduce (); |
| 443 | if (nuseless_productions > 0) |
| 444 | reduce_grammar_tables (); |
| 445 | |
| 446 | if (trace_flag & trace_grammar) |
| 447 | { |
| 448 | grammar_dump (stderr, "Reduced Grammar"); |
| 449 | |
| 450 | fprintf (stderr, "reduced %s defines %d terminals, %d nonterminals\ |
| 451 | , and %d productions.\n", |
| 452 | infile, ntokens, nvars, nrules); |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | |
| 457 | /*-----------------------------------------------------------. |
| 458 | | Free the global sets used to compute the reduced grammar. | |
| 459 | `-----------------------------------------------------------*/ |
| 460 | |
| 461 | void |
| 462 | reduce_free (void) |
| 463 | { |
| 464 | bitset_free (N); |
| 465 | bitset_free (V); |
| 466 | bitset_free (V1); |
| 467 | bitset_free (P); |
| 468 | } |