]>
git.saurik.com Git - bison.git/blob - src/reduce.c
a654f0c710450e36fa580fa64bf4be2e9f00ee3a
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, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
23 * Reduce the grammar: Find and eliminate unreachable terminals,
24 * nonterminals, and productions. David S. Bakin.
28 * Don't eliminate unreachable terminals: They may be used by the user's
40 extern char **tags
; /* reader.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 extern void reduce_grammar
PARAMS((void));
66 static bool bits_equal
PARAMS((BSet
, BSet
, int));
67 static int nbits
PARAMS((unsigned));
68 static int bits_size
PARAMS((BSet
, int));
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));
75 #if 0 /* XXX currently unused. */
76 static void dump_grammar
PARAMS((void));
82 bits_equal (BSet L
, BSet R
, int n
)
86 for (i
= n
- 1; i
>= 0; i
--)
99 i
^= (i
& ((unsigned) (- (int) i
)));
107 bits_size (BSet S
, int n
)
111 for (i
= n
- 1; i
>= 0; i
--)
112 count
+= nbits(S
[i
]);
117 reduce_grammar (void)
121 /* Allocate the global sets used to compute the reduced grammar */
123 N
= NEW2(WORDSIZE(nvars
), unsigned);
124 P
= NEW2(WORDSIZE(nrules
+ 1), unsigned);
125 V
= NEW2(WORDSIZE(nsyms
), unsigned);
126 V1
= NEW2(WORDSIZE(nsyms
), unsigned);
128 useless_nonterminals();
129 inaccessable_symbols();
131 reduced
= (bool) (nuseless_nonterminals
+ nuseless_productions
> 0);
136 if (reduced
== FALSE
)
141 if (!BITISSET(N
, start_symbol
- ntokens
))
142 fatal (_("Start symbol %s does not derive any sentence"),
145 reduce_grammar_tables();
149 fprintf(foutput
, "REDUCED GRAMMAR\n\n");
153 /**/ statisticsflag
= FALSE
; /* someday getopts should handle this */
154 if (statisticsflag
== TRUE
)
156 _("reduced %s defines %d terminal%s, %d nonterminal%s\
157 , and %d production%s.\n"), infile
,
158 ntokens
, (ntokens
== 1 ? "" : "s"),
159 nvars
, (nvars
== 1 ? "" : "s"),
160 nrules
, (nrules
== 1 ? "" : "s"));
164 /* Free the global sets used to compute the reduced grammar */
173 * Another way to do this would be with a set for each production and then do
174 * subset tests against N0, but even for the C grammar the whole reducing
175 * process takes only 2 seconds on my 8Mhz AT.
179 useful_production (int i
, BSet N0
)
185 * A production is useful if all of the nonterminals in its RHS
186 * appear in the set of useful nonterminals.
189 for (r
= &ritem
[rrhs
[i
]]; *r
> 0; r
++)
191 if (!BITISSET(N0
, n
- ntokens
))
197 /* Remember that rules are 1-origin, symbols are 0-origin. */
200 useless_nonterminals (void)
206 * N is set as built. Np is set being built this iteration. P is set
207 * of all productions which have a RHS all in N.
210 Np
= NEW2(WORDSIZE(nvars
), unsigned);
213 * The set being computed is a set of nonterminals which can derive
214 * the empty string or strings consisting of all terminals. At each
215 * iteration a nonterminal is added to the set if there is a
216 * production with that nonterminal as its LHS for which all the
217 * nonterminals in its RHS are already in the set. Iterate until the
218 * set being computed remains unchanged. Any nonterminals not in the
219 * set at that point are useless in that they will never be used in
220 * deriving a sentence of the language.
222 * This iteration doesn't use any special traversal over the
223 * productions. A set is kept of all productions for which all the
224 * nonterminals in the RHS are in useful. Only productions not in
225 * this set are scanned on each iteration. At the end, this set is
226 * saved to be used when finding useful productions: only productions
227 * in this set will appear in the final grammar.
233 for (i
= WORDSIZE(nvars
) - 1; i
>= 0; i
--)
235 for (i
= 1; i
<= nrules
; i
++)
239 if (useful_production(i
, N
))
241 SETBIT(Np
, rlhs
[i
] - ntokens
);
246 if (bits_equal(N
, Np
, WORDSIZE(nvars
)))
257 inaccessable_symbols (void)
265 * Find out which productions are reachable and which symbols are
266 * used. Starting with an empty set of productions and a set of
267 * symbols which only has the start symbol in it, iterate over all
268 * productions until the set of productions remains unchanged for an
269 * iteration. For each production which has a LHS in the set of
270 * reachable symbols, add the production to the set of reachable
271 * productions, and add all of the nonterminals in the RHS of the
272 * production to the set of reachable symbols.
274 * Consider only the (partially) reduced grammar which has only
275 * nonterminals in N and productions in P.
277 * The result is the set P of productions in the reduced grammar, and
278 * the set V of symbols in the reduced grammar.
280 * Although this algorithm also computes the set of terminals which are
281 * reachable, no terminal will be deleted from the grammar. Some
282 * terminals might not be in the grammar but might be generated by
283 * semantic routines, and so the user might want them available with
284 * specified numbers. (Is this true?) However, the nonreachable
285 * terminals are printed (if running in verbose mode) so that the user
289 Vp
= NEW2(WORDSIZE(nsyms
), unsigned);
290 Pp
= NEW2(WORDSIZE(nrules
+ 1), unsigned);
292 /* If the start symbol isn't useful, then nothing will be useful. */
293 if (!BITISSET(N
, start_symbol
- ntokens
))
296 SETBIT(V
, start_symbol
);
301 for (i
= WORDSIZE(nsyms
) - 1; i
>= 0; i
--)
303 for (i
= 1; i
<= nrules
; i
++)
305 if (!BITISSET(Pp
, i
) && BITISSET(P
, i
) &&
306 BITISSET(V
, rlhs
[i
]))
308 for (r
= &ritem
[rrhs
[i
]]; *r
>= 0; r
++)
311 || BITISSET(N
, t
- ntokens
))
319 if (bits_equal(V
, Vp
, WORDSIZE(nsyms
)))
332 /* Tokens 0, 1, and 2 are internal to Bison. Consider them useful. */
333 SETBIT(V
, 0); /* end-of-input token */
334 SETBIT(V
, 1); /* error token */
335 SETBIT(V
, 2); /* some undefined token */
340 nuseful_productions
= bits_size(P
, WORDSIZE(nrules
+ 1));
341 nuseless_productions
= nrules
- nuseful_productions
;
343 nuseful_nonterminals
= 0;
344 for (i
= ntokens
; i
< nsyms
; i
++)
346 nuseful_nonterminals
++;
347 nuseless_nonterminals
= nvars
- nuseful_nonterminals
;
349 /* A token that was used in %prec should not be warned about. */
350 for (i
= 1; i
< nrules
; i
++)
351 if (rprecsym
[i
] != 0)
352 SETBIT(V1
, rprecsym
[i
]);
356 reduce_grammar_tables (void)
358 /* This is turned off because we would need to change the numbers
359 in the case statements in the actions file. */
361 /* remove useless productions */
362 if (nuseless_productions
> 0)
364 short np
, pn
, ni
, pi
;
368 for (pn
= 1; pn
<= nrules
; pn
++)
376 rline
[np
] = rline
[pn
];
377 rprec
[np
] = rprec
[pn
];
378 rassoc
[np
] = rassoc
[pn
];
384 while (ritem
[pi
] >= 0)
385 ritem
[ni
++] = ritem
[pi
++];
389 while (ritem
[ni
++] >= 0);
394 nrules
-= nuseless_productions
;
398 * Is it worth it to reduce the amount of memory for the
399 * grammar? Probably not.
404 /* Disable useless productions,
405 since they may contain useless nonterms
406 that would get mapped below to -1 and confuse everyone. */
407 if (nuseless_productions
> 0)
411 for (pn
= 1; pn
<= nrules
; pn
++)
413 if (!BITISSET(P
, pn
))
420 /* remove useless symbols */
421 if (nuseless_nonterminals
> 0)
425 /* short j; JF unused */
430 * create a map of nonterminal number to new nonterminal
431 * number. -1 in the map means it was useless and is being
435 nontermmap
= NEW2(nvars
, short) - ntokens
;
436 for (i
= ntokens
; i
< nsyms
; i
++)
440 for (i
= ntokens
; i
< nsyms
; i
++)
444 /* Shuffle elements of tables indexed by symbol number. */
446 for (i
= ntokens
; i
< nsyms
; i
++)
451 sassoc
[n
] = sassoc
[i
];
459 /* Replace all symbol numbers in valid data structures. */
461 for (i
= 1; i
<= nrules
; i
++)
463 /* Ignore the rules disabled above. */
465 rlhs
[i
] = nontermmap
[rlhs
[i
]];
466 if (ISVAR (rprecsym
[i
]))
467 /* Can this happen? */
468 rprecsym
[i
] = nontermmap
[rprecsym
[i
]];
471 for (r
= ritem
; *r
; r
++)
475 start_symbol
= nontermmap
[start_symbol
];
477 nsyms
-= nuseless_nonterminals
;
478 nvars
-= nuseless_nonterminals
;
480 free(&nontermmap
[ntokens
]);
488 /* short j; JF unused */
492 if (nuseless_nonterminals
> 0)
494 fprintf(foutput
, _("Useless nonterminals:\n\n"));
495 for (i
= ntokens
; i
< nsyms
; i
++)
497 fprintf(foutput
, " %s\n", tags
[i
]);
500 for (i
= 0; i
< ntokens
; i
++)
502 if (!BITISSET(V
, i
) && !BITISSET(V1
, i
))
506 fprintf(foutput
, _("\n\nTerminals which are not used:\n\n"));
509 fprintf(foutput
, " %s\n", tags
[i
]);
513 if (nuseless_productions
> 0)
515 fprintf(foutput
, _("\n\nUseless rules:\n\n"));
516 for (i
= 1; i
<= nrules
; i
++)
520 fprintf(foutput
, "#%-4d ", i
);
521 fprintf(foutput
, "%s :\t", tags
[rlhs
[i
]]);
522 for (r
= &ritem
[rrhs
[i
]]; *r
>= 0; r
++)
524 fprintf(foutput
, " %s", tags
[*r
]);
526 fprintf(foutput
, ";\n");
530 if (nuseless_nonterminals
> 0 || nuseless_productions
> 0 || b
)
531 fprintf(foutput
, "\n\n");
534 #if 0 /* XXX currently unused. */
542 "ntokens = %d, nvars = %d, nsyms = %d, nrules = %d, nitems = %d\n\n",
543 ntokens
, nvars
, nsyms
, nrules
, nitems
);
544 fprintf(foutput
, _("Variables\n---------\n\n"));
545 fprintf(foutput
, _("Value Sprec Sassoc Tag\n"));
546 for (i
= ntokens
; i
< nsyms
; i
++)
547 fprintf(foutput
, "%5d %5d %5d %s\n",
548 i
, sprec
[i
], sassoc
[i
], tags
[i
]);
549 fprintf(foutput
, "\n\n");
550 fprintf(foutput
, _("Rules\n-----\n\n"));
551 for (i
= 1; i
<= nrules
; i
++)
553 fprintf(foutput
, "%-5d(%5d%5d)%5d : (@%-5d)",
554 i
, rprec
[i
], rassoc
[i
], rlhs
[i
], rrhs
[i
]);
555 for (r
= &ritem
[rrhs
[i
]]; *r
> 0; r
++)
556 fprintf(foutput
, "%5d", *r
);
557 fprintf(foutput
, " [%d]\n", -(*r
));
559 fprintf(foutput
, "\n\n");
560 fprintf(foutput
, _("Rules interpreted\n-----------------\n\n"));
561 for (i
= 1; i
<= nrules
; i
++)
563 fprintf(foutput
, "%-5d %s :", i
, tags
[rlhs
[i
]]);
564 for (r
= &ritem
[rrhs
[i
]]; *r
> 0; r
++)
565 fprintf(foutput
, " %s", tags
[*r
]);
566 fprintf(foutput
, "\n");
568 fprintf(foutput
, "\n\n");
576 if (fixed_outfiles
&& nuseless_productions
)
577 fprintf(stderr
, _("%d rules never reduced\n"), nuseless_productions
);
579 fprintf(stderr
, _("%s contains "), infile
);
581 if (nuseless_nonterminals
> 0)
583 fprintf(stderr
, _("%d useless nonterminal%s"),
584 nuseless_nonterminals
,
585 (nuseless_nonterminals
== 1 ? "" : "s"));
587 if (nuseless_nonterminals
> 0 && nuseless_productions
> 0)
588 fprintf(stderr
, _(" and "));
590 if (nuseless_productions
> 0)
592 fprintf(stderr
, _("%d useless rule%s"),
593 nuseless_productions
,
594 (nuseless_productions
== 1 ? "" : "s"));
596 fprintf(stderr
, "\n");