]>
git.saurik.com Git - bison.git/blob - src/reduce.c
1 /* Grammar reduction for Bison.
2 Copyright (C) 1988, 1989 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, 675 Mass Ave, Cambridge, MA 02139, USA. */
22 * Reduce the grammar: Find and eliminate unreachable terminals,
23 * nonterminals, and productions. David S. Bakin.
27 * Don't eliminate unreachable terminals: They may be used by the user's
39 extern char **tags
; /* reader.c */
40 extern int verboseflag
; /* getargs.c */
41 static int statisticsflag
; /* XXXXXXX */
42 extern int fixed_outfiles
;
49 typedef unsigned *BSet
;
54 * N is set of all nonterminals which are not useless. P is set of all rules
55 * which have no useless nonterminals in their RHS. V is the set of all
59 static BSet N
, P
, V
, V1
;
61 static int nuseful_productions
, nuseless_productions
,
62 nuseful_nonterminals
, nuseless_nonterminals
;
65 bool bits_equal
PARAMS((BSet
, BSet
, int));
66 int nbits
PARAMS((unsigned));
67 int bits_size
PARAMS((BSet
, int));
68 void reduce_grammar
PARAMS((void));
69 static void useless_nonterminals
PARAMS((void));
70 static void inaccessable_symbols
PARAMS((void));
71 static void reduce_grammar_tables
PARAMS((void));
72 static void print_results
PARAMS((void));
73 static void print_notices
PARAMS((void));
74 void dump_grammar
PARAMS((void));
76 extern void fatals
PARAMS((char *, char *));
80 bits_equal (BSet L
, BSet R
, int n
)
84 for (i
= n
- 1; i
>= 0; i
--)
97 i
^= (i
& ((unsigned) (- (int) i
)));
105 bits_size (BSet S
, int n
)
109 for (i
= n
- 1; i
>= 0; i
--)
110 count
+= nbits(S
[i
]);
115 reduce_grammar (void)
119 /* Allocate the global sets used to compute the reduced grammar */
121 N
= NEW2(WORDSIZE(nvars
), unsigned);
122 P
= NEW2(WORDSIZE(nrules
+ 1), unsigned);
123 V
= NEW2(WORDSIZE(nsyms
), unsigned);
124 V1
= NEW2(WORDSIZE(nsyms
), unsigned);
126 useless_nonterminals();
127 inaccessable_symbols();
129 reduced
= (bool) (nuseless_nonterminals
+ nuseless_productions
> 0);
134 if (reduced
== FALSE
)
139 if (!BITISSET(N
, start_symbol
- ntokens
))
140 fatals(_("Start symbol %s does not derive any sentence"),
143 reduce_grammar_tables();
144 /* if (verboseflag) {
145 fprintf(foutput, "REDUCED GRAMMAR\n\n");
150 /**/ statisticsflag
= FALSE
; /* someday getopts should handle this */
151 if (statisticsflag
== TRUE
)
153 _("reduced %s defines %d terminal%s, %d nonterminal%s\
154 , and %d production%s.\n"), infile
,
155 ntokens
, (ntokens
== 1 ? "" : "s"),
156 nvars
, (nvars
== 1 ? "" : "s"),
157 nrules
, (nrules
== 1 ? "" : "s"));
161 /* Free the global sets used to compute the reduced grammar */
170 * Another way to do this would be with a set for each production and then do
171 * subset tests against N0, but even for the C grammar the whole reducing
172 * process takes only 2 seconds on my 8Mhz AT.
176 useful_production (int i
, BSet N0
)
182 * A production is useful if all of the nonterminals in its RHS
183 * appear in the set of useful nonterminals.
186 for (r
= &ritem
[rrhs
[i
]]; *r
> 0; r
++)
188 if (!BITISSET(N0
, n
- ntokens
))
194 /* Remember that rules are 1-origin, symbols are 0-origin. */
197 useless_nonterminals (void)
203 * N is set as built. Np is set being built this iteration. P is set
204 * of all productions which have a RHS all in N.
207 Np
= NEW2(WORDSIZE(nvars
), unsigned);
210 * The set being computed is a set of nonterminals which can derive
211 * the empty string or strings consisting of all terminals. At each
212 * iteration a nonterminal is added to the set if there is a
213 * production with that nonterminal as its LHS for which all the
214 * nonterminals in its RHS are already in the set. Iterate until the
215 * set being computed remains unchanged. Any nonterminals not in the
216 * set at that point are useless in that they will never be used in
217 * deriving a sentence of the language.
219 * This iteration doesn't use any special traversal over the
220 * productions. A set is kept of all productions for which all the
221 * nonterminals in the RHS are in useful. Only productions not in
222 * this set are scanned on each iteration. At the end, this set is
223 * saved to be used when finding useful productions: only productions
224 * in this set will appear in the final grammar.
230 for (i
= WORDSIZE(nvars
) - 1; i
>= 0; i
--)
232 for (i
= 1; i
<= nrules
; i
++)
236 if (useful_production(i
, N
))
238 SETBIT(Np
, rlhs
[i
] - ntokens
);
243 if (bits_equal(N
, Np
, WORDSIZE(nvars
)))
254 inaccessable_symbols (void)
262 * Find out which productions are reachable and which symbols are
263 * used. Starting with an empty set of productions and a set of
264 * symbols which only has the start symbol in it, iterate over all
265 * productions until the set of productions remains unchanged for an
266 * iteration. For each production which has a LHS in the set of
267 * reachable symbols, add the production to the set of reachable
268 * productions, and add all of the nonterminals in the RHS of the
269 * production to the set of reachable symbols.
271 * Consider only the (partially) reduced grammar which has only
272 * nonterminals in N and productions in P.
274 * The result is the set P of productions in the reduced grammar, and
275 * the set V of symbols in the reduced grammar.
277 * Although this algorithm also computes the set of terminals which are
278 * reachable, no terminal will be deleted from the grammar. Some
279 * terminals might not be in the grammar but might be generated by
280 * semantic routines, and so the user might want them available with
281 * specified numbers. (Is this true?) However, the nonreachable
282 * terminals are printed (if running in verbose mode) so that the user
286 Vp
= NEW2(WORDSIZE(nsyms
), unsigned);
287 Pp
= NEW2(WORDSIZE(nrules
+ 1), unsigned);
289 /* If the start symbol isn't useful, then nothing will be useful. */
290 if (!BITISSET(N
, start_symbol
- ntokens
))
293 SETBIT(V
, start_symbol
);
298 for (i
= WORDSIZE(nsyms
) - 1; i
>= 0; i
--)
300 for (i
= 1; i
<= nrules
; i
++)
302 if (!BITISSET(Pp
, i
) && BITISSET(P
, i
) &&
303 BITISSET(V
, rlhs
[i
]))
305 for (r
= &ritem
[rrhs
[i
]]; *r
>= 0; r
++)
308 || BITISSET(N
, t
- ntokens
))
316 if (bits_equal(V
, Vp
, WORDSIZE(nsyms
)))
329 /* Tokens 0, 1, and 2 are internal to Bison. Consider them useful. */
330 SETBIT(V
, 0); /* end-of-input token */
331 SETBIT(V
, 1); /* error token */
332 SETBIT(V
, 2); /* some undefined token */
337 nuseful_productions
= bits_size(P
, WORDSIZE(nrules
+ 1));
338 nuseless_productions
= nrules
- nuseful_productions
;
340 nuseful_nonterminals
= 0;
341 for (i
= ntokens
; i
< nsyms
; i
++)
343 nuseful_nonterminals
++;
344 nuseless_nonterminals
= nvars
- nuseful_nonterminals
;
346 /* A token that was used in %prec should not be warned about. */
347 for (i
= 1; i
< nrules
; i
++)
348 if (rprecsym
[i
] != 0)
349 SETBIT(V1
, rprecsym
[i
]);
353 reduce_grammar_tables (void)
355 /* This is turned off because we would need to change the numbers
356 in the case statements in the actions file. */
358 /* remove useless productions */
359 if (nuseless_productions
> 0)
361 short np
, pn
, ni
, pi
;
365 for (pn
= 1; pn
<= nrules
; pn
++)
373 rline
[np
] = rline
[pn
];
374 rprec
[np
] = rprec
[pn
];
375 rassoc
[np
] = rassoc
[pn
];
381 while (ritem
[pi
] >= 0)
382 ritem
[ni
++] = ritem
[pi
++];
386 while (ritem
[ni
++] >= 0);
391 nrules
-= nuseless_productions
;
395 * Is it worth it to reduce the amount of memory for the
396 * grammar? Probably not.
401 /* Disable useless productions,
402 since they may contain useless nonterms
403 that would get mapped below to -1 and confuse everyone. */
404 if (nuseless_productions
> 0)
408 for (pn
= 1; pn
<= nrules
; pn
++)
410 if (!BITISSET(P
, pn
))
417 /* remove useless symbols */
418 if (nuseless_nonterminals
> 0)
422 /* short j; JF unused */
427 * create a map of nonterminal number to new nonterminal
428 * number. -1 in the map means it was useless and is being
432 nontermmap
= NEW2(nvars
, short) - ntokens
;
433 for (i
= ntokens
; i
< nsyms
; i
++)
437 for (i
= ntokens
; i
< nsyms
; i
++)
441 /* Shuffle elements of tables indexed by symbol number. */
443 for (i
= ntokens
; i
< nsyms
; i
++)
448 sassoc
[n
] = sassoc
[i
];
456 /* Replace all symbol numbers in valid data structures. */
458 for (i
= 1; i
<= nrules
; i
++)
460 /* Ignore the rules disabled above. */
462 rlhs
[i
] = nontermmap
[rlhs
[i
]];
463 if (ISVAR (rprecsym
[i
]))
464 /* Can this happen? */
465 rprecsym
[i
] = nontermmap
[rprecsym
[i
]];
468 for (r
= ritem
; *r
; r
++)
472 start_symbol
= nontermmap
[start_symbol
];
474 nsyms
-= nuseless_nonterminals
;
475 nvars
-= nuseless_nonterminals
;
477 free(&nontermmap
[ntokens
]);
485 /* short j; JF unused */
489 if (nuseless_nonterminals
> 0)
491 fprintf(foutput
, _("Useless nonterminals:\n\n"));
492 for (i
= ntokens
; i
< nsyms
; i
++)
494 fprintf(foutput
, " %s\n", tags
[i
]);
497 for (i
= 0; i
< ntokens
; i
++)
499 if (!BITISSET(V
, i
) && !BITISSET(V1
, i
))
503 fprintf(foutput
, _("\n\nTerminals which are not used:\n\n"));
506 fprintf(foutput
, " %s\n", tags
[i
]);
510 if (nuseless_productions
> 0)
512 fprintf(foutput
, _("\n\nUseless rules:\n\n"));
513 for (i
= 1; i
<= nrules
; i
++)
517 fprintf(foutput
, "#%-4d ", i
);
518 fprintf(foutput
, "%s :\t", tags
[rlhs
[i
]]);
519 for (r
= &ritem
[rrhs
[i
]]; *r
>= 0; r
++)
521 fprintf(foutput
, " %s", tags
[*r
]);
523 fprintf(foutput
, ";\n");
527 if (nuseless_nonterminals
> 0 || nuseless_productions
> 0 || b
)
528 fprintf(foutput
, "\n\n");
538 "ntokens = %d, nvars = %d, nsyms = %d, nrules = %d, nitems = %d\n\n",
539 ntokens
, nvars
, nsyms
, nrules
, nitems
);
540 fprintf(foutput
, _("Variables\n---------\n\n"));
541 fprintf(foutput
, _("Value Sprec Sassoc Tag\n"));
542 for (i
= ntokens
; i
< nsyms
; i
++)
543 fprintf(foutput
, "%5d %5d %5d %s\n",
544 i
, sprec
[i
], sassoc
[i
], tags
[i
]);
545 fprintf(foutput
, "\n\n");
546 fprintf(foutput
, _("Rules\n-----\n\n"));
547 for (i
= 1; i
<= nrules
; i
++)
549 fprintf(foutput
, "%-5d(%5d%5d)%5d : (@%-5d)",
550 i
, rprec
[i
], rassoc
[i
], rlhs
[i
], rrhs
[i
]);
551 for (r
= &ritem
[rrhs
[i
]]; *r
> 0; r
++)
552 fprintf(foutput
, "%5d", *r
);
553 fprintf(foutput
, " [%d]\n", -(*r
));
555 fprintf(foutput
, "\n\n");
556 fprintf(foutput
, _("Rules interpreted\n-----------------\n\n"));
557 for (i
= 1; i
<= nrules
; i
++)
559 fprintf(foutput
, "%-5d %s :", i
, tags
[rlhs
[i
]]);
560 for (r
= &ritem
[rrhs
[i
]]; *r
> 0; r
++)
561 fprintf(foutput
, " %s", tags
[*r
]);
562 fprintf(foutput
, "\n");
564 fprintf(foutput
, "\n\n");
571 if (fixed_outfiles
&& nuseless_productions
)
572 fprintf(stderr
, _("%d rules never reduced\n"), nuseless_productions
);
574 fprintf(stderr
, _("%s contains "), infile
);
576 if (nuseless_nonterminals
> 0)
578 fprintf(stderr
, _("%d useless nonterminal%s"),
579 nuseless_nonterminals
,
580 (nuseless_nonterminals
== 1 ? "" : "s"));
582 if (nuseless_nonterminals
> 0 && nuseless_productions
> 0)
583 fprintf(stderr
, _(" and "));
585 if (nuseless_productions
> 0)
587 fprintf(stderr
, _("%d useless rule%s"),
588 nuseless_productions
,
589 (nuseless_productions
== 1 ? "" : "s"));
591 fprintf(stderr
, "\n");