]> git.saurik.com Git - bison.git/blame - src/conflicts.c
* Makefile.maint, GNUmakefile: New, from Autoconf 2.52.
[bison.git] / src / conflicts.c
CommitLineData
08089d5d 1/* Find and resolve or report look-ahead conflicts for bison,
22c821f3 2 Copyright 1984, 1989, 1992, 2000, 2001 Free Software Foundation, Inc.
08089d5d 3
ceed8467 4 This file is part of Bison, the GNU Compiler Compiler.
08089d5d 5
ceed8467
AD
6 Bison is free software; you can redistribute it and/or modify it
7 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.
08089d5d 10
ceed8467
AD
11 Bison is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
08089d5d 15
ceed8467
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 the Free
18 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 02111-1307, USA. */
08089d5d 20
08089d5d 21#include "system.h"
ceed8467 22#include "getargs.h"
d7913476 23#include "xalloc.h"
08089d5d
DM
24#include "files.h"
25#include "gram.h"
26#include "state.h"
720d742f 27#include "lalr.h"
0619caf0 28#include "conflicts.h"
b2ca4022
AD
29#include "reader.h"
30#include "LR0.h"
d2729d44 31
0619caf0 32int any_conflicts = 0;
08089d5d
DM
33errs **err_table;
34int expected_conflicts;
4a120d45 35static char *conflicts;
08089d5d 36
08089d5d
DM
37static unsigned *shiftset;
38static unsigned *lookaheadset;
39static int src_total;
40static int rrc_total;
41static int src_count;
42static int rrc_count;
c29240e7 43\f
08089d5d 44
c29240e7
AD
45static inline void
46log_resolution (int state, int LAno, int token, char *resolution)
08089d5d 47{
ff4423cc
AD
48 obstack_fgrow4 (&output_obstack,
49 _("\
c29240e7 50Conflict in state %d between rule %d and token %s resolved as %s.\n"),
ff4423cc 51 state, LAruleno[LAno], tags[token], resolution);
08089d5d
DM
52}
53
54
c29240e7
AD
55/*------------------------------------------------------------------.
56| Turn off the shift recorded for the specified token in the |
57| specified state. Used when we resolve a shift-reduce conflict in |
58| favor of the reduction. |
59`------------------------------------------------------------------*/
60
4a120d45 61static void
c29240e7 62flush_shift (int state, int token)
08089d5d 63{
c29240e7
AD
64 shifts *shiftp;
65 int k, i;
08089d5d
DM
66
67 shiftp = shift_table[state];
c29240e7 68
08089d5d
DM
69 if (shiftp)
70 {
71 k = shiftp->nshifts;
72 for (i = 0; i < k; i++)
73 {
c29240e7
AD
74 if (shiftp->shifts[i]
75 && token == accessing_symbol[shiftp->shifts[i]])
76 (shiftp->shifts[i]) = 0;
08089d5d 77 }
08089d5d
DM
78 }
79}
80
81
c29240e7
AD
82/*------------------------------------------------------------------.
83| Attempt to resolve shift-reduce conflict for one rule by means of |
84| precedence declarations. It has already been checked that the |
85| rule has a precedence. A conflict is resolved by modifying the |
86| shift or reduce tables so that there is no longer a conflict. |
87`------------------------------------------------------------------*/
08089d5d 88
4a120d45 89static void
d2729d44 90resolve_sr_conflict (int state, int lookaheadnum)
08089d5d 91{
c29240e7
AD
92 int i;
93 int mask;
94 unsigned *fp1;
95 unsigned *fp2;
96 int redprec;
d7913476 97 errs *errp = (errs *) xcalloc (sizeof (errs) + ntokens * sizeof (short), 1);
08089d5d
DM
98 short *errtokens = errp->errs;
99
100 /* find the rule to reduce by to get precedence of reduction */
101 redprec = rprec[LAruleno[lookaheadnum]];
102
103 mask = 1;
104 fp1 = LA + lookaheadnum * tokensetsize;
105 fp2 = lookaheadset;
106 for (i = 0; i < ntokens; i++)
107 {
108 if ((mask & *fp2 & *fp1) && sprec[i])
109 /* Shift-reduce conflict occurs for token number i
110 and it has a precedence.
111 The precedence of shifting is that of token i. */
112 {
113 if (sprec[i] < redprec)
114 {
c29240e7
AD
115 log_resolution (state, lookaheadnum, i, _("reduce"));
116 *fp2 &= ~mask; /* flush the shift for this token */
117 flush_shift (state, i);
08089d5d
DM
118 }
119 else if (sprec[i] > redprec)
120 {
c29240e7
AD
121 log_resolution (state, lookaheadnum, i, _("shift"));
122 *fp1 &= ~mask; /* flush the reduce for this token */
08089d5d
DM
123 }
124 else
125 {
126 /* Matching precedence levels.
c29240e7
AD
127 For left association, keep only the reduction.
128 For right association, keep only the shift.
129 For nonassociation, keep neither. */
08089d5d
DM
130
131 switch (sassoc[i])
132 {
d7020c20 133 case right_assoc:
c29240e7 134 log_resolution (state, lookaheadnum, i, _("shift"));
08089d5d
DM
135 break;
136
d7020c20 137 case left_assoc:
c29240e7 138 log_resolution (state, lookaheadnum, i, _("reduce"));
08089d5d
DM
139 break;
140
d7020c20 141 case non_assoc:
c29240e7 142 log_resolution (state, lookaheadnum, i, _("an error"));
08089d5d
DM
143 break;
144 }
145
d7020c20 146 if (sassoc[i] != right_assoc)
08089d5d 147 {
c29240e7
AD
148 *fp2 &= ~mask; /* flush the shift for this token */
149 flush_shift (state, i);
08089d5d 150 }
d7020c20 151 if (sassoc[i] != left_assoc)
c29240e7
AD
152 {
153 *fp1 &= ~mask; /* flush the reduce for this token */
08089d5d 154 }
d7020c20 155 if (sassoc[i] == non_assoc)
08089d5d
DM
156 {
157 /* Record an explicit error for this token. */
158 *errtokens++ = i;
159 }
160 }
161 }
162
163 mask <<= 1;
164 if (mask == 0)
165 {
166 mask = 1;
c29240e7
AD
167 fp2++;
168 fp1++;
08089d5d
DM
169 }
170 }
171 errp->nerrs = errtokens - errp->errs;
172 if (errp->nerrs)
173 {
174 /* Some tokens have been explicitly made errors. Allocate
c29240e7 175 a permanent errs structure for this state, to record them. */
08089d5d 176 i = (char *) errtokens - (char *) errp;
d7913476 177 err_table[state] = (errs *) xcalloc ((unsigned int) i, 1);
08089d5d
DM
178 bcopy (errp, err_table[state], i);
179 }
180 else
181 err_table[state] = 0;
c29240e7 182 free (errp);
08089d5d
DM
183}
184
185
4a120d45 186static void
c29240e7 187set_conflicts (int state)
08089d5d 188{
c29240e7
AD
189 int i;
190 int k;
191 shifts *shiftp;
192 unsigned *fp2;
193 unsigned *fp3;
194 unsigned *fp4;
195 unsigned *fp1;
196 int symbol;
197
198 if (consistent[state])
199 return;
08089d5d 200
c29240e7
AD
201 for (i = 0; i < tokensetsize; i++)
202 lookaheadset[i] = 0;
08089d5d 203
c29240e7 204 shiftp = shift_table[state];
08089d5d
DM
205 if (shiftp)
206 {
207 k = shiftp->nshifts;
208 for (i = 0; i < k; i++)
209 {
c29240e7
AD
210 symbol = accessing_symbol[shiftp->shifts[i]];
211 if (ISVAR (symbol))
212 break;
213 SETBIT (lookaheadset, symbol);
08089d5d
DM
214 }
215 }
08089d5d 216
c29240e7
AD
217 k = lookaheads[state + 1];
218 fp4 = lookaheadset + tokensetsize;
08089d5d 219
c29240e7
AD
220 /* Loop over all rules which require lookahead in this state. First
221 check for shift-reduce conflict, and try to resolve using
222 precedence */
223 for (i = lookaheads[state]; i < k; i++)
224 if (rprec[LAruleno[i]])
225 {
226 fp1 = LA + i * tokensetsize;
227 fp2 = fp1;
228 fp3 = lookaheadset;
08089d5d 229
c29240e7
AD
230 while (fp3 < fp4)
231 {
232 if (*fp2++ & *fp3++)
233 {
234 resolve_sr_conflict (state, i);
235 break;
236 }
237 }
238 }
08089d5d 239
08089d5d 240
c29240e7
AD
241 /* Loop over all rules which require lookahead in this state. Check
242 for conflicts not resolved above. */
243 for (i = lookaheads[state]; i < k; i++)
08089d5d 244 {
c29240e7
AD
245 fp1 = LA + i * tokensetsize;
246 fp2 = fp1;
247 fp3 = lookaheadset;
248
249 while (fp3 < fp4)
08089d5d 250 {
c29240e7
AD
251 if (*fp2++ & *fp3++)
252 {
253 conflicts[state] = 1;
254 any_conflicts = 1;
255 }
08089d5d 256 }
08089d5d 257
c29240e7
AD
258 fp2 = fp1;
259 fp3 = lookaheadset;
ceed8467 260
c29240e7
AD
261 while (fp3 < fp4)
262 *fp3++ |= *fp2++;
263 }
264}
08089d5d
DM
265
266void
c29240e7 267initialize_conflicts (void)
08089d5d 268{
c29240e7 269 int i;
08089d5d 270
d7913476
AD
271 conflicts = XCALLOC (char, nstates);
272 shiftset = XCALLOC (unsigned, tokensetsize);
273 lookaheadset = XCALLOC (unsigned, tokensetsize);
08089d5d 274
d7913476 275 err_table = XCALLOC (errs *, nstates);
08089d5d 276
c29240e7 277 any_conflicts = 0;
08089d5d 278
c29240e7
AD
279 for (i = 0; i < nstates; i++)
280 set_conflicts (i);
08089d5d
DM
281}
282
283
c29240e7
AD
284/*---------------------------------------------.
285| Count the number of shift/reduce conflicts. |
286`---------------------------------------------*/
287
4a120d45 288static void
d2729d44 289count_sr_conflicts (int state)
08089d5d 290{
c29240e7
AD
291 int i;
292 int k;
293 int mask;
294 shifts *shiftp;
295 unsigned *fp1;
296 unsigned *fp2;
297 unsigned *fp3;
298 int symbol;
08089d5d
DM
299
300 src_count = 0;
301
302 shiftp = shift_table[state];
c29240e7
AD
303 if (!shiftp)
304 return;
08089d5d
DM
305
306 for (i = 0; i < tokensetsize; i++)
307 {
308 shiftset[i] = 0;
309 lookaheadset[i] = 0;
310 }
311
312 k = shiftp->nshifts;
313 for (i = 0; i < k; i++)
314 {
c29240e7
AD
315 if (!shiftp->shifts[i])
316 continue;
08089d5d 317 symbol = accessing_symbol[shiftp->shifts[i]];
c29240e7
AD
318 if (ISVAR (symbol))
319 break;
320 SETBIT (shiftset, symbol);
08089d5d
DM
321 }
322
323 k = lookaheads[state + 1];
324 fp3 = lookaheadset + tokensetsize;
325
326 for (i = lookaheads[state]; i < k; i++)
327 {
328 fp1 = LA + i * tokensetsize;
329 fp2 = lookaheadset;
330
331 while (fp2 < fp3)
332 *fp2++ |= *fp1++;
333 }
334
335 fp1 = shiftset;
336 fp2 = lookaheadset;
337
338 while (fp2 < fp3)
339 *fp2++ &= *fp1++;
340
341 mask = 1;
342 fp2 = lookaheadset;
343 for (i = 0; i < ntokens; i++)
344 {
345 if (mask & *fp2)
346 src_count++;
347
348 mask <<= 1;
349 if (mask == 0)
350 {
351 mask = 1;
352 fp2++;
353 }
354 }
355}
356
357
c29240e7
AD
358/*----------------------------------------------.
359| Count the number of reduce/reduce conflicts. |
360`----------------------------------------------*/
361
4a120d45 362static void
d2729d44 363count_rr_conflicts (int state)
08089d5d 364{
c29240e7
AD
365 int i;
366 int j;
367 int count;
368 unsigned mask;
369 unsigned *baseword;
370 unsigned *wordp;
371 int m;
372 int n;
08089d5d
DM
373
374 rrc_count = 0;
375
376 m = lookaheads[state];
377 n = lookaheads[state + 1];
378
c29240e7
AD
379 if (n - m < 2)
380 return;
08089d5d
DM
381
382 mask = 1;
383 baseword = LA + m * tokensetsize;
384 for (i = 0; i < ntokens; i++)
385 {
386 wordp = baseword;
387
388 count = 0;
389 for (j = m; j < n; j++)
390 {
391 if (mask & *wordp)
392 count++;
393
394 wordp += tokensetsize;
395 }
396
c29240e7
AD
397 if (count >= 2)
398 rrc_count++;
08089d5d
DM
399
400 mask <<= 1;
401 if (mask == 0)
402 {
403 mask = 1;
404 baseword++;
405 }
406 }
407}
408
ff4423cc
AD
409/*--------------------------------------------------------------.
410| Return a human readable string which reports shift/reduce and |
411| reduce/reduce conflict numbers (SRC_NUM, RRC_NUM). |
412`--------------------------------------------------------------*/
c29240e7 413
ff4423cc
AD
414static const char *
415conflict_report (int src_num, int rrc_num)
c29240e7 416{
ff4423cc
AD
417 static char res[4096];
418 char *cp = res;
419
0619caf0 420 if (src_num == 1)
22c821f3
AD
421 {
422 sprintf (cp, _(" 1 shift/reduce conflict"));
423 cp += strlen (cp);
424 }
0619caf0 425 else if (src_num > 1)
22c821f3
AD
426 {
427 sprintf (cp, _(" %d shift/reduce conflicts"), src_num);
428 cp += strlen (cp);
429 }
c29240e7 430
0619caf0 431 if (src_num > 0 && rrc_num > 0)
22c821f3
AD
432 {
433 sprintf (cp, _(" and"));
434 cp += strlen (cp);
435 }
c29240e7 436
0619caf0 437 if (rrc_num == 1)
22c821f3
AD
438 {
439 sprintf (cp, _(" 1 reduce/reduce conflict"));
440 cp += strlen (cp);
441 }
0619caf0 442 else if (rrc_num > 1)
22c821f3
AD
443 {
444 sprintf (cp, _(" %d reduce/reduce conflicts"), rrc_num);
445 cp += strlen (cp);
446 }
ff4423cc
AD
447
448 *cp++ = '.';
449 *cp++ = '\n';
450 *cp++ = '\0';
c29240e7 451
ff4423cc 452 return res;
c29240e7
AD
453}
454
455
456/*---------------------------------------------.
457| Compute and give a report on the conflicts. |
458`---------------------------------------------*/
459
460void
0619caf0 461print_conflicts (void)
c29240e7
AD
462{
463 int i;
464
465 src_total = 0;
466 rrc_total = 0;
467
0619caf0
AD
468 /* Count the total number of conflicts, and if wanted, give a
469 detailed report in FOUTPUT. */
c29240e7
AD
470 for (i = 0; i < nstates; i++)
471 {
472 if (conflicts[i])
473 {
474 count_sr_conflicts (i);
475 count_rr_conflicts (i);
476 src_total += src_count;
477 rrc_total += rrc_count;
0619caf0 478
89cab50d 479 if (verbose_flag)
0619caf0 480 {
ff4423cc
AD
481 obstack_fgrow1 (&output_obstack, _("State %d contains"), i);
482 obstack_sgrow (&output_obstack,
483 conflict_report (src_count, rrc_count));
0619caf0 484 }
c29240e7
AD
485 }
486 }
487
0619caf0 488 /* Report the total number of conflicts on STDERR. */
89cab50d 489 if (yacc_flag)
c29240e7 490 {
0619caf0
AD
491 /* If invoked with `--yacc', use the output format specified by
492 POSIX. */
493 fprintf (stderr, _("conflicts: "));
494 if (src_total > 0)
495 fprintf (stderr, _(" %d shift/reduce"), src_total);
496 if (src_total > 0 && rrc_total > 0)
497 fprintf (stderr, ",");
498 if (rrc_total > 0)
499 fprintf (stderr, _(" %d reduce/reduce"), rrc_total);
500 putc ('\n', stderr);
501 }
502 else
503 {
504 fprintf (stderr, _("%s contains"), infile);
ff4423cc 505 fputs (conflict_report (src_total, rrc_total), stderr);
c29240e7 506 }
c29240e7
AD
507}
508
509
08089d5d 510void
d2729d44 511print_reductions (int state)
08089d5d 512{
c29240e7
AD
513 int i;
514 int j;
515 int k;
516 unsigned *fp1;
517 unsigned *fp2;
518 unsigned *fp3;
519 unsigned *fp4;
520 int rule;
521 int symbol;
522 unsigned mask;
523 int m;
524 int n;
525 int default_LA;
526 int default_rule = 0;
527 int cmax;
528 int count;
529 shifts *shiftp;
530 errs *errp;
08089d5d
DM
531 int nodefault = 0;
532
533 for (i = 0; i < tokensetsize; i++)
534 shiftset[i] = 0;
535
536 shiftp = shift_table[state];
537 if (shiftp)
538 {
539 k = shiftp->nshifts;
540 for (i = 0; i < k; i++)
541 {
c29240e7
AD
542 if (!shiftp->shifts[i])
543 continue;
08089d5d 544 symbol = accessing_symbol[shiftp->shifts[i]];
c29240e7
AD
545 if (ISVAR (symbol))
546 break;
08089d5d
DM
547 /* if this state has a shift for the error token,
548 don't use a default rule. */
c29240e7
AD
549 if (symbol == error_token_number)
550 nodefault = 1;
551 SETBIT (shiftset, symbol);
08089d5d
DM
552 }
553 }
554
555 errp = err_table[state];
556 if (errp)
557 {
558 k = errp->nerrs;
559 for (i = 0; i < k; i++)
560 {
c29240e7
AD
561 if (!errp->errs[i])
562 continue;
08089d5d 563 symbol = errp->errs[i];
c29240e7 564 SETBIT (shiftset, symbol);
08089d5d
DM
565 }
566 }
567
568 m = lookaheads[state];
569 n = lookaheads[state + 1];
570
c29240e7 571 if (n - m == 1 && !nodefault)
08089d5d
DM
572 {
573 default_rule = LAruleno[m];
574
575 fp1 = LA + m * tokensetsize;
576 fp2 = shiftset;
577 fp3 = lookaheadset;
578 fp4 = lookaheadset + tokensetsize;
579
580 while (fp3 < fp4)
581 *fp3++ = *fp1++ & *fp2++;
582
583 mask = 1;
584 fp3 = lookaheadset;
585
586 for (i = 0; i < ntokens; i++)
587 {
588 if (mask & *fp3)
ff4423cc
AD
589 obstack_fgrow3 (&output_obstack,
590 _(" %-4s\t[reduce using rule %d (%s)]\n"),
591 tags[i], default_rule, tags[rlhs[default_rule]]);
08089d5d
DM
592
593 mask <<= 1;
594 if (mask == 0)
595 {
596 mask = 1;
597 fp3++;
598 }
599 }
600
ff4423cc
AD
601 obstack_fgrow2 (&output_obstack,
602 _(" $default\treduce using rule %d (%s)\n\n"),
603 default_rule, tags[rlhs[default_rule]]);
08089d5d
DM
604 }
605 else if (n - m >= 1)
606 {
607 cmax = 0;
608 default_LA = -1;
609 fp4 = lookaheadset + tokensetsize;
610
c29240e7 611 if (!nodefault)
08089d5d
DM
612 for (i = m; i < n; i++)
613 {
614 fp1 = LA + i * tokensetsize;
615 fp2 = shiftset;
616 fp3 = lookaheadset;
ceed8467 617
08089d5d 618 while (fp3 < fp4)
b65534e5 619 *fp3++ = *fp1++ & (~(*fp2++));
ceed8467 620
08089d5d
DM
621 count = 0;
622 mask = 1;
623 fp3 = lookaheadset;
624 for (j = 0; j < ntokens; j++)
625 {
626 if (mask & *fp3)
627 count++;
ceed8467 628
08089d5d
DM
629 mask <<= 1;
630 if (mask == 0)
631 {
632 mask = 1;
633 fp3++;
634 }
635 }
ceed8467 636
08089d5d
DM
637 if (count > cmax)
638 {
639 cmax = count;
640 default_LA = i;
641 default_rule = LAruleno[i];
642 }
ceed8467 643
08089d5d
DM
644 fp2 = shiftset;
645 fp3 = lookaheadset;
ceed8467 646
08089d5d
DM
647 while (fp3 < fp4)
648 *fp2++ |= *fp3++;
649 }
650
651 for (i = 0; i < tokensetsize; i++)
c29240e7 652 shiftset[i] = 0;
08089d5d
DM
653
654 if (shiftp)
c29240e7
AD
655 {
656 k = shiftp->nshifts;
657 for (i = 0; i < k; i++)
08089d5d 658 {
c29240e7
AD
659 if (!shiftp->shifts[i])
660 continue;
08089d5d 661 symbol = accessing_symbol[shiftp->shifts[i]];
c29240e7
AD
662 if (ISVAR (symbol))
663 break;
664 SETBIT (shiftset, symbol);
08089d5d 665 }
c29240e7 666 }
08089d5d
DM
667
668 mask = 1;
669 fp1 = LA + m * tokensetsize;
670 fp2 = shiftset;
671 for (i = 0; i < ntokens; i++)
672 {
673 int defaulted = 0;
674
675 if (mask & *fp2)
676 count = 1;
677 else
678 count = 0;
679
680 fp3 = fp1;
681 for (j = m; j < n; j++)
682 {
683 if (mask & *fp3)
684 {
685 if (count == 0)
686 {
687 if (j != default_LA)
688 {
689 rule = LAruleno[j];
ff4423cc 690 obstack_fgrow3 (&output_obstack,
c29240e7
AD
691 _(" %-4s\treduce using rule %d (%s)\n"),
692 tags[i], rule, tags[rlhs[rule]]);
08089d5d 693 }
c29240e7
AD
694 else
695 defaulted = 1;
08089d5d
DM
696
697 count++;
698 }
699 else
700 {
701 if (defaulted)
702 {
703 rule = LAruleno[default_LA];
ff4423cc 704 obstack_fgrow3 (&output_obstack,
c29240e7
AD
705 _(" %-4s\treduce using rule %d (%s)\n"),
706 tags[i], rule, tags[rlhs[rule]]);
08089d5d
DM
707 defaulted = 0;
708 }
709 rule = LAruleno[j];
ff4423cc 710 obstack_fgrow3 (&output_obstack,
c29240e7
AD
711 _(" %-4s\t[reduce using rule %d (%s)]\n"),
712 tags[i], rule, tags[rlhs[rule]]);
08089d5d
DM
713 }
714 }
715
716 fp3 += tokensetsize;
717 }
718
719 mask <<= 1;
720 if (mask == 0)
721 {
722 mask = 1;
808e2021 723 /* We tried incrementing just fp1, and just fp2; both seem wrong.
c29240e7 724 It seems necessary to increment both in sync. */
808e2021 725 fp1++;
08089d5d
DM
726 fp2++;
727 }
728 }
729
730 if (default_LA >= 0)
ff4423cc
AD
731 obstack_fgrow2 (&output_obstack,
732 _(" $default\treduce using rule %d (%s)\n"),
733 default_rule, tags[rlhs[default_rule]]);
08089d5d 734
ff4423cc 735 obstack_1grow (&output_obstack, '\n');
08089d5d
DM
736 }
737}
738
739
740void
d2729d44 741finalize_conflicts (void)
08089d5d 742{
d7913476
AD
743 XFREE (conflicts);
744 XFREE (shiftset);
745 XFREE (lookaheadset);
08089d5d 746}