]> git.saurik.com Git - bison.git/blame - src/reduce.c
* src/output.c (token_actions): Introduce a temporary array,
[bison.git] / src / reduce.c
CommitLineData
572909b5
RS
1/* Grammar reduction for Bison.
2 Copyright (C) 1988, 1989 Free Software Foundation, Inc.
3
4This file is part of Bison, the GNU Compiler Compiler.
5
6Bison is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2, or (at your option)
9any later version.
10
11Bison is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with Bison; see the file COPYING. If not, write to
c49a8e71
JT
18the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19Boston, MA 02111-1307, USA. */
572909b5
RS
20
21
22/*
23 * Reduce the grammar: Find and eliminate unreachable terminals,
24 * nonterminals, and productions. David S. Bakin.
25 */
26
27/*
28 * Don't eliminate unreachable terminals: They may be used by the user's
29 * parser.
30 */
31
572909b5 32#include "system.h"
ceed8467 33#include "getargs.h"
572909b5
RS
34#include "files.h"
35#include "gram.h"
7612000c 36#include "alloc.h"
a0f6b076 37#include "complain.h"
572909b5
RS
38
39
40extern char **tags; /* reader.c */
572909b5 41static int statisticsflag; /* XXXXXXX */
d2729d44 42extern int fixed_outfiles;
572909b5
RS
43
44#ifndef TRUE
45#define TRUE (1)
46#define FALSE (0)
47#endif
48typedef int bool;
49typedef unsigned *BSet;
50typedef short *rule;
51
52
53/*
54 * N is set of all nonterminals which are not useless. P is set of all rules
55 * which have no useless nonterminals in their RHS. V is the set of all
56 * accessible symbols.
57 */
58
59static BSet N, P, V, V1;
60
61static int nuseful_productions, nuseless_productions,
62 nuseful_nonterminals, nuseless_nonterminals;
63
64
4a120d45
JT
65extern void reduce_grammar PARAMS((void));
66static bool bits_equal PARAMS((BSet, BSet, int));
67static int nbits PARAMS((unsigned));
68static int bits_size PARAMS((BSet, int));
d2729d44
JT
69static void useless_nonterminals PARAMS((void));
70static void inaccessable_symbols PARAMS((void));
71static void reduce_grammar_tables PARAMS((void));
72static void print_results PARAMS((void));
73static void print_notices PARAMS((void));
4a120d45
JT
74
75#if 0 /* XXX currently unused. */
76static void dump_grammar PARAMS((void));
77#endif
572909b5 78
572909b5
RS
79\f
80
4a120d45 81static bool
d2729d44 82bits_equal (BSet L, BSet R, int n)
572909b5
RS
83{
84 int i;
85
86 for (i = n - 1; i >= 0; i--)
87 if (L[i] != R[i])
88 return FALSE;
89 return TRUE;
90}
91
92
4a120d45 93static int
d2729d44 94nbits (unsigned i)
572909b5
RS
95{
96 int count = 0;
97
98 while (i != 0) {
2f9040d9 99 i ^= (i & ((unsigned) (- (int) i)));
572909b5
RS
100 ++count;
101 }
102 return count;
103}
104
105
4a120d45 106static int
d2729d44 107bits_size (BSet S, int n)
572909b5
RS
108{
109 int i, count = 0;
110
111 for (i = n - 1; i >= 0; i--)
112 count += nbits(S[i]);
113 return count;
114}
115\f
116void
d2729d44 117reduce_grammar (void)
572909b5
RS
118{
119 bool reduced;
120
121 /* Allocate the global sets used to compute the reduced grammar */
122
123 N = NEW2(WORDSIZE(nvars), unsigned);
124 P = NEW2(WORDSIZE(nrules + 1), unsigned);
125 V = NEW2(WORDSIZE(nsyms), unsigned);
126 V1 = NEW2(WORDSIZE(nsyms), unsigned);
127
128 useless_nonterminals();
129 inaccessable_symbols();
130
131 reduced = (bool) (nuseless_nonterminals + nuseless_productions > 0);
132
133 if (verboseflag)
134 print_results();
135
136 if (reduced == FALSE)
137 goto done_reducing;
138
139 print_notices();
140
141 if (!BITISSET(N, start_symbol - ntokens))
a0f6b076 142 fatal (_("Start symbol %s does not derive any sentence"),
572909b5
RS
143 tags[start_symbol]);
144
145 reduce_grammar_tables();
4a120d45 146#if 0
cb7db13e
AD
147 if (verboseflag)
148 {
149 fprintf(foutput, "REDUCED GRAMMAR\n\n");
150 dump_grammar();
151 }
4a120d45 152#endif
572909b5
RS
153 /**/ statisticsflag = FALSE; /* someday getopts should handle this */
154 if (statisticsflag == TRUE)
155 fprintf(stderr,
a083fbbf
RS
156 _("reduced %s defines %d terminal%s, %d nonterminal%s\
157, and %d production%s.\n"), infile,
572909b5
RS
158 ntokens, (ntokens == 1 ? "" : "s"),
159 nvars, (nvars == 1 ? "" : "s"),
160 nrules, (nrules == 1 ? "" : "s"));
161
162 done_reducing:
163
164 /* Free the global sets used to compute the reduced grammar */
165
166 FREE(N);
167 FREE(V);
168 FREE(P);
169
170}
171\f
172/*
173 * Another way to do this would be with a set for each production and then do
d2729d44 174 * subset tests against N0, but even for the C grammar the whole reducing
572909b5
RS
175 * process takes only 2 seconds on my 8Mhz AT.
176 */
177
a083fbbf 178static bool
d2729d44 179useful_production (int i, BSet N0)
572909b5
RS
180{
181 rule r;
182 short n;
183
184 /*
185 * A production is useful if all of the nonterminals in its RHS
186 * appear in the set of useful nonterminals.
187 */
188
189 for (r = &ritem[rrhs[i]]; *r > 0; r++)
190 if (ISVAR(n = *r))
d2729d44 191 if (!BITISSET(N0, n - ntokens))
572909b5
RS
192 return FALSE;
193 return TRUE;
194}
195
196
197/* Remember that rules are 1-origin, symbols are 0-origin. */
198
a083fbbf 199static void
d2729d44 200useless_nonterminals (void)
572909b5
RS
201{
202 BSet Np, Ns;
203 int i, n;
204
205 /*
206 * N is set as built. Np is set being built this iteration. P is set
207 * of all productions which have a RHS all in N.
208 */
209
210 Np = NEW2(WORDSIZE(nvars), unsigned);
211
212 /*
213 * The set being computed is a set of nonterminals which can derive
214 * the empty string or strings consisting of all terminals. At each
215 * iteration a nonterminal is added to the set if there is a
216 * production with that nonterminal as its LHS for which all the
217 * nonterminals in its RHS are already in the set. Iterate until the
218 * set being computed remains unchanged. Any nonterminals not in the
219 * set at that point are useless in that they will never be used in
220 * deriving a sentence of the language.
a083fbbf 221 *
572909b5
RS
222 * This iteration doesn't use any special traversal over the
223 * productions. A set is kept of all productions for which all the
224 * nonterminals in the RHS are in useful. Only productions not in
225 * this set are scanned on each iteration. At the end, this set is
226 * saved to be used when finding useful productions: only productions
227 * in this set will appear in the final grammar.
228 */
229
230 n = 0;
231 while (1)
232 {
233 for (i = WORDSIZE(nvars) - 1; i >= 0; i--)
234 Np[i] = N[i];
235 for (i = 1; i <= nrules; i++)
236 {
237 if (!BITISSET(P, i))
238 {
239 if (useful_production(i, N))
240 {
241 SETBIT(Np, rlhs[i] - ntokens);
242 SETBIT(P, i);
243 }
244 }
245 }
246 if (bits_equal(N, Np, WORDSIZE(nvars)))
247 break;
248 Ns = Np;
249 Np = N;
250 N = Ns;
251 }
252 FREE(N);
253 N = Np;
254}
255\f
a083fbbf 256static void
d2729d44 257inaccessable_symbols (void)
572909b5
RS
258{
259 BSet Vp, Vs, Pp;
260 int i, n;
261 short t;
262 rule r;
263
264 /*
265 * Find out which productions are reachable and which symbols are
266 * used. Starting with an empty set of productions and a set of
267 * symbols which only has the start symbol in it, iterate over all
268 * productions until the set of productions remains unchanged for an
269 * iteration. For each production which has a LHS in the set of
270 * reachable symbols, add the production to the set of reachable
271 * productions, and add all of the nonterminals in the RHS of the
272 * production to the set of reachable symbols.
a083fbbf 273 *
572909b5
RS
274 * Consider only the (partially) reduced grammar which has only
275 * nonterminals in N and productions in P.
a083fbbf 276 *
572909b5
RS
277 * The result is the set P of productions in the reduced grammar, and
278 * the set V of symbols in the reduced grammar.
a083fbbf 279 *
572909b5
RS
280 * Although this algorithm also computes the set of terminals which are
281 * reachable, no terminal will be deleted from the grammar. Some
282 * terminals might not be in the grammar but might be generated by
283 * semantic routines, and so the user might want them available with
284 * specified numbers. (Is this true?) However, the nonreachable
285 * terminals are printed (if running in verbose mode) so that the user
286 * can know.
287 */
288
289 Vp = NEW2(WORDSIZE(nsyms), unsigned);
290 Pp = NEW2(WORDSIZE(nrules + 1), unsigned);
291
292 /* If the start symbol isn't useful, then nothing will be useful. */
293 if (!BITISSET(N, start_symbol - ntokens))
294 goto end_iteration;
295
296 SETBIT(V, start_symbol);
297
298 n = 0;
299 while (1)
300 {
301 for (i = WORDSIZE(nsyms) - 1; i >= 0; i--)
302 Vp[i] = V[i];
303 for (i = 1; i <= nrules; i++)
304 {
a083fbbf 305 if (!BITISSET(Pp, i) && BITISSET(P, i) &&
572909b5
RS
306 BITISSET(V, rlhs[i]))
307 {
308 for (r = &ritem[rrhs[i]]; *r >= 0; r++)
309 {
310 if (ISTOKEN(t = *r)
311 || BITISSET(N, t - ntokens))
312 {
313 SETBIT(Vp, t);
314 }
315 }
316 SETBIT(Pp, i);
317 }
318 }
319 if (bits_equal(V, Vp, WORDSIZE(nsyms)))
320 {
321 break;
322 }
323 Vs = Vp;
324 Vp = V;
325 V = Vs;
326 }
327 end_iteration:
328
329 FREE(V);
330 V = Vp;
331
332 /* Tokens 0, 1, and 2 are internal to Bison. Consider them useful. */
333 SETBIT(V, 0); /* end-of-input token */
334 SETBIT(V, 1); /* error token */
335 SETBIT(V, 2); /* some undefined token */
336
337 FREE(P);
338 P = Pp;
339
340 nuseful_productions = bits_size(P, WORDSIZE(nrules + 1));
341 nuseless_productions = nrules - nuseful_productions;
342
343 nuseful_nonterminals = 0;
344 for (i = ntokens; i < nsyms; i++)
345 if (BITISSET(V, i))
346 nuseful_nonterminals++;
347 nuseless_nonterminals = nvars - nuseful_nonterminals;
348
349 /* A token that was used in %prec should not be warned about. */
350 for (i = 1; i < nrules; i++)
351 if (rprecsym[i] != 0)
352 SETBIT(V1, rprecsym[i]);
353}
354\f
a083fbbf 355static void
d2729d44 356reduce_grammar_tables (void)
572909b5
RS
357{
358/* This is turned off because we would need to change the numbers
359 in the case statements in the actions file. */
360#if 0
361 /* remove useless productions */
362 if (nuseless_productions > 0)
363 {
364 short np, pn, ni, pi;
365
366 np = 0;
367 ni = 0;
368 for (pn = 1; pn <= nrules; pn++)
369 {
370 if (BITISSET(P, pn))
371 {
372 np++;
373 if (pn != np)
374 {
375 rlhs[np] = rlhs[pn];
376 rline[np] = rline[pn];
377 rprec[np] = rprec[pn];
378 rassoc[np] = rassoc[pn];
379 rrhs[np] = rrhs[pn];
380 if (rrhs[np] != ni)
381 {
382 pi = rrhs[np];
383 rrhs[np] = ni;
384 while (ritem[pi] >= 0)
385 ritem[ni++] = ritem[pi++];
386 ritem[ni++] = -np;
387 }
388 } else {
389 while (ritem[ni++] >= 0);
390 }
391 }
392 }
393 ritem[ni] = 0;
394 nrules -= nuseless_productions;
395 nitems = ni;
396
397 /*
398 * Is it worth it to reduce the amount of memory for the
399 * grammar? Probably not.
400 */
401
402 }
403#endif /* 0 */
404 /* Disable useless productions,
405 since they may contain useless nonterms
406 that would get mapped below to -1 and confuse everyone. */
407 if (nuseless_productions > 0)
408 {
409 int pn;
410
411 for (pn = 1; pn <= nrules; pn++)
412 {
413 if (!BITISSET(P, pn))
414 {
415 rlhs[pn] = -1;
416 }
417 }
418 }
419
420 /* remove useless symbols */
421 if (nuseless_nonterminals > 0)
422 {
423
424 int i, n;
425/* short j; JF unused */
426 short *nontermmap;
427 rule r;
428
429 /*
430 * create a map of nonterminal number to new nonterminal
431 * number. -1 in the map means it was useless and is being
432 * eliminated.
433 */
434
435 nontermmap = NEW2(nvars, short) - ntokens;
436 for (i = ntokens; i < nsyms; i++)
437 nontermmap[i] = -1;
438
439 n = ntokens;
440 for (i = ntokens; i < nsyms; i++)
441 if (BITISSET(V, i))
442 nontermmap[i] = n++;
443
444 /* Shuffle elements of tables indexed by symbol number. */
445
446 for (i = ntokens; i < nsyms; i++)
447 {
448 n = nontermmap[i];
449 if (n >= 0)
450 {
451 sassoc[n] = sassoc[i];
452 sprec[n] = sprec[i];
453 tags[n] = tags[i];
454 } else {
455 free(tags[i]);
456 }
457 }
458
459 /* Replace all symbol numbers in valid data structures. */
460
461 for (i = 1; i <= nrules; i++)
462 {
463 /* Ignore the rules disabled above. */
464 if (rlhs[i] >= 0)
465 rlhs[i] = nontermmap[rlhs[i]];
466 if (ISVAR (rprecsym[i]))
467 /* Can this happen? */
468 rprecsym[i] = nontermmap[rprecsym[i]];
469 }
470
471 for (r = ritem; *r; r++)
472 if (ISVAR(*r))
473 *r = nontermmap[*r];
474
475 start_symbol = nontermmap[start_symbol];
476
477 nsyms -= nuseless_nonterminals;
478 nvars -= nuseless_nonterminals;
479
480 free(&nontermmap[ntokens]);
481 }
482}
483\f
a083fbbf 484static void
d2729d44 485print_results (void)
572909b5
RS
486{
487 int i;
488/* short j; JF unused */
489 rule r;
490 bool b;
491
492 if (nuseless_nonterminals > 0)
493 {
a083fbbf 494 fprintf(foutput, _("Useless nonterminals:\n\n"));
572909b5
RS
495 for (i = ntokens; i < nsyms; i++)
496 if (!BITISSET(V, i))
497 fprintf(foutput, " %s\n", tags[i]);
498 }
499 b = FALSE;
500 for (i = 0; i < ntokens; i++)
501 {
502 if (!BITISSET(V, i) && !BITISSET(V1, i))
503 {
504 if (!b)
505 {
a083fbbf 506 fprintf(foutput, _("\n\nTerminals which are not used:\n\n"));
572909b5
RS
507 b = TRUE;
508 }
509 fprintf(foutput, " %s\n", tags[i]);
510 }
511 }
512
513 if (nuseless_productions > 0)
514 {
a083fbbf 515 fprintf(foutput, _("\n\nUseless rules:\n\n"));
572909b5
RS
516 for (i = 1; i <= nrules; i++)
517 {
518 if (!BITISSET(P, i))
519 {
520 fprintf(foutput, "#%-4d ", i);
521 fprintf(foutput, "%s :\t", tags[rlhs[i]]);
522 for (r = &ritem[rrhs[i]]; *r >= 0; r++)
523 {
524 fprintf(foutput, " %s", tags[*r]);
525 }
526 fprintf(foutput, ";\n");
527 }
528 }
529 }
530 if (nuseless_nonterminals > 0 || nuseless_productions > 0 || b)
531 fprintf(foutput, "\n\n");
532}
533\f
4a120d45
JT
534#if 0 /* XXX currently unused. */
535static void
d2729d44 536dump_grammar (void)
572909b5
RS
537{
538 int i;
539 rule r;
540
541 fprintf(foutput,
542 "ntokens = %d, nvars = %d, nsyms = %d, nrules = %d, nitems = %d\n\n",
543 ntokens, nvars, nsyms, nrules, nitems);
a083fbbf
RS
544 fprintf(foutput, _("Variables\n---------\n\n"));
545 fprintf(foutput, _("Value Sprec Sassoc Tag\n"));
572909b5
RS
546 for (i = ntokens; i < nsyms; i++)
547 fprintf(foutput, "%5d %5d %5d %s\n",
548 i, sprec[i], sassoc[i], tags[i]);
549 fprintf(foutput, "\n\n");
a083fbbf 550 fprintf(foutput, _("Rules\n-----\n\n"));
572909b5
RS
551 for (i = 1; i <= nrules; i++)
552 {
a083fbbf 553 fprintf(foutput, "%-5d(%5d%5d)%5d : (@%-5d)",
572909b5
RS
554 i, rprec[i], rassoc[i], rlhs[i], rrhs[i]);
555 for (r = &ritem[rrhs[i]]; *r > 0; r++)
556 fprintf(foutput, "%5d", *r);
557 fprintf(foutput, " [%d]\n", -(*r));
558 }
559 fprintf(foutput, "\n\n");
a083fbbf 560 fprintf(foutput, _("Rules interpreted\n-----------------\n\n"));
572909b5
RS
561 for (i = 1; i <= nrules; i++)
562 {
563 fprintf(foutput, "%-5d %s :", i, tags[rlhs[i]]);
564 for (r = &ritem[rrhs[i]]; *r > 0; r++)
565 fprintf(foutput, " %s", tags[*r]);
566 fprintf(foutput, "\n");
567 }
568 fprintf(foutput, "\n\n");
569}
4a120d45 570#endif
572909b5
RS
571
572
a083fbbf 573static void
d2729d44 574print_notices (void)
572909b5 575{
572909b5 576 if (fixed_outfiles && nuseless_productions)
a083fbbf 577 fprintf(stderr, _("%d rules never reduced\n"), nuseless_productions);
572909b5 578
a083fbbf 579 fprintf(stderr, _("%s contains "), infile);
572909b5
RS
580
581 if (nuseless_nonterminals > 0)
582 {
a083fbbf 583 fprintf(stderr, _("%d useless nonterminal%s"),
572909b5
RS
584 nuseless_nonterminals,
585 (nuseless_nonterminals == 1 ? "" : "s"));
586 }
587 if (nuseless_nonterminals > 0 && nuseless_productions > 0)
a083fbbf 588 fprintf(stderr, _(" and "));
572909b5
RS
589
590 if (nuseless_productions > 0)
591 {
a083fbbf 592 fprintf(stderr, _("%d useless rule%s"),
572909b5
RS
593 nuseless_productions,
594 (nuseless_productions == 1 ? "" : "s"));
595 }
d129c2c0 596 fprintf(stderr, "\n");
572909b5
RS
597 fflush(stderr);
598}