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