]>
git.saurik.com Git - bison.git/blob - src/reduce.c
109f69a5b549df6c59768ba81dd4056972da6e07
1 /* Grammar reduction for Bison.
2 Copyright 1988, 1989, 2000, 2001 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. */
22 /* Reduce the grammar: Find and eliminate unreachable terminals,
23 nonterminals, and productions. David S. Bakin. */
25 /* Don't eliminate unreachable terminals: They may be used by the
42 /* Set of all nonterminals which are not useless. */
45 /* Set of all rules which have no useless nonterminals in their RHS. */
48 /* Set of all accessible symbols. */
51 /* Set of symbols used to define rule precedence (so they are
52 `useless', but no warning should be issued). */
55 static int nuseful_productions
;
56 static int nuseless_productions
;
57 static int nuseful_nonterminals
;
58 int nuseless_nonterminals
;
60 /*-------------------------------------------------------------------.
61 | Another way to do this would be with a set for each production and |
62 | then do subset tests against N0, but even for the C grammar the |
63 | whole reducing process takes only 2 seconds on my 8Mhz AT. |
64 `-------------------------------------------------------------------*/
67 useful_production (int i
, bitset N0
)
72 /* A production is useful if all of the nonterminals in its appear
73 in the set of useful nonterminals. */
75 for (r
= rules
[i
].rhs
; *r
>= 0; r
++)
76 if (ISVAR (n
= *r
) && !bitset_test (N0
, n
- ntokens
))
82 /*---------------------------------------------------------.
83 | Remember that rules are 1-origin, symbols are 0-origin. |
84 `---------------------------------------------------------*/
87 useless_nonterminals (void)
92 /* N is set as built. Np is set being built this iteration. P is
93 set of all productions which have a RHS all in N. */
95 Np
= bitset_create (nvars
, BITSET_FIXED
);
98 /* The set being computed is a set of nonterminals which can derive
99 the empty string or strings consisting of all terminals. At each
100 iteration a nonterminal is added to the set if there is a
101 production with that nonterminal as its LHS for which all the
102 nonterminals in its RHS are already in the set. Iterate until
103 the set being computed remains unchanged. Any nonterminals not
104 in the set at that point are useless in that they will never be
105 used in deriving a sentence of the language.
107 This iteration doesn't use any special traversal over the
108 productions. A set is kept of all productions for which all the
109 nonterminals in the RHS are in useful. Only productions not in
110 this set are scanned on each iteration. At the end, this set is
111 saved to be used when finding useful productions: only
112 productions in this set will appear in the final grammar. */
117 for (i
= 1; i
<= nrules
; i
++)
118 if (!bitset_test (P
, i
)
119 && useful_production (i
, N
))
121 bitset_set (Np
, rules
[i
].lhs
- ntokens
);
124 if (bitset_equal_p (N
, Np
))
136 inaccessable_symbols (void)
143 /* Find out which productions are reachable and which symbols are
144 used. Starting with an empty set of productions and a set of
145 symbols which only has the start symbol in it, iterate over all
146 productions until the set of productions remains unchanged for an
147 iteration. For each production which has a LHS in the set of
148 reachable symbols, add the production to the set of reachable
149 productions, and add all of the nonterminals in the RHS of the
150 production to the set of reachable symbols.
152 Consider only the (partially) reduced grammar which has only
153 nonterminals in N and productions in P.
155 The result is the set P of productions in the reduced grammar,
156 and the set V of symbols in the reduced grammar.
158 Although this algorithm also computes the set of terminals which
159 are reachable, no terminal will be deleted from the grammar. Some
160 terminals might not be in the grammar but might be generated by
161 semantic routines, and so the user might want them available with
162 specified numbers. (Is this true?) However, the nonreachable
163 terminals are printed (if running in verbose mode) so that the
166 Vp
= bitset_create (nsyms
, BITSET_FIXED
);
167 Pp
= bitset_create (nrules
+ 1, BITSET_FIXED
);
169 /* If the start symbol isn't useful, then nothing will be useful. */
170 if (bitset_test (N
, start_symbol
- ntokens
))
172 bitset_set (V
, start_symbol
);
177 for (i
= 1; i
<= nrules
; i
++)
179 if (!bitset_test (Pp
, i
)
180 && bitset_test (P
, i
)
181 && bitset_test (V
, rules
[i
].lhs
))
183 for (r
= rules
[i
].rhs
; *r
>= 0; r
++)
184 if (ISTOKEN (t
= *r
) || bitset_test (N
, t
- ntokens
))
189 if (bitset_equal_p (V
, Vp
))
200 /* Tokens 0, 1, and 2 are internal to Bison. Consider them useful. */
201 bitset_set (V
, 0); /* end-of-input token */
202 bitset_set (V
, 1); /* error token */
203 bitset_set (V
, 2); /* some undefined token */
208 nuseful_productions
= bitset_count (P
);
209 nuseless_productions
= nrules
- nuseful_productions
;
211 nuseful_nonterminals
= 0;
212 for (i
= ntokens
; i
< nsyms
; i
++)
213 if (bitset_test (V
, i
))
214 nuseful_nonterminals
++;
215 nuseless_nonterminals
= nvars
- nuseful_nonterminals
;
217 /* A token that was used in %prec should not be warned about. */
218 for (i
= 1; i
< nrules
; i
++)
219 if (rules
[i
].precsym
!= 0)
220 bitset_set (V1
, rules
[i
].precsym
);
224 reduce_grammar_tables (void)
226 /* This is turned off because we would need to change the numbers in
227 the case statements in the actions file.
229 We don't disable it via CPP so that it is still checked with the
230 rest of the code, to avoid its becoming completely obsolete.
232 FIXME: I think the comment above demonstrates this code must be
233 turned off for *semantic* parser, not in the general case. Try
234 to understand this better --akim. */
237 /* remove useless productions */
238 if (nuseless_productions
> 0)
240 short np
, pn
, ni
, pi
;
244 for (pn
= 1; pn
<= nrules
; pn
++)
245 if (bitset_test (P
, pn
))
250 rules
[np
].lhs
= rules
[pn
].lhs
;
251 rules
[np
].line
= rules
[pn
].line
;
252 rules
[np
].prec
= rules
[pn
].prec
;
253 rules
[np
].assoc
= rules
[pn
].assoc
;
254 rules
[np
].rhs
= rules
[pn
].rhs
;
255 if (rules
[np
].rhs
- ritem
!= ni
)
257 pi
= rules
[np
].rhs
- ritem
;
258 rules
[np
].rhs
= ritem
+ ni
;
259 while (ritem
[pi
] >= 0)
260 ritem
[ni
++] = ritem
[pi
++];
266 while (ritem
[ni
++] >= 0)
272 nrules
-= nuseless_productions
;
276 /* Is it worth it to reduce the amount of memory for the
277 grammar? Probably not. */
280 /* Disable useless productions. */
281 if (nuseless_productions
> 0)
284 for (pn
= 1; pn
<= nrules
; pn
++)
285 rules
[pn
].useful
= bitset_test (P
, pn
);
290 /*------------------------------.
291 | Remove useless nonterminals. |
292 `------------------------------*/
295 nonterminals_reduce (void)
299 /* Map the nonterminals to their new index: useful first, useless
300 afterwards. Kept for later report. */
302 short *nontermmap
= XCALLOC (short, nvars
) - ntokens
;
304 for (i
= ntokens
; i
< nsyms
; i
++)
305 if (bitset_test (V
, i
))
307 for (i
= ntokens
; i
< nsyms
; i
++)
308 if (!bitset_test (V
, i
))
312 /* Shuffle elements of tables indexed by symbol number. */
314 bucket
**symbols_sorted
= XMALLOC (bucket
*, nvars
) - ntokens
;
316 for (i
= ntokens
; i
< nsyms
; i
++)
317 symbols_sorted
[nontermmap
[i
]] = symbols
[i
];
318 for (i
= ntokens
; i
< nsyms
; i
++)
319 symbols
[i
] = symbols_sorted
[i
];
320 free (symbols_sorted
+ ntokens
);
323 /* Replace all symbol numbers in valid data structures. */
325 for (i
= 1; i
<= nrules
; i
++)
327 rules
[i
].lhs
= nontermmap
[rules
[i
].lhs
];
328 if (ISVAR (rules
[i
].precsym
))
329 /* Can this happen? */
330 rules
[i
].precsym
= nontermmap
[rules
[i
].precsym
];
333 for (i
= 0; i
< nritems
; ++i
)
334 if (ISVAR (ritem
[i
]))
335 ritem
[i
] = nontermmap
[ritem
[i
]];
337 start_symbol
= nontermmap
[start_symbol
];
339 nsyms
-= nuseless_nonterminals
;
340 nvars
-= nuseless_nonterminals
;
342 free (nontermmap
+ ntokens
);
346 /*------------------------------------------------------------------.
347 | Output the detailed results of the reductions. For FILE.output. |
348 `------------------------------------------------------------------*/
351 reduce_output (FILE *out
)
353 if (nuseless_nonterminals
> 0)
356 fprintf (out
, "%s\n\n", _("Useless nonterminals:"));
357 for (i
= 0; i
< nuseless_nonterminals
; ++i
)
358 fprintf (out
, " %s\n", symbols
[nsyms
+ i
]->tag
);
365 for (i
= 0; i
< ntokens
; i
++)
366 if (!bitset_test (V
, i
) && !bitset_test (V1
, i
))
369 fprintf (out
, "%s\n\n", _("Terminals which are not used:"));
371 fprintf (out
, " %s\n", symbols
[i
]->tag
);
377 if (nuseless_productions
> 0)
380 fprintf (out
, "%s\n\n", _("Useless rules:"));
381 for (i
= 1; i
<= nrules
; i
++)
382 if (!rules
[i
].useful
)
385 fprintf (out
, "#%-4d ", i
- 1);
386 fprintf (out
, "%s:", symbols
[rules
[i
].lhs
]->tag
);
387 for (r
= rules
[i
].rhs
; *r
>= 0; r
++)
388 fprintf (out
, " %s", symbols
[*r
]->tag
);
396 dump_grammar (FILE *out
)
401 fprintf (out
, "REDUCED GRAMMAR\n\n");
403 "ntokens = %d, nvars = %d, nsyms = %d, nrules = %d, nitems = %d\n\n",
404 ntokens
, nvars
, nsyms
, nrules
, nitems
);
405 fprintf (out
, "Variables\n---------\n\n");
406 fprintf (out
, "Value Sprec Sassoc Tag\n");
407 for (i
= ntokens
; i
< nsyms
; i
++)
408 fprintf (out
, "%5d %5d %5d %s\n",
410 symbols
[i
]->prec
, symbols
[i
]->assoc
, symbols
[i
]->tag
);
411 fprintf (out
, "\n\n");
412 fprintf (out
, "Rules\n-----\n\n");
413 fprintf (out
, "Num (Prec, Assoc, Useful, Ritem Range) Lhs -> Rhs (Ritem range) [Num]\n");
414 for (i
= 1; i
<= nrules
; i
++)
417 /* Find the last RHS index in ritems. */
418 for (r
= rules
[i
].rhs
; *r
>= 0; ++r
)
420 fprintf (out
, "%3d (%2d, %2d, %2d, %2d-%2d) %2d ->",
422 rules
[i
].prec
, rules
[i
].assoc
, rules
[i
].useful
,
423 rules
[i
].rhs
- ritem
, rules
[i
].rhs
- ritem
+ rhs_count
- 1,
425 /* Dumped the RHS. */
426 for (r
= rules
[i
].rhs
; *r
>= 0; r
++)
427 fprintf (out
, "%3d", *r
);
428 fprintf (out
, " [%d]\n", -(*r
) - 1);
430 fprintf (out
, "\n\n");
431 fprintf (out
, "Rules interpreted\n-----------------\n\n");
432 for (i
= 1; i
<= nrules
; i
++)
434 fprintf (out
, "%-5d %s :", i
, symbols
[rules
[i
].lhs
]->tag
);
435 for (r
= rules
[i
].rhs
; *r
>= 0; r
++)
436 fprintf (out
, " %s", symbols
[*r
]->tag
);
439 fprintf (out
, "\n\n");
444 /*-------------------------------.
445 | Report the results to STDERR. |
446 `-------------------------------*/
451 if (yacc_flag
&& nuseless_productions
)
452 fprintf (stderr
, ngettext ("%d rule never reduced\n",
453 "%d rules never reduced\n",
454 nuseless_productions
),
455 nuseless_productions
);
457 fprintf (stderr
, _("%s contains "), infile
);
459 if (nuseless_nonterminals
> 0)
460 fprintf (stderr
, ngettext ("%d useless nonterminal",
461 "%d useless nonterminals",
462 nuseless_nonterminals
),
463 nuseless_nonterminals
);
465 if (nuseless_nonterminals
> 0 && nuseless_productions
> 0)
466 fprintf (stderr
, _(" and "));
468 if (nuseless_productions
> 0)
469 fprintf (stderr
, ngettext ("%d useless rule",
471 nuseless_productions
),
472 nuseless_productions
);
473 fprintf (stderr
, "\n");
478 reduce_grammar (void)
482 /* Allocate the global sets used to compute the reduced grammar */
484 N
= bitset_create (nvars
, BITSET_FIXED
);
485 P
= bitset_create (nrules
+ 1, BITSET_FIXED
);
486 V
= bitset_create (nsyms
, BITSET_FIXED
);
487 V1
= bitset_create (nsyms
, BITSET_FIXED
);
489 useless_nonterminals ();
490 inaccessable_symbols ();
492 reduced
= (bool) (nuseless_nonterminals
+ nuseless_productions
> 0);
498 if (!bitset_test (N
, start_symbol
- ntokens
))
499 fatal (_("Start symbol %s does not derive any sentence"),
500 symbols
[start_symbol
]->tag
);
502 reduce_grammar_tables ();
503 if (nuseless_nonterminals
> 0)
504 nonterminals_reduce ();
508 dump_grammar (stderr
);
510 fprintf (stderr
, "reduced %s defines %d terminals, %d nonterminals\
511 , and %d productions.\n",
512 infile
, ntokens
, nvars
, nrules
);
517 /*-----------------------------------------------------------.
518 | Free the global sets used to compute the reduced grammar. |
519 `-----------------------------------------------------------*/