]> git.saurik.com Git - bison.git/blame - src/reduce.c
* tests/regression.at (AT_TEST_CPP_GUARD_H): New.
[bison.git] / src / reduce.c
CommitLineData
572909b5 1/* Grammar reduction for Bison.
aa7815f5 2 Copyright 1988, 1989, 2000 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
RS
30#include "files.h"
31#include "gram.h"
a0f6b076 32#include "complain.h"
015acc48 33#include "reduce.h"
b2ca4022
AD
34#include "reader.h"
35#include "getargs.h"
572909b5 36
572909b5 37typedef unsigned *BSet;
015acc48 38typedef short *rule;
572909b5 39
572909b5 40
015acc48
AD
41/* N is set of all nonterminals which are not useless. P is set of
42 all rules which have no useless nonterminals in their RHS. V is
43 the set of all accessible symbols. */
572909b5 44
015acc48 45static BSet N, P, V, V1;
572909b5 46
015acc48
AD
47static int nuseful_productions;
48static int nuseless_productions;
49static int nuseful_nonterminals;
50static int nuseless_nonterminals;
572909b5 51\f
4a120d45 52static bool
d2729d44 53bits_equal (BSet L, BSet R, int n)
572909b5
RS
54{
55 int i;
56
57 for (i = n - 1; i >= 0; i--)
58 if (L[i] != R[i])
59 return FALSE;
60 return TRUE;
61}
62
63
4a120d45 64static int
d2729d44 65nbits (unsigned i)
572909b5
RS
66{
67 int count = 0;
68
015acc48
AD
69 while (i != 0)
70 {
71 i ^= (i & ((unsigned) (-(int) i)));
72 ++count;
73 }
572909b5
RS
74 return count;
75}
76
77
4a120d45 78static int
d2729d44 79bits_size (BSet S, int n)
572909b5
RS
80{
81 int i, count = 0;
82
83 for (i = n - 1; i >= 0; i--)
015acc48 84 count += nbits (S[i]);
572909b5
RS
85 return count;
86}
87\f
015acc48
AD
88/*-------------------------------------------------------------------.
89| Another way to do this would be with a set for each production and |
90| then do subset tests against N0, but even for the C grammar the |
91| whole reducing process takes only 2 seconds on my 8Mhz AT. |
92`-------------------------------------------------------------------*/
572909b5 93
a083fbbf 94static bool
d2729d44 95useful_production (int i, BSet N0)
572909b5 96{
015acc48 97 rule r;
572909b5
RS
98 short n;
99
015acc48
AD
100 /* A production is useful if all of the nonterminals in its appear
101 in the set of useful nonterminals. */
572909b5
RS
102
103 for (r = &ritem[rrhs[i]]; *r > 0; r++)
015acc48
AD
104 if (ISVAR (n = *r))
105 if (!BITISSET (N0, n - ntokens))
572909b5
RS
106 return FALSE;
107 return TRUE;
108}
109
110
015acc48
AD
111/*---------------------------------------------------------.
112| Remember that rules are 1-origin, symbols are 0-origin. |
113`---------------------------------------------------------*/
572909b5 114
a083fbbf 115static void
d2729d44 116useless_nonterminals (void)
572909b5
RS
117{
118 BSet Np, Ns;
342b8b6e 119 int i;
015acc48
AD
120
121 /* N is set as built. Np is set being built this iteration. P is
122 set of all productions which have a RHS all in N. */
123
d7913476 124 Np = XCALLOC (unsigned, WORDSIZE (nvars));
015acc48
AD
125
126 /* The set being computed is a set of nonterminals which can derive
127 the empty string or strings consisting of all terminals. At each
128 iteration a nonterminal is added to the set if there is a
129 production with that nonterminal as its LHS for which all the
130 nonterminals in its RHS are already in the set. Iterate until
131 the set being computed remains unchanged. Any nonterminals not
132 in the set at that point are useless in that they will never be
133 used in deriving a sentence of the language.
134
135 This iteration doesn't use any special traversal over the
136 productions. A set is kept of all productions for which all the
137 nonterminals in the RHS are in useful. Only productions not in
138 this set are scanned on each iteration. At the end, this set is
139 saved to be used when finding useful productions: only
140 productions in this set will appear in the final grammar. */
572909b5 141
572909b5
RS
142 while (1)
143 {
015acc48 144 for (i = WORDSIZE (nvars) - 1; i >= 0; i--)
572909b5
RS
145 Np[i] = N[i];
146 for (i = 1; i <= nrules; i++)
147 {
015acc48 148 if (!BITISSET (P, i))
572909b5 149 {
015acc48 150 if (useful_production (i, N))
572909b5 151 {
015acc48
AD
152 SETBIT (Np, rlhs[i] - ntokens);
153 SETBIT (P, i);
572909b5
RS
154 }
155 }
156 }
015acc48 157 if (bits_equal (N, Np, WORDSIZE (nvars)))
572909b5
RS
158 break;
159 Ns = Np;
160 Np = N;
161 N = Ns;
162 }
d7913476 163 XFREE (N);
572909b5
RS
164 N = Np;
165}
015acc48
AD
166
167
a083fbbf 168static void
d2729d44 169inaccessable_symbols (void)
572909b5 170{
015acc48 171 BSet Vp, Vs, Pp;
342b8b6e 172 int i;
572909b5 173 short t;
015acc48
AD
174 rule r;
175
176 /* Find out which productions are reachable and which symbols are
177 used. Starting with an empty set of productions and a set of
178 symbols which only has the start symbol in it, iterate over all
179 productions until the set of productions remains unchanged for an
180 iteration. For each production which has a LHS in the set of
181 reachable symbols, add the production to the set of reachable
182 productions, and add all of the nonterminals in the RHS of the
183 production to the set of reachable symbols.
184
185 Consider only the (partially) reduced grammar which has only
186 nonterminals in N and productions in P.
187
188 The result is the set P of productions in the reduced grammar,
189 and the set V of symbols in the reduced grammar.
190
191 Although this algorithm also computes the set of terminals which
192 are reachable, no terminal will be deleted from the grammar. Some
193 terminals might not be in the grammar but might be generated by
194 semantic routines, and so the user might want them available with
195 specified numbers. (Is this true?) However, the nonreachable
196 terminals are printed (if running in verbose mode) so that the
197 user can know. */
198
d7913476
AD
199 Vp = XCALLOC (unsigned, WORDSIZE (nsyms));
200 Pp = XCALLOC (unsigned, WORDSIZE (nrules + 1));
572909b5
RS
201
202 /* If the start symbol isn't useful, then nothing will be useful. */
015acc48 203 if (!BITISSET (N, start_symbol - ntokens))
572909b5
RS
204 goto end_iteration;
205
015acc48 206 SETBIT (V, start_symbol);
572909b5 207
572909b5
RS
208 while (1)
209 {
015acc48 210 for (i = WORDSIZE (nsyms) - 1; i >= 0; i--)
572909b5
RS
211 Vp[i] = V[i];
212 for (i = 1; i <= nrules; i++)
213 {
015acc48 214 if (!BITISSET (Pp, i) && BITISSET (P, i) && BITISSET (V, rlhs[i]))
572909b5
RS
215 {
216 for (r = &ritem[rrhs[i]]; *r >= 0; r++)
217 {
015acc48 218 if (ISTOKEN (t = *r) || BITISSET (N, t - ntokens))
572909b5 219 {
015acc48 220 SETBIT (Vp, t);
572909b5
RS
221 }
222 }
015acc48 223 SETBIT (Pp, i);
572909b5
RS
224 }
225 }
015acc48 226 if (bits_equal (V, Vp, WORDSIZE (nsyms)))
572909b5
RS
227 {
228 break;
229 }
230 Vs = Vp;
231 Vp = V;
232 V = Vs;
233 }
015acc48 234end_iteration:
572909b5 235
d7913476 236 XFREE (V);
572909b5
RS
237 V = Vp;
238
239 /* Tokens 0, 1, and 2 are internal to Bison. Consider them useful. */
015acc48
AD
240 SETBIT (V, 0); /* end-of-input token */
241 SETBIT (V, 1); /* error token */
242 SETBIT (V, 2); /* some undefined token */
572909b5 243
d7913476 244 XFREE (P);
572909b5
RS
245 P = Pp;
246
015acc48 247 nuseful_productions = bits_size (P, WORDSIZE (nrules + 1));
572909b5
RS
248 nuseless_productions = nrules - nuseful_productions;
249
250 nuseful_nonterminals = 0;
251 for (i = ntokens; i < nsyms; i++)
015acc48 252 if (BITISSET (V, i))
572909b5
RS
253 nuseful_nonterminals++;
254 nuseless_nonterminals = nvars - nuseful_nonterminals;
255
256 /* A token that was used in %prec should not be warned about. */
257 for (i = 1; i < nrules; i++)
258 if (rprecsym[i] != 0)
015acc48 259 SETBIT (V1, rprecsym[i]);
572909b5 260}
015acc48 261
a083fbbf 262static void
d2729d44 263reduce_grammar_tables (void)
572909b5
RS
264{
265/* This is turned off because we would need to change the numbers
266 in the case statements in the actions file. */
267#if 0
268 /* remove useless productions */
269 if (nuseless_productions > 0)
270 {
271 short np, pn, ni, pi;
272
273 np = 0;
274 ni = 0;
275 for (pn = 1; pn <= nrules; pn++)
276 {
015acc48 277 if (BITISSET (P, pn))
572909b5
RS
278 {
279 np++;
280 if (pn != np)
281 {
282 rlhs[np] = rlhs[pn];
283 rline[np] = rline[pn];
284 rprec[np] = rprec[pn];
285 rassoc[np] = rassoc[pn];
286 rrhs[np] = rrhs[pn];
287 if (rrhs[np] != ni)
288 {
289 pi = rrhs[np];
290 rrhs[np] = ni;
291 while (ritem[pi] >= 0)
292 ritem[ni++] = ritem[pi++];
293 ritem[ni++] = -np;
294 }
015acc48
AD
295 }
296 else
297 {
572909b5
RS
298 while (ritem[ni++] >= 0);
299 }
300 }
301 }
302 ritem[ni] = 0;
303 nrules -= nuseless_productions;
304 nitems = ni;
305
015acc48
AD
306 /* Is it worth it to reduce the amount of memory for the
307 grammar? Probably not. */
572909b5
RS
308
309 }
310#endif /* 0 */
311 /* Disable useless productions,
312 since they may contain useless nonterms
313 that would get mapped below to -1 and confuse everyone. */
314 if (nuseless_productions > 0)
315 {
316 int pn;
317
318 for (pn = 1; pn <= nrules; pn++)
319 {
015acc48 320 if (!BITISSET (P, pn))
572909b5
RS
321 {
322 rlhs[pn] = -1;
323 }
324 }
325 }
326
327 /* remove useless symbols */
328 if (nuseless_nonterminals > 0)
329 {
330
015acc48 331 int i, n;
572909b5
RS
332/* short j; JF unused */
333 short *nontermmap;
015acc48 334 rule r;
572909b5 335
015acc48
AD
336 /* Create a map of nonterminal number to new nonterminal
337 number. -1 in the map means it was useless and is being
338 eliminated. */
572909b5 339
d7913476 340 nontermmap = XCALLOC (short, nvars) - ntokens;
572909b5
RS
341 for (i = ntokens; i < nsyms; i++)
342 nontermmap[i] = -1;
343
344 n = ntokens;
345 for (i = ntokens; i < nsyms; i++)
015acc48 346 if (BITISSET (V, i))
572909b5
RS
347 nontermmap[i] = n++;
348
349 /* Shuffle elements of tables indexed by symbol number. */
350
351 for (i = ntokens; i < nsyms; i++)
352 {
353 n = nontermmap[i];
354 if (n >= 0)
355 {
356 sassoc[n] = sassoc[i];
357 sprec[n] = sprec[i];
358 tags[n] = tags[i];
015acc48
AD
359 }
360 else
361 {
362 free (tags[i]);
572909b5
RS
363 }
364 }
365
366 /* Replace all symbol numbers in valid data structures. */
367
368 for (i = 1; i <= nrules; i++)
369 {
370 /* Ignore the rules disabled above. */
371 if (rlhs[i] >= 0)
372 rlhs[i] = nontermmap[rlhs[i]];
373 if (ISVAR (rprecsym[i]))
374 /* Can this happen? */
375 rprecsym[i] = nontermmap[rprecsym[i]];
376 }
377
378 for (r = ritem; *r; r++)
015acc48 379 if (ISVAR (*r))
572909b5
RS
380 *r = nontermmap[*r];
381
382 start_symbol = nontermmap[start_symbol];
383
384 nsyms -= nuseless_nonterminals;
385 nvars -= nuseless_nonterminals;
386
015acc48 387 free (&nontermmap[ntokens]);
572909b5
RS
388 }
389}
015acc48 390
a083fbbf 391static void
d2729d44 392print_results (void)
572909b5 393{
015acc48 394 int i;
572909b5 395/* short j; JF unused */
015acc48
AD
396 rule r;
397 bool b;
572909b5
RS
398
399 if (nuseless_nonterminals > 0)
400 {
ff4423cc
AD
401 obstack_sgrow (&output_obstack, _("Useless nonterminals:"));
402 obstack_sgrow (&output_obstack, "\n\n");
572909b5 403 for (i = ntokens; i < nsyms; i++)
015acc48 404 if (!BITISSET (V, i))
ff4423cc 405 obstack_fgrow1 (&output_obstack, " %s\n", tags[i]);
572909b5
RS
406 }
407 b = FALSE;
408 for (i = 0; i < ntokens; i++)
409 {
015acc48 410 if (!BITISSET (V, i) && !BITISSET (V1, i))
572909b5
RS
411 {
412 if (!b)
413 {
ff4423cc
AD
414 obstack_sgrow (&output_obstack, "\n\n");
415 obstack_sgrow (&output_obstack,
416 _("Terminals which are not used:"));
417 obstack_sgrow (&output_obstack, "\n\n");
572909b5
RS
418 b = TRUE;
419 }
ff4423cc 420 obstack_fgrow1 (&output_obstack, " %s\n", tags[i]);
572909b5
RS
421 }
422 }
423
424 if (nuseless_productions > 0)
425 {
ff4423cc
AD
426 obstack_sgrow (&output_obstack, "\n\n");
427 obstack_sgrow (&output_obstack, _("Useless rules:"));
428 obstack_sgrow (&output_obstack, "\n\n");
572909b5
RS
429 for (i = 1; i <= nrules; i++)
430 {
015acc48 431 if (!BITISSET (P, i))
572909b5 432 {
ff4423cc
AD
433 obstack_fgrow1 (&output_obstack, "#%-4d ", i);
434 obstack_fgrow1 (&output_obstack, "%s :\t", tags[rlhs[i]]);
572909b5 435 for (r = &ritem[rrhs[i]]; *r >= 0; r++)
ff4423cc
AD
436 obstack_fgrow1 (&output_obstack, " %s", tags[*r]);
437 obstack_sgrow (&output_obstack, ";\n");
572909b5
RS
438 }
439 }
440 }
441 if (nuseless_nonterminals > 0 || nuseless_productions > 0 || b)
ff4423cc 442 obstack_sgrow (&output_obstack, "\n\n");
572909b5
RS
443}
444\f
015acc48 445#if 0 /* XXX currently unused. */
4a120d45 446static void
d2729d44 447dump_grammar (void)
572909b5
RS
448{
449 int i;
450 rule r;
451
ff4423cc
AD
452 obstack_fgrow5 (&output_obstack,
453 "ntokens = %d, nvars = %d, nsyms = %d, nrules = %d, nitems = %d\n\n",
454 ntokens, nvars, nsyms, nrules, nitems);
455 obstack_sgrow (&output_obstack,
456 _("Variables\n---------\n\n"));
457 obstack_sgrow (&output_obstack,
458 _("Value Sprec Sassoc Tag\n"));
572909b5 459 for (i = ntokens; i < nsyms; i++)
ff4423cc
AD
460 obstack_fgrow4 (&output_obstack,
461 "%5d %5d %5d %s\n", i, sprec[i], sassoc[i], tags[i]);
462 obstack_sgrow (&output_obstack, "\n\n");
463 obstack_sgrow (&output_obstack, _("Rules\n-----\n\n"));
572909b5
RS
464 for (i = 1; i <= nrules; i++)
465 {
ff4423cc
AD
466 obstack_fgrow5 (&output_obstack, "%-5d(%5d%5d)%5d : (@%-5d)",
467 i, rprec[i], rassoc[i], rlhs[i], rrhs[i]);
572909b5 468 for (r = &ritem[rrhs[i]]; *r > 0; r++)
ff4423cc
AD
469 obstack_fgrow1 (&output_obstack, "%5d", *r);
470 obstack_fgrow1 (&output_obstack, " [%d]\n", -(*r));
572909b5 471 }
ff4423cc
AD
472 obstack_sgrow (&output_obstack, "\n\n");
473 obstack_sgrow (&output_obstack,
474 _("Rules interpreted\n-----------------\n\n"));
572909b5
RS
475 for (i = 1; i <= nrules; i++)
476 {
ff4423cc 477 obstack_fgrow2 (&output_obstack, "%-5d %s :", i, tags[rlhs[i]]);
572909b5 478 for (r = &ritem[rrhs[i]]; *r > 0; r++)
ff4423cc
AD
479 obstack_fgrow1 (&output_obstack, " %s", tags[*r]);
480 obstack_grow1 (&output_obstack, '\n');
572909b5 481 }
ff4423cc 482 obstack_sgrow (&output_obstack, "\n\n");
572909b5 483}
4a120d45 484#endif
572909b5
RS
485
486
a083fbbf 487static void
d2729d44 488print_notices (void)
572909b5 489{
89cab50d 490 if (yacc_flag && nuseless_productions)
015acc48 491 fprintf (stderr, _("%d rules never reduced\n"), nuseless_productions);
572909b5 492
015acc48 493 fprintf (stderr, _("%s contains "), infile);
572909b5
RS
494
495 if (nuseless_nonterminals > 0)
496 {
015acc48
AD
497 fprintf (stderr, _("%d useless nonterminal%s"),
498 nuseless_nonterminals,
499 (nuseless_nonterminals == 1 ? "" : "s"));
572909b5
RS
500 }
501 if (nuseless_nonterminals > 0 && nuseless_productions > 0)
015acc48 502 fprintf (stderr, _(" and "));
572909b5
RS
503
504 if (nuseless_productions > 0)
505 {
015acc48
AD
506 fprintf (stderr, _("%d useless rule%s"),
507 nuseless_productions, (nuseless_productions == 1 ? "" : "s"));
572909b5 508 }
015acc48
AD
509 fprintf (stderr, "\n");
510 fflush (stderr);
511}
512\f
513void
514reduce_grammar (void)
515{
516 bool reduced;
517
518 /* Allocate the global sets used to compute the reduced grammar */
519
d7913476
AD
520 N = XCALLOC (unsigned, WORDSIZE (nvars));
521 P = XCALLOC (unsigned, WORDSIZE (nrules + 1));
522 V = XCALLOC (unsigned, WORDSIZE (nsyms));
523 V1 = XCALLOC (unsigned, WORDSIZE (nsyms));
015acc48
AD
524
525 useless_nonterminals ();
526 inaccessable_symbols ();
527
528 reduced = (bool) (nuseless_nonterminals + nuseless_productions > 0);
529
89cab50d 530 if (verbose_flag)
015acc48
AD
531 print_results ();
532
533 if (reduced == FALSE)
534 goto done_reducing;
535
536 print_notices ();
537
538 if (!BITISSET (N, start_symbol - ntokens))
539 fatal (_("Start symbol %s does not derive any sentence"),
540 tags[start_symbol]);
541
542 reduce_grammar_tables ();
543#if 0
89cab50d 544 if (verbose_flag)
015acc48 545 {
ff4423cc 546 obstack_fgrow1 (&output_obstack, "REDUCED GRAMMAR\n\n");
015acc48
AD
547 dump_grammar ();
548 }
549#endif
b2ca4022 550
89cab50d 551 if (statistics_flag)
015acc48
AD
552 fprintf (stderr, _("reduced %s defines %d terminal%s, %d nonterminal%s\
553, and %d production%s.\n"),
554 infile,
555 ntokens,
556 (ntokens == 1 ? "" : "s"),
557 nvars,
558 (nvars == 1 ? "" : "s"),
559 nrules,
560 (nrules == 1 ? "" : "s"));
561
562done_reducing:
563 /* Free the global sets used to compute the reduced grammar */
564
d7913476
AD
565 XFREE (N);
566 XFREE (V);
567 XFREE (P);
572909b5 568}