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