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