]>
git.saurik.com Git - bison.git/blob - src/reduce.c
1 /* Grammar reduction for Bison.
2 Copyright 1988, 1989, 2000, 2001 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 /* Set of all nonterminals which are not useless. */
45 /* Set of all rules which have no useless nonterminals in their RHS. */
48 /* Set of all accessible symbols. */
51 /* Set of symbols used to define rule precedence (so they are
52 `useless', but no warning should be issued). */
55 static int nuseful_productions
;
56 static int nuseless_productions
;
57 static int nuseful_nonterminals
;
58 int nuseless_nonterminals
;
61 bits_equal (BSet L
, BSet R
, int n
)
65 for (i
= n
- 1; i
>= 0; i
--)
79 i
^= (i
& ((unsigned) (-(int) i
)));
87 bits_size (BSet S
, int n
)
91 for (i
= n
- 1; i
>= 0; i
--)
92 count
+= nbits (S
[i
]);
96 /*-------------------------------------------------------------------.
97 | Another way to do this would be with a set for each production and |
98 | then do subset tests against N0, but even for the C grammar the |
99 | whole reducing process takes only 2 seconds on my 8Mhz AT. |
100 `-------------------------------------------------------------------*/
103 useful_production (int i
, BSet N0
)
108 /* A production is useful if all of the nonterminals in its appear
109 in the set of useful nonterminals. */
111 for (r
= &ritem
[rules
[i
].rhs
]; *r
>= 0; r
++)
113 if (!BITISSET (N0
, n
- ntokens
))
119 /*---------------------------------------------------------.
120 | Remember that rules are 1-origin, symbols are 0-origin. |
121 `---------------------------------------------------------*/
124 useless_nonterminals (void)
129 /* N is set as built. Np is set being built this iteration. P is
130 set of all productions which have a RHS all in N. */
132 Np
= XCALLOC (unsigned, WORDSIZE (nvars
));
134 /* The set being computed is a set of nonterminals which can derive
135 the empty string or strings consisting of all terminals. At each
136 iteration a nonterminal is added to the set if there is a
137 production with that nonterminal as its LHS for which all the
138 nonterminals in its RHS are already in the set. Iterate until
139 the set being computed remains unchanged. Any nonterminals not
140 in the set at that point are useless in that they will never be
141 used in deriving a sentence of the language.
143 This iteration doesn't use any special traversal over the
144 productions. A set is kept of all productions for which all the
145 nonterminals in the RHS are in useful. Only productions not in
146 this set are scanned on each iteration. At the end, this set is
147 saved to be used when finding useful productions: only
148 productions in this set will appear in the final grammar. */
152 for (i
= WORDSIZE (nvars
) - 1; i
>= 0; i
--)
154 for (i
= 1; i
<= nrules
; i
++)
156 if (!BITISSET (P
, i
))
158 if (useful_production (i
, N
))
160 SETBIT (Np
, rules
[i
].lhs
- ntokens
);
165 if (bits_equal (N
, Np
, WORDSIZE (nvars
)))
177 inaccessable_symbols (void)
184 /* Find out which productions are reachable and which symbols are
185 used. Starting with an empty set of productions and a set of
186 symbols which only has the start symbol in it, iterate over all
187 productions until the set of productions remains unchanged for an
188 iteration. For each production which has a LHS in the set of
189 reachable symbols, add the production to the set of reachable
190 productions, and add all of the nonterminals in the RHS of the
191 production to the set of reachable symbols.
193 Consider only the (partially) reduced grammar which has only
194 nonterminals in N and productions in P.
196 The result is the set P of productions in the reduced grammar,
197 and the set V of symbols in the reduced grammar.
199 Although this algorithm also computes the set of terminals which
200 are reachable, no terminal will be deleted from the grammar. Some
201 terminals might not be in the grammar but might be generated by
202 semantic routines, and so the user might want them available with
203 specified numbers. (Is this true?) However, the nonreachable
204 terminals are printed (if running in verbose mode) so that the
207 Vp
= XCALLOC (unsigned, WORDSIZE (nsyms
));
208 Pp
= XCALLOC (unsigned, WORDSIZE (nrules
+ 1));
210 /* If the start symbol isn't useful, then nothing will be useful. */
211 if (BITISSET (N
, start_symbol
- ntokens
))
213 SETBIT (V
, start_symbol
);
217 for (i
= WORDSIZE (nsyms
) - 1; i
>= 0; i
--)
219 for (i
= 1; i
<= nrules
; i
++)
221 if (!BITISSET (Pp
, i
)
223 && BITISSET (V
, rules
[i
].lhs
))
225 for (r
= &ritem
[rules
[i
].rhs
]; *r
>= 0; r
++)
226 if (ISTOKEN (t
= *r
) || BITISSET (N
, t
- ntokens
))
231 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 (rules
[i
].precsym
!= 0)
262 SETBIT (V1
, rules
[i
].precsym
);
266 reduce_grammar_tables (void)
268 /* This is turned off because we would need to change the numbers in
269 the case statements in the actions file.
271 We don't disable it via CPP so that it is still checked with the
272 rest of the code, to avoid its becoming completely obsolete.
274 FIXME: I think the comment above demonstrates this code must be
275 turned off for *semantic* parser, not in the general case. Try
276 to understand this better --akim. */
279 /* remove useless productions */
280 if (nuseless_productions
> 0)
282 short np
, pn
, ni
, pi
;
286 for (pn
= 1; pn
<= nrules
; pn
++)
287 if (BITISSET (P
, pn
))
292 rules
[np
].lhs
= rules
[pn
].lhs
;
293 rules
[np
].line
= rules
[pn
].line
;
294 rules
[np
].prec
= rules
[pn
].prec
;
295 rules
[np
].assoc
= rules
[pn
].assoc
;
296 rules
[np
].rhs
= rules
[pn
].rhs
;
297 if (rules
[np
].rhs
!= ni
)
301 while (ritem
[pi
] >= 0)
302 ritem
[ni
++] = ritem
[pi
++];
308 while (ritem
[ni
++] >= 0)
314 nrules
-= nuseless_productions
;
318 /* Is it worth it to reduce the amount of memory for the
319 grammar? Probably not. */
322 /* Disable useless productions. */
323 if (nuseless_productions
> 0)
326 for (pn
= 1; pn
<= nrules
; pn
++)
327 rules
[pn
].useful
= BITISSET (P
, pn
);
332 /*------------------------------.
333 | Remove useless nonterminals. |
334 `------------------------------*/
337 nonterminals_reduce (void)
341 /* Map the nonterminals to their new index: useful first, useless
342 afterwards. Kept for later report. */
344 short *nontermmap
= XCALLOC (short, nvars
) - ntokens
;
346 for (i
= ntokens
; i
< nsyms
; i
++)
349 for (i
= ntokens
; i
< nsyms
; i
++)
350 if (!BITISSET (V
, i
))
354 /* Shuffle elements of tables indexed by symbol number. */
356 bucket
**symbols_sorted
= XMALLOC (bucket
*, nvars
) - ntokens
;
358 for (i
= ntokens
; i
< nsyms
; i
++)
359 symbols_sorted
[nontermmap
[i
]] = symbols
[i
];
360 for (i
= ntokens
; i
< nsyms
; i
++)
361 symbols
[i
] = symbols_sorted
[i
];
362 free (symbols_sorted
+ ntokens
);
365 /* Replace all symbol numbers in valid data structures. */
367 for (i
= 1; i
<= nrules
; i
++)
369 rules
[i
].lhs
= nontermmap
[rules
[i
].lhs
];
370 if (ISVAR (rules
[i
].precsym
))
371 /* Can this happen? */
372 rules
[i
].precsym
= nontermmap
[rules
[i
].precsym
];
375 for (i
= 0; i
< nritems
; ++i
)
376 if (ISVAR (ritem
[i
]))
377 ritem
[i
] = nontermmap
[ritem
[i
]];
379 start_symbol
= nontermmap
[start_symbol
];
381 nsyms
-= nuseless_nonterminals
;
382 nvars
-= nuseless_nonterminals
;
384 free (nontermmap
+ ntokens
);
388 /*------------------------------------------------------------------.
389 | Output the detailed results of the reductions. For FILE.output. |
390 `------------------------------------------------------------------*/
393 reduce_output (FILE *out
)
395 if (nuseless_nonterminals
> 0)
398 fprintf (out
, "%s\n\n", _("Useless nonterminals:"));
399 for (i
= 0; i
< nuseless_nonterminals
; ++i
)
400 fprintf (out
, " %s\n", symbols
[nsyms
+ i
]->tag
);
407 for (i
= 0; i
< ntokens
; i
++)
408 if (!BITISSET (V
, i
) && !BITISSET (V1
, i
))
411 fprintf (out
, "%s\n\n", _("Terminals which are not used:"));
413 fprintf (out
, " %s\n", symbols
[i
]->tag
);
419 if (nuseless_productions
> 0)
422 fprintf (out
, "%s\n\n", _("Useless rules:"));
423 for (i
= 1; i
<= nrules
; i
++)
424 if (!rules
[i
].useful
)
427 fprintf (out
, "#%-4d ", i
- 1);
428 fprintf (out
, "%s:", symbols
[rules
[i
].lhs
]->tag
);
429 for (r
= &ritem
[rules
[i
].rhs
]; *r
>= 0; r
++)
430 fprintf (out
, " %s", symbols
[*r
]->tag
);
438 dump_grammar (FILE *out
)
443 fprintf (out
, "REDUCED GRAMMAR\n\n");
445 "ntokens = %d, nvars = %d, nsyms = %d, nrules = %d, nitems = %d\n\n",
446 ntokens
, nvars
, nsyms
, nrules
, nitems
);
447 fprintf (out
, "Variables\n---------\n\n");
448 fprintf (out
, "Value Sprec Sassoc Tag\n");
449 for (i
= ntokens
; i
< nsyms
; i
++)
450 fprintf (out
, "%5d %5d %5d %s\n",
452 symbols
[i
]->prec
, symbols
[i
]->assoc
, symbols
[i
]->tag
);
453 fprintf (out
, "\n\n");
454 fprintf (out
, "Rules\n-----\n\n");
455 fprintf (out
, "Num (Prec, Assoc, Useful, Ritem Range) Lhs -> Rhs (Ritem range) [Num]\n");
456 for (i
= 1; i
<= nrules
; i
++)
459 /* Find the last RHS index in ritems. */
460 for (r
= &ritem
[rules
[i
].rhs
]; *r
>= 0; ++r
)
462 fprintf (out
, "%3d (%2d, %2d, %2d, %2d-%2d) %2d ->",
464 rules
[i
].prec
, rules
[i
].assoc
, rules
[i
].useful
,
465 rules
[i
].rhs
, rules
[i
].rhs
+ rhs_count
- 1,
467 /* Dumped the RHS. */
468 for (r
= &ritem
[rules
[i
].rhs
]; *r
>= 0; r
++)
469 fprintf (out
, "%3d", *r
);
470 fprintf (out
, " [%d]\n", -(*r
) - 1);
472 fprintf (out
, "\n\n");
473 fprintf (out
, "Rules interpreted\n-----------------\n\n");
474 for (i
= 1; i
<= nrules
; i
++)
476 fprintf (out
, "%-5d %s :", i
, symbols
[rules
[i
].lhs
]->tag
);
477 for (r
= &ritem
[rules
[i
].rhs
]; *r
>= 0; r
++)
478 fprintf (out
, " %s", symbols
[*r
]->tag
);
481 fprintf (out
, "\n\n");
486 /*-------------------------------.
487 | Report the results to STDERR. |
488 `-------------------------------*/
493 if (yacc_flag
&& nuseless_productions
)
494 fprintf (stderr
, ngettext ("%d rule never reduced\n",
495 "%d rules never reduced\n",
496 nuseless_productions
),
497 nuseless_productions
);
499 fprintf (stderr
, _("%s contains "), infile
);
501 if (nuseless_nonterminals
> 0)
502 fprintf (stderr
, ngettext ("%d useless nonterminal",
503 "%d useless nonterminals",
504 nuseless_nonterminals
),
505 nuseless_nonterminals
);
507 if (nuseless_nonterminals
> 0 && nuseless_productions
> 0)
508 fprintf (stderr
, _(" and "));
510 if (nuseless_productions
> 0)
511 fprintf (stderr
, ngettext ("%d useless rule",
513 nuseless_productions
),
514 nuseless_productions
);
515 fprintf (stderr
, "\n");
520 reduce_grammar (void)
524 /* Allocate the global sets used to compute the reduced grammar */
526 N
= XCALLOC (unsigned, WORDSIZE (nvars
));
527 P
= XCALLOC (unsigned, WORDSIZE (nrules
+ 1));
528 V
= XCALLOC (unsigned, WORDSIZE (nsyms
));
529 V1
= XCALLOC (unsigned, WORDSIZE (nsyms
));
531 useless_nonterminals ();
532 inaccessable_symbols ();
534 reduced
= (bool) (nuseless_nonterminals
+ nuseless_productions
> 0);
541 if (!BITISSET (N
, start_symbol
- ntokens
))
542 fatal (_("Start symbol %s does not derive any sentence"),
543 symbols
[start_symbol
]->tag
);
545 reduce_grammar_tables ();
546 if (nuseless_nonterminals
> 0)
547 nonterminals_reduce ();
551 dump_grammar (stderr
);
553 fprintf (stderr
, "reduced %s defines %d terminals, %d nonterminals\
554 , and %d productions.\n",
555 infile
, ntokens
, nvars
, nrules
);
560 /*-----------------------------------------------------------.
561 | Free the global sets used to compute the reduced grammar. |
562 `-----------------------------------------------------------*/