]> git.saurik.com Git - bison.git/blame - src/reduce.c
* doc/bison.texinfo: Update copyright date.
[bison.git] / src / reduce.c
CommitLineData
572909b5 1/* Grammar reduction for Bison.
337c5bd1 2 Copyright 1988, 1989, 2000, 2001 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"
ceed8467 29#include "getargs.h"
572909b5 30#include "files.h"
0e78e603 31#include "symtab.h"
572909b5 32#include "gram.h"
a0f6b076 33#include "complain.h"
015acc48 34#include "reduce.h"
b2ca4022
AD
35#include "reader.h"
36#include "getargs.h"
ed86e78c 37#include "bitset.h"
572909b5 38
015acc48 39typedef short *rule;
572909b5 40
572909b5 41
00238958 42/* Set of all nonterminals which are not useless. */
ed86e78c 43static bitset N;
572909b5 44
00238958 45/* Set of all rules which have no useless nonterminals in their RHS. */
ed86e78c 46static bitset P;
00238958
AD
47
48/* Set of all accessible symbols. */
ed86e78c 49static bitset V;
00238958
AD
50
51/* Set of symbols used to define rule precedence (so they are
52 `useless', but no warning should be issued). */
ed86e78c 53static bitset V1;
572909b5 54
015acc48
AD
55static int nuseful_productions;
56static int nuseless_productions;
57static int nuseful_nonterminals;
630e182b 58int nuseless_nonterminals;
572909b5 59\f
015acc48
AD
60/*-------------------------------------------------------------------.
61| Another way to do this would be with a set for each production and |
62| then do subset tests against N0, but even for the C grammar the |
63| whole reducing process takes only 2 seconds on my 8Mhz AT. |
64`-------------------------------------------------------------------*/
572909b5 65
a083fbbf 66static bool
ed86e78c 67useful_production (int i, bitset N0)
572909b5 68{
015acc48 69 rule r;
572909b5
RS
70 short n;
71
015acc48
AD
72 /* A production is useful if all of the nonterminals in its appear
73 in the set of useful nonterminals. */
572909b5 74
1a2b5d37 75 for (r = &ritem[rules[i].rhs]; *r >= 0; r++)
914feea9
AD
76 if (ISVAR (n = *r) && !bitset_test (N0, n - ntokens))
77 return FALSE;
572909b5
RS
78 return TRUE;
79}
80
81
015acc48
AD
82/*---------------------------------------------------------.
83| Remember that rules are 1-origin, symbols are 0-origin. |
84`---------------------------------------------------------*/
572909b5 85
a083fbbf 86static void
d2729d44 87useless_nonterminals (void)
572909b5 88{
ed86e78c 89 bitset Np, Ns;
342b8b6e 90 int i;
015acc48
AD
91
92 /* N is set as built. Np is set being built this iteration. P is
93 set of all productions which have a RHS all in N. */
94
ed86e78c 95 Np = bitset_create (nvars, BITSET_FIXED);
ed86e78c 96
015acc48
AD
97
98 /* The set being computed is a set of nonterminals which can derive
99 the empty string or strings consisting of all terminals. At each
100 iteration a nonterminal is added to the set if there is a
101 production with that nonterminal as its LHS for which all the
102 nonterminals in its RHS are already in the set. Iterate until
103 the set being computed remains unchanged. Any nonterminals not
104 in the set at that point are useless in that they will never be
105 used in deriving a sentence of the language.
106
107 This iteration doesn't use any special traversal over the
108 productions. A set is kept of all productions for which all the
109 nonterminals in the RHS are in useful. Only productions not in
110 this set are scanned on each iteration. At the end, this set is
111 saved to be used when finding useful productions: only
112 productions in this set will appear in the final grammar. */
572909b5 113
572909b5
RS
114 while (1)
115 {
ed86e78c 116 bitset_copy (Np, N);
572909b5 117 for (i = 1; i <= nrules; i++)
f9abaa2c
AD
118 if (!bitset_test (P, i)
119 && useful_production (i, N))
120 {
121 bitset_set (Np, rules[i].lhs - ntokens);
122 bitset_set (P, i);
123 }
ed86e78c 124 if (bitset_equal_p (N, Np))
572909b5
RS
125 break;
126 Ns = Np;
127 Np = N;
128 N = Ns;
129 }
ed86e78c 130 bitset_free (N);
572909b5
RS
131 N = Np;
132}
015acc48
AD
133
134
a083fbbf 135static void
d2729d44 136inaccessable_symbols (void)
572909b5 137{
ed86e78c 138 bitset Vp, Vs, Pp;
342b8b6e 139 int i;
572909b5 140 short t;
015acc48
AD
141 rule r;
142
143 /* Find out which productions are reachable and which symbols are
144 used. Starting with an empty set of productions and a set of
145 symbols which only has the start symbol in it, iterate over all
146 productions until the set of productions remains unchanged for an
147 iteration. For each production which has a LHS in the set of
148 reachable symbols, add the production to the set of reachable
149 productions, and add all of the nonterminals in the RHS of the
150 production to the set of reachable symbols.
151
152 Consider only the (partially) reduced grammar which has only
153 nonterminals in N and productions in P.
154
155 The result is the set P of productions in the reduced grammar,
156 and the set V of symbols in the reduced grammar.
157
158 Although this algorithm also computes the set of terminals which
159 are reachable, no terminal will be deleted from the grammar. Some
160 terminals might not be in the grammar but might be generated by
161 semantic routines, and so the user might want them available with
162 specified numbers. (Is this true?) However, the nonreachable
163 terminals are printed (if running in verbose mode) so that the
164 user can know. */
165
ed86e78c 166 Vp = bitset_create (nsyms, BITSET_FIXED);
ed86e78c 167 Pp = bitset_create (nrules + 1, BITSET_FIXED);
572909b5
RS
168
169 /* If the start symbol isn't useful, then nothing will be useful. */
ed86e78c 170 if (bitset_test (N, start_symbol - ntokens))
572909b5 171 {
ed86e78c 172 bitset_set (V, start_symbol);
2c5f66ed
AD
173
174 while (1)
572909b5 175 {
ed86e78c 176 bitset_copy (Vp, V);
2c5f66ed 177 for (i = 1; i <= nrules; i++)
572909b5 178 {
ed86e78c
AD
179 if (!bitset_test (Pp, i)
180 && bitset_test (P, i)
181 && bitset_test (V, rules[i].lhs))
572909b5 182 {
1a2b5d37 183 for (r = &ritem[rules[i].rhs]; *r >= 0; r++)
ed86e78c
AD
184 if (ISTOKEN (t = *r) || bitset_test (N, t - ntokens))
185 bitset_set (Vp, t);
186 bitset_set (Pp, i);
572909b5 187 }
572909b5 188 }
ed86e78c 189 if (bitset_equal_p (V, Vp))
2c5f66ed
AD
190 break;
191 Vs = Vp;
192 Vp = V;
193 V = Vs;
572909b5 194 }
572909b5 195 }
572909b5 196
ed86e78c 197 bitset_free (V);
572909b5
RS
198 V = Vp;
199
200 /* Tokens 0, 1, and 2 are internal to Bison. Consider them useful. */
ed86e78c
AD
201 bitset_set (V, 0); /* end-of-input token */
202 bitset_set (V, 1); /* error token */
203 bitset_set (V, 2); /* some undefined token */
572909b5 204
ed86e78c 205 bitset_free (P);
572909b5
RS
206 P = Pp;
207
914feea9 208 nuseful_productions = bitset_count (P);
572909b5
RS
209 nuseless_productions = nrules - nuseful_productions;
210
211 nuseful_nonterminals = 0;
212 for (i = ntokens; i < nsyms; i++)
ed86e78c 213 if (bitset_test (V, i))
572909b5
RS
214 nuseful_nonterminals++;
215 nuseless_nonterminals = nvars - nuseful_nonterminals;
216
217 /* A token that was used in %prec should not be warned about. */
218 for (i = 1; i < nrules; i++)
1a2b5d37 219 if (rules[i].precsym != 0)
ed86e78c 220 bitset_set (V1, rules[i].precsym);
572909b5 221}
015acc48 222
a083fbbf 223static void
d2729d44 224reduce_grammar_tables (void)
572909b5 225{
076ab033
AD
226 /* This is turned off because we would need to change the numbers in
227 the case statements in the actions file.
572909b5 228
076ab033
AD
229 We don't disable it via CPP so that it is still checked with the
230 rest of the code, to avoid its becoming completely obsolete.
231
232 FIXME: I think the comment above demonstrates this code must be
233 turned off for *semantic* parser, not in the general case. Try
234 to understand this better --akim. */
235
236 if (0)
237 /* remove useless productions */
238 if (nuseless_productions > 0)
239 {
240 short np, pn, ni, pi;
241
242 np = 0;
243 ni = 0;
244 for (pn = 1; pn <= nrules; pn++)
ed86e78c 245 if (bitset_test (P, pn))
572909b5
RS
246 {
247 np++;
248 if (pn != np)
249 {
1a2b5d37
AD
250 rules[np].lhs = rules[pn].lhs;
251 rules[np].line = rules[pn].line;
252 rules[np].prec = rules[pn].prec;
253 rules[np].assoc = rules[pn].assoc;
254 rules[np].rhs = rules[pn].rhs;
255 if (rules[np].rhs != ni)
572909b5 256 {
1a2b5d37
AD
257 pi = rules[np].rhs;
258 rules[np].rhs = ni;
572909b5
RS
259 while (ritem[pi] >= 0)
260 ritem[ni++] = ritem[pi++];
261 ritem[ni++] = -np;
262 }
015acc48
AD
263 }
264 else
265 {
3d4daee3
AD
266 while (ritem[ni++] >= 0)
267 /* Nothing. */;
572909b5
RS
268 }
269 }
572909b5 270
076ab033
AD
271 ritem[ni] = 0;
272 nrules -= nuseless_productions;
273 nitems = ni;
3d4daee3 274 nritems = ni;
076ab033
AD
275
276 /* Is it worth it to reduce the amount of memory for the
277 grammar? Probably not. */
278 }
572909b5 279
68f1e3ed 280 /* Disable useless productions. */
572909b5
RS
281 if (nuseless_productions > 0)
282 {
283 int pn;
572909b5 284 for (pn = 1; pn <= nrules; pn++)
ed86e78c 285 rules[pn].useful = bitset_test (P, pn);
572909b5 286 }
00238958 287}
572909b5 288
572909b5 289
00238958
AD
290/*------------------------------.
291| Remove useless nonterminals. |
292`------------------------------*/
572909b5 293
00238958
AD
294static void
295nonterminals_reduce (void)
296{
297 int i, n;
572909b5 298
68f1e3ed
AD
299 /* Map the nonterminals to their new index: useful first, useless
300 afterwards. Kept for later report. */
572909b5 301
00238958 302 short *nontermmap = XCALLOC (short, nvars) - ntokens;
00238958
AD
303 n = ntokens;
304 for (i = ntokens; i < nsyms; i++)
ed86e78c 305 if (bitset_test (V, i))
00238958 306 nontermmap[i] = n++;
760b53a8 307 for (i = ntokens; i < nsyms; i++)
ed86e78c 308 if (!bitset_test (V, i))
760b53a8 309 nontermmap[i] = n++;
572909b5 310
00238958 311
760b53a8
AD
312 /* Shuffle elements of tables indexed by symbol number. */
313 {
0e78e603 314 bucket **symbols_sorted = XMALLOC (bucket *, nvars) - ntokens;
760b53a8
AD
315
316 for (i = ntokens; i < nsyms; i++)
0e78e603 317 symbols_sorted[nontermmap[i]] = symbols[i];
760b53a8 318 for (i = ntokens; i < nsyms; i++)
0e78e603
AD
319 symbols[i] = symbols_sorted[i];
320 free (symbols_sorted + ntokens);
760b53a8 321 }
572909b5 322
00238958 323 /* Replace all symbol numbers in valid data structures. */
572909b5 324
00238958
AD
325 for (i = 1; i <= nrules; i++)
326 {
1a2b5d37
AD
327 rules[i].lhs = nontermmap[rules[i].lhs];
328 if (ISVAR (rules[i].precsym))
00238958 329 /* Can this happen? */
1a2b5d37 330 rules[i].precsym = nontermmap[rules[i].precsym];
00238958 331 }
572909b5 332
9e7f6bbd
AD
333 for (i = 0; i < nritems; ++i)
334 if (ISVAR (ritem[i]))
335 ritem[i] = nontermmap[ritem[i]];
572909b5 336
00238958 337 start_symbol = nontermmap[start_symbol];
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)
0e78e603 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;
0e78e603 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)
378 {
d2d1b42b
AD
379 int i;
380 fprintf (out, "%s\n\n", _("Useless rules:"));
572909b5 381 for (i = 1; i <= nrules; i++)
1a2b5d37 382 if (!rules[i].useful)
2c5f66ed 383 {
d2d1b42b 384 rule r;
30171f79 385 fprintf (out, "#%-4d ", i - 1);
1a2b5d37
AD
386 fprintf (out, "%s:", symbols[rules[i].lhs]->tag);
387 for (r = &ritem[rules[i].rhs]; *r >= 0; r++)
0e78e603 388 fprintf (out, " %s", symbols[*r]->tag);
d2d1b42b 389 fputs (";\n", out);
2c5f66ed 390 }
d2d1b42b 391 fputs ("\n\n", out);
572909b5 392 }
572909b5
RS
393}
394\f
4a120d45 395static void
337c5bd1 396dump_grammar (FILE *out)
572909b5
RS
397{
398 int i;
399 rule r;
400
2c5f66ed 401 fprintf (out, "REDUCED GRAMMAR\n\n");
337c5bd1 402 fprintf (out,
6013d43f
AD
403 "ntokens = %d, nvars = %d, nsyms = %d, nrules = %d, nitems = %d\n\n",
404 ntokens, nvars, nsyms, nrules, nitems);
405 fprintf (out, "Variables\n---------\n\n");
cb487d7d 406 fprintf (out, "Value Sprec Sassoc Tag\n");
572909b5 407 for (i = ntokens; i < nsyms; i++)
0e78e603
AD
408 fprintf (out, "%5d %5d %5d %s\n",
409 i,
410 symbols[i]->prec, symbols[i]->assoc, symbols[i]->tag);
337c5bd1 411 fprintf (out, "\n\n");
6013d43f 412 fprintf (out, "Rules\n-----\n\n");
3067fbef 413 fprintf (out, "Num (Prec, Assoc, Useful, Ritem Range) Lhs -> Rhs (Ritem range) [Num]\n");
572909b5
RS
414 for (i = 1; i <= nrules; i++)
415 {
3067fbef
AD
416 int rhs_count = 0;
417 /* Find the last RHS index in ritems. */
1a2b5d37 418 for (r = &ritem[rules[i].rhs]; *r >= 0; ++r)
3067fbef
AD
419 ++rhs_count;
420 fprintf (out, "%3d (%2d, %2d, %2d, %2d-%2d) %2d ->",
3d4daee3 421 i - 1,
1a2b5d37
AD
422 rules[i].prec, rules[i].assoc, rules[i].useful,
423 rules[i].rhs, rules[i].rhs + rhs_count - 1,
424 rules[i].lhs);
3067fbef 425 /* Dumped the RHS. */
1a2b5d37 426 for (r = &ritem[rules[i].rhs]; *r >= 0; r++)
3067fbef 427 fprintf (out, "%3d", *r);
3d4daee3 428 fprintf (out, " [%d]\n", -(*r) - 1);
572909b5 429 }
337c5bd1 430 fprintf (out, "\n\n");
6013d43f 431 fprintf (out, "Rules interpreted\n-----------------\n\n");
572909b5
RS
432 for (i = 1; i <= nrules; i++)
433 {
1a2b5d37
AD
434 fprintf (out, "%-5d %s :", i, symbols[rules[i].lhs]->tag);
435 for (r = &ritem[rules[i].rhs]; *r >= 0; r++)
0e78e603 436 fprintf (out, " %s", symbols[*r]->tag);
337c5bd1 437 fputc ('\n', out);
572909b5 438 }
337c5bd1 439 fprintf (out, "\n\n");
572909b5
RS
440}
441
442
337c5bd1
AD
443
444/*-------------------------------.
445| Report the results to STDERR. |
446`-------------------------------*/
447
a083fbbf 448static void
337c5bd1 449reduce_print (void)
572909b5 450{
89cab50d 451 if (yacc_flag && nuseless_productions)
cb487d7d
AD
452 fprintf (stderr, ngettext ("%d rule never reduced\n",
453 "%d rules never reduced\n",
454 nuseless_productions),
455 nuseless_productions);
572909b5 456
015acc48 457 fprintf (stderr, _("%s contains "), infile);
572909b5
RS
458
459 if (nuseless_nonterminals > 0)
cb487d7d
AD
460 fprintf (stderr, ngettext ("%d useless nonterminal",
461 "%d useless nonterminals",
462 nuseless_nonterminals),
463 nuseless_nonterminals);
464
572909b5 465 if (nuseless_nonterminals > 0 && nuseless_productions > 0)
015acc48 466 fprintf (stderr, _(" and "));
572909b5
RS
467
468 if (nuseless_productions > 0)
cb487d7d
AD
469 fprintf (stderr, ngettext ("%d useless rule",
470 "%d useless rules",
471 nuseless_productions),
472 nuseless_productions);
015acc48
AD
473 fprintf (stderr, "\n");
474 fflush (stderr);
475}
476\f
477void
478reduce_grammar (void)
479{
480 bool reduced;
481
482 /* Allocate the global sets used to compute the reduced grammar */
483
ed86e78c 484 N = bitset_create (nvars, BITSET_FIXED);
ed86e78c 485 P = bitset_create (nrules + 1, BITSET_FIXED);
ed86e78c 486 V = bitset_create (nsyms, BITSET_FIXED);
ed86e78c 487 V1 = bitset_create (nsyms, BITSET_FIXED);
015acc48
AD
488
489 useless_nonterminals ();
490 inaccessable_symbols ();
491
492 reduced = (bool) (nuseless_nonterminals + nuseless_productions > 0);
493
337c5bd1
AD
494 if (!reduced)
495 return;
015acc48 496
337c5bd1 497 reduce_print ();
015acc48 498
ed86e78c 499 if (!bitset_test (N, start_symbol - ntokens))
015acc48 500 fatal (_("Start symbol %s does not derive any sentence"),
0e78e603 501 symbols[start_symbol]->tag);
015acc48
AD
502
503 reduce_grammar_tables ();
00238958
AD
504 if (nuseless_nonterminals > 0)
505 nonterminals_reduce ();
9bfe901c
AD
506
507 if (trace_flag)
508 {
509 dump_grammar (stderr);
510
511 fprintf (stderr, "reduced %s defines %d terminals, %d nonterminals\
512, and %d productions.\n",
513 infile, ntokens, nvars, nrules);
514 }
337c5bd1 515}
015acc48 516
015acc48 517
337c5bd1
AD
518/*-----------------------------------------------------------.
519| Free the global sets used to compute the reduced grammar. |
520`-----------------------------------------------------------*/
521
522void
523reduce_free (void)
524{
ed86e78c
AD
525 bitset_free (N);
526 bitset_free (V);
527 bitset_free (V1);
528 bitset_free (P);
572909b5 529}