]>
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, 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
41 extern char **tags
; /* reader.c */
42 extern int verboseflag
; /* getargs.c */
43 static int statisticsflag
; /* XXXXXXX */
44 extern int fixed_outfiles
;
51 typedef unsigned *BSet
;
56 * N is set of all nonterminals which are not useless. P is set of all rules
57 * which have no useless nonterminals in their RHS. V is the set of all
61 static BSet N
, P
, V
, V1
;
63 static int nuseful_productions
, nuseless_productions
,
64 nuseful_nonterminals
, nuseless_nonterminals
;
67 bool bits_equal
PARAMS((BSet
, BSet
, int));
68 int nbits
PARAMS((unsigned));
69 int bits_size
PARAMS((BSet
, int));
70 void reduce_grammar
PARAMS((void));
71 static void useless_nonterminals
PARAMS((void));
72 static void inaccessable_symbols
PARAMS((void));
73 static void reduce_grammar_tables
PARAMS((void));
74 static void print_results
PARAMS((void));
75 static void print_notices
PARAMS((void));
76 void dump_grammar
PARAMS((void));
81 bits_equal (BSet L
, BSet R
, int n
)
85 for (i
= n
- 1; i
>= 0; i
--)
98 i
^= (i
& ((unsigned) (- (int) i
)));
106 bits_size (BSet S
, int n
)
110 for (i
= n
- 1; i
>= 0; i
--)
111 count
+= nbits(S
[i
]);
116 reduce_grammar (void)
120 /* Allocate the global sets used to compute the reduced grammar */
122 N
= NEW2(WORDSIZE(nvars
), unsigned);
123 P
= NEW2(WORDSIZE(nrules
+ 1), unsigned);
124 V
= NEW2(WORDSIZE(nsyms
), unsigned);
125 V1
= NEW2(WORDSIZE(nsyms
), unsigned);
127 useless_nonterminals();
128 inaccessable_symbols();
130 reduced
= (bool) (nuseless_nonterminals
+ nuseless_productions
> 0);
135 if (reduced
== FALSE
)
140 if (!BITISSET(N
, start_symbol
- ntokens
))
141 fatal (_("Start symbol %s does not derive any sentence"),
144 reduce_grammar_tables();
148 fprintf(foutput, "REDUCED GRAMMAR\n\n");
152 /**/ statisticsflag
= FALSE
; /* someday getopts should handle this */
153 if (statisticsflag
== TRUE
)
155 _("reduced %s defines %d terminal%s, %d nonterminal%s\
156 , and %d production%s.\n"), infile
,
157 ntokens
, (ntokens
== 1 ? "" : "s"),
158 nvars
, (nvars
== 1 ? "" : "s"),
159 nrules
, (nrules
== 1 ? "" : "s"));
163 /* Free the global sets used to compute the reduced grammar */
172 * Another way to do this would be with a set for each production and then do
173 * subset tests against N0, but even for the C grammar the whole reducing
174 * process takes only 2 seconds on my 8Mhz AT.
178 useful_production (int i
, BSet N0
)
184 * A production is useful if all of the nonterminals in its RHS
185 * appear in the set of useful nonterminals.
188 for (r
= &ritem
[rrhs
[i
]]; *r
> 0; r
++)
190 if (!BITISSET(N0
, n
- ntokens
))
196 /* Remember that rules are 1-origin, symbols are 0-origin. */
199 useless_nonterminals (void)
205 * N is set as built. Np is set being built this iteration. P is set
206 * of all productions which have a RHS all in N.
209 Np
= NEW2(WORDSIZE(nvars
), unsigned);
212 * The set being computed is a set of nonterminals which can derive
213 * the empty string or strings consisting of all terminals. At each
214 * iteration a nonterminal is added to the set if there is a
215 * production with that nonterminal as its LHS for which all the
216 * nonterminals in its RHS are already in the set. Iterate until the
217 * set being computed remains unchanged. Any nonterminals not in the
218 * set at that point are useless in that they will never be used in
219 * deriving a sentence of the language.
221 * This iteration doesn't use any special traversal over the
222 * productions. A set is kept of all productions for which all the
223 * nonterminals in the RHS are in useful. Only productions not in
224 * this set are scanned on each iteration. At the end, this set is
225 * saved to be used when finding useful productions: only productions
226 * in this set will appear in the final grammar.
232 for (i
= WORDSIZE(nvars
) - 1; i
>= 0; i
--)
234 for (i
= 1; i
<= nrules
; i
++)
238 if (useful_production(i
, N
))
240 SETBIT(Np
, rlhs
[i
] - ntokens
);
245 if (bits_equal(N
, Np
, WORDSIZE(nvars
)))
256 inaccessable_symbols (void)
264 * Find out which productions are reachable and which symbols are
265 * used. Starting with an empty set of productions and a set of
266 * symbols which only has the start symbol in it, iterate over all
267 * productions until the set of productions remains unchanged for an
268 * iteration. For each production which has a LHS in the set of
269 * reachable symbols, add the production to the set of reachable
270 * productions, and add all of the nonterminals in the RHS of the
271 * production to the set of reachable symbols.
273 * Consider only the (partially) reduced grammar which has only
274 * nonterminals in N and productions in P.
276 * The result is the set P of productions in the reduced grammar, and
277 * the set V of symbols in the reduced grammar.
279 * Although this algorithm also computes the set of terminals which are
280 * reachable, no terminal will be deleted from the grammar. Some
281 * terminals might not be in the grammar but might be generated by
282 * semantic routines, and so the user might want them available with
283 * specified numbers. (Is this true?) However, the nonreachable
284 * terminals are printed (if running in verbose mode) so that the user
288 Vp
= NEW2(WORDSIZE(nsyms
), unsigned);
289 Pp
= NEW2(WORDSIZE(nrules
+ 1), unsigned);
291 /* If the start symbol isn't useful, then nothing will be useful. */
292 if (!BITISSET(N
, start_symbol
- ntokens
))
295 SETBIT(V
, start_symbol
);
300 for (i
= WORDSIZE(nsyms
) - 1; i
>= 0; i
--)
302 for (i
= 1; i
<= nrules
; i
++)
304 if (!BITISSET(Pp
, i
) && BITISSET(P
, i
) &&
305 BITISSET(V
, rlhs
[i
]))
307 for (r
= &ritem
[rrhs
[i
]]; *r
>= 0; r
++)
310 || BITISSET(N
, t
- ntokens
))
318 if (bits_equal(V
, Vp
, WORDSIZE(nsyms
)))
331 /* Tokens 0, 1, and 2 are internal to Bison. Consider them useful. */
332 SETBIT(V
, 0); /* end-of-input token */
333 SETBIT(V
, 1); /* error token */
334 SETBIT(V
, 2); /* some undefined token */
339 nuseful_productions
= bits_size(P
, WORDSIZE(nrules
+ 1));
340 nuseless_productions
= nrules
- nuseful_productions
;
342 nuseful_nonterminals
= 0;
343 for (i
= ntokens
; i
< nsyms
; i
++)
345 nuseful_nonterminals
++;
346 nuseless_nonterminals
= nvars
- nuseful_nonterminals
;
348 /* A token that was used in %prec should not be warned about. */
349 for (i
= 1; i
< nrules
; i
++)
350 if (rprecsym
[i
] != 0)
351 SETBIT(V1
, rprecsym
[i
]);
355 reduce_grammar_tables (void)
357 /* This is turned off because we would need to change the numbers
358 in the case statements in the actions file. */
360 /* remove useless productions */
361 if (nuseless_productions
> 0)
363 short np
, pn
, ni
, pi
;
367 for (pn
= 1; pn
<= nrules
; pn
++)
375 rline
[np
] = rline
[pn
];
376 rprec
[np
] = rprec
[pn
];
377 rassoc
[np
] = rassoc
[pn
];
383 while (ritem
[pi
] >= 0)
384 ritem
[ni
++] = ritem
[pi
++];
388 while (ritem
[ni
++] >= 0);
393 nrules
-= nuseless_productions
;
397 * Is it worth it to reduce the amount of memory for the
398 * grammar? Probably not.
403 /* Disable useless productions,
404 since they may contain useless nonterms
405 that would get mapped below to -1 and confuse everyone. */
406 if (nuseless_productions
> 0)
410 for (pn
= 1; pn
<= nrules
; pn
++)
412 if (!BITISSET(P
, pn
))
419 /* remove useless symbols */
420 if (nuseless_nonterminals
> 0)
424 /* short j; JF unused */
429 * create a map of nonterminal number to new nonterminal
430 * number. -1 in the map means it was useless and is being
434 nontermmap
= NEW2(nvars
, short) - ntokens
;
435 for (i
= ntokens
; i
< nsyms
; i
++)
439 for (i
= ntokens
; i
< nsyms
; i
++)
443 /* Shuffle elements of tables indexed by symbol number. */
445 for (i
= ntokens
; i
< nsyms
; i
++)
450 sassoc
[n
] = sassoc
[i
];
458 /* Replace all symbol numbers in valid data structures. */
460 for (i
= 1; i
<= nrules
; i
++)
462 /* Ignore the rules disabled above. */
464 rlhs
[i
] = nontermmap
[rlhs
[i
]];
465 if (ISVAR (rprecsym
[i
]))
466 /* Can this happen? */
467 rprecsym
[i
] = nontermmap
[rprecsym
[i
]];
470 for (r
= ritem
; *r
; r
++)
474 start_symbol
= nontermmap
[start_symbol
];
476 nsyms
-= nuseless_nonterminals
;
477 nvars
-= nuseless_nonterminals
;
479 free(&nontermmap
[ntokens
]);
487 /* short j; JF unused */
491 if (nuseless_nonterminals
> 0)
493 fprintf(foutput
, _("Useless nonterminals:\n\n"));
494 for (i
= ntokens
; i
< nsyms
; i
++)
496 fprintf(foutput
, " %s\n", tags
[i
]);
499 for (i
= 0; i
< ntokens
; i
++)
501 if (!BITISSET(V
, i
) && !BITISSET(V1
, i
))
505 fprintf(foutput
, _("\n\nTerminals which are not used:\n\n"));
508 fprintf(foutput
, " %s\n", tags
[i
]);
512 if (nuseless_productions
> 0)
514 fprintf(foutput
, _("\n\nUseless rules:\n\n"));
515 for (i
= 1; i
<= nrules
; i
++)
519 fprintf(foutput
, "#%-4d ", i
);
520 fprintf(foutput
, "%s :\t", tags
[rlhs
[i
]]);
521 for (r
= &ritem
[rrhs
[i
]]; *r
>= 0; r
++)
523 fprintf(foutput
, " %s", tags
[*r
]);
525 fprintf(foutput
, ";\n");
529 if (nuseless_nonterminals
> 0 || nuseless_productions
> 0 || b
)
530 fprintf(foutput
, "\n\n");
540 "ntokens = %d, nvars = %d, nsyms = %d, nrules = %d, nitems = %d\n\n",
541 ntokens
, nvars
, nsyms
, nrules
, nitems
);
542 fprintf(foutput
, _("Variables\n---------\n\n"));
543 fprintf(foutput
, _("Value Sprec Sassoc Tag\n"));
544 for (i
= ntokens
; i
< nsyms
; i
++)
545 fprintf(foutput
, "%5d %5d %5d %s\n",
546 i
, sprec
[i
], sassoc
[i
], tags
[i
]);
547 fprintf(foutput
, "\n\n");
548 fprintf(foutput
, _("Rules\n-----\n\n"));
549 for (i
= 1; i
<= nrules
; i
++)
551 fprintf(foutput
, "%-5d(%5d%5d)%5d : (@%-5d)",
552 i
, rprec
[i
], rassoc
[i
], rlhs
[i
], rrhs
[i
]);
553 for (r
= &ritem
[rrhs
[i
]]; *r
> 0; r
++)
554 fprintf(foutput
, "%5d", *r
);
555 fprintf(foutput
, " [%d]\n", -(*r
));
557 fprintf(foutput
, "\n\n");
558 fprintf(foutput
, _("Rules interpreted\n-----------------\n\n"));
559 for (i
= 1; i
<= nrules
; i
++)
561 fprintf(foutput
, "%-5d %s :", i
, tags
[rlhs
[i
]]);
562 for (r
= &ritem
[rrhs
[i
]]; *r
> 0; r
++)
563 fprintf(foutput
, " %s", tags
[*r
]);
564 fprintf(foutput
, "\n");
566 fprintf(foutput
, "\n\n");
573 if (fixed_outfiles
&& nuseless_productions
)
574 fprintf(stderr
, _("%d rules never reduced\n"), nuseless_productions
);
576 fprintf(stderr
, _("%s contains "), infile
);
578 if (nuseless_nonterminals
> 0)
580 fprintf(stderr
, _("%d useless nonterminal%s"),
581 nuseless_nonterminals
,
582 (nuseless_nonterminals
== 1 ? "" : "s"));
584 if (nuseless_nonterminals
> 0 && nuseless_productions
> 0)
585 fprintf(stderr
, _(" and "));
587 if (nuseless_productions
> 0)
589 fprintf(stderr
, _("%d useless rule%s"),
590 nuseless_productions
,
591 (nuseless_productions
== 1 ? "" : "s"));
593 fprintf(stderr
, "\n");