]>
git.saurik.com Git - bison.git/blob - src/reduce.c
1 /* Grammar reduction for Bison.
2 Copyright (C) 1988, 1989, 2000 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. */
22 /* Reduce the grammar: Find and eliminate unreachable terminals,
23 nonterminals, and productions. David S. Bakin. */
25 /* Don't eliminate unreachable terminals: They may be used by the
38 typedef unsigned *BSet
;
42 /* N is set of all nonterminals which are not useless. P is set of
43 all rules which have no useless nonterminals in their RHS. V is
44 the set of all accessible symbols. */
46 static BSet N
, P
, V
, V1
;
48 static int nuseful_productions
;
49 static int nuseless_productions
;
50 static int nuseful_nonterminals
;
51 static int nuseless_nonterminals
;
54 bits_equal (BSet L
, BSet R
, int n
)
58 for (i
= n
- 1; i
>= 0; i
--)
72 i
^= (i
& ((unsigned) (-(int) i
)));
80 bits_size (BSet S
, int n
)
84 for (i
= n
- 1; i
>= 0; i
--)
85 count
+= nbits (S
[i
]);
89 /*-------------------------------------------------------------------.
90 | Another way to do this would be with a set for each production and |
91 | then do subset tests against N0, but even for the C grammar the |
92 | whole reducing process takes only 2 seconds on my 8Mhz AT. |
93 `-------------------------------------------------------------------*/
96 useful_production (int i
, BSet N0
)
101 /* A production is useful if all of the nonterminals in its appear
102 in the set of useful nonterminals. */
104 for (r
= &ritem
[rrhs
[i
]]; *r
> 0; r
++)
106 if (!BITISSET (N0
, n
- ntokens
))
112 /*---------------------------------------------------------.
113 | Remember that rules are 1-origin, symbols are 0-origin. |
114 `---------------------------------------------------------*/
117 useless_nonterminals (void)
122 /* N is set as built. Np is set being built this iteration. P is
123 set of all productions which have a RHS all in N. */
125 Np
= NEW2 (WORDSIZE (nvars
), unsigned);
127 /* The set being computed is a set of nonterminals which can derive
128 the empty string or strings consisting of all terminals. At each
129 iteration a nonterminal is added to the set if there is a
130 production with that nonterminal as its LHS for which all the
131 nonterminals in its RHS are already in the set. Iterate until
132 the set being computed remains unchanged. Any nonterminals not
133 in the set at that point are useless in that they will never be
134 used in deriving a sentence of the language.
136 This iteration doesn't use any special traversal over the
137 productions. A set is kept of all productions for which all the
138 nonterminals in the RHS are in useful. Only productions not in
139 this set are scanned on each iteration. At the end, this set is
140 saved to be used when finding useful productions: only
141 productions in this set will appear in the final grammar. */
146 for (i
= WORDSIZE (nvars
) - 1; i
>= 0; i
--)
148 for (i
= 1; i
<= nrules
; i
++)
150 if (!BITISSET (P
, i
))
152 if (useful_production (i
, N
))
154 SETBIT (Np
, rlhs
[i
] - ntokens
);
159 if (bits_equal (N
, Np
, WORDSIZE (nvars
)))
171 inaccessable_symbols (void)
178 /* Find out which productions are reachable and which symbols are
179 used. Starting with an empty set of productions and a set of
180 symbols which only has the start symbol in it, iterate over all
181 productions until the set of productions remains unchanged for an
182 iteration. For each production which has a LHS in the set of
183 reachable symbols, add the production to the set of reachable
184 productions, and add all of the nonterminals in the RHS of the
185 production to the set of reachable symbols.
187 Consider only the (partially) reduced grammar which has only
188 nonterminals in N and productions in P.
190 The result is the set P of productions in the reduced grammar,
191 and the set V of symbols in the reduced grammar.
193 Although this algorithm also computes the set of terminals which
194 are reachable, no terminal will be deleted from the grammar. Some
195 terminals might not be in the grammar but might be generated by
196 semantic routines, and so the user might want them available with
197 specified numbers. (Is this true?) However, the nonreachable
198 terminals are printed (if running in verbose mode) so that the
201 Vp
= NEW2 (WORDSIZE (nsyms
), unsigned);
202 Pp
= NEW2 (WORDSIZE (nrules
+ 1), unsigned);
204 /* If the start symbol isn't useful, then nothing will be useful. */
205 if (!BITISSET (N
, start_symbol
- ntokens
))
208 SETBIT (V
, start_symbol
);
213 for (i
= WORDSIZE (nsyms
) - 1; i
>= 0; i
--)
215 for (i
= 1; i
<= nrules
; i
++)
217 if (!BITISSET (Pp
, i
) && BITISSET (P
, i
) && BITISSET (V
, rlhs
[i
]))
219 for (r
= &ritem
[rrhs
[i
]]; *r
>= 0; r
++)
221 if (ISTOKEN (t
= *r
) || BITISSET (N
, t
- ntokens
))
229 if (bits_equal (V
, Vp
, WORDSIZE (nsyms
)))
242 /* Tokens 0, 1, and 2 are internal to Bison. Consider them useful. */
243 SETBIT (V
, 0); /* end-of-input token */
244 SETBIT (V
, 1); /* error token */
245 SETBIT (V
, 2); /* some undefined token */
250 nuseful_productions
= bits_size (P
, WORDSIZE (nrules
+ 1));
251 nuseless_productions
= nrules
- nuseful_productions
;
253 nuseful_nonterminals
= 0;
254 for (i
= ntokens
; i
< nsyms
; i
++)
256 nuseful_nonterminals
++;
257 nuseless_nonterminals
= nvars
- nuseful_nonterminals
;
259 /* A token that was used in %prec should not be warned about. */
260 for (i
= 1; i
< nrules
; i
++)
261 if (rprecsym
[i
] != 0)
262 SETBIT (V1
, rprecsym
[i
]);
266 reduce_grammar_tables (void)
268 /* This is turned off because we would need to change the numbers
269 in the case statements in the actions file. */
271 /* remove useless productions */
272 if (nuseless_productions
> 0)
274 short np
, pn
, ni
, pi
;
278 for (pn
= 1; pn
<= nrules
; pn
++)
280 if (BITISSET (P
, pn
))
286 rline
[np
] = rline
[pn
];
287 rprec
[np
] = rprec
[pn
];
288 rassoc
[np
] = rassoc
[pn
];
294 while (ritem
[pi
] >= 0)
295 ritem
[ni
++] = ritem
[pi
++];
301 while (ritem
[ni
++] >= 0);
306 nrules
-= nuseless_productions
;
309 /* Is it worth it to reduce the amount of memory for the
310 grammar? Probably not. */
314 /* Disable useless productions,
315 since they may contain useless nonterms
316 that would get mapped below to -1 and confuse everyone. */
317 if (nuseless_productions
> 0)
321 for (pn
= 1; pn
<= nrules
; pn
++)
323 if (!BITISSET (P
, pn
))
330 /* remove useless symbols */
331 if (nuseless_nonterminals
> 0)
335 /* short j; JF unused */
339 /* Create a map of nonterminal number to new nonterminal
340 number. -1 in the map means it was useless and is being
343 nontermmap
= NEW2 (nvars
, short) - ntokens
;
344 for (i
= ntokens
; i
< nsyms
; i
++)
348 for (i
= ntokens
; i
< nsyms
; i
++)
352 /* Shuffle elements of tables indexed by symbol number. */
354 for (i
= ntokens
; i
< nsyms
; i
++)
359 sassoc
[n
] = sassoc
[i
];
369 /* Replace all symbol numbers in valid data structures. */
371 for (i
= 1; i
<= nrules
; i
++)
373 /* Ignore the rules disabled above. */
375 rlhs
[i
] = nontermmap
[rlhs
[i
]];
376 if (ISVAR (rprecsym
[i
]))
377 /* Can this happen? */
378 rprecsym
[i
] = nontermmap
[rprecsym
[i
]];
381 for (r
= ritem
; *r
; r
++)
385 start_symbol
= nontermmap
[start_symbol
];
387 nsyms
-= nuseless_nonterminals
;
388 nvars
-= nuseless_nonterminals
;
390 free (&nontermmap
[ntokens
]);
398 /* short j; JF unused */
402 if (nuseless_nonterminals
> 0)
404 fprintf (foutput
, _("Useless nonterminals:\n\n"));
405 for (i
= ntokens
; i
< nsyms
; i
++)
406 if (!BITISSET (V
, i
))
407 fprintf (foutput
, " %s\n", tags
[i
]);
410 for (i
= 0; i
< ntokens
; i
++)
412 if (!BITISSET (V
, i
) && !BITISSET (V1
, i
))
416 fprintf (foutput
, _("\n\nTerminals which are not used:\n\n"));
419 fprintf (foutput
, " %s\n", tags
[i
]);
423 if (nuseless_productions
> 0)
425 fprintf (foutput
, _("\n\nUseless rules:\n\n"));
426 for (i
= 1; i
<= nrules
; i
++)
428 if (!BITISSET (P
, i
))
430 fprintf (foutput
, "#%-4d ", i
);
431 fprintf (foutput
, "%s :\t", tags
[rlhs
[i
]]);
432 for (r
= &ritem
[rrhs
[i
]]; *r
>= 0; r
++)
434 fprintf (foutput
, " %s", tags
[*r
]);
436 fprintf (foutput
, ";\n");
440 if (nuseless_nonterminals
> 0 || nuseless_productions
> 0 || b
)
441 fprintf (foutput
, "\n\n");
444 #if 0 /* XXX currently unused. */
452 "ntokens = %d, nvars = %d, nsyms = %d, nrules = %d, nitems = %d\n\n",
453 ntokens
, nvars
, nsyms
, nrules
, nitems
);
454 fprintf (foutput
, _("Variables\n---------\n\n"));
455 fprintf (foutput
, _("Value Sprec Sassoc Tag\n"));
456 for (i
= ntokens
; i
< nsyms
; i
++)
457 fprintf (foutput
, "%5d %5d %5d %s\n", i
, sprec
[i
], sassoc
[i
], tags
[i
]);
458 fprintf (foutput
, "\n\n");
459 fprintf (foutput
, _("Rules\n-----\n\n"));
460 for (i
= 1; i
<= nrules
; i
++)
462 fprintf (foutput
, "%-5d(%5d%5d)%5d : (@%-5d)",
463 i
, rprec
[i
], rassoc
[i
], rlhs
[i
], rrhs
[i
]);
464 for (r
= &ritem
[rrhs
[i
]]; *r
> 0; r
++)
465 fprintf (foutput
, "%5d", *r
);
466 fprintf (foutput
, " [%d]\n", -(*r
));
468 fprintf (foutput
, "\n\n");
469 fprintf (foutput
, _("Rules interpreted\n-----------------\n\n"));
470 for (i
= 1; i
<= nrules
; i
++)
472 fprintf (foutput
, "%-5d %s :", i
, tags
[rlhs
[i
]]);
473 for (r
= &ritem
[rrhs
[i
]]; *r
> 0; r
++)
474 fprintf (foutput
, " %s", tags
[*r
]);
475 fprintf (foutput
, "\n");
477 fprintf (foutput
, "\n\n");
486 if (fixed_outfiles
&& nuseless_productions
)
487 fprintf (stderr
, _("%d rules never reduced\n"), nuseless_productions
);
489 fprintf (stderr
, _("%s contains "), infile
);
491 if (nuseless_nonterminals
> 0)
493 fprintf (stderr
, _("%d useless nonterminal%s"),
494 nuseless_nonterminals
,
495 (nuseless_nonterminals
== 1 ? "" : "s"));
497 if (nuseless_nonterminals
> 0 && nuseless_productions
> 0)
498 fprintf (stderr
, _(" and "));
500 if (nuseless_productions
> 0)
502 fprintf (stderr
, _("%d useless rule%s"),
503 nuseless_productions
, (nuseless_productions
== 1 ? "" : "s"));
505 fprintf (stderr
, "\n");
510 reduce_grammar (void)
514 /* Allocate the global sets used to compute the reduced grammar */
516 N
= NEW2 (WORDSIZE (nvars
), unsigned);
517 P
= NEW2 (WORDSIZE (nrules
+ 1), unsigned);
518 V
= NEW2 (WORDSIZE (nsyms
), unsigned);
519 V1
= NEW2 (WORDSIZE (nsyms
), unsigned);
521 useless_nonterminals ();
522 inaccessable_symbols ();
524 reduced
= (bool) (nuseless_nonterminals
+ nuseless_productions
> 0);
529 if (reduced
== FALSE
)
534 if (!BITISSET (N
, start_symbol
- ntokens
))
535 fatal (_("Start symbol %s does not derive any sentence"),
538 reduce_grammar_tables ();
542 fprintf (foutput
, "REDUCED GRAMMAR\n\n");
548 fprintf (stderr
, _("reduced %s defines %d terminal%s, %d nonterminal%s\
549 , and %d production%s.\n"),
552 (ntokens
== 1 ? "" : "s"),
554 (nvars
== 1 ? "" : "s"),
556 (nrules
== 1 ? "" : "s"));
559 /* Free the global sets used to compute the reduced grammar */