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