]> git.saurik.com Git - bison.git/blame - src/conflicts.c
* src/conflicts.c (flush_shift, resolve_sr_conflict): De-obfuscate
[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
08089d5d 63 if (shiftp)
9f136c07
AD
64 for (i = 0; i < shiftp->nshifts; i++)
65 if (shiftp->shifts[i] && SHIFT_SYMBOL (shiftp, i) == token)
66 shiftp->shifts[i] = 0;
08089d5d
DM
67}
68
69
c29240e7
AD
70/*------------------------------------------------------------------.
71| Attempt to resolve shift-reduce conflict for one rule by means of |
72| precedence declarations. It has already been checked that the |
73| rule has a precedence. A conflict is resolved by modifying the |
74| shift or reduce tables so that there is no longer a conflict. |
75`------------------------------------------------------------------*/
08089d5d 76
4a120d45 77static void
d2729d44 78resolve_sr_conflict (int state, int lookaheadnum)
08089d5d 79{
c29240e7 80 int i;
9f136c07
AD
81 /* find the rule to reduce by to get precedence of reduction */
82 int redprec = rule_table[LAruleno[lookaheadnum]].prec;
f59c437a 83 errs *errp = ERRS_ALLOC (ntokens + 1);
08089d5d
DM
84 short *errtokens = errp->errs;
85
08089d5d
DM
86 for (i = 0; i < ntokens; i++)
87 {
9f136c07
AD
88 if (BITISSET (LA (lookaheadnum), i)
89 && BITISSET (lookaheadset, i)
90 && sprec[i])
08089d5d
DM
91 /* Shift-reduce conflict occurs for token number i
92 and it has a precedence.
93 The precedence of shifting is that of token i. */
94 {
95 if (sprec[i] < redprec)
96 {
c29240e7 97 log_resolution (state, lookaheadnum, i, _("reduce"));
9f136c07
AD
98 /* flush the shift for this token */
99 RESETBIT (lookaheadset, i);
c29240e7 100 flush_shift (state, i);
08089d5d
DM
101 }
102 else if (sprec[i] > redprec)
103 {
c29240e7 104 log_resolution (state, lookaheadnum, i, _("shift"));
9f136c07
AD
105 /* flush the reduce for this token */
106 RESETBIT (LA (lookaheadnum), i);
08089d5d
DM
107 }
108 else
109 {
110 /* Matching precedence levels.
c29240e7
AD
111 For left association, keep only the reduction.
112 For right association, keep only the shift.
113 For nonassociation, keep neither. */
08089d5d
DM
114
115 switch (sassoc[i])
116 {
d7020c20 117 case right_assoc:
c29240e7 118 log_resolution (state, lookaheadnum, i, _("shift"));
08089d5d
DM
119 break;
120
d7020c20 121 case left_assoc:
c29240e7 122 log_resolution (state, lookaheadnum, i, _("reduce"));
08089d5d
DM
123 break;
124
d7020c20 125 case non_assoc:
c29240e7 126 log_resolution (state, lookaheadnum, i, _("an error"));
08089d5d
DM
127 break;
128 }
129
d7020c20 130 if (sassoc[i] != right_assoc)
08089d5d 131 {
9f136c07
AD
132 /* flush the shift for this token */
133 RESETBIT (lookaheadset, i);
c29240e7 134 flush_shift (state, i);
08089d5d 135 }
d7020c20 136 if (sassoc[i] != left_assoc)
c29240e7 137 {
9f136c07
AD
138 /* flush the reduce for this token */
139 RESETBIT (LA (lookaheadnum), i);
08089d5d 140 }
d7020c20 141 if (sassoc[i] == non_assoc)
08089d5d
DM
142 {
143 /* Record an explicit error for this token. */
144 *errtokens++ = i;
145 }
146 }
147 }
08089d5d
DM
148 }
149 errp->nerrs = errtokens - errp->errs;
150 if (errp->nerrs)
151 {
152 /* Some tokens have been explicitly made errors. Allocate
c29240e7 153 a permanent errs structure for this state, to record them. */
08089d5d 154 i = (char *) errtokens - (char *) errp;
f59c437a 155 err_table[state] = ERRS_ALLOC (i + 1);
08089d5d
DM
156 bcopy (errp, err_table[state], i);
157 }
158 else
159 err_table[state] = 0;
c29240e7 160 free (errp);
08089d5d
DM
161}
162
163
4a120d45 164static void
c29240e7 165set_conflicts (int state)
08089d5d 166{
d8cf039f 167 int i, j;
c29240e7 168 shifts *shiftp;
c29240e7 169
de326cc0 170 if (state_table[state].consistent)
c29240e7 171 return;
08089d5d 172
c29240e7
AD
173 for (i = 0; i < tokensetsize; i++)
174 lookaheadset[i] = 0;
08089d5d 175
90b4416b 176 shiftp = state_table[state].shift_table;
08089d5d 177 if (shiftp)
b608206e
AD
178 for (i = 0; i < shiftp->nshifts && SHIFT_IS_SHIFT (shiftp, i); i++)
179 SETBIT (lookaheadset, SHIFT_SYMBOL (shiftp, i));
08089d5d 180
c29240e7
AD
181 /* Loop over all rules which require lookahead in this state. First
182 check for shift-reduce conflict, and try to resolve using
183 precedence */
d8cf039f
AD
184 for (i = state_table[state].lookaheads;
185 i < state_table[state + 1].lookaheads;
186 ++i)
652a871c 187 if (rule_table[LAruleno[i]].prec)
d8cf039f
AD
188 for (j = 0; j < tokensetsize; ++j)
189 if (LA (i)[j] & lookaheadset[j])
c29240e7 190 {
d8cf039f
AD
191 resolve_sr_conflict (state, i);
192 break;
c29240e7 193 }
08089d5d 194
08089d5d 195
c29240e7
AD
196 /* Loop over all rules which require lookahead in this state. Check
197 for conflicts not resolved above. */
d8cf039f
AD
198 for (i = state_table[state].lookaheads;
199 i < state_table[state + 1].lookaheads;
200 ++i)
08089d5d 201 {
d8cf039f
AD
202 for (j = 0; j < tokensetsize; ++j)
203 if (LA (i)[j] & lookaheadset[j])
0df87bb6 204 conflicts[state] = 1;
08089d5d 205
d8cf039f
AD
206 for (j = 0; j < tokensetsize; ++j)
207 lookaheadset[j] |= LA (i)[j];
c29240e7
AD
208 }
209}
08089d5d
DM
210
211void
342b8b6e 212solve_conflicts (void)
08089d5d 213{
c29240e7 214 int i;
08089d5d 215
d7913476
AD
216 conflicts = XCALLOC (char, nstates);
217 shiftset = XCALLOC (unsigned, tokensetsize);
218 lookaheadset = XCALLOC (unsigned, tokensetsize);
08089d5d 219
d7913476 220 err_table = XCALLOC (errs *, nstates);
08089d5d 221
c29240e7
AD
222 for (i = 0; i < nstates; i++)
223 set_conflicts (i);
08089d5d
DM
224}
225
226
c29240e7
AD
227/*---------------------------------------------.
228| Count the number of shift/reduce conflicts. |
229`---------------------------------------------*/
230
0df87bb6 231static int
d2729d44 232count_sr_conflicts (int state)
08089d5d 233{
52afa962 234 int i, k;
0df87bb6 235 int src_count = 0;
52afa962 236 shifts *shiftp = state_table[state].shift_table;
08089d5d 237
c29240e7 238 if (!shiftp)
0df87bb6 239 return 0;
08089d5d
DM
240
241 for (i = 0; i < tokensetsize; i++)
242 {
243 shiftset[i] = 0;
244 lookaheadset[i] = 0;
245 }
246
52afa962 247 for (i = 0; i < shiftp->nshifts && SHIFT_IS_SHIFT (shiftp, i); i++)
b608206e 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;
08089d5d 437 if (shiftp)
b608206e 438 for (i = 0; i < shiftp->nshifts && SHIFT_IS_SHIFT (shiftp, i); i++)
c8ea038e 439 {
b608206e
AD
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))
c8ea038e 443 nodefault = 1;
b608206e 444 SETBIT (shiftset, SHIFT_SYMBOL (shiftp, i));
c8ea038e 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
DM
506
507 if (shiftp)
b608206e
AD
508 for (i = 0; i < shiftp->nshifts && SHIFT_IS_SHIFT (shiftp, i); 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}