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