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