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