]> git.saurik.com Git - bison.git/blob - src/reduce.c
b7a7f45685309da5de1b46135009a1e0557de5d9
[bison.git] / src / reduce.c
1 /* Grammar reduction for Bison.
2 Copyright 1988, 1989, 2000, 2001 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 "getargs.h"
30 #include "files.h"
31 #include "symtab.h"
32 #include "gram.h"
33 #include "complain.h"
34 #include "reduce.h"
35 #include "reader.h"
36 #include "getargs.h"
37 #include "bitset.h"
38
39 typedef short *rule;
40
41
42 /* Set of all nonterminals which are not useless. */
43 static bitset N;
44
45 /* Set of all rules which have no useless nonterminals in their RHS. */
46 static bitset P;
47
48 /* Set of all accessible symbols. */
49 static bitset V;
50
51 /* Set of symbols used to define rule precedence (so they are
52 `useless', but no warning should be issued). */
53 static bitset V1;
54
55 static int nuseful_productions;
56 static int nuseless_productions;
57 static int nuseful_nonterminals;
58 int nuseless_nonterminals;
59 \f
60 /*-------------------------------------------------------------------.
61 | Another way to do this would be with a set for each production and |
62 | then do subset tests against N0, but even for the C grammar the |
63 | whole reducing process takes only 2 seconds on my 8Mhz AT. |
64 `-------------------------------------------------------------------*/
65
66 static bool
67 useful_production (int i, bitset N0)
68 {
69 rule r;
70 short n;
71
72 /* A production is useful if all of the nonterminals in its appear
73 in the set of useful nonterminals. */
74
75 for (r = rules[i].rhs; *r >= 0; r++)
76 if (ISVAR (n = *r) && !bitset_test (N0, n - ntokens))
77 return FALSE;
78 return TRUE;
79 }
80
81
82 /*---------------------------------------------------------.
83 | Remember that rules are 1-origin, symbols are 0-origin. |
84 `---------------------------------------------------------*/
85
86 static void
87 useless_nonterminals (void)
88 {
89 bitset Np, Ns;
90 int i;
91
92 /* N is set as built. Np is set being built this iteration. P is
93 set of all productions which have a RHS all in N. */
94
95 Np = bitset_create (nvars, BITSET_FIXED);
96
97
98 /* The set being computed is a set of nonterminals which can derive
99 the empty string or strings consisting of all terminals. At each
100 iteration a nonterminal is added to the set if there is a
101 production with that nonterminal as its LHS for which all the
102 nonterminals in its RHS are already in the set. Iterate until
103 the set being computed remains unchanged. Any nonterminals not
104 in the set at that point are useless in that they will never be
105 used in deriving a sentence of the language.
106
107 This iteration doesn't use any special traversal over the
108 productions. A set is kept of all productions for which all the
109 nonterminals in the RHS are in useful. Only productions not in
110 this set are scanned on each iteration. At the end, this set is
111 saved to be used when finding useful productions: only
112 productions in this set will appear in the final grammar. */
113
114 while (1)
115 {
116 bitset_copy (Np, N);
117 for (i = 1; i < nrules + 1; i++)
118 if (!bitset_test (P, i)
119 && useful_production (i, N))
120 {
121 bitset_set (Np, rules[i].lhs - ntokens);
122 bitset_set (P, i);
123 }
124 if (bitset_equal_p (N, Np))
125 break;
126 Ns = Np;
127 Np = N;
128 N = Ns;
129 }
130 bitset_free (N);
131 N = Np;
132 }
133
134
135 static void
136 inaccessable_symbols (void)
137 {
138 bitset Vp, Vs, Pp;
139 int i;
140 short t;
141 rule r;
142
143 /* Find out which productions are reachable and which symbols are
144 used. Starting with an empty set of productions and a set of
145 symbols which only has the start symbol in it, iterate over all
146 productions until the set of productions remains unchanged for an
147 iteration. For each production which has a LHS in the set of
148 reachable symbols, add the production to the set of reachable
149 productions, and add all of the nonterminals in the RHS of the
150 production to the set of reachable symbols.
151
152 Consider only the (partially) reduced grammar which has only
153 nonterminals in N and productions in P.
154
155 The result is the set P of productions in the reduced grammar,
156 and the set V of symbols in the reduced grammar.
157
158 Although this algorithm also computes the set of terminals which
159 are reachable, no terminal will be deleted from the grammar. Some
160 terminals might not be in the grammar but might be generated by
161 semantic routines, and so the user might want them available with
162 specified numbers. (Is this true?) However, the nonreachable
163 terminals are printed (if running in verbose mode) so that the
164 user can know. */
165
166 Vp = bitset_create (nsyms, BITSET_FIXED);
167 Pp = bitset_create (nrules + 1, BITSET_FIXED);
168
169 /* If the start symbol isn't useful, then nothing will be useful. */
170 if (bitset_test (N, start_symbol - ntokens))
171 {
172 bitset_set (V, start_symbol);
173
174 while (1)
175 {
176 bitset_copy (Vp, V);
177 for (i = 1; i < nrules + 1; i++)
178 {
179 if (!bitset_test (Pp, i)
180 && bitset_test (P, i)
181 && bitset_test (V, rules[i].lhs))
182 {
183 for (r = rules[i].rhs; *r >= 0; r++)
184 if (ISTOKEN (t = *r) || bitset_test (N, t - ntokens))
185 bitset_set (Vp, t);
186 bitset_set (Pp, i);
187 }
188 }
189 if (bitset_equal_p (V, Vp))
190 break;
191 Vs = Vp;
192 Vp = V;
193 V = Vs;
194 }
195 }
196
197 bitset_free (V);
198 V = Vp;
199
200 /* Tokens 0, 1, and 2 are internal to Bison. Consider them useful. */
201 bitset_set (V, 0); /* end-of-input token */
202 bitset_set (V, 1); /* error token */
203 bitset_set (V, 2); /* some undefined token */
204
205 bitset_free (P);
206 P = Pp;
207
208 nuseful_productions = bitset_count (P);
209 nuseless_productions = nrules - nuseful_productions;
210
211 nuseful_nonterminals = 0;
212 for (i = ntokens; i < nsyms; i++)
213 if (bitset_test (V, i))
214 nuseful_nonterminals++;
215 nuseless_nonterminals = nvars - nuseful_nonterminals;
216
217 /* A token that was used in %prec should not be warned about. */
218 for (i = 1; i < nrules + 1; i++)
219 if (rules[i].precsym != 0)
220 bitset_set (V1, rules[i].precsym);
221 }
222
223
224 /*-------------------------------------------------------------------.
225 | Put the useless productions at the end of RULES, and adjust NRULES |
226 | accordingly. |
227 `-------------------------------------------------------------------*/
228
229 static void
230 reduce_grammar_tables (void)
231 {
232 /* Flag useless productions. */
233 {
234 int pn;
235 for (pn = 1; pn < nrules + 1; pn++)
236 rules[pn].useful = bitset_test (P, pn);
237 }
238
239 /* Map the nonterminals to their new index: useful first, useless
240 afterwards. Kept for later report. */
241 {
242 int useful = 1;
243 int useless = nrules + 1 - nuseless_productions;
244 rule_t *rules_sorted = XMALLOC (rule_t, nrules + 1) - 1;
245 int i;
246 for (i = 1; i < nrules + 1; ++i)
247 rules_sorted[rules[i].useful ? useful++ : useless++] = rules[i];
248 free (rules + 1);
249 rules = rules_sorted;
250
251 /* Also reorder ritems. */
252 {
253 short *ritems_sorted = XCALLOC (short, nitems + 1);
254 short *ritemsp = ritems_sorted;
255 for (i = 1; i < nrules + 1; ++i)
256 {
257 short *rhsp = rules[i].rhs;
258 rules[i].rhs = ritemsp;
259 for (/* Nothing. */; *rhsp >= 0; ++rhsp)
260 *ritemsp++ = *rhsp;
261 *ritemsp++ = -i;
262 }
263 *ritemsp++ = 0;
264 free (ritem);
265 ritem = ritems_sorted;
266 }
267 nrules -= nuseless_productions;
268 }
269
270 /* Adjust NRITEMS and NITEMS. */
271 {
272 int r;
273 int length;
274 for (r = nrules + 1; r < nrules + 1 + nuseless_productions; ++r)
275 {
276 length = rule_rhs_length (&rules[r]);
277 nritems -= length + 1;
278 nitems -= length + 1;
279 }
280 }
281 }
282
283
284 /*------------------------------.
285 | Remove useless nonterminals. |
286 `------------------------------*/
287
288 static void
289 nonterminals_reduce (void)
290 {
291 int i, n;
292
293 /* Map the nonterminals to their new index: useful first, useless
294 afterwards. Kept for later report. */
295
296 short *nontermmap = XCALLOC (short, nvars) - ntokens;
297 n = ntokens;
298 for (i = ntokens; i < nsyms; i++)
299 if (bitset_test (V, i))
300 nontermmap[i] = n++;
301 for (i = ntokens; i < nsyms; i++)
302 if (!bitset_test (V, i))
303 nontermmap[i] = n++;
304
305
306 /* Shuffle elements of tables indexed by symbol number. */
307 {
308 bucket **symbols_sorted = XMALLOC (bucket *, nvars) - ntokens;
309
310 for (i = ntokens; i < nsyms; i++)
311 symbols_sorted[nontermmap[i]] = symbols[i];
312 for (i = ntokens; i < nsyms; i++)
313 symbols[i] = symbols_sorted[i];
314 free (symbols_sorted + ntokens);
315 }
316
317 /* Replace all symbol numbers in valid data structures. */
318
319 for (i = 1; i < nrules + 1; i++)
320 {
321 rules[i].lhs = nontermmap[rules[i].lhs];
322 if (ISVAR (rules[i].precsym))
323 /* Can this happen? */
324 rules[i].precsym = nontermmap[rules[i].precsym];
325 }
326
327 for (i = 0; i < nritems; ++i)
328 if (ISVAR (ritem[i]))
329 ritem[i] = nontermmap[ritem[i]];
330
331 start_symbol = nontermmap[start_symbol];
332
333 nsyms -= nuseless_nonterminals;
334 nvars -= nuseless_nonterminals;
335
336 free (nontermmap + ntokens);
337 }
338
339
340 /*------------------------------------------------------------------.
341 | Output the detailed results of the reductions. For FILE.output. |
342 `------------------------------------------------------------------*/
343
344 void
345 reduce_output (FILE *out)
346 {
347 if (nuseless_nonterminals > 0)
348 {
349 int i;
350 fprintf (out, "%s\n\n", _("Useless nonterminals:"));
351 for (i = 0; i < nuseless_nonterminals; ++i)
352 fprintf (out, " %s\n", symbols[nsyms + i]->tag);
353 fputs ("\n\n", out);
354 }
355
356 {
357 bool b = FALSE;
358 int i;
359 for (i = 0; i < ntokens; i++)
360 if (!bitset_test (V, i) && !bitset_test (V1, i))
361 {
362 if (!b)
363 fprintf (out, "%s\n\n", _("Terminals which are not used:"));
364 b = TRUE;
365 fprintf (out, " %s\n", symbols[i]->tag);
366 }
367 if (b)
368 fputs ("\n\n", out);
369 }
370
371 if (nuseless_productions > 0)
372 {
373 int i;
374 fprintf (out, "%s\n\n", _("Useless rules:"));
375 for (i = nrules + 1; i < nuseless_productions + nrules + 1; i++)
376 {
377 rule r;
378 fprintf (out, "#%-4d ", rules[i].number - 1);
379 fprintf (out, "%s:", symbols[rules[i].lhs]->tag);
380 for (r = rules[i].rhs; *r >= 0; r++)
381 fprintf (out, " %s", symbols[*r]->tag);
382 fputs (";\n", out);
383 }
384 fputs ("\n\n", out);
385 }
386 }
387 \f
388 static void
389 dump_grammar (FILE *out)
390 {
391 int i;
392 rule r;
393
394 fprintf (out, "REDUCED GRAMMAR\n\n");
395 fprintf (out,
396 "ntokens = %d, nvars = %d, nsyms = %d, nrules = %d, nitems = %d\n\n",
397 ntokens, nvars, nsyms, nrules, nitems);
398 fprintf (out, "Variables\n---------\n\n");
399 fprintf (out, "Value Sprec Sassoc Tag\n");
400 for (i = ntokens; i < nsyms; i++)
401 fprintf (out, "%5d %5d %5d %s\n",
402 i,
403 symbols[i]->prec, symbols[i]->assoc, symbols[i]->tag);
404 fprintf (out, "\n\n");
405 fprintf (out, "Rules\n-----\n\n");
406 fprintf (out, "Num (Prec, Assoc, Useful, Ritem Range) Lhs -> Rhs (Ritem range) [Num]\n");
407 for (i = 1; i < nrules + nuseless_productions + 1; i++)
408 {
409 int rhs_count = 0;
410 /* Find the last RHS index in ritems. */
411 for (r = rules[i].rhs; *r >= 0; ++r)
412 ++rhs_count;
413 fprintf (out, "%3d (%2d, %2d, %2d, %2d-%2d) %2d ->",
414 i - 1,
415 rules[i].prec, rules[i].assoc, rules[i].useful,
416 rules[i].rhs - ritem, rules[i].rhs - ritem + rhs_count - 1,
417 rules[i].lhs);
418 /* Dumped the RHS. */
419 for (r = rules[i].rhs; *r >= 0; r++)
420 fprintf (out, "%3d", *r);
421 fprintf (out, " [%d]\n", -(*r) - 1);
422 }
423 fprintf (out, "\n\n");
424 fprintf (out, "Rules interpreted\n-----------------\n\n");
425 for (i = 1; i < nrules + nuseless_productions + 1; i++)
426 {
427 fprintf (out, "%-5d %s :", i, symbols[rules[i].lhs]->tag);
428 for (r = rules[i].rhs; *r >= 0; r++)
429 fprintf (out, " %s", symbols[*r]->tag);
430 fputc ('\n', out);
431 }
432 fprintf (out, "\n\n");
433 }
434
435
436
437 /*-------------------------------.
438 | Report the results to STDERR. |
439 `-------------------------------*/
440
441 static void
442 reduce_print (void)
443 {
444 if (yacc_flag && nuseless_productions)
445 fprintf (stderr, ngettext ("%d rule never reduced\n",
446 "%d rules never reduced\n",
447 nuseless_productions),
448 nuseless_productions);
449
450 fprintf (stderr, _("%s contains "), infile);
451
452 if (nuseless_nonterminals > 0)
453 fprintf (stderr, ngettext ("%d useless nonterminal",
454 "%d useless nonterminals",
455 nuseless_nonterminals),
456 nuseless_nonterminals);
457
458 if (nuseless_nonterminals > 0 && nuseless_productions > 0)
459 fprintf (stderr, _(" and "));
460
461 if (nuseless_productions > 0)
462 fprintf (stderr, ngettext ("%d useless rule",
463 "%d useless rules",
464 nuseless_productions),
465 nuseless_productions);
466 fprintf (stderr, "\n");
467 fflush (stderr);
468 }
469 \f
470 void
471 reduce_grammar (void)
472 {
473 bool reduced;
474
475 /* Allocate the global sets used to compute the reduced grammar */
476
477 N = bitset_create (nvars, BITSET_FIXED);
478 P = bitset_create (nrules + 1, BITSET_FIXED);
479 V = bitset_create (nsyms, BITSET_FIXED);
480 V1 = bitset_create (nsyms, BITSET_FIXED);
481
482 useless_nonterminals ();
483 inaccessable_symbols ();
484
485 reduced = (bool) (nuseless_nonterminals + nuseless_productions > 0);
486 if (!reduced)
487 return;
488
489 reduce_print ();
490
491 if (!bitset_test (N, start_symbol - ntokens))
492 fatal (_("Start symbol %s does not derive any sentence"),
493 symbols[start_symbol]->tag);
494
495 if (nuseless_productions > 0)
496 reduce_grammar_tables ();
497 if (nuseless_nonterminals > 0)
498 nonterminals_reduce ();
499
500 if (trace_flag)
501 {
502 dump_grammar (stderr);
503
504 fprintf (stderr, "reduced %s defines %d terminals, %d nonterminals\
505 , and %d productions.\n",
506 infile, ntokens, nvars, nrules);
507 }
508 }
509
510
511 /*-----------------------------------------------------------.
512 | Free the global sets used to compute the reduced grammar. |
513 `-----------------------------------------------------------*/
514
515 void
516 reduce_free (void)
517 {
518 bitset_free (N);
519 bitset_free (V);
520 bitset_free (V1);
521 bitset_free (P);
522 }