]> git.saurik.com Git - bison.git/blob - src/conflicts.c
bb6f8ac7b849a900ef874a8dfeb2973300c6e1cc
[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 = state_table[state].shift_table;
64
65 if (shiftp)
66 {
67 k = shiftp->nshifts;
68 for (i = 0; i < k; i++)
69 {
70 if (shiftp->shifts[i]
71 && token == state_table[shiftp->shifts[i]].accessing_symbol)
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_ALLOC (ntokens + 1);
94 short *errtokens = errp->errs;
95
96 /* find the rule to reduce by to get precedence of reduction */
97 redprec = rule_table[LAruleno[lookaheadnum]].prec;
98
99 mask = 1;
100 fp1 = LA (lookaheadnum);
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_ALLOC (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, j;
186 shifts *shiftp;
187 int symbol;
188
189 if (state_table[state].consistent)
190 return;
191
192 for (i = 0; i < tokensetsize; i++)
193 lookaheadset[i] = 0;
194
195 shiftp = state_table[state].shift_table;
196 if (shiftp)
197 for (i = 0; i < shiftp->nshifts; i++)
198 {
199 symbol = state_table[shiftp->shifts[i]].accessing_symbol;
200 if (ISVAR (symbol))
201 break;
202 SETBIT (lookaheadset, symbol);
203 }
204
205 /* Loop over all rules which require lookahead in this state. First
206 check for shift-reduce conflict, and try to resolve using
207 precedence */
208 for (i = state_table[state].lookaheads;
209 i < state_table[state + 1].lookaheads;
210 ++i)
211 if (rule_table[LAruleno[i]].prec)
212 for (j = 0; j < tokensetsize; ++j)
213 if (LA (i)[j] & lookaheadset[j])
214 {
215 resolve_sr_conflict (state, i);
216 break;
217 }
218
219
220 /* Loop over all rules which require lookahead in this state. Check
221 for conflicts not resolved above. */
222 for (i = state_table[state].lookaheads;
223 i < state_table[state + 1].lookaheads;
224 ++i)
225 {
226 for (j = 0; j < tokensetsize; ++j)
227 if (LA (i)[j] & lookaheadset[j])
228 conflicts[state] = 1;
229
230 for (j = 0; j < tokensetsize; ++j)
231 lookaheadset[j] |= LA (i)[j];
232 }
233 }
234
235 void
236 solve_conflicts (void)
237 {
238 int i;
239
240 conflicts = XCALLOC (char, nstates);
241 shiftset = XCALLOC (unsigned, tokensetsize);
242 lookaheadset = XCALLOC (unsigned, tokensetsize);
243
244 err_table = XCALLOC (errs *, nstates);
245
246 for (i = 0; i < nstates; i++)
247 set_conflicts (i);
248 }
249
250
251 /*---------------------------------------------.
252 | Count the number of shift/reduce conflicts. |
253 `---------------------------------------------*/
254
255 static int
256 count_sr_conflicts (int state)
257 {
258 int i, k;
259 int src_count = 0;
260 shifts *shiftp = state_table[state].shift_table;
261
262 if (!shiftp)
263 return 0;
264
265 for (i = 0; i < tokensetsize; i++)
266 {
267 shiftset[i] = 0;
268 lookaheadset[i] = 0;
269 }
270
271 for (i = 0; i < shiftp->nshifts && SHIFT_IS_SHIFT (shiftp, i); i++)
272 {
273 int symbol = state_table[shiftp->shifts[i]].accessing_symbol;
274 SETBIT (shiftset, symbol);
275 }
276
277 for (i = state_table[state].lookaheads;
278 i < state_table[state + 1].lookaheads;
279 ++i)
280 for (k = 0; k < tokensetsize; ++k)
281 lookaheadset[k] |= LA (i)[k];
282
283 for (k = 0; k < tokensetsize; ++k)
284 lookaheadset[k] &= shiftset[k];
285
286 for (i = 0; i < ntokens; i++)
287 if (BITISSET (lookaheadset, i))
288 src_count++;
289
290 return src_count;
291 }
292
293
294 /*----------------------------------------------.
295 | Count the number of reduce/reduce conflicts. |
296 `----------------------------------------------*/
297
298 static int
299 count_rr_conflicts (int state)
300 {
301 int i;
302 int rrc_count = 0;
303
304 int m = state_table[state].lookaheads;
305 int n = state_table[state + 1].lookaheads;
306
307 if (n - m < 2)
308 return 0;
309
310 for (i = 0; i < ntokens; i++)
311 {
312 int count = 0;
313 int j;
314 for (j = m; j < n; j++)
315 if (BITISSET (LA (m), j))
316 count++;
317
318 if (count >= 2)
319 rrc_count++;
320 }
321
322 return rrc_count;
323 }
324
325 /*--------------------------------------------------------------.
326 | Return a human readable string which reports shift/reduce and |
327 | reduce/reduce conflict numbers (SRC_NUM, RRC_NUM). |
328 `--------------------------------------------------------------*/
329
330 static const char *
331 conflict_report (int src_num, int rrc_num)
332 {
333 static char res[4096];
334 char *cp = res;
335
336 if (src_num >= 1)
337 {
338 sprintf (cp, ngettext ("%d shift/reduce conflict",
339 "%d shift/reduce conflicts", src_num), src_num);
340 cp += strlen (cp);
341 }
342
343 if (src_num > 0 && rrc_num > 0)
344 {
345 sprintf (cp, " %s ", _("and"));
346 cp += strlen (cp);
347 }
348
349 if (rrc_num >= 1)
350 {
351 sprintf (cp, ngettext ("%d reduce/reduce conflict",
352 "%d reduce/reduce conflicts", rrc_num), rrc_num);
353 cp += strlen (cp);
354 }
355
356 *cp++ = '.';
357 *cp++ = '\n';
358 *cp++ = '\0';
359
360 return res;
361 }
362
363
364 /*-----------------------------------------------------------.
365 | Output the detailed description of states with conflicts. |
366 `-----------------------------------------------------------*/
367
368 void
369 conflicts_output (FILE *out)
370 {
371 bool printed_sth = FALSE;
372 int i;
373 for (i = 0; i < nstates; i++)
374 if (conflicts[i])
375 {
376 fprintf (out, _("State %d contains "), i);
377 fputs (conflict_report (count_sr_conflicts (i),
378 count_rr_conflicts (i)), out);
379 printed_sth = TRUE;
380 }
381 if (printed_sth)
382 fputs ("\n\n", out);
383 }
384
385
386 /*------------------------------------------.
387 | Reporting the total number of conflicts. |
388 `------------------------------------------*/
389
390 void
391 conflicts_print (void)
392 {
393 int i;
394
395 /* Is the number of SR conflicts OK? Either EXPECTED_CONFLICTS is
396 not set, and then we want 0 SR, or else it is specified, in which
397 case we want equality. */
398 int src_ok = 0;
399
400 int src_total = 0;
401 int rrc_total = 0;
402
403 /* Conflicts by state. */
404 for (i = 0; i < nstates; i++)
405 if (conflicts[i])
406 {
407 src_total += count_sr_conflicts (i);
408 rrc_total += count_rr_conflicts (i);
409 }
410
411 src_ok = src_total == (expected_conflicts == -1 ? 0 : expected_conflicts);
412
413 /* If there are no RR conflicts, and as many SR conflicts as
414 expected, then there is nothing to report. */
415 if (!rrc_total && src_ok)
416 return;
417
418 /* Report the total number of conflicts on STDERR. */
419 if (yacc_flag)
420 {
421 /* If invoked with `--yacc', use the output format specified by
422 POSIX. */
423 fprintf (stderr, _("conflicts: "));
424 if (src_total > 0)
425 fprintf (stderr, _(" %d shift/reduce"), src_total);
426 if (src_total > 0 && rrc_total > 0)
427 fprintf (stderr, ",");
428 if (rrc_total > 0)
429 fprintf (stderr, _(" %d reduce/reduce"), rrc_total);
430 putc ('\n', stderr);
431 }
432 else
433 {
434 fprintf (stderr, _("%s contains "), infile);
435 fputs (conflict_report (src_total, rrc_total), stderr);
436 }
437
438 if (expected_conflicts != -1 && !src_ok)
439 {
440 complain_message_count++;
441 fprintf (stderr, ngettext ("expected %d shift/reduce conflict\n",
442 "expected %d shift/reduce conflicts\n",
443 expected_conflicts),
444 expected_conflicts);
445 }
446 }
447
448
449 void
450 print_reductions (FILE *out, int state)
451 {
452 int i;
453 int j;
454 int symbol;
455 int m;
456 int n;
457 shifts *shiftp;
458 errs *errp;
459 int nodefault = 0;
460
461 for (i = 0; i < tokensetsize; i++)
462 shiftset[i] = 0;
463
464 shiftp = state_table[state].shift_table;
465 if (shiftp)
466 for (i = 0; i < shiftp->nshifts; i++)
467 {
468 if (!shiftp->shifts[i])
469 continue;
470 symbol = state_table[shiftp->shifts[i]].accessing_symbol;
471 if (ISVAR (symbol))
472 break;
473 /* if this state has a shift for the error token,
474 don't use a default rule. */
475 if (symbol == error_token_number)
476 nodefault = 1;
477 SETBIT (shiftset, symbol);
478 }
479
480 errp = err_table[state];
481 if (errp)
482 for (i = 0; i < errp->nerrs; i++)
483 if (errp->errs[i])
484 SETBIT (shiftset, errp->errs[i]);
485
486 m = state_table[state].lookaheads;
487 n = state_table[state + 1].lookaheads;
488
489 if (n - m == 1 && !nodefault)
490 {
491 int k;
492 int default_rule = LAruleno[m];
493
494 for (k = 0; k < tokensetsize; ++k)
495 lookaheadset[k] = LA (m)[k] & shiftset[k];
496
497 for (i = 0; i < ntokens; i++)
498 if (BITISSET (lookaheadset, i))
499 fprintf (out, _(" %-4s\t[reduce using rule %d (%s)]\n"),
500 tags[i], default_rule,
501 tags[rule_table[default_rule].lhs]);
502
503 fprintf (out, _(" $default\treduce using rule %d (%s)\n\n"),
504 default_rule, tags[rule_table[default_rule].lhs]);
505 }
506 else if (n - m >= 1)
507 {
508 int k;
509
510 int cmax = 0;
511 int default_LA = -1;
512 int default_rule = 0;
513
514 if (!nodefault)
515 for (i = m; i < n; i++)
516 {
517 int count = 0;
518
519 for (k = 0; k < tokensetsize; ++k)
520 lookaheadset[k] = LA (i)[k] & ~shiftset[k];
521
522 for (j = 0; j < ntokens; j++)
523 if (BITISSET (lookaheadset, j))
524 count++;
525
526 if (count > cmax)
527 {
528 cmax = count;
529 default_LA = i;
530 default_rule = LAruleno[i];
531 }
532
533 for (k = 0; k < tokensetsize; ++k)
534 shiftset[k] |= lookaheadset[k];
535 }
536
537 for (i = 0; i < tokensetsize; i++)
538 shiftset[i] = 0;
539
540 if (shiftp)
541 for (i = 0; i < shiftp->nshifts; i++)
542 {
543 if (!shiftp->shifts[i])
544 continue;
545 symbol = state_table[shiftp->shifts[i]].accessing_symbol;
546 if (ISVAR (symbol))
547 break;
548 SETBIT (shiftset, symbol);
549 }
550
551 for (i = 0; i < ntokens; i++)
552 {
553 int defaulted = 0;
554 int count = BITISSET (shiftset, i);
555
556 for (j = m; j < n; j++)
557 {
558 if (BITISSET (LA (m), j))
559 {
560 if (count == 0)
561 {
562 if (j != default_LA)
563 fprintf (out,
564 _(" %-4s\treduce using rule %d (%s)\n"),
565 tags[i],
566 LAruleno[j],
567 tags[rule_table[LAruleno[j]].lhs]);
568 else
569 defaulted = 1;
570
571 count++;
572 }
573 else
574 {
575 if (defaulted)
576 fprintf (out,
577 _(" %-4s\treduce using rule %d (%s)\n"),
578 tags[i],
579 LAruleno[default_LA],
580 tags[rule_table[LAruleno[default_LA]].lhs]);
581 defaulted = 0;
582 fprintf (out,
583 _(" %-4s\t[reduce using rule %d (%s)]\n"),
584 tags[i],
585 LAruleno[j],
586 tags[rule_table[LAruleno[j]].lhs]);
587 }
588 }
589 }
590 }
591
592 if (default_LA >= 0)
593 fprintf (out, _(" $default\treduce using rule %d (%s)\n"),
594 default_rule, tags[rule_table[default_rule].lhs]);
595 }
596 }
597
598
599 void
600 free_conflicts (void)
601 {
602 XFREE (conflicts);
603 XFREE (shiftset);
604 XFREE (lookaheadset);
605 }