]>
git.saurik.com Git - bison.git/blob - src/reduce.c
1 /* Grammar reduction for Bison.
2 Copyright (C) 1988, 1989 Free Software Foundation, Inc.
4 This file is part of Bison, the GNU Compiler Compiler.
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)
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.
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, 675 Mass Ave, Cambridge, MA 02139, USA. */
22 * Reduce the grammar: Find and eliminate unreachable terminals,
23 * nonterminals, and productions. David S. Bakin.
27 * Don't eliminate unreachable terminals: They may be used by the user's
39 extern char **tags
; /* reader.c */
40 extern int verboseflag
; /* getargs.c */
41 static int statisticsflag
; /* XXXXXXX */
48 typedef unsigned *BSet
;
53 * N is set of all nonterminals which are not useless. P is set of all rules
54 * which have no useless nonterminals in their RHS. V is the set of all
58 static BSet N
, P
, V
, V1
;
60 static int nuseful_productions
, nuseless_productions
,
61 nuseful_nonterminals
, nuseless_nonterminals
;
64 static void useless_nonterminals();
65 static void inaccessable_symbols();
66 static void reduce_grammar_tables();
67 static void print_results();
68 static void print_notices();
71 extern void fatals ();
82 for (i
= n
- 1; i
>= 0; i
--)
96 i
^= (i
& ((unsigned) (- (int) i
)));
110 for (i
= n
- 1; i
>= 0; i
--)
111 count
+= nbits(S
[i
]);
120 /* Allocate the global sets used to compute the reduced grammar */
122 N
= NEW2(WORDSIZE(nvars
), unsigned);
123 P
= NEW2(WORDSIZE(nrules
+ 1), unsigned);
124 V
= NEW2(WORDSIZE(nsyms
), unsigned);
125 V1
= NEW2(WORDSIZE(nsyms
), unsigned);
127 useless_nonterminals();
128 inaccessable_symbols();
130 reduced
= (bool) (nuseless_nonterminals
+ nuseless_productions
> 0);
135 if (reduced
== FALSE
)
140 if (!BITISSET(N
, start_symbol
- ntokens
))
141 fatals(_("Start symbol %s does not derive any sentence"),
144 reduce_grammar_tables();
145 /* if (verboseflag) {
146 fprintf(foutput, "REDUCED GRAMMAR\n\n");
151 /**/ statisticsflag
= FALSE
; /* someday getopts should handle this */
152 if (statisticsflag
== TRUE
)
154 _("reduced %s defines %d terminal%s, %d nonterminal%s\
155 , and %d production%s.\n"), infile
,
156 ntokens
, (ntokens
== 1 ? "" : "s"),
157 nvars
, (nvars
== 1 ? "" : "s"),
158 nrules
, (nrules
== 1 ? "" : "s"));
162 /* Free the global sets used to compute the reduced grammar */
171 * Another way to do this would be with a set for each production and then do
172 * subset tests against N, but even for the C grammar the whole reducing
173 * process takes only 2 seconds on my 8Mhz AT.
177 useful_production (i
, N
)
185 * A production is useful if all of the nonterminals in its RHS
186 * appear in the set of useful nonterminals.
189 for (r
= &ritem
[rrhs
[i
]]; *r
> 0; r
++)
191 if (!BITISSET(N
, n
- ntokens
))
197 /* Remember that rules are 1-origin, symbols are 0-origin. */
200 useless_nonterminals ()
206 * N is set as built. Np is set being built this iteration. P is set
207 * of all productions which have a RHS all in N.
210 Np
= NEW2(WORDSIZE(nvars
), unsigned);
213 * The set being computed is a set of nonterminals which can derive
214 * the empty string or strings consisting of all terminals. At each
215 * iteration a nonterminal is added to the set if there is a
216 * production with that nonterminal as its LHS for which all the
217 * nonterminals in its RHS are already in the set. Iterate until the
218 * set being computed remains unchanged. Any nonterminals not in the
219 * set at that point are useless in that they will never be used in
220 * deriving a sentence of the language.
222 * This iteration doesn't use any special traversal over the
223 * productions. A set is kept of all productions for which all the
224 * nonterminals in the RHS are in useful. Only productions not in
225 * this set are scanned on each iteration. At the end, this set is
226 * saved to be used when finding useful productions: only productions
227 * in this set will appear in the final grammar.
233 for (i
= WORDSIZE(nvars
) - 1; i
>= 0; i
--)
235 for (i
= 1; i
<= nrules
; i
++)
239 if (useful_production(i
, N
))
241 SETBIT(Np
, rlhs
[i
] - ntokens
);
246 if (bits_equal(N
, Np
, WORDSIZE(nvars
)))
257 inaccessable_symbols ()
265 * Find out which productions are reachable and which symbols are
266 * used. Starting with an empty set of productions and a set of
267 * symbols which only has the start symbol in it, iterate over all
268 * productions until the set of productions remains unchanged for an
269 * iteration. For each production which has a LHS in the set of
270 * reachable symbols, add the production to the set of reachable
271 * productions, and add all of the nonterminals in the RHS of the
272 * production to the set of reachable symbols.
274 * Consider only the (partially) reduced grammar which has only
275 * nonterminals in N and productions in P.
277 * The result is the set P of productions in the reduced grammar, and
278 * the set V of symbols in the reduced grammar.
280 * Although this algorithm also computes the set of terminals which are
281 * reachable, no terminal will be deleted from the grammar. Some
282 * terminals might not be in the grammar but might be generated by
283 * semantic routines, and so the user might want them available with
284 * specified numbers. (Is this true?) However, the nonreachable
285 * terminals are printed (if running in verbose mode) so that the user
289 Vp
= NEW2(WORDSIZE(nsyms
), unsigned);
290 Pp
= NEW2(WORDSIZE(nrules
+ 1), unsigned);
292 /* If the start symbol isn't useful, then nothing will be useful. */
293 if (!BITISSET(N
, start_symbol
- ntokens
))
296 SETBIT(V
, start_symbol
);
301 for (i
= WORDSIZE(nsyms
) - 1; i
>= 0; i
--)
303 for (i
= 1; i
<= nrules
; i
++)
305 if (!BITISSET(Pp
, i
) && BITISSET(P
, i
) &&
306 BITISSET(V
, rlhs
[i
]))
308 for (r
= &ritem
[rrhs
[i
]]; *r
>= 0; r
++)
311 || BITISSET(N
, t
- ntokens
))
319 if (bits_equal(V
, Vp
, WORDSIZE(nsyms
)))
332 /* Tokens 0, 1, and 2 are internal to Bison. Consider them useful. */
333 SETBIT(V
, 0); /* end-of-input token */
334 SETBIT(V
, 1); /* error token */
335 SETBIT(V
, 2); /* some undefined token */
340 nuseful_productions
= bits_size(P
, WORDSIZE(nrules
+ 1));
341 nuseless_productions
= nrules
- nuseful_productions
;
343 nuseful_nonterminals
= 0;
344 for (i
= ntokens
; i
< nsyms
; i
++)
346 nuseful_nonterminals
++;
347 nuseless_nonterminals
= nvars
- nuseful_nonterminals
;
349 /* A token that was used in %prec should not be warned about. */
350 for (i
= 1; i
< nrules
; i
++)
351 if (rprecsym
[i
] != 0)
352 SETBIT(V1
, rprecsym
[i
]);
356 reduce_grammar_tables ()
358 /* This is turned off because we would need to change the numbers
359 in the case statements in the actions file. */
361 /* remove useless productions */
362 if (nuseless_productions
> 0)
364 short np
, pn
, ni
, pi
;
368 for (pn
= 1; pn
<= nrules
; pn
++)
376 rline
[np
] = rline
[pn
];
377 rprec
[np
] = rprec
[pn
];
378 rassoc
[np
] = rassoc
[pn
];
384 while (ritem
[pi
] >= 0)
385 ritem
[ni
++] = ritem
[pi
++];
389 while (ritem
[ni
++] >= 0);
394 nrules
-= nuseless_productions
;
398 * Is it worth it to reduce the amount of memory for the
399 * grammar? Probably not.
404 /* Disable useless productions,
405 since they may contain useless nonterms
406 that would get mapped below to -1 and confuse everyone. */
407 if (nuseless_productions
> 0)
411 for (pn
= 1; pn
<= nrules
; pn
++)
413 if (!BITISSET(P
, pn
))
420 /* remove useless symbols */
421 if (nuseless_nonterminals
> 0)
425 /* short j; JF unused */
430 * create a map of nonterminal number to new nonterminal
431 * number. -1 in the map means it was useless and is being
435 nontermmap
= NEW2(nvars
, short) - ntokens
;
436 for (i
= ntokens
; i
< nsyms
; i
++)
440 for (i
= ntokens
; i
< nsyms
; i
++)
444 /* Shuffle elements of tables indexed by symbol number. */
446 for (i
= ntokens
; i
< nsyms
; i
++)
451 sassoc
[n
] = sassoc
[i
];
459 /* Replace all symbol numbers in valid data structures. */
461 for (i
= 1; i
<= nrules
; i
++)
463 /* Ignore the rules disabled above. */
465 rlhs
[i
] = nontermmap
[rlhs
[i
]];
466 if (ISVAR (rprecsym
[i
]))
467 /* Can this happen? */
468 rprecsym
[i
] = nontermmap
[rprecsym
[i
]];
471 for (r
= ritem
; *r
; r
++)
475 start_symbol
= nontermmap
[start_symbol
];
477 nsyms
-= nuseless_nonterminals
;
478 nvars
-= nuseless_nonterminals
;
480 free(&nontermmap
[ntokens
]);
488 /* short j; JF unused */
492 if (nuseless_nonterminals
> 0)
494 fprintf(foutput
, _("Useless nonterminals:\n\n"));
495 for (i
= ntokens
; i
< nsyms
; i
++)
497 fprintf(foutput
, " %s\n", tags
[i
]);
500 for (i
= 0; i
< ntokens
; i
++)
502 if (!BITISSET(V
, i
) && !BITISSET(V1
, i
))
506 fprintf(foutput
, _("\n\nTerminals which are not used:\n\n"));
509 fprintf(foutput
, " %s\n", tags
[i
]);
513 if (nuseless_productions
> 0)
515 fprintf(foutput
, _("\n\nUseless rules:\n\n"));
516 for (i
= 1; i
<= nrules
; i
++)
520 fprintf(foutput
, "#%-4d ", i
);
521 fprintf(foutput
, "%s :\t", tags
[rlhs
[i
]]);
522 for (r
= &ritem
[rrhs
[i
]]; *r
>= 0; r
++)
524 fprintf(foutput
, " %s", tags
[*r
]);
526 fprintf(foutput
, ";\n");
530 if (nuseless_nonterminals
> 0 || nuseless_productions
> 0 || b
)
531 fprintf(foutput
, "\n\n");
541 "ntokens = %d, nvars = %d, nsyms = %d, nrules = %d, nitems = %d\n\n",
542 ntokens
, nvars
, nsyms
, nrules
, nitems
);
543 fprintf(foutput
, _("Variables\n---------\n\n"));
544 fprintf(foutput
, _("Value Sprec Sassoc Tag\n"));
545 for (i
= ntokens
; i
< nsyms
; i
++)
546 fprintf(foutput
, "%5d %5d %5d %s\n",
547 i
, sprec
[i
], sassoc
[i
], tags
[i
]);
548 fprintf(foutput
, "\n\n");
549 fprintf(foutput
, _("Rules\n-----\n\n"));
550 for (i
= 1; i
<= nrules
; i
++)
552 fprintf(foutput
, "%-5d(%5d%5d)%5d : (@%-5d)",
553 i
, rprec
[i
], rassoc
[i
], rlhs
[i
], rrhs
[i
]);
554 for (r
= &ritem
[rrhs
[i
]]; *r
> 0; r
++)
555 fprintf(foutput
, "%5d", *r
);
556 fprintf(foutput
, " [%d]\n", -(*r
));
558 fprintf(foutput
, "\n\n");
559 fprintf(foutput
, _("Rules interpreted\n-----------------\n\n"));
560 for (i
= 1; i
<= nrules
; i
++)
562 fprintf(foutput
, "%-5d %s :", i
, tags
[rlhs
[i
]]);
563 for (r
= &ritem
[rrhs
[i
]]; *r
> 0; r
++)
564 fprintf(foutput
, " %s", tags
[*r
]);
565 fprintf(foutput
, "\n");
567 fprintf(foutput
, "\n\n");
574 extern int fixed_outfiles
;
576 if (fixed_outfiles
&& nuseless_productions
)
577 fprintf(stderr
, _("%d rules never reduced\n"), nuseless_productions
);
579 fprintf(stderr
, _("%s contains "), infile
);
581 if (nuseless_nonterminals
> 0)
583 fprintf(stderr
, _("%d useless nonterminal%s"),
584 nuseless_nonterminals
,
585 (nuseless_nonterminals
== 1 ? "" : "s"));
587 if (nuseless_nonterminals
> 0 && nuseless_productions
> 0)
588 fprintf(stderr
, _(" and "));
590 if (nuseless_productions
> 0)
592 fprintf(stderr
, _("%d useless rule%s"),
593 nuseless_productions
,
594 (nuseless_productions
== 1 ? "" : "s"));
596 fprintf(stderr
, "\n");