]> git.saurik.com Git - bison.git/blame - src/reduce.c
* src/reduce.c (reduce_grammar): When the language is empty,
[bison.git] / src / reduce.c
CommitLineData
572909b5 1/* Grammar reduction for Bison.
8b3df748 2 Copyright (C) 1988, 1989, 2000, 2001, 2002 Free Software Foundation, Inc.
572909b5 3
015acc48 4 This file is part of Bison, the GNU Compiler Compiler.
572909b5 5
015acc48
AD
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.
572909b5 10
015acc48
AD
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.
572909b5 15
015acc48
AD
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. */
572909b5
RS
20
21
015acc48
AD
22/* Reduce the grammar: Find and eliminate unreachable terminals,
23 nonterminals, and productions. David S. Bakin. */
572909b5 24
015acc48
AD
25/* Don't eliminate unreachable terminals: They may be used by the
26 user's parser. */
572909b5 27
572909b5 28#include "system.h"
8b3df748 29#include "quotearg.h"
ceed8467 30#include "getargs.h"
572909b5 31#include "files.h"
0e78e603 32#include "symtab.h"
572909b5 33#include "gram.h"
a0f6b076 34#include "complain.h"
015acc48 35#include "reduce.h"
b2ca4022
AD
36#include "reader.h"
37#include "getargs.h"
ed86e78c 38#include "bitset.h"
572909b5 39
00238958 40/* Set of all nonterminals which are not useless. */
ed86e78c 41static bitset N;
572909b5 42
00238958 43/* Set of all rules which have no useless nonterminals in their RHS. */
ed86e78c 44static bitset P;
00238958
AD
45
46/* Set of all accessible symbols. */
ed86e78c 47static bitset V;
00238958
AD
48
49/* Set of symbols used to define rule precedence (so they are
50 `useless', but no warning should be issued). */
ed86e78c 51static bitset V1;
572909b5 52
9222837b
AD
53static rule_number_t nuseful_productions;
54rule_number_t nuseless_productions;
015acc48 55static int nuseful_nonterminals;
9222837b 56symbol_number_t nuseless_nonterminals;
572909b5 57\f
015acc48
AD
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`-------------------------------------------------------------------*/
572909b5 63
a083fbbf 64static bool
9222837b 65useful_production (rule_number_t r, bitset N0)
572909b5 66{
9222837b 67 item_number_t *rhsp;
572909b5 68
015acc48
AD
69 /* A production is useful if all of the nonterminals in its appear
70 in the set of useful nonterminals. */
572909b5 71
9222837b
AD
72 for (rhsp = rules[r].rhs; *rhsp >= 0; ++rhsp)
73 if (ISVAR (*rhsp) && !bitset_test (N0, *rhsp - ntokens))
914feea9 74 return FALSE;
572909b5
RS
75 return TRUE;
76}
77
78
015acc48
AD
79/*---------------------------------------------------------.
80| Remember that rules are 1-origin, symbols are 0-origin. |
81`---------------------------------------------------------*/
572909b5 82
a083fbbf 83static void
d2729d44 84useless_nonterminals (void)
572909b5 85{
ed86e78c 86 bitset Np, Ns;
9222837b 87 rule_number_t r;
015acc48
AD
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
ed86e78c 92 Np = bitset_create (nvars, BITSET_FIXED);
ed86e78c 93
015acc48
AD
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. */
572909b5 110
572909b5
RS
111 while (1)
112 {
ed86e78c 113 bitset_copy (Np, N);
4b3d3a8e 114 for (r = 0; r < nrules; r++)
9222837b
AD
115 if (!bitset_test (P, r)
116 && useful_production (r, N))
f9abaa2c 117 {
9222837b
AD
118 bitset_set (Np, rules[r].lhs->number - ntokens);
119 bitset_set (P, r);
f9abaa2c 120 }
ed86e78c 121 if (bitset_equal_p (N, Np))
572909b5
RS
122 break;
123 Ns = Np;
124 Np = N;
125 N = Ns;
126 }
ed86e78c 127 bitset_free (N);
572909b5
RS
128 N = Np;
129}
015acc48
AD
130
131
a083fbbf 132static void
d2729d44 133inaccessable_symbols (void)
572909b5 134{
ed86e78c 135 bitset Vp, Vs, Pp;
015acc48
AD
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
ed86e78c 160 Vp = bitset_create (nsyms, BITSET_FIXED);
4b3d3a8e 161 Pp = bitset_create (nrules, BITSET_FIXED);
572909b5
RS
162
163 /* If the start symbol isn't useful, then nothing will be useful. */
2f1afb73 164 if (bitset_test (N, axiom->number - ntokens))
572909b5 165 {
2f1afb73 166 bitset_set (V, axiom->number);
2c5f66ed
AD
167
168 while (1)
572909b5 169 {
9222837b 170 rule_number_t r;
ed86e78c 171 bitset_copy (Vp, V);
4b3d3a8e 172 for (r = 0; r < nrules; r++)
572909b5 173 {
9222837b
AD
174 if (!bitset_test (Pp, r)
175 && bitset_test (P, r)
176 && bitset_test (V, rules[r].lhs->number))
572909b5 177 {
9222837b
AD
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);
572909b5 183 }
572909b5 184 }
ed86e78c 185 if (bitset_equal_p (V, Vp))
2c5f66ed
AD
186 break;
187 Vs = Vp;
188 Vp = V;
189 V = Vs;
572909b5 190 }
572909b5 191 }
572909b5 192
ed86e78c 193 bitset_free (V);
572909b5
RS
194 V = Vp;
195
196 /* Tokens 0, 1, and 2 are internal to Bison. Consider them useful. */
72a23c97
AD
197 bitset_set (V, eoftoken->number); /* end-of-input token */
198 bitset_set (V, errtoken->number); /* error token */
199 bitset_set (V, undeftoken->number); /* some undefined token */
572909b5 200
ed86e78c 201 bitset_free (P);
572909b5
RS
202 P = Pp;
203
914feea9 204 nuseful_productions = bitset_count (P);
572909b5
RS
205 nuseless_productions = nrules - nuseful_productions;
206
207 nuseful_nonterminals = 0;
9222837b
AD
208 {
209 symbol_number_t i;
210 for (i = ntokens; i < nsyms; i++)
211 if (bitset_test (V, i))
212 nuseful_nonterminals++;
213 }
572909b5
RS
214 nuseless_nonterminals = nvars - nuseful_nonterminals;
215
216 /* A token that was used in %prec should not be warned about. */
9222837b 217 {
4b3d3a8e
AD
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);
9222837b 222 }
572909b5 223}
015acc48 224
c3b407f4
AD
225
226/*-------------------------------------------------------------------.
227| Put the useless productions at the end of RULES, and adjust NRULES |
228| accordingly. |
229`-------------------------------------------------------------------*/
230
a083fbbf 231static void
d2729d44 232reduce_grammar_tables (void)
572909b5 233{
6b98e4b5 234 /* Report and flag useless productions. */
c3b407f4 235 {
9222837b 236 rule_number_t r;
4b3d3a8e 237 for (r = 0; r < nrules; r++)
6b98e4b5
AD
238 {
239 rules[r].useful = bitset_test (P, r);
240 if (!rules[r].useful)
241 {
242 LOCATION_PRINT (stderr, rules[r].location);
243 fprintf (stderr, ": %s: %s: ", _("warning"), _("useless rule"));
244 rule_print (&rules[r], stderr);
245 }
246 }
c3b407f4 247 }
572909b5 248
c3b407f4
AD
249 /* Map the nonterminals to their new index: useful first, useless
250 afterwards. Kept for later report. */
251 {
4b3d3a8e
AD
252 int useful = 0;
253 int useless = nrules - nuseless_productions;
254 rule_t *rules_sorted = XMALLOC (rule_t, nrules);
9222837b 255 rule_number_t r;
4b3d3a8e 256 for (r = 0; r < nrules; ++r)
9222837b 257 rules_sorted[rules[r].useful ? useful++ : useless++] = rules[r];
4b3d3a8e 258 free (rules);
c3b407f4 259 rules = rules_sorted;
076ab033 260
cc9305dd 261 /* Renumber the rules markers in RITEMS. */
4b3d3a8e 262 for (r = 0; r < nrules; ++r)
cc9305dd 263 {
9222837b 264 item_number_t *rhsp = rules[r].rhs;
cc9305dd
AD
265 for (/* Nothing. */; *rhsp >= 0; ++rhsp)
266 /* Nothing. */;
12b0043a 267 *rhsp = rule_number_as_item_number (r);
9222837b 268 rules[r].number = r;
cc9305dd 269 }
c3b407f4
AD
270 nrules -= nuseless_productions;
271 }
076ab033 272
a900a624 273 /* Adjust NRITEMS. */
c3b407f4
AD
274 {
275 int r;
276 int length;
4b3d3a8e 277 for (r = nrules; r < nrules + nuseless_productions; ++r)
076ab033 278 {
c3b407f4
AD
279 length = rule_rhs_length (&rules[r]);
280 nritems -= length + 1;
076ab033 281 }
c3b407f4 282 }
00238958 283}
572909b5 284
572909b5 285
00238958
AD
286/*------------------------------.
287| Remove useless nonterminals. |
288`------------------------------*/
572909b5 289
00238958
AD
290static void
291nonterminals_reduce (void)
292{
a49aecd5 293 symbol_number_t i, n;
572909b5 294
68f1e3ed
AD
295 /* Map the nonterminals to their new index: useful first, useless
296 afterwards. Kept for later report. */
572909b5 297
a49aecd5 298 symbol_number_t *nontermmap = XCALLOC (symbol_number_t, nvars) - ntokens;
00238958
AD
299 n = ntokens;
300 for (i = ntokens; i < nsyms; i++)
ed86e78c 301 if (bitset_test (V, i))
00238958 302 nontermmap[i] = n++;
760b53a8 303 for (i = ntokens; i < nsyms; i++)
ed86e78c 304 if (!bitset_test (V, i))
6b98e4b5
AD
305 {
306 nontermmap[i] = n++;
307 LOCATION_PRINT (stderr, symbols[i]->location);
308 fprintf (stderr, ": %s: %s: %s\n",
309 _("warning"), _("useless nonterminal"),
97650f4e 310 symbols[i]->tag);
6b98e4b5 311 }
572909b5 312
00238958 313
760b53a8
AD
314 /* Shuffle elements of tables indexed by symbol number. */
315 {
db8837cb 316 symbol_t **symbols_sorted = XMALLOC (symbol_t *, nvars) - ntokens;
760b53a8 317
bba97eb2
AD
318 for (i = ntokens; i < nsyms; i++)
319 symbols[i]->number = nontermmap[i];
760b53a8 320 for (i = ntokens; i < nsyms; i++)
0e78e603 321 symbols_sorted[nontermmap[i]] = symbols[i];
760b53a8 322 for (i = ntokens; i < nsyms; i++)
0e78e603
AD
323 symbols[i] = symbols_sorted[i];
324 free (symbols_sorted + ntokens);
760b53a8 325 }
572909b5 326
0c2d3f4c 327 {
9222837b 328 rule_number_t r;
4b3d3a8e 329 for (r = 0; r < nrules; ++r)
0c2d3f4c
AD
330 {
331 item_number_t *rhsp;
332 for (rhsp = rules[r].rhs; *rhsp >= 0; ++rhsp)
333 if (ISVAR (*rhsp))
a49aecd5 334 *rhsp = symbol_number_as_item_number (nontermmap[*rhsp]);
0c2d3f4c 335 }
2f1afb73 336 axiom->number = nontermmap[axiom->number];
0c2d3f4c 337 }
572909b5 338
00238958
AD
339 nsyms -= nuseless_nonterminals;
340 nvars -= nuseless_nonterminals;
572909b5 341
68f1e3ed 342 free (nontermmap + ntokens);
572909b5 343}
015acc48 344
337c5bd1 345
00238958
AD
346/*------------------------------------------------------------------.
347| Output the detailed results of the reductions. For FILE.output. |
348`------------------------------------------------------------------*/
337c5bd1
AD
349
350void
351reduce_output (FILE *out)
572909b5 352{
572909b5
RS
353 if (nuseless_nonterminals > 0)
354 {
d2d1b42b
AD
355 int i;
356 fprintf (out, "%s\n\n", _("Useless nonterminals:"));
760b53a8 357 for (i = 0; i < nuseless_nonterminals; ++i)
97650f4e 358 fprintf (out, " %s\n", symbols[nsyms + i]->tag);
d2d1b42b 359 fputs ("\n\n", out);
572909b5 360 }
d2d1b42b
AD
361
362 {
363 bool b = FALSE;
364 int i;
365 for (i = 0; i < ntokens; i++)
ed86e78c 366 if (!bitset_test (V, i) && !bitset_test (V1, i))
572909b5
RS
367 {
368 if (!b)
d2d1b42b
AD
369 fprintf (out, "%s\n\n", _("Terminals which are not used:"));
370 b = TRUE;
97650f4e 371 fprintf (out, " %s\n", symbols[i]->tag);
572909b5 372 }
d2d1b42b
AD
373 if (b)
374 fputs ("\n\n", out);
375 }
572909b5
RS
376
377 if (nuseless_productions > 0)
9757c359 378 grammar_rules_partial_print (out, _("Useless rules"),
4b3d3a8e
AD
379 nrules,
380 nrules + nuseless_productions);
572909b5
RS
381}
382\f
572909b5 383
572909b5
RS
384
385
337c5bd1
AD
386
387/*-------------------------------.
388| Report the results to STDERR. |
389`-------------------------------*/
390
a083fbbf 391static void
337c5bd1 392reduce_print (void)
572909b5 393{
89cab50d 394 if (yacc_flag && nuseless_productions)
cb487d7d
AD
395 fprintf (stderr, ngettext ("%d rule never reduced\n",
396 "%d rules never reduced\n",
397 nuseless_productions),
398 nuseless_productions);
572909b5 399
6b98e4b5 400 fprintf (stderr, "%s: %s: ", infile, _("warning"));
572909b5
RS
401
402 if (nuseless_nonterminals > 0)
cb487d7d
AD
403 fprintf (stderr, ngettext ("%d useless nonterminal",
404 "%d useless nonterminals",
405 nuseless_nonterminals),
406 nuseless_nonterminals);
407
572909b5 408 if (nuseless_nonterminals > 0 && nuseless_productions > 0)
015acc48 409 fprintf (stderr, _(" and "));
572909b5
RS
410
411 if (nuseless_productions > 0)
cb487d7d
AD
412 fprintf (stderr, ngettext ("%d useless rule",
413 "%d useless rules",
414 nuseless_productions),
415 nuseless_productions);
015acc48
AD
416 fprintf (stderr, "\n");
417 fflush (stderr);
418}
419\f
420void
421reduce_grammar (void)
422{
423 bool reduced;
424
425 /* Allocate the global sets used to compute the reduced grammar */
426
ed86e78c 427 N = bitset_create (nvars, BITSET_FIXED);
4b3d3a8e 428 P = bitset_create (nrules, BITSET_FIXED);
ed86e78c 429 V = bitset_create (nsyms, BITSET_FIXED);
ed86e78c 430 V1 = bitset_create (nsyms, BITSET_FIXED);
015acc48
AD
431
432 useless_nonterminals ();
433 inaccessable_symbols ();
434
435 reduced = (bool) (nuseless_nonterminals + nuseless_productions > 0);
337c5bd1
AD
436 if (!reduced)
437 return;
015acc48 438
337c5bd1 439 reduce_print ();
015acc48 440
2f1afb73 441 if (!bitset_test (N, axiom->number - ntokens))
1bfb97db
AD
442 fatal_at (startsymbol_location,
443 _("start symbol %s does not derive any sentence"),
444 startsymbol->tag);
015acc48 445
bb88b0fc
AD
446 /* First reduce the nonterminals, as they renumber themselves in the
447 whole grammar. If you change the order, nonterms would be
448 renumbered only in the reduced grammar. */
00238958
AD
449 if (nuseless_nonterminals > 0)
450 nonterminals_reduce ();
bb88b0fc
AD
451 if (nuseless_productions > 0)
452 reduce_grammar_tables ();
9bfe901c
AD
453
454 if (trace_flag)
455 {
78ab8f67 456 grammar_dump (stderr, "Reduced Grammar");
9bfe901c
AD
457
458 fprintf (stderr, "reduced %s defines %d terminals, %d nonterminals\
459, and %d productions.\n",
460 infile, ntokens, nvars, nrules);
461 }
337c5bd1 462}
015acc48 463
015acc48 464
337c5bd1
AD
465/*-----------------------------------------------------------.
466| Free the global sets used to compute the reduced grammar. |
467`-----------------------------------------------------------*/
468
469void
470reduce_free (void)
471{
ed86e78c
AD
472 bitset_free (N);
473 bitset_free (V);
474 bitset_free (V1);
475 bitset_free (P);
572909b5 476}