]> git.saurik.com Git - bison.git/blob - src/reduce.c
* src/LR0.c (state_list_t, state_list_append): New.
[bison.git] / src / reduce.c
1 /* Grammar reduction for Bison.
2 Copyright (C) 1988, 1989, 2000, 2001, 2002 Free Software Foundation, Inc.
3
4 This file is part of Bison, the GNU Compiler Compiler.
5
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)
9 any later version.
10
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.
15
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. */
20
21
22 /* Reduce the grammar: Find and eliminate unreachable terminals,
23 nonterminals, and productions. David S. Bakin. */
24
25 /* Don't eliminate unreachable terminals: They may be used by the
26 user's parser. */
27
28 #include "system.h"
29 #include "quotearg.h"
30 #include "getargs.h"
31 #include "files.h"
32 #include "symtab.h"
33 #include "gram.h"
34 #include "complain.h"
35 #include "reduce.h"
36 #include "reader.h"
37 #include "getargs.h"
38 #include "bitset.h"
39
40 /* Set of all nonterminals which are not useless. */
41 static bitset N;
42
43 /* Set of all rules which have no useless nonterminals in their RHS. */
44 static bitset P;
45
46 /* Set of all accessible symbols. */
47 static bitset V;
48
49 /* Set of symbols used to define rule precedence (so they are
50 `useless', but no warning should be issued). */
51 static bitset V1;
52
53 static int nuseful_productions;
54 int nuseless_productions;
55 static int nuseful_nonterminals;
56 int nuseless_nonterminals;
57 \f
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 `-------------------------------------------------------------------*/
63
64 static bool
65 useful_production (int i, bitset N0)
66 {
67 item_number_t *r;
68 short n;
69
70 /* A production is useful if all of the nonterminals in its appear
71 in the set of useful nonterminals. */
72
73 for (r = rules[i].rhs; *r >= 0; r++)
74 if (ISVAR (n = *r) && !bitset_test (N0, n - ntokens))
75 return FALSE;
76 return TRUE;
77 }
78
79
80 /*---------------------------------------------------------.
81 | Remember that rules are 1-origin, symbols are 0-origin. |
82 `---------------------------------------------------------*/
83
84 static void
85 useless_nonterminals (void)
86 {
87 bitset Np, Ns;
88 int i;
89
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. */
92
93 Np = bitset_create (nvars, BITSET_FIXED);
94
95
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.
104
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. */
111
112 while (1)
113 {
114 bitset_copy (Np, N);
115 for (i = 1; i < nrules + 1; i++)
116 if (!bitset_test (P, i)
117 && useful_production (i, N))
118 {
119 bitset_set (Np, rules[i].lhs->number - ntokens);
120 bitset_set (P, i);
121 }
122 if (bitset_equal_p (N, Np))
123 break;
124 Ns = Np;
125 Np = N;
126 N = Ns;
127 }
128 bitset_free (N);
129 N = Np;
130 }
131
132
133 static void
134 inaccessable_symbols (void)
135 {
136 bitset Vp, Vs, Pp;
137 int i;
138 short t;
139 item_number_t *r;
140
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.
149
150 Consider only the (partially) reduced grammar which has only
151 nonterminals in N and productions in P.
152
153 The result is the set P of productions in the reduced grammar,
154 and the set V of symbols in the reduced grammar.
155
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
162 user can know. */
163
164 Vp = bitset_create (nsyms, BITSET_FIXED);
165 Pp = bitset_create (nrules + 1, BITSET_FIXED);
166
167 /* If the start symbol isn't useful, then nothing will be useful. */
168 if (bitset_test (N, axiom->number - ntokens))
169 {
170 bitset_set (V, axiom->number);
171
172 while (1)
173 {
174 bitset_copy (Vp, V);
175 for (i = 1; i < nrules + 1; i++)
176 {
177 if (!bitset_test (Pp, i)
178 && bitset_test (P, i)
179 && bitset_test (V, rules[i].lhs->number))
180 {
181 for (r = rules[i].rhs; *r >= 0; r++)
182 if (ISTOKEN (t = *r) || bitset_test (N, t - ntokens))
183 bitset_set (Vp, t);
184 bitset_set (Pp, i);
185 }
186 }
187 if (bitset_equal_p (V, Vp))
188 break;
189 Vs = Vp;
190 Vp = V;
191 V = Vs;
192 }
193 }
194
195 bitset_free (V);
196 V = Vp;
197
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 */
202
203 bitset_free (P);
204 P = Pp;
205
206 nuseful_productions = bitset_count (P);
207 nuseless_productions = nrules - nuseful_productions;
208
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;
214
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);
219 }
220
221
222 /*-------------------------------------------------------------------.
223 | Put the useless productions at the end of RULES, and adjust NRULES |
224 | accordingly. |
225 `-------------------------------------------------------------------*/
226
227 static void
228 reduce_grammar_tables (void)
229 {
230 /* Report and flag useless productions. */
231 {
232 int r;
233 for (r = 1; r < nrules + 1; r++)
234 {
235 rules[r].useful = bitset_test (P, r);
236 if (!rules[r].useful)
237 {
238 LOCATION_PRINT (stderr, rules[r].location);
239 fprintf (stderr, ": %s: %s: ", _("warning"), _("useless rule"));
240 rule_print (&rules[r], stderr);
241 }
242 }
243 }
244
245 /* Map the nonterminals to their new index: useful first, useless
246 afterwards. Kept for later report. */
247 {
248 int useful = 1;
249 int useless = nrules + 1 - nuseless_productions;
250 rule_t *rules_sorted = XMALLOC (rule_t, nrules + 1) - 1;
251 int i;
252 for (i = 1; i < nrules + 1; ++i)
253 rules_sorted[rules[i].useful ? useful++ : useless++] = rules[i];
254 free (rules + 1);
255 rules = rules_sorted;
256
257 /* Renumber the rules markers in RITEMS. */
258 for (i = 1; i < nrules + 1; ++i)
259 {
260 item_number_t *rhsp = rules[i].rhs;
261 for (/* Nothing. */; *rhsp >= 0; ++rhsp)
262 /* Nothing. */;
263 *rhsp = -i;
264 rules[i].number = i;
265 }
266 nrules -= nuseless_productions;
267 }
268
269 /* Adjust NRITEMS. */
270 {
271 int r;
272 int length;
273 for (r = nrules + 1; r < nrules + 1 + nuseless_productions; ++r)
274 {
275 length = rule_rhs_length (&rules[r]);
276 nritems -= length + 1;
277 }
278 }
279 }
280
281
282 /*------------------------------.
283 | Remove useless nonterminals. |
284 `------------------------------*/
285
286 static void
287 nonterminals_reduce (void)
288 {
289 symbol_number_t i, n;
290
291 /* Map the nonterminals to their new index: useful first, useless
292 afterwards. Kept for later report. */
293
294 symbol_number_t *nontermmap = XCALLOC (symbol_number_t, nvars) - ntokens;
295 n = ntokens;
296 for (i = ntokens; i < nsyms; i++)
297 if (bitset_test (V, i))
298 nontermmap[i] = n++;
299 for (i = ntokens; i < nsyms; i++)
300 if (!bitset_test (V, i))
301 {
302 nontermmap[i] = n++;
303 LOCATION_PRINT (stderr, symbols[i]->location);
304 fprintf (stderr, ": %s: %s: %s\n",
305 _("warning"), _("useless nonterminal"),
306 symbol_tag_get (symbols[i]));
307 }
308
309
310 /* Shuffle elements of tables indexed by symbol number. */
311 {
312 symbol_t **symbols_sorted = XMALLOC (symbol_t *, nvars) - ntokens;
313
314 for (i = ntokens; i < nsyms; i++)
315 symbols[i]->number = nontermmap[i];
316 for (i = ntokens; i < nsyms; i++)
317 symbols_sorted[nontermmap[i]] = symbols[i];
318 for (i = ntokens; i < nsyms; i++)
319 symbols[i] = symbols_sorted[i];
320 free (symbols_sorted + ntokens);
321 }
322
323 {
324 int r;
325 for (r = 1; r < nrules + 1; ++r)
326 {
327 item_number_t *rhsp;
328 for (rhsp = rules[r].rhs; *rhsp >= 0; ++rhsp)
329 if (ISVAR (*rhsp))
330 *rhsp = symbol_number_as_item_number (nontermmap[*rhsp]);
331 }
332 axiom->number = nontermmap[axiom->number];
333 }
334
335 nsyms -= nuseless_nonterminals;
336 nvars -= nuseless_nonterminals;
337
338 free (nontermmap + ntokens);
339 }
340
341
342 /*------------------------------------------------------------------.
343 | Output the detailed results of the reductions. For FILE.output. |
344 `------------------------------------------------------------------*/
345
346 void
347 reduce_output (FILE *out)
348 {
349 if (nuseless_nonterminals > 0)
350 {
351 int i;
352 fprintf (out, "%s\n\n", _("Useless nonterminals:"));
353 for (i = 0; i < nuseless_nonterminals; ++i)
354 fprintf (out, " %s\n", symbol_tag_get (symbols[nsyms + i]));
355 fputs ("\n\n", out);
356 }
357
358 {
359 bool b = FALSE;
360 int i;
361 for (i = 0; i < ntokens; i++)
362 if (!bitset_test (V, i) && !bitset_test (V1, i))
363 {
364 if (!b)
365 fprintf (out, "%s\n\n", _("Terminals which are not used:"));
366 b = TRUE;
367 fprintf (out, " %s\n", symbol_tag_get (symbols[i]));
368 }
369 if (b)
370 fputs ("\n\n", out);
371 }
372
373 if (nuseless_productions > 0)
374 grammar_rules_partial_print (out, _("Useless rules"),
375 nrules + 1,
376 nuseless_productions + nrules + 1);
377 }
378 \f
379
380
381
382
383 /*-------------------------------.
384 | Report the results to STDERR. |
385 `-------------------------------*/
386
387 static void
388 reduce_print (void)
389 {
390 if (yacc_flag && nuseless_productions)
391 fprintf (stderr, ngettext ("%d rule never reduced\n",
392 "%d rules never reduced\n",
393 nuseless_productions),
394 nuseless_productions);
395
396 fprintf (stderr, "%s: %s: ", infile, _("warning"));
397
398 if (nuseless_nonterminals > 0)
399 fprintf (stderr, ngettext ("%d useless nonterminal",
400 "%d useless nonterminals",
401 nuseless_nonterminals),
402 nuseless_nonterminals);
403
404 if (nuseless_nonterminals > 0 && nuseless_productions > 0)
405 fprintf (stderr, _(" and "));
406
407 if (nuseless_productions > 0)
408 fprintf (stderr, ngettext ("%d useless rule",
409 "%d useless rules",
410 nuseless_productions),
411 nuseless_productions);
412 fprintf (stderr, "\n");
413 fflush (stderr);
414 }
415 \f
416 void
417 reduce_grammar (void)
418 {
419 bool reduced;
420
421 /* Allocate the global sets used to compute the reduced grammar */
422
423 N = bitset_create (nvars, BITSET_FIXED);
424 P = bitset_create (nrules + 1, BITSET_FIXED);
425 V = bitset_create (nsyms, BITSET_FIXED);
426 V1 = bitset_create (nsyms, BITSET_FIXED);
427
428 useless_nonterminals ();
429 inaccessable_symbols ();
430
431 reduced = (bool) (nuseless_nonterminals + nuseless_productions > 0);
432 if (!reduced)
433 return;
434
435 reduce_print ();
436
437 if (!bitset_test (N, axiom->number - ntokens))
438 fatal (_("Start symbol %s does not derive any sentence"),
439 symbol_tag_get (symbols[axiom->number]));
440
441 /* First reduce the nonterminals, as they renumber themselves in the
442 whole grammar. If you change the order, nonterms would be
443 renumbered only in the reduced grammar. */
444 if (nuseless_nonterminals > 0)
445 nonterminals_reduce ();
446 if (nuseless_productions > 0)
447 reduce_grammar_tables ();
448
449 if (trace_flag)
450 {
451 grammar_dump (stderr, "Reduced Grammar");
452
453 fprintf (stderr, "reduced %s defines %d terminals, %d nonterminals\
454 , and %d productions.\n",
455 infile, ntokens, nvars, nrules);
456 }
457 }
458
459
460 /*-----------------------------------------------------------.
461 | Free the global sets used to compute the reduced grammar. |
462 `-----------------------------------------------------------*/
463
464 void
465 reduce_free (void)
466 {
467 bitset_free (N);
468 bitset_free (V);
469 bitset_free (V1);
470 bitset_free (P);
471 }