]> git.saurik.com Git - bison.git/blame - src/conflicts.c
* src/reduce.c (reduce_grammar_tables): No longer disable the
[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"
7da99ede 22#include "complain.h"
ceed8467 23#include "getargs.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
342b8b6e 32errs **err_table = NULL;
7da99ede
AD
33/* -1 stands for not specified. */
34int expected_conflicts = -1;
342b8b6e 35static char *conflicts = NULL;
08089d5d 36
342b8b6e
AD
37static unsigned *shiftset = NULL;
38static unsigned *lookaheadset = NULL;
c29240e7 39\f
08089d5d 40
c29240e7
AD
41static inline void
42log_resolution (int state, int LAno, int token, char *resolution)
08089d5d 43{
ff4423cc
AD
44 obstack_fgrow4 (&output_obstack,
45 _("\
c29240e7 46Conflict in state %d between rule %d and token %s resolved as %s.\n"),
ff4423cc 47 state, LAruleno[LAno], tags[token], resolution);
08089d5d
DM
48}
49
50
c29240e7
AD
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
4a120d45 57static void
c29240e7 58flush_shift (int state, int token)
08089d5d 59{
9f136c07
AD
60 shifts *shiftp = state_table[state].shift_table;
61 int i;
c29240e7 62
d954473d
AD
63 for (i = 0; i < shiftp->nshifts; i++)
64 if (!SHIFT_IS_DISABLED (shiftp, i) && SHIFT_SYMBOL (shiftp, i) == token)
65 SHIFT_DISABLE (shiftp, i);
08089d5d
DM
66}
67
68
c29240e7
AD
69/*------------------------------------------------------------------.
70| Attempt to resolve shift-reduce conflict for one rule by means of |
71| precedence declarations. It has already been checked that the |
72| rule has a precedence. A conflict is resolved by modifying the |
73| shift or reduce tables so that there is no longer a conflict. |
74`------------------------------------------------------------------*/
08089d5d 75
4a120d45 76static void
d2729d44 77resolve_sr_conflict (int state, int lookaheadnum)
08089d5d 78{
c29240e7 79 int i;
9f136c07
AD
80 /* find the rule to reduce by to get precedence of reduction */
81 int redprec = rule_table[LAruleno[lookaheadnum]].prec;
f59c437a 82 errs *errp = ERRS_ALLOC (ntokens + 1);
08089d5d
DM
83 short *errtokens = errp->errs;
84
08089d5d
DM
85 for (i = 0; i < ntokens; i++)
86 {
9f136c07
AD
87 if (BITISSET (LA (lookaheadnum), i)
88 && BITISSET (lookaheadset, i)
89 && sprec[i])
08089d5d
DM
90 /* Shift-reduce conflict occurs for token number i
91 and it has a precedence.
92 The precedence of shifting is that of token i. */
93 {
94 if (sprec[i] < redprec)
95 {
c29240e7 96 log_resolution (state, lookaheadnum, i, _("reduce"));
9f136c07
AD
97 /* flush the shift for this token */
98 RESETBIT (lookaheadset, i);
c29240e7 99 flush_shift (state, i);
08089d5d
DM
100 }
101 else if (sprec[i] > redprec)
102 {
c29240e7 103 log_resolution (state, lookaheadnum, i, _("shift"));
9f136c07
AD
104 /* flush the reduce for this token */
105 RESETBIT (LA (lookaheadnum), i);
08089d5d
DM
106 }
107 else
108 {
109 /* Matching precedence levels.
c29240e7
AD
110 For left association, keep only the reduction.
111 For right association, keep only the shift.
112 For nonassociation, keep neither. */
08089d5d
DM
113
114 switch (sassoc[i])
115 {
d7020c20 116 case right_assoc:
c29240e7 117 log_resolution (state, lookaheadnum, i, _("shift"));
08089d5d
DM
118 break;
119
d7020c20 120 case left_assoc:
c29240e7 121 log_resolution (state, lookaheadnum, i, _("reduce"));
08089d5d
DM
122 break;
123
d7020c20 124 case non_assoc:
c29240e7 125 log_resolution (state, lookaheadnum, i, _("an error"));
08089d5d
DM
126 break;
127 }
128
d7020c20 129 if (sassoc[i] != right_assoc)
08089d5d 130 {
9f136c07
AD
131 /* flush the shift for this token */
132 RESETBIT (lookaheadset, i);
c29240e7 133 flush_shift (state, i);
08089d5d 134 }
d7020c20 135 if (sassoc[i] != left_assoc)
c29240e7 136 {
9f136c07
AD
137 /* flush the reduce for this token */
138 RESETBIT (LA (lookaheadnum), i);
08089d5d 139 }
d7020c20 140 if (sassoc[i] == non_assoc)
08089d5d
DM
141 {
142 /* Record an explicit error for this token. */
143 *errtokens++ = i;
144 }
145 }
146 }
08089d5d
DM
147 }
148 errp->nerrs = errtokens - errp->errs;
149 if (errp->nerrs)
150 {
151 /* Some tokens have been explicitly made errors. Allocate
c29240e7 152 a permanent errs structure for this state, to record them. */
08089d5d 153 i = (char *) errtokens - (char *) errp;
f59c437a 154 err_table[state] = ERRS_ALLOC (i + 1);
08089d5d
DM
155 bcopy (errp, err_table[state], i);
156 }
157 else
158 err_table[state] = 0;
c29240e7 159 free (errp);
08089d5d
DM
160}
161
162
4a120d45 163static void
c29240e7 164set_conflicts (int state)
08089d5d 165{
d8cf039f 166 int i, j;
c29240e7 167 shifts *shiftp;
c29240e7 168
de326cc0 169 if (state_table[state].consistent)
c29240e7 170 return;
08089d5d 171
c29240e7
AD
172 for (i = 0; i < tokensetsize; i++)
173 lookaheadset[i] = 0;
08089d5d 174
90b4416b 175 shiftp = state_table[state].shift_table;
d954473d
AD
176 for (i = 0; i < shiftp->nshifts && SHIFT_IS_SHIFT (shiftp, i); i++)
177 if (!SHIFT_IS_DISABLED (shiftp, i))
178 SETBIT (lookaheadset, SHIFT_SYMBOL (shiftp, i));
08089d5d 179
c29240e7
AD
180 /* Loop over all rules which require lookahead in this state. First
181 check for shift-reduce conflict, and try to resolve using
182 precedence */
d8cf039f
AD
183 for (i = state_table[state].lookaheads;
184 i < state_table[state + 1].lookaheads;
185 ++i)
652a871c 186 if (rule_table[LAruleno[i]].prec)
d8cf039f
AD
187 for (j = 0; j < tokensetsize; ++j)
188 if (LA (i)[j] & lookaheadset[j])
c29240e7 189 {
d8cf039f
AD
190 resolve_sr_conflict (state, i);
191 break;
c29240e7 192 }
08089d5d 193
08089d5d 194
c29240e7
AD
195 /* Loop over all rules which require lookahead in this state. Check
196 for conflicts not resolved above. */
d8cf039f
AD
197 for (i = state_table[state].lookaheads;
198 i < state_table[state + 1].lookaheads;
199 ++i)
08089d5d 200 {
d8cf039f
AD
201 for (j = 0; j < tokensetsize; ++j)
202 if (LA (i)[j] & lookaheadset[j])
0df87bb6 203 conflicts[state] = 1;
08089d5d 204
d8cf039f
AD
205 for (j = 0; j < tokensetsize; ++j)
206 lookaheadset[j] |= LA (i)[j];
c29240e7
AD
207 }
208}
08089d5d
DM
209
210void
342b8b6e 211solve_conflicts (void)
08089d5d 212{
c29240e7 213 int i;
08089d5d 214
d7913476
AD
215 conflicts = XCALLOC (char, nstates);
216 shiftset = XCALLOC (unsigned, tokensetsize);
217 lookaheadset = XCALLOC (unsigned, tokensetsize);
08089d5d 218
d7913476 219 err_table = XCALLOC (errs *, nstates);
08089d5d 220
c29240e7
AD
221 for (i = 0; i < nstates; i++)
222 set_conflicts (i);
08089d5d
DM
223}
224
225
c29240e7
AD
226/*---------------------------------------------.
227| Count the number of shift/reduce conflicts. |
228`---------------------------------------------*/
229
0df87bb6 230static int
d2729d44 231count_sr_conflicts (int state)
08089d5d 232{
52afa962 233 int i, k;
0df87bb6 234 int src_count = 0;
52afa962 235 shifts *shiftp = state_table[state].shift_table;
08089d5d 236
c29240e7 237 if (!shiftp)
0df87bb6 238 return 0;
08089d5d
DM
239
240 for (i = 0; i < tokensetsize; i++)
241 {
242 shiftset[i] = 0;
243 lookaheadset[i] = 0;
244 }
245
52afa962 246 for (i = 0; i < shiftp->nshifts && SHIFT_IS_SHIFT (shiftp, i); i++)
9839bbe5
AD
247 if (!SHIFT_IS_DISABLED (shiftp, i))
248 SETBIT (shiftset, SHIFT_SYMBOL (shiftp, i));
08089d5d 249
52afa962
AD
250 for (i = state_table[state].lookaheads;
251 i < state_table[state + 1].lookaheads;
252 ++i)
253 for (k = 0; k < tokensetsize; ++k)
254 lookaheadset[k] |= LA (i)[k];
08089d5d 255
52afa962
AD
256 for (k = 0; k < tokensetsize; ++k)
257 lookaheadset[k] &= shiftset[k];
08089d5d 258
08089d5d 259 for (i = 0; i < ntokens; i++)
52afa962
AD
260 if (BITISSET (lookaheadset, i))
261 src_count++;
0df87bb6
AD
262
263 return src_count;
08089d5d
DM
264}
265
266
c29240e7
AD
267/*----------------------------------------------.
268| Count the number of reduce/reduce conflicts. |
269`----------------------------------------------*/
270
0df87bb6 271static int
d2729d44 272count_rr_conflicts (int state)
08089d5d 273{
c29240e7 274 int i;
0df87bb6 275 int rrc_count = 0;
08089d5d 276
f004bf6a
AD
277 int m = state_table[state].lookaheads;
278 int n = state_table[state + 1].lookaheads;
08089d5d 279
c29240e7 280 if (n - m < 2)
0df87bb6 281 return 0;
08089d5d 282
08089d5d
DM
283 for (i = 0; i < ntokens; i++)
284 {
0df87bb6
AD
285 int count = 0;
286 int j;
08089d5d 287 for (j = m; j < n; j++)
52afa962
AD
288 if (BITISSET (LA (m), j))
289 count++;
08089d5d 290
c29240e7
AD
291 if (count >= 2)
292 rrc_count++;
08089d5d 293 }
0df87bb6
AD
294
295 return rrc_count;
08089d5d
DM
296}
297
ff4423cc
AD
298/*--------------------------------------------------------------.
299| Return a human readable string which reports shift/reduce and |
300| reduce/reduce conflict numbers (SRC_NUM, RRC_NUM). |
301`--------------------------------------------------------------*/
c29240e7 302
ff4423cc
AD
303static const char *
304conflict_report (int src_num, int rrc_num)
c29240e7 305{
ff4423cc
AD
306 static char res[4096];
307 char *cp = res;
308
7da99ede 309 if (src_num >= 1)
22c821f3 310 {
7da99ede
AD
311 sprintf (cp, ngettext ("%d shift/reduce conflict",
312 "%d shift/reduce conflicts", src_num), src_num);
22c821f3
AD
313 cp += strlen (cp);
314 }
c29240e7 315
0619caf0 316 if (src_num > 0 && rrc_num > 0)
22c821f3 317 {
7da99ede 318 sprintf (cp, " %s ", _("and"));
22c821f3
AD
319 cp += strlen (cp);
320 }
c29240e7 321
7da99ede 322 if (rrc_num >= 1)
22c821f3 323 {
7da99ede
AD
324 sprintf (cp, ngettext ("%d reduce/reduce conflict",
325 "%d reduce/reduce conflicts", rrc_num), rrc_num);
22c821f3
AD
326 cp += strlen (cp);
327 }
ff4423cc
AD
328
329 *cp++ = '.';
330 *cp++ = '\n';
331 *cp++ = '\0';
c29240e7 332
ff4423cc 333 return res;
c29240e7
AD
334}
335
336
0df87bb6
AD
337/*-----------------------------------------------------------.
338| Output the detailed description of states with conflicts. |
339`-----------------------------------------------------------*/
c29240e7
AD
340
341void
0df87bb6 342conflicts_output (FILE *out)
c29240e7 343{
d2d1b42b 344 bool printed_sth = FALSE;
c29240e7 345 int i;
0df87bb6
AD
346 for (i = 0; i < nstates; i++)
347 if (conflicts[i])
348 {
7da99ede 349 fprintf (out, _("State %d contains "), i);
0df87bb6
AD
350 fputs (conflict_report (count_sr_conflicts (i),
351 count_rr_conflicts (i)), out);
d2d1b42b 352 printed_sth = TRUE;
0df87bb6 353 }
d2d1b42b
AD
354 if (printed_sth)
355 fputs ("\n\n", out);
0df87bb6 356}
c29240e7 357
c29240e7 358
0df87bb6
AD
359/*------------------------------------------.
360| Reporting the total number of conflicts. |
361`------------------------------------------*/
0619caf0 362
0df87bb6
AD
363void
364conflicts_print (void)
365{
366 int i;
367
a034c8b8
AD
368 /* Is the number of SR conflicts OK? Either EXPECTED_CONFLICTS is
369 not set, and then we want 0 SR, or else it is specified, in which
370 case we want equality. */
371 int src_ok = 0;
372
0df87bb6
AD
373 int src_total = 0;
374 int rrc_total = 0;
375
376 /* Conflicts by state. */
377 for (i = 0; i < nstates; i++)
378 if (conflicts[i])
379 {
380 src_total += count_sr_conflicts (i);
381 rrc_total += count_rr_conflicts (i);
382 }
c29240e7 383
a034c8b8
AD
384 src_ok = src_total == (expected_conflicts == -1 ? 0 : expected_conflicts);
385
386 /* If there are no RR conflicts, and as many SR conflicts as
387 expected, then there is nothing to report. */
388 if (!rrc_total && src_ok)
389 return;
390
0619caf0 391 /* Report the total number of conflicts on STDERR. */
a034c8b8 392 if (yacc_flag)
09b503c8 393 {
a034c8b8
AD
394 /* If invoked with `--yacc', use the output format specified by
395 POSIX. */
396 fprintf (stderr, _("conflicts: "));
397 if (src_total > 0)
398 fprintf (stderr, _(" %d shift/reduce"), src_total);
399 if (src_total > 0 && rrc_total > 0)
400 fprintf (stderr, ",");
401 if (rrc_total > 0)
402 fprintf (stderr, _(" %d reduce/reduce"), rrc_total);
403 putc ('\n', stderr);
404 }
405 else
406 {
407 fprintf (stderr, _("%s contains "), infile);
408 fputs (conflict_report (src_total, rrc_total), stderr);
09b503c8 409 }
7da99ede 410
a034c8b8 411 if (expected_conflicts != -1 && !src_ok)
7da99ede
AD
412 {
413 complain_message_count++;
81e895c0
AD
414 fprintf (stderr, ngettext ("expected %d shift/reduce conflict\n",
415 "expected %d shift/reduce conflicts\n",
7da99ede
AD
416 expected_conflicts),
417 expected_conflicts);
418 }
c29240e7
AD
419}
420
421
08089d5d 422void
c73a41af 423print_reductions (FILE *out, int state)
08089d5d 424{
c29240e7
AD
425 int i;
426 int j;
c29240e7
AD
427 int m;
428 int n;
c29240e7
AD
429 shifts *shiftp;
430 errs *errp;
08089d5d
DM
431 int nodefault = 0;
432
433 for (i = 0; i < tokensetsize; i++)
434 shiftset[i] = 0;
435
90b4416b 436 shiftp = state_table[state].shift_table;
d954473d
AD
437 for (i = 0; i < shiftp->nshifts && SHIFT_IS_SHIFT (shiftp, i); i++)
438 if (!SHIFT_IS_DISABLED (shiftp, i))
439 {
440 /* if this state has a shift for the error token, don't use a
441 default rule. */
442 if (SHIFT_IS_ERROR (shiftp, i))
443 nodefault = 1;
444 SETBIT (shiftset, SHIFT_SYMBOL (shiftp, i));
445 }
08089d5d
DM
446
447 errp = err_table[state];
448 if (errp)
e74dc321
AD
449 for (i = 0; i < errp->nerrs; i++)
450 if (errp->errs[i])
451 SETBIT (shiftset, errp->errs[i]);
08089d5d 452
f004bf6a
AD
453 m = state_table[state].lookaheads;
454 n = state_table[state + 1].lookaheads;
08089d5d 455
c29240e7 456 if (n - m == 1 && !nodefault)
08089d5d 457 {
a04bc341 458 int k;
52afa962 459 int default_rule = LAruleno[m];
08089d5d 460
a04bc341
AD
461 for (k = 0; k < tokensetsize; ++k)
462 lookaheadset[k] = LA (m)[k] & shiftset[k];
08089d5d
DM
463
464 for (i = 0; i < ntokens; i++)
a04bc341
AD
465 if (BITISSET (lookaheadset, i))
466 fprintf (out, _(" %-4s\t[reduce using rule %d (%s)]\n"),
467 tags[i], default_rule,
468 tags[rule_table[default_rule].lhs]);
08089d5d 469
c73a41af 470 fprintf (out, _(" $default\treduce using rule %d (%s)\n\n"),
b2ed6e58 471 default_rule, tags[rule_table[default_rule].lhs]);
08089d5d
DM
472 }
473 else if (n - m >= 1)
474 {
768fca83 475 int k;
c8ea038e 476
52afa962
AD
477 int cmax = 0;
478 int default_LA = -1;
479 int default_rule = 0;
08089d5d 480
c29240e7 481 if (!nodefault)
08089d5d
DM
482 for (i = m; i < n; i++)
483 {
e74dc321
AD
484 int count = 0;
485
768fca83
AD
486 for (k = 0; k < tokensetsize; ++k)
487 lookaheadset[k] = LA (i)[k] & ~shiftset[k];
ceed8467 488
08089d5d 489 for (j = 0; j < ntokens; j++)
768fca83
AD
490 if (BITISSET (lookaheadset, j))
491 count++;
ceed8467 492
08089d5d
DM
493 if (count > cmax)
494 {
495 cmax = count;
496 default_LA = i;
497 default_rule = LAruleno[i];
498 }
ceed8467 499
e74dc321
AD
500 for (k = 0; k < tokensetsize; ++k)
501 shiftset[k] |= lookaheadset[k];
08089d5d
DM
502 }
503
504 for (i = 0; i < tokensetsize; i++)
c29240e7 505 shiftset[i] = 0;
08089d5d 506
d954473d
AD
507 for (i = 0; i < shiftp->nshifts && SHIFT_IS_SHIFT (shiftp, i); i++)
508 if (!SHIFT_IS_DISABLED (shiftp, i))
509 SETBIT (shiftset, SHIFT_SYMBOL (shiftp, i));
08089d5d 510
08089d5d
DM
511 for (i = 0; i < ntokens; i++)
512 {
513 int defaulted = 0;
e74dc321 514 int count = BITISSET (shiftset, i);
08089d5d 515
08089d5d
DM
516 for (j = m; j < n; j++)
517 {
e74dc321 518 if (BITISSET (LA (m), j))
08089d5d
DM
519 {
520 if (count == 0)
521 {
522 if (j != default_LA)
a17e599f
AD
523 fprintf (out,
524 _(" %-4s\treduce using rule %d (%s)\n"),
525 tags[i],
526 LAruleno[j],
527 tags[rule_table[LAruleno[j]].lhs]);
c29240e7
AD
528 else
529 defaulted = 1;
08089d5d
DM
530
531 count++;
532 }
533 else
534 {
535 if (defaulted)
a17e599f
AD
536 fprintf (out,
537 _(" %-4s\treduce using rule %d (%s)\n"),
538 tags[i],
539 LAruleno[default_LA],
540 tags[rule_table[LAruleno[default_LA]].lhs]);
541 defaulted = 0;
c73a41af 542 fprintf (out,
c29240e7 543 _(" %-4s\t[reduce using rule %d (%s)]\n"),
a17e599f
AD
544 tags[i],
545 LAruleno[j],
546 tags[rule_table[LAruleno[j]].lhs]);
08089d5d
DM
547 }
548 }
08089d5d
DM
549 }
550 }
551
552 if (default_LA >= 0)
c73a41af 553 fprintf (out, _(" $default\treduce using rule %d (%s)\n"),
b2ed6e58 554 default_rule, tags[rule_table[default_rule].lhs]);
08089d5d
DM
555 }
556}
557
558
559void
342b8b6e 560free_conflicts (void)
08089d5d 561{
d7913476
AD
562 XFREE (conflicts);
563 XFREE (shiftset);
564 XFREE (lookaheadset);
08089d5d 565}