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