]> git.saurik.com Git - bison.git/blob - src/conflicts.c
Get rid of the ad hoc handling of token_buffer in the scanner: use
[bison.git] / src / conflicts.c
1 /* Find and resolve or report look-ahead conflicts for bison,
2 Copyright 1984, 1989, 1992, 2000 Free Software Foundation, Inc.
3
4 This file is part of Bison, the GNU Compiler Compiler.
5
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.
10
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.
15
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. */
20
21 #include "system.h"
22 #include "getargs.h"
23 #include "xalloc.h"
24 #include "files.h"
25 #include "gram.h"
26 #include "state.h"
27 #include "lalr.h"
28 #include "conflicts.h"
29 #include "reader.h"
30 #include "LR0.h"
31
32 int any_conflicts = 0;
33 errs **err_table;
34 int expected_conflicts;
35 static char *conflicts;
36
37 static unsigned *shiftset;
38 static unsigned *lookaheadset;
39 static int src_total;
40 static int rrc_total;
41 static int src_count;
42 static int rrc_count;
43 \f
44
45 static inline void
46 log_resolution (int state, int LAno, int token, char *resolution)
47 {
48 obstack_fgrow4 (&output_obstack,
49 _("\
50 Conflict in state %d between rule %d and token %s resolved as %s.\n"),
51 state, LAruleno[LAno], tags[token], resolution);
52 }
53
54
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
61 static void
62 flush_shift (int state, int token)
63 {
64 shifts *shiftp;
65 int k, i;
66
67 shiftp = shift_table[state];
68
69 if (shiftp)
70 {
71 k = shiftp->nshifts;
72 for (i = 0; i < k; i++)
73 {
74 if (shiftp->shifts[i]
75 && token == accessing_symbol[shiftp->shifts[i]])
76 (shiftp->shifts[i]) = 0;
77 }
78 }
79 }
80
81
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 `------------------------------------------------------------------*/
88
89 static void
90 resolve_sr_conflict (int state, int lookaheadnum)
91 {
92 int i;
93 int mask;
94 unsigned *fp1;
95 unsigned *fp2;
96 int redprec;
97 errs *errp = (errs *) xcalloc (sizeof (errs) + ntokens * sizeof (short), 1);
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 {
115 log_resolution (state, lookaheadnum, i, _("reduce"));
116 *fp2 &= ~mask; /* flush the shift for this token */
117 flush_shift (state, i);
118 }
119 else if (sprec[i] > redprec)
120 {
121 log_resolution (state, lookaheadnum, i, _("shift"));
122 *fp1 &= ~mask; /* flush the reduce for this token */
123 }
124 else
125 {
126 /* Matching precedence levels.
127 For left association, keep only the reduction.
128 For right association, keep only the shift.
129 For nonassociation, keep neither. */
130
131 switch (sassoc[i])
132 {
133 case right_assoc:
134 log_resolution (state, lookaheadnum, i, _("shift"));
135 break;
136
137 case left_assoc:
138 log_resolution (state, lookaheadnum, i, _("reduce"));
139 break;
140
141 case non_assoc:
142 log_resolution (state, lookaheadnum, i, _("an error"));
143 break;
144 }
145
146 if (sassoc[i] != right_assoc)
147 {
148 *fp2 &= ~mask; /* flush the shift for this token */
149 flush_shift (state, i);
150 }
151 if (sassoc[i] != left_assoc)
152 {
153 *fp1 &= ~mask; /* flush the reduce for this token */
154 }
155 if (sassoc[i] == non_assoc)
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;
167 fp2++;
168 fp1++;
169 }
170 }
171 errp->nerrs = errtokens - errp->errs;
172 if (errp->nerrs)
173 {
174 /* Some tokens have been explicitly made errors. Allocate
175 a permanent errs structure for this state, to record them. */
176 i = (char *) errtokens - (char *) errp;
177 err_table[state] = (errs *) xcalloc ((unsigned int) i, 1);
178 bcopy (errp, err_table[state], i);
179 }
180 else
181 err_table[state] = 0;
182 free (errp);
183 }
184
185
186 static void
187 set_conflicts (int state)
188 {
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;
200
201 for (i = 0; i < tokensetsize; i++)
202 lookaheadset[i] = 0;
203
204 shiftp = shift_table[state];
205 if (shiftp)
206 {
207 k = shiftp->nshifts;
208 for (i = 0; i < k; i++)
209 {
210 symbol = accessing_symbol[shiftp->shifts[i]];
211 if (ISVAR (symbol))
212 break;
213 SETBIT (lookaheadset, symbol);
214 }
215 }
216
217 k = lookaheads[state + 1];
218 fp4 = lookaheadset + tokensetsize;
219
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;
229
230 while (fp3 < fp4)
231 {
232 if (*fp2++ & *fp3++)
233 {
234 resolve_sr_conflict (state, i);
235 break;
236 }
237 }
238 }
239
240
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++)
244 {
245 fp1 = LA + i * tokensetsize;
246 fp2 = fp1;
247 fp3 = lookaheadset;
248
249 while (fp3 < fp4)
250 {
251 if (*fp2++ & *fp3++)
252 {
253 conflicts[state] = 1;
254 any_conflicts = 1;
255 }
256 }
257
258 fp2 = fp1;
259 fp3 = lookaheadset;
260
261 while (fp3 < fp4)
262 *fp3++ |= *fp2++;
263 }
264 }
265
266 void
267 initialize_conflicts (void)
268 {
269 int i;
270
271 conflicts = XCALLOC (char, nstates);
272 shiftset = XCALLOC (unsigned, tokensetsize);
273 lookaheadset = XCALLOC (unsigned, tokensetsize);
274
275 err_table = XCALLOC (errs *, nstates);
276
277 any_conflicts = 0;
278
279 for (i = 0; i < nstates; i++)
280 set_conflicts (i);
281 }
282
283
284 /*---------------------------------------------.
285 | Count the number of shift/reduce conflicts. |
286 `---------------------------------------------*/
287
288 static void
289 count_sr_conflicts (int state)
290 {
291 int i;
292 int k;
293 int mask;
294 shifts *shiftp;
295 unsigned *fp1;
296 unsigned *fp2;
297 unsigned *fp3;
298 int symbol;
299
300 src_count = 0;
301
302 shiftp = shift_table[state];
303 if (!shiftp)
304 return;
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 {
315 if (!shiftp->shifts[i])
316 continue;
317 symbol = accessing_symbol[shiftp->shifts[i]];
318 if (ISVAR (symbol))
319 break;
320 SETBIT (shiftset, symbol);
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
358 /*----------------------------------------------.
359 | Count the number of reduce/reduce conflicts. |
360 `----------------------------------------------*/
361
362 static void
363 count_rr_conflicts (int state)
364 {
365 int i;
366 int j;
367 int count;
368 unsigned mask;
369 unsigned *baseword;
370 unsigned *wordp;
371 int m;
372 int n;
373
374 rrc_count = 0;
375
376 m = lookaheads[state];
377 n = lookaheads[state + 1];
378
379 if (n - m < 2)
380 return;
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
397 if (count >= 2)
398 rrc_count++;
399
400 mask <<= 1;
401 if (mask == 0)
402 {
403 mask = 1;
404 baseword++;
405 }
406 }
407 }
408
409 /*--------------------------------------------------------------.
410 | Return a human readable string which reports shift/reduce and |
411 | reduce/reduce conflict numbers (SRC_NUM, RRC_NUM). |
412 `--------------------------------------------------------------*/
413
414 static const char *
415 conflict_report (int src_num, int rrc_num)
416 {
417 static char res[4096];
418 char *cp = res;
419
420 if (src_num == 1)
421 sprintf (cp, _(" 1 shift/reduce conflict"));
422 else if (src_num > 1)
423 sprintf (cp, _(" %d shift/reduce conflicts"), src_num);
424 cp += strlen (cp);
425
426 if (src_num > 0 && rrc_num > 0)
427 sprintf (cp, _(" and"));
428 cp += strlen (cp);
429
430 if (rrc_num == 1)
431 sprintf (cp, _(" 1 reduce/reduce conflict"));
432 else if (rrc_num > 1)
433 sprintf (cp, _(" %d reduce/reduce conflicts"), rrc_num);
434 cp += strlen (cp);
435
436 *cp++ = '.';
437 *cp++ = '\n';
438 *cp++ = '\0';
439
440 return res;
441 }
442
443
444 /*---------------------------------------------.
445 | Compute and give a report on the conflicts. |
446 `---------------------------------------------*/
447
448 void
449 print_conflicts (void)
450 {
451 int i;
452
453 src_total = 0;
454 rrc_total = 0;
455
456 /* Count the total number of conflicts, and if wanted, give a
457 detailed report in FOUTPUT. */
458 for (i = 0; i < nstates; i++)
459 {
460 if (conflicts[i])
461 {
462 count_sr_conflicts (i);
463 count_rr_conflicts (i);
464 src_total += src_count;
465 rrc_total += rrc_count;
466
467 if (verbose_flag)
468 {
469 obstack_fgrow1 (&output_obstack, _("State %d contains"), i);
470 obstack_sgrow (&output_obstack,
471 conflict_report (src_count, rrc_count));
472 }
473 }
474 }
475
476 /* Report the total number of conflicts on STDERR. */
477 if (yacc_flag)
478 {
479 /* If invoked with `--yacc', use the output format specified by
480 POSIX. */
481 fprintf (stderr, _("conflicts: "));
482 if (src_total > 0)
483 fprintf (stderr, _(" %d shift/reduce"), src_total);
484 if (src_total > 0 && rrc_total > 0)
485 fprintf (stderr, ",");
486 if (rrc_total > 0)
487 fprintf (stderr, _(" %d reduce/reduce"), rrc_total);
488 putc ('\n', stderr);
489 }
490 else
491 {
492 fprintf (stderr, _("%s contains"), infile);
493 fputs (conflict_report (src_total, rrc_total), stderr);
494 }
495 }
496
497
498 void
499 print_reductions (int state)
500 {
501 int i;
502 int j;
503 int k;
504 unsigned *fp1;
505 unsigned *fp2;
506 unsigned *fp3;
507 unsigned *fp4;
508 int rule;
509 int symbol;
510 unsigned mask;
511 int m;
512 int n;
513 int default_LA;
514 int default_rule = 0;
515 int cmax;
516 int count;
517 shifts *shiftp;
518 errs *errp;
519 int nodefault = 0;
520
521 for (i = 0; i < tokensetsize; i++)
522 shiftset[i] = 0;
523
524 shiftp = shift_table[state];
525 if (shiftp)
526 {
527 k = shiftp->nshifts;
528 for (i = 0; i < k; i++)
529 {
530 if (!shiftp->shifts[i])
531 continue;
532 symbol = accessing_symbol[shiftp->shifts[i]];
533 if (ISVAR (symbol))
534 break;
535 /* if this state has a shift for the error token,
536 don't use a default rule. */
537 if (symbol == error_token_number)
538 nodefault = 1;
539 SETBIT (shiftset, symbol);
540 }
541 }
542
543 errp = err_table[state];
544 if (errp)
545 {
546 k = errp->nerrs;
547 for (i = 0; i < k; i++)
548 {
549 if (!errp->errs[i])
550 continue;
551 symbol = errp->errs[i];
552 SETBIT (shiftset, symbol);
553 }
554 }
555
556 m = lookaheads[state];
557 n = lookaheads[state + 1];
558
559 if (n - m == 1 && !nodefault)
560 {
561 default_rule = LAruleno[m];
562
563 fp1 = LA + m * tokensetsize;
564 fp2 = shiftset;
565 fp3 = lookaheadset;
566 fp4 = lookaheadset + tokensetsize;
567
568 while (fp3 < fp4)
569 *fp3++ = *fp1++ & *fp2++;
570
571 mask = 1;
572 fp3 = lookaheadset;
573
574 for (i = 0; i < ntokens; i++)
575 {
576 if (mask & *fp3)
577 obstack_fgrow3 (&output_obstack,
578 _(" %-4s\t[reduce using rule %d (%s)]\n"),
579 tags[i], default_rule, tags[rlhs[default_rule]]);
580
581 mask <<= 1;
582 if (mask == 0)
583 {
584 mask = 1;
585 fp3++;
586 }
587 }
588
589 obstack_fgrow2 (&output_obstack,
590 _(" $default\treduce using rule %d (%s)\n\n"),
591 default_rule, tags[rlhs[default_rule]]);
592 }
593 else if (n - m >= 1)
594 {
595 cmax = 0;
596 default_LA = -1;
597 fp4 = lookaheadset + tokensetsize;
598
599 if (!nodefault)
600 for (i = m; i < n; i++)
601 {
602 fp1 = LA + i * tokensetsize;
603 fp2 = shiftset;
604 fp3 = lookaheadset;
605
606 while (fp3 < fp4)
607 *fp3++ = *fp1++ & (~(*fp2++));
608
609 count = 0;
610 mask = 1;
611 fp3 = lookaheadset;
612 for (j = 0; j < ntokens; j++)
613 {
614 if (mask & *fp3)
615 count++;
616
617 mask <<= 1;
618 if (mask == 0)
619 {
620 mask = 1;
621 fp3++;
622 }
623 }
624
625 if (count > cmax)
626 {
627 cmax = count;
628 default_LA = i;
629 default_rule = LAruleno[i];
630 }
631
632 fp2 = shiftset;
633 fp3 = lookaheadset;
634
635 while (fp3 < fp4)
636 *fp2++ |= *fp3++;
637 }
638
639 for (i = 0; i < tokensetsize; i++)
640 shiftset[i] = 0;
641
642 if (shiftp)
643 {
644 k = shiftp->nshifts;
645 for (i = 0; i < k; i++)
646 {
647 if (!shiftp->shifts[i])
648 continue;
649 symbol = accessing_symbol[shiftp->shifts[i]];
650 if (ISVAR (symbol))
651 break;
652 SETBIT (shiftset, symbol);
653 }
654 }
655
656 mask = 1;
657 fp1 = LA + m * tokensetsize;
658 fp2 = shiftset;
659 for (i = 0; i < ntokens; i++)
660 {
661 int defaulted = 0;
662
663 if (mask & *fp2)
664 count = 1;
665 else
666 count = 0;
667
668 fp3 = fp1;
669 for (j = m; j < n; j++)
670 {
671 if (mask & *fp3)
672 {
673 if (count == 0)
674 {
675 if (j != default_LA)
676 {
677 rule = LAruleno[j];
678 obstack_fgrow3 (&output_obstack,
679 _(" %-4s\treduce using rule %d (%s)\n"),
680 tags[i], rule, tags[rlhs[rule]]);
681 }
682 else
683 defaulted = 1;
684
685 count++;
686 }
687 else
688 {
689 if (defaulted)
690 {
691 rule = LAruleno[default_LA];
692 obstack_fgrow3 (&output_obstack,
693 _(" %-4s\treduce using rule %d (%s)\n"),
694 tags[i], rule, tags[rlhs[rule]]);
695 defaulted = 0;
696 }
697 rule = LAruleno[j];
698 obstack_fgrow3 (&output_obstack,
699 _(" %-4s\t[reduce using rule %d (%s)]\n"),
700 tags[i], rule, tags[rlhs[rule]]);
701 }
702 }
703
704 fp3 += tokensetsize;
705 }
706
707 mask <<= 1;
708 if (mask == 0)
709 {
710 mask = 1;
711 /* We tried incrementing just fp1, and just fp2; both seem wrong.
712 It seems necessary to increment both in sync. */
713 fp1++;
714 fp2++;
715 }
716 }
717
718 if (default_LA >= 0)
719 obstack_fgrow2 (&output_obstack,
720 _(" $default\treduce using rule %d (%s)\n"),
721 default_rule, tags[rlhs[default_rule]]);
722
723 obstack_1grow (&output_obstack, '\n');
724 }
725 }
726
727
728 void
729 finalize_conflicts (void)
730 {
731 XFREE (conflicts);
732 XFREE (shiftset);
733 XFREE (lookaheadset);
734 }