]>
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, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
23 * Reduce the grammar: Find and eliminate unreachable terminals,
24 * nonterminals, and productions. David S. Bakin.
28 * Don't eliminate unreachable terminals: They may be used by the user's
41 extern char **tags
; /* reader.c */
42 extern int verboseflag
; /* getargs.c */
43 static int statisticsflag
; /* XXXXXXX */
44 extern int fixed_outfiles
;
51 typedef unsigned *BSet
;
56 * N is set of all nonterminals which are not useless. P is set of all rules
57 * which have no useless nonterminals in their RHS. V is the set of all
61 static BSet N
, P
, V
, V1
;
63 static int nuseful_productions
, nuseless_productions
,
64 nuseful_nonterminals
, nuseless_nonterminals
;
67 extern void reduce_grammar
PARAMS((void));
68 static bool bits_equal
PARAMS((BSet
, BSet
, int));
69 static int nbits
PARAMS((unsigned));
70 static int bits_size
PARAMS((BSet
, int));
71 static void useless_nonterminals
PARAMS((void));
72 static void inaccessable_symbols
PARAMS((void));
73 static void reduce_grammar_tables
PARAMS((void));
74 static void print_results
PARAMS((void));
75 static void print_notices
PARAMS((void));
77 #if 0 /* XXX currently unused. */
78 static void dump_grammar
PARAMS((void));
84 bits_equal (BSet L
, BSet R
, int n
)
88 for (i
= n
- 1; i
>= 0; i
--)
101 i
^= (i
& ((unsigned) (- (int) i
)));
109 bits_size (BSet S
, int n
)
113 for (i
= n
- 1; i
>= 0; i
--)
114 count
+= nbits(S
[i
]);
119 reduce_grammar (void)
123 /* Allocate the global sets used to compute the reduced grammar */
125 N
= NEW2(WORDSIZE(nvars
), unsigned);
126 P
= NEW2(WORDSIZE(nrules
+ 1), unsigned);
127 V
= NEW2(WORDSIZE(nsyms
), unsigned);
128 V1
= NEW2(WORDSIZE(nsyms
), unsigned);
130 useless_nonterminals();
131 inaccessable_symbols();
133 reduced
= (bool) (nuseless_nonterminals
+ nuseless_productions
> 0);
138 if (reduced
== FALSE
)
143 if (!BITISSET(N
, start_symbol
- ntokens
))
144 fatal (_("Start symbol %s does not derive any sentence"),
147 reduce_grammar_tables();
151 fprintf(foutput
, "REDUCED GRAMMAR\n\n");
155 /**/ statisticsflag
= FALSE
; /* someday getopts should handle this */
156 if (statisticsflag
== TRUE
)
158 _("reduced %s defines %d terminal%s, %d nonterminal%s\
159 , and %d production%s.\n"), infile
,
160 ntokens
, (ntokens
== 1 ? "" : "s"),
161 nvars
, (nvars
== 1 ? "" : "s"),
162 nrules
, (nrules
== 1 ? "" : "s"));
166 /* Free the global sets used to compute the reduced grammar */
175 * Another way to do this would be with a set for each production and then do
176 * subset tests against N0, but even for the C grammar the whole reducing
177 * process takes only 2 seconds on my 8Mhz AT.
181 useful_production (int i
, BSet N0
)
187 * A production is useful if all of the nonterminals in its RHS
188 * appear in the set of useful nonterminals.
191 for (r
= &ritem
[rrhs
[i
]]; *r
> 0; r
++)
193 if (!BITISSET(N0
, n
- ntokens
))
199 /* Remember that rules are 1-origin, symbols are 0-origin. */
202 useless_nonterminals (void)
208 * N is set as built. Np is set being built this iteration. P is set
209 * of all productions which have a RHS all in N.
212 Np
= NEW2(WORDSIZE(nvars
), unsigned);
215 * The set being computed is a set of nonterminals which can derive
216 * the empty string or strings consisting of all terminals. At each
217 * iteration a nonterminal is added to the set if there is a
218 * production with that nonterminal as its LHS for which all the
219 * nonterminals in its RHS are already in the set. Iterate until the
220 * set being computed remains unchanged. Any nonterminals not in the
221 * set at that point are useless in that they will never be used in
222 * deriving a sentence of the language.
224 * This iteration doesn't use any special traversal over the
225 * productions. A set is kept of all productions for which all the
226 * nonterminals in the RHS are in useful. Only productions not in
227 * this set are scanned on each iteration. At the end, this set is
228 * saved to be used when finding useful productions: only productions
229 * in this set will appear in the final grammar.
235 for (i
= WORDSIZE(nvars
) - 1; i
>= 0; i
--)
237 for (i
= 1; i
<= nrules
; i
++)
241 if (useful_production(i
, N
))
243 SETBIT(Np
, rlhs
[i
] - ntokens
);
248 if (bits_equal(N
, Np
, WORDSIZE(nvars
)))
259 inaccessable_symbols (void)
267 * Find out which productions are reachable and which symbols are
268 * used. Starting with an empty set of productions and a set of
269 * symbols which only has the start symbol in it, iterate over all
270 * productions until the set of productions remains unchanged for an
271 * iteration. For each production which has a LHS in the set of
272 * reachable symbols, add the production to the set of reachable
273 * productions, and add all of the nonterminals in the RHS of the
274 * production to the set of reachable symbols.
276 * Consider only the (partially) reduced grammar which has only
277 * nonterminals in N and productions in P.
279 * The result is the set P of productions in the reduced grammar, and
280 * the set V of symbols in the reduced grammar.
282 * Although this algorithm also computes the set of terminals which are
283 * reachable, no terminal will be deleted from the grammar. Some
284 * terminals might not be in the grammar but might be generated by
285 * semantic routines, and so the user might want them available with
286 * specified numbers. (Is this true?) However, the nonreachable
287 * terminals are printed (if running in verbose mode) so that the user
291 Vp
= NEW2(WORDSIZE(nsyms
), unsigned);
292 Pp
= NEW2(WORDSIZE(nrules
+ 1), unsigned);
294 /* If the start symbol isn't useful, then nothing will be useful. */
295 if (!BITISSET(N
, start_symbol
- ntokens
))
298 SETBIT(V
, start_symbol
);
303 for (i
= WORDSIZE(nsyms
) - 1; i
>= 0; i
--)
305 for (i
= 1; i
<= nrules
; i
++)
307 if (!BITISSET(Pp
, i
) && BITISSET(P
, i
) &&
308 BITISSET(V
, rlhs
[i
]))
310 for (r
= &ritem
[rrhs
[i
]]; *r
>= 0; r
++)
313 || BITISSET(N
, t
- ntokens
))
321 if (bits_equal(V
, Vp
, WORDSIZE(nsyms
)))
334 /* Tokens 0, 1, and 2 are internal to Bison. Consider them useful. */
335 SETBIT(V
, 0); /* end-of-input token */
336 SETBIT(V
, 1); /* error token */
337 SETBIT(V
, 2); /* some undefined token */
342 nuseful_productions
= bits_size(P
, WORDSIZE(nrules
+ 1));
343 nuseless_productions
= nrules
- nuseful_productions
;
345 nuseful_nonterminals
= 0;
346 for (i
= ntokens
; i
< nsyms
; i
++)
348 nuseful_nonterminals
++;
349 nuseless_nonterminals
= nvars
- nuseful_nonterminals
;
351 /* A token that was used in %prec should not be warned about. */
352 for (i
= 1; i
< nrules
; i
++)
353 if (rprecsym
[i
] != 0)
354 SETBIT(V1
, rprecsym
[i
]);
358 reduce_grammar_tables (void)
360 /* This is turned off because we would need to change the numbers
361 in the case statements in the actions file. */
363 /* remove useless productions */
364 if (nuseless_productions
> 0)
366 short np
, pn
, ni
, pi
;
370 for (pn
= 1; pn
<= nrules
; pn
++)
378 rline
[np
] = rline
[pn
];
379 rprec
[np
] = rprec
[pn
];
380 rassoc
[np
] = rassoc
[pn
];
386 while (ritem
[pi
] >= 0)
387 ritem
[ni
++] = ritem
[pi
++];
391 while (ritem
[ni
++] >= 0);
396 nrules
-= nuseless_productions
;
400 * Is it worth it to reduce the amount of memory for the
401 * grammar? Probably not.
406 /* Disable useless productions,
407 since they may contain useless nonterms
408 that would get mapped below to -1 and confuse everyone. */
409 if (nuseless_productions
> 0)
413 for (pn
= 1; pn
<= nrules
; pn
++)
415 if (!BITISSET(P
, pn
))
422 /* remove useless symbols */
423 if (nuseless_nonterminals
> 0)
427 /* short j; JF unused */
432 * create a map of nonterminal number to new nonterminal
433 * number. -1 in the map means it was useless and is being
437 nontermmap
= NEW2(nvars
, short) - ntokens
;
438 for (i
= ntokens
; i
< nsyms
; i
++)
442 for (i
= ntokens
; i
< nsyms
; i
++)
446 /* Shuffle elements of tables indexed by symbol number. */
448 for (i
= ntokens
; i
< nsyms
; i
++)
453 sassoc
[n
] = sassoc
[i
];
461 /* Replace all symbol numbers in valid data structures. */
463 for (i
= 1; i
<= nrules
; i
++)
465 /* Ignore the rules disabled above. */
467 rlhs
[i
] = nontermmap
[rlhs
[i
]];
468 if (ISVAR (rprecsym
[i
]))
469 /* Can this happen? */
470 rprecsym
[i
] = nontermmap
[rprecsym
[i
]];
473 for (r
= ritem
; *r
; r
++)
477 start_symbol
= nontermmap
[start_symbol
];
479 nsyms
-= nuseless_nonterminals
;
480 nvars
-= nuseless_nonterminals
;
482 free(&nontermmap
[ntokens
]);
490 /* short j; JF unused */
494 if (nuseless_nonterminals
> 0)
496 fprintf(foutput
, _("Useless nonterminals:\n\n"));
497 for (i
= ntokens
; i
< nsyms
; i
++)
499 fprintf(foutput
, " %s\n", tags
[i
]);
502 for (i
= 0; i
< ntokens
; i
++)
504 if (!BITISSET(V
, i
) && !BITISSET(V1
, i
))
508 fprintf(foutput
, _("\n\nTerminals which are not used:\n\n"));
511 fprintf(foutput
, " %s\n", tags
[i
]);
515 if (nuseless_productions
> 0)
517 fprintf(foutput
, _("\n\nUseless rules:\n\n"));
518 for (i
= 1; i
<= nrules
; i
++)
522 fprintf(foutput
, "#%-4d ", i
);
523 fprintf(foutput
, "%s :\t", tags
[rlhs
[i
]]);
524 for (r
= &ritem
[rrhs
[i
]]; *r
>= 0; r
++)
526 fprintf(foutput
, " %s", tags
[*r
]);
528 fprintf(foutput
, ";\n");
532 if (nuseless_nonterminals
> 0 || nuseless_productions
> 0 || b
)
533 fprintf(foutput
, "\n\n");
536 #if 0 /* XXX currently unused. */
544 "ntokens = %d, nvars = %d, nsyms = %d, nrules = %d, nitems = %d\n\n",
545 ntokens
, nvars
, nsyms
, nrules
, nitems
);
546 fprintf(foutput
, _("Variables\n---------\n\n"));
547 fprintf(foutput
, _("Value Sprec Sassoc Tag\n"));
548 for (i
= ntokens
; i
< nsyms
; i
++)
549 fprintf(foutput
, "%5d %5d %5d %s\n",
550 i
, sprec
[i
], sassoc
[i
], tags
[i
]);
551 fprintf(foutput
, "\n\n");
552 fprintf(foutput
, _("Rules\n-----\n\n"));
553 for (i
= 1; i
<= nrules
; i
++)
555 fprintf(foutput
, "%-5d(%5d%5d)%5d : (@%-5d)",
556 i
, rprec
[i
], rassoc
[i
], rlhs
[i
], rrhs
[i
]);
557 for (r
= &ritem
[rrhs
[i
]]; *r
> 0; r
++)
558 fprintf(foutput
, "%5d", *r
);
559 fprintf(foutput
, " [%d]\n", -(*r
));
561 fprintf(foutput
, "\n\n");
562 fprintf(foutput
, _("Rules interpreted\n-----------------\n\n"));
563 for (i
= 1; i
<= nrules
; i
++)
565 fprintf(foutput
, "%-5d %s :", i
, tags
[rlhs
[i
]]);
566 for (r
= &ritem
[rrhs
[i
]]; *r
> 0; r
++)
567 fprintf(foutput
, " %s", tags
[*r
]);
568 fprintf(foutput
, "\n");
570 fprintf(foutput
, "\n\n");
578 if (fixed_outfiles
&& nuseless_productions
)
579 fprintf(stderr
, _("%d rules never reduced\n"), nuseless_productions
);
581 fprintf(stderr
, _("%s contains "), infile
);
583 if (nuseless_nonterminals
> 0)
585 fprintf(stderr
, _("%d useless nonterminal%s"),
586 nuseless_nonterminals
,
587 (nuseless_nonterminals
== 1 ? "" : "s"));
589 if (nuseless_nonterminals
> 0 && nuseless_productions
> 0)
590 fprintf(stderr
, _(" and "));
592 if (nuseless_productions
> 0)
594 fprintf(stderr
, _("%d useless rule%s"),
595 nuseless_productions
,
596 (nuseless_productions
== 1 ? "" : "s"));
598 fprintf(stderr
, "\n");