]>
git.saurik.com Git - bison.git/blob - src/reduce.c
1 /* Grammar reduction for Bison.
2 Copyright (C) 1988, 1989, 2000, 2001, 2002 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
40 /* Set of all nonterminals which are not useless. */
43 /* Set of all rules which have no useless nonterminals in their RHS. */
46 /* Set of all accessible symbols. */
49 /* Set of symbols used to define rule precedence (so they are
50 `useless', but no warning should be issued). */
53 static int nuseful_productions
;
54 static int nuseless_productions
;
55 static int nuseful_nonterminals
;
56 int nuseless_nonterminals
;
58 /*-------------------------------------------------------------------.
59 | Another way to do this would be with a set for each production and |
60 | then do subset tests against N0, but even for the C grammar the |
61 | whole reducing process takes only 2 seconds on my 8Mhz AT. |
62 `-------------------------------------------------------------------*/
65 useful_production (int i
, bitset N0
)
70 /* A production is useful if all of the nonterminals in its appear
71 in the set of useful nonterminals. */
73 for (r
= rules
[i
].rhs
; *r
>= 0; r
++)
74 if (ISVAR (n
= *r
) && !bitset_test (N0
, n
- ntokens
))
80 /*---------------------------------------------------------.
81 | Remember that rules are 1-origin, symbols are 0-origin. |
82 `---------------------------------------------------------*/
85 useless_nonterminals (void)
90 /* N is set as built. Np is set being built this iteration. P is
91 set of all productions which have a RHS all in N. */
93 Np
= bitset_create (nvars
, BITSET_FIXED
);
96 /* The set being computed is a set of nonterminals which can derive
97 the empty string or strings consisting of all terminals. At each
98 iteration a nonterminal is added to the set if there is a
99 production with that nonterminal as its LHS for which all the
100 nonterminals in its RHS are already in the set. Iterate until
101 the set being computed remains unchanged. Any nonterminals not
102 in the set at that point are useless in that they will never be
103 used in deriving a sentence of the language.
105 This iteration doesn't use any special traversal over the
106 productions. A set is kept of all productions for which all the
107 nonterminals in the RHS are in useful. Only productions not in
108 this set are scanned on each iteration. At the end, this set is
109 saved to be used when finding useful productions: only
110 productions in this set will appear in the final grammar. */
115 for (i
= 1; i
< nrules
+ 1; i
++)
116 if (!bitset_test (P
, i
)
117 && useful_production (i
, N
))
119 bitset_set (Np
, rules
[i
].lhs
->number
- ntokens
);
122 if (bitset_equal_p (N
, Np
))
134 inaccessable_symbols (void)
141 /* Find out which productions are reachable and which symbols are
142 used. Starting with an empty set of productions and a set of
143 symbols which only has the start symbol in it, iterate over all
144 productions until the set of productions remains unchanged for an
145 iteration. For each production which has a LHS in the set of
146 reachable symbols, add the production to the set of reachable
147 productions, and add all of the nonterminals in the RHS of the
148 production to the set of reachable symbols.
150 Consider only the (partially) reduced grammar which has only
151 nonterminals in N and productions in P.
153 The result is the set P of productions in the reduced grammar,
154 and the set V of symbols in the reduced grammar.
156 Although this algorithm also computes the set of terminals which
157 are reachable, no terminal will be deleted from the grammar. Some
158 terminals might not be in the grammar but might be generated by
159 semantic routines, and so the user might want them available with
160 specified numbers. (Is this true?) However, the nonreachable
161 terminals are printed (if running in verbose mode) so that the
164 Vp
= bitset_create (nsyms
, BITSET_FIXED
);
165 Pp
= bitset_create (nrules
+ 1, BITSET_FIXED
);
167 /* If the start symbol isn't useful, then nothing will be useful. */
168 if (bitset_test (N
, start_symbol
- ntokens
))
170 bitset_set (V
, start_symbol
);
175 for (i
= 1; i
< nrules
+ 1; i
++)
177 if (!bitset_test (Pp
, i
)
178 && bitset_test (P
, i
)
179 && bitset_test (V
, rules
[i
].lhs
->number
))
181 for (r
= rules
[i
].rhs
; *r
>= 0; r
++)
182 if (ISTOKEN (t
= *r
) || bitset_test (N
, t
- ntokens
))
187 if (bitset_equal_p (V
, Vp
))
198 /* Tokens 0, 1, and 2 are internal to Bison. Consider them useful. */
199 bitset_set (V
, eoftoken
->number
); /* end-of-input token */
200 bitset_set (V
, errtoken
->number
); /* error token */
201 bitset_set (V
, undeftoken
->number
); /* some undefined token */
206 nuseful_productions
= bitset_count (P
);
207 nuseless_productions
= nrules
- nuseful_productions
;
209 nuseful_nonterminals
= 0;
210 for (i
= ntokens
; i
< nsyms
; i
++)
211 if (bitset_test (V
, i
))
212 nuseful_nonterminals
++;
213 nuseless_nonterminals
= nvars
- nuseful_nonterminals
;
215 /* A token that was used in %prec should not be warned about. */
216 for (i
= 1; i
< nrules
+ 1; i
++)
217 if (rules
[i
].precsym
!= 0)
218 bitset_set (V1
, rules
[i
].precsym
->number
);
222 /*-------------------------------------------------------------------.
223 | Put the useless productions at the end of RULES, and adjust NRULES |
225 `-------------------------------------------------------------------*/
228 reduce_grammar_tables (void)
230 /* Flag useless productions. */
233 for (pn
= 1; pn
< nrules
+ 1; pn
++)
234 rules
[pn
].useful
= bitset_test (P
, pn
);
237 /* Map the nonterminals to their new index: useful first, useless
238 afterwards. Kept for later report. */
241 int useless
= nrules
+ 1 - nuseless_productions
;
242 rule_t
*rules_sorted
= XMALLOC (rule_t
, nrules
+ 1) - 1;
244 for (i
= 1; i
< nrules
+ 1; ++i
)
245 rules_sorted
[rules
[i
].useful
? useful
++ : useless
++] = rules
[i
];
247 rules
= rules_sorted
;
249 /* Renumber the rules markers in RITEMS. */
250 for (i
= 1; i
< nrules
+ 1; ++i
)
252 item_number_t
*rhsp
= rules
[i
].rhs
;
253 for (/* Nothing. */; *rhsp
>= 0; ++rhsp
)
258 nrules
-= nuseless_productions
;
261 /* Adjust NRITEMS and NITEMS. */
265 for (r
= nrules
+ 1; r
< nrules
+ 1 + nuseless_productions
; ++r
)
267 length
= rule_rhs_length (&rules
[r
]);
268 nritems
-= length
+ 1;
274 /*------------------------------.
275 | Remove useless nonterminals. |
276 `------------------------------*/
279 nonterminals_reduce (void)
283 /* Map the nonterminals to their new index: useful first, useless
284 afterwards. Kept for later report. */
286 short *nontermmap
= XCALLOC (short, nvars
) - ntokens
;
288 for (i
= ntokens
; i
< nsyms
; i
++)
289 if (bitset_test (V
, i
))
291 for (i
= ntokens
; i
< nsyms
; i
++)
292 if (!bitset_test (V
, i
))
296 /* Shuffle elements of tables indexed by symbol number. */
298 symbol_t
**symbols_sorted
= XMALLOC (symbol_t
*, nvars
) - ntokens
;
300 for (i
= ntokens
; i
< nsyms
; i
++)
301 symbols
[i
]->number
= nontermmap
[i
];
302 for (i
= ntokens
; i
< nsyms
; i
++)
303 symbols_sorted
[nontermmap
[i
]] = symbols
[i
];
304 for (i
= ntokens
; i
< nsyms
; i
++)
305 symbols
[i
] = symbols_sorted
[i
];
306 free (symbols_sorted
+ ntokens
);
309 for (i
= 0; i
< nritems
; ++i
)
310 if (ISVAR (ritem
[i
]))
311 ritem
[i
] = nontermmap
[ritem
[i
]];
313 start_symbol
= nontermmap
[start_symbol
];
315 nsyms
-= nuseless_nonterminals
;
316 nvars
-= nuseless_nonterminals
;
318 free (nontermmap
+ ntokens
);
322 /*------------------------------------------------------------------.
323 | Output the detailed results of the reductions. For FILE.output. |
324 `------------------------------------------------------------------*/
327 reduce_output (FILE *out
)
329 if (nuseless_nonterminals
> 0)
332 fprintf (out
, "%s\n\n", _("Useless nonterminals:"));
333 for (i
= 0; i
< nuseless_nonterminals
; ++i
)
334 fprintf (out
, " %s\n", quotearg_style (escape_quoting_style
,
335 symbols
[nsyms
+ i
]->tag
));
342 for (i
= 0; i
< ntokens
; i
++)
343 if (!bitset_test (V
, i
) && !bitset_test (V1
, i
))
346 fprintf (out
, "%s\n\n", _("Terminals which are not used:"));
348 fprintf (out
, " %s\n", quotearg_style (escape_quoting_style
,
355 if (nuseless_productions
> 0)
358 fprintf (out
, "%s\n\n", _("Useless rules:"));
359 for (i
= nrules
+ 1; i
< nuseless_productions
+ nrules
+ 1; i
++)
362 fprintf (out
, "#%-4d ", rules
[i
].user_number
- 1);
363 fprintf (out
, "%s:", quotearg_style (escape_quoting_style
,
365 for (r
= rules
[i
].rhs
; *r
>= 0; r
++)
366 fprintf (out
, " %s", quotearg_style (escape_quoting_style
,
375 dump_grammar (FILE *out
)
380 fprintf (out
, "REDUCED GRAMMAR\n\n");
382 "ntokens = %d, nvars = %d, nsyms = %d, nrules = %d, nritems = %d\n\n",
383 ntokens
, nvars
, nsyms
, nrules
, nritems
);
384 fprintf (out
, "Variables\n---------\n\n");
385 fprintf (out
, "Value Sprec Sassoc Tag\n");
386 for (i
= ntokens
; i
< nsyms
; i
++)
387 fprintf (out
, "%5d %5d %5d %s\n",
389 symbols
[i
]->prec
, symbols
[i
]->assoc
,
390 quotearg_style (escape_quoting_style
, symbols
[i
]->tag
));
391 fprintf (out
, "\n\n");
392 fprintf (out
, "Rules\n-----\n\n");
393 fprintf (out
, "Num (Prec, Assoc, Useful, Ritem Range) Lhs -> Rhs (Ritem range) [Num]\n");
394 for (i
= 1; i
< nrules
+ nuseless_productions
+ 1; i
++)
397 /* Find the last RHS index in ritems. */
398 for (r
= rules
[i
].rhs
; *r
>= 0; ++r
)
400 fprintf (out
, "%3d (%2d, %2d, %2d, %2d-%2d) %2d ->",
403 rules
[i
].prec
->assoc
,
405 rules
[i
].rhs
- ritem
,
406 rules
[i
].rhs
- ritem
+ rhs_count
- 1,
407 rules
[i
].lhs
->number
);
408 /* Dumped the RHS. */
409 for (r
= rules
[i
].rhs
; *r
>= 0; r
++)
410 fprintf (out
, "%3d", *r
);
411 fprintf (out
, " [%d]\n", -(*r
) - 1);
413 fprintf (out
, "\n\n");
414 fprintf (out
, "Rules interpreted\n-----------------\n\n");
415 for (i
= 1; i
< nrules
+ nuseless_productions
+ 1; i
++)
417 fprintf (out
, "%-5d %s :",
418 i
, quotearg_style (escape_quoting_style
, rules
[i
].lhs
->tag
));
419 for (r
= rules
[i
].rhs
; *r
>= 0; r
++)
421 quotearg_style (escape_quoting_style
, symbols
[*r
]->tag
));
424 fprintf (out
, "\n\n");
429 /*-------------------------------.
430 | Report the results to STDERR. |
431 `-------------------------------*/
436 if (yacc_flag
&& nuseless_productions
)
437 fprintf (stderr
, ngettext ("%d rule never reduced\n",
438 "%d rules never reduced\n",
439 nuseless_productions
),
440 nuseless_productions
);
442 fprintf (stderr
, _("%s contains "), infile
);
444 if (nuseless_nonterminals
> 0)
445 fprintf (stderr
, ngettext ("%d useless nonterminal",
446 "%d useless nonterminals",
447 nuseless_nonterminals
),
448 nuseless_nonterminals
);
450 if (nuseless_nonterminals
> 0 && nuseless_productions
> 0)
451 fprintf (stderr
, _(" and "));
453 if (nuseless_productions
> 0)
454 fprintf (stderr
, ngettext ("%d useless rule",
456 nuseless_productions
),
457 nuseless_productions
);
458 fprintf (stderr
, "\n");
463 reduce_grammar (void)
467 /* Allocate the global sets used to compute the reduced grammar */
469 N
= bitset_create (nvars
, BITSET_FIXED
);
470 P
= bitset_create (nrules
+ 1, BITSET_FIXED
);
471 V
= bitset_create (nsyms
, BITSET_FIXED
);
472 V1
= bitset_create (nsyms
, BITSET_FIXED
);
474 useless_nonterminals ();
475 inaccessable_symbols ();
477 reduced
= (bool) (nuseless_nonterminals
+ nuseless_productions
> 0);
483 if (!bitset_test (N
, start_symbol
- ntokens
))
484 fatal (_("Start symbol %s does not derive any sentence"),
485 quotearg_style (escape_quoting_style
, symbols
[start_symbol
]->tag
));
487 /* First reduce the nonterminals, as they renumber themselves in the
488 whole grammar. If you change the order, nonterms would be
489 renumbered only in the reduced grammar. */
490 if (nuseless_nonterminals
> 0)
491 nonterminals_reduce ();
492 if (nuseless_productions
> 0)
493 reduce_grammar_tables ();
497 dump_grammar (stderr
);
499 fprintf (stderr
, "reduced %s defines %d terminals, %d nonterminals\
500 , and %d productions.\n",
501 infile
, ntokens
, nvars
, nrules
);
506 /*-----------------------------------------------------------.
507 | Free the global sets used to compute the reduced grammar. |
508 `-----------------------------------------------------------*/