]> git.saurik.com Git - bison.git/blame - src/conflicts.c
* src/main.c (main): If there are complains after grammar
[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 40static inline void
065fbd27 41log_resolution (state_t *state, int LAno, int token, char *resolution)
08089d5d 42{
64d15509
AD
43 if (verbose_flag)
44 obstack_fgrow4 (&output_obstack,
45 _("\
c29240e7 46Conflict in state %d between rule %d and token %s resolved as %s.\n"),
065fbd27 47 state->number, 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
065fbd27 58flush_shift (state_t *state, int token)
08089d5d 59{
065fbd27 60 shifts *shiftp = state->shifts;
9f136c07 61 int i;
c29240e7 62
709ae8c6 63 RESETBIT (lookaheadset, token);
d954473d
AD
64 for (i = 0; i < shiftp->nshifts; i++)
65 if (!SHIFT_IS_DISABLED (shiftp, i) && SHIFT_SYMBOL (shiftp, i) == token)
66 SHIFT_DISABLE (shiftp, i);
08089d5d
DM
67}
68
69
709ae8c6
AD
70/*-------------------------------------------------------------------.
71| Turn off the reduce recorded for the specified token for the |
72| specified lookahead. Used when we resolve a shift-reduce conflict |
73| in favor of the shift. |
74`-------------------------------------------------------------------*/
75
76static void
77flush_reduce (int lookahead, int token)
78{
79 RESETBIT (LA (lookahead), token);
80}
81
82
c29240e7
AD
83/*------------------------------------------------------------------.
84| Attempt to resolve shift-reduce conflict for one rule by means of |
85| precedence declarations. It has already been checked that the |
86| rule has a precedence. A conflict is resolved by modifying the |
87| shift or reduce tables so that there is no longer a conflict. |
88`------------------------------------------------------------------*/
08089d5d 89
4a120d45 90static void
065fbd27 91resolve_sr_conflict (state_t *state, int lookahead)
08089d5d 92{
c29240e7 93 int i;
9f136c07 94 /* find the rule to reduce by to get precedence of reduction */
709ae8c6 95 int redprec = rule_table[LAruleno[lookahead]].prec;
2cec70b9
AD
96 errs *errp = errs_new (ntokens + 1);
97 errp->nerrs = 0;
08089d5d 98
08089d5d 99 for (i = 0; i < ntokens; i++)
709ae8c6 100 if (BITISSET (LA (lookahead), i)
92b16366
AD
101 && BITISSET (lookaheadset, i)
102 && sprec[i])
92b16366 103 {
709ae8c6
AD
104 /* Shift-reduce conflict occurs for token number i
105 and it has a precedence.
106 The precedence of shifting is that of token i. */
92b16366
AD
107 if (sprec[i] < redprec)
108 {
709ae8c6 109 log_resolution (state, lookahead, i, _("reduce"));
92b16366
AD
110 flush_shift (state, i);
111 }
112 else if (sprec[i] > redprec)
113 {
709ae8c6
AD
114 log_resolution (state, lookahead, i, _("shift"));
115 flush_reduce (lookahead, i);
92b16366
AD
116 }
117 else
709ae8c6
AD
118 /* Matching precedence levels.
119 For left association, keep only the reduction.
120 For right association, keep only the shift.
121 For nonassociation, keep neither. */
122
123 switch (sassoc[i])
124 {
125 case right_assoc:
126 log_resolution (state, lookahead, i, _("shift"));
127 flush_reduce (lookahead, i);
128 break;
129
130 case left_assoc:
131 log_resolution (state, lookahead, i, _("reduce"));
132 flush_shift (state, i);
133 break;
134
135 case non_assoc:
136 log_resolution (state, lookahead, i, _("an error"));
137 flush_shift (state, i);
138 flush_reduce (lookahead, i);
139 /* Record an explicit error for this token. */
2cec70b9 140 errp->errs[errp->nerrs++] = i;
709ae8c6
AD
141 break;
142 }
92b16366
AD
143 }
144
92b16366
AD
145 /* Some tokens have been explicitly made errors. Allocate a
146 permanent errs structure for this state, to record them. */
2cec70b9 147 state->errs = errs_dup (errp);
c29240e7 148 free (errp);
08089d5d
DM
149}
150
151
4a120d45 152static void
065fbd27 153set_conflicts (state_t *state)
08089d5d 154{
d8cf039f 155 int i, j;
c29240e7 156 shifts *shiftp;
c29240e7 157
065fbd27 158 if (state->consistent)
c29240e7 159 return;
08089d5d 160
c29240e7
AD
161 for (i = 0; i < tokensetsize; i++)
162 lookaheadset[i] = 0;
08089d5d 163
065fbd27 164 shiftp = state->shifts;
d954473d
AD
165 for (i = 0; i < shiftp->nshifts && SHIFT_IS_SHIFT (shiftp, i); i++)
166 if (!SHIFT_IS_DISABLED (shiftp, i))
167 SETBIT (lookaheadset, SHIFT_SYMBOL (shiftp, i));
08089d5d 168
c29240e7
AD
169 /* Loop over all rules which require lookahead in this state. First
170 check for shift-reduce conflict, and try to resolve using
171 precedence */
065fbd27
AD
172 for (i = 0; i < state->nlookaheads; ++i)
173 if (rule_table[LAruleno[state->lookaheadsp + i]].prec)
d8cf039f 174 for (j = 0; j < tokensetsize; ++j)
065fbd27 175 if (LA (state->lookaheadsp + i)[j] & lookaheadset[j])
c29240e7 176 {
065fbd27 177 resolve_sr_conflict (state, state->lookaheadsp + i);
d8cf039f 178 break;
c29240e7 179 }
08089d5d 180
08089d5d 181
c29240e7
AD
182 /* Loop over all rules which require lookahead in this state. Check
183 for conflicts not resolved above. */
065fbd27 184 for (i = 0; i < state->nlookaheads; ++i)
08089d5d 185 {
d8cf039f 186 for (j = 0; j < tokensetsize; ++j)
065fbd27
AD
187 if (LA (state->lookaheadsp + i)[j] & lookaheadset[j])
188 conflicts[state->number] = 1;
08089d5d 189
d8cf039f 190 for (j = 0; j < tokensetsize; ++j)
065fbd27 191 lookaheadset[j] |= LA (state->lookaheadsp + i)[j];
c29240e7
AD
192 }
193}
08089d5d
DM
194
195void
342b8b6e 196solve_conflicts (void)
08089d5d 197{
c29240e7 198 int i;
08089d5d 199
d7913476
AD
200 conflicts = XCALLOC (char, nstates);
201 shiftset = XCALLOC (unsigned, tokensetsize);
202 lookaheadset = XCALLOC (unsigned, tokensetsize);
08089d5d 203
c29240e7 204 for (i = 0; i < nstates; i++)
065fbd27 205 set_conflicts (state_table[i]);
08089d5d
DM
206}
207
208
c29240e7
AD
209/*---------------------------------------------.
210| Count the number of shift/reduce conflicts. |
211`---------------------------------------------*/
212
0df87bb6 213static int
065fbd27 214count_sr_conflicts (state_t *state)
08089d5d 215{
52afa962 216 int i, k;
0df87bb6 217 int src_count = 0;
065fbd27 218 shifts *shiftp = state->shifts;
08089d5d 219
c29240e7 220 if (!shiftp)
0df87bb6 221 return 0;
08089d5d
DM
222
223 for (i = 0; i < tokensetsize; i++)
224 {
225 shiftset[i] = 0;
226 lookaheadset[i] = 0;
227 }
228
52afa962 229 for (i = 0; i < shiftp->nshifts && SHIFT_IS_SHIFT (shiftp, i); i++)
9839bbe5
AD
230 if (!SHIFT_IS_DISABLED (shiftp, i))
231 SETBIT (shiftset, SHIFT_SYMBOL (shiftp, i));
08089d5d 232
065fbd27 233 for (i = 0; i < state->nlookaheads; ++i)
52afa962 234 for (k = 0; k < tokensetsize; ++k)
065fbd27 235 lookaheadset[k] |= LA (state->lookaheadsp + i)[k];
08089d5d 236
52afa962
AD
237 for (k = 0; k < tokensetsize; ++k)
238 lookaheadset[k] &= shiftset[k];
08089d5d 239
08089d5d 240 for (i = 0; i < ntokens; i++)
52afa962
AD
241 if (BITISSET (lookaheadset, i))
242 src_count++;
0df87bb6
AD
243
244 return src_count;
08089d5d
DM
245}
246
247
c29240e7
AD
248/*----------------------------------------------.
249| Count the number of reduce/reduce conflicts. |
250`----------------------------------------------*/
251
0df87bb6 252static int
065fbd27 253count_rr_conflicts (state_t *state)
08089d5d 254{
c29240e7 255 int i;
0df87bb6 256 int rrc_count = 0;
08089d5d 257
065fbd27 258 if (state->nlookaheads < 2)
0df87bb6 259 return 0;
08089d5d 260
08089d5d
DM
261 for (i = 0; i < ntokens; i++)
262 {
0df87bb6
AD
263 int count = 0;
264 int j;
065fbd27
AD
265 for (j = 0; j < state->nlookaheads; ++j)
266 if (BITISSET (LA (state->lookaheadsp), state->lookaheadsp + j))
52afa962 267 count++;
08089d5d 268
c29240e7
AD
269 if (count >= 2)
270 rrc_count++;
08089d5d 271 }
0df87bb6
AD
272
273 return rrc_count;
08089d5d
DM
274}
275
ff4423cc
AD
276/*--------------------------------------------------------------.
277| Return a human readable string which reports shift/reduce and |
278| reduce/reduce conflict numbers (SRC_NUM, RRC_NUM). |
279`--------------------------------------------------------------*/
c29240e7 280
ff4423cc
AD
281static const char *
282conflict_report (int src_num, int rrc_num)
c29240e7 283{
ff4423cc
AD
284 static char res[4096];
285 char *cp = res;
286
7da99ede 287 if (src_num >= 1)
22c821f3 288 {
7da99ede
AD
289 sprintf (cp, ngettext ("%d shift/reduce conflict",
290 "%d shift/reduce conflicts", src_num), src_num);
22c821f3
AD
291 cp += strlen (cp);
292 }
c29240e7 293
0619caf0 294 if (src_num > 0 && rrc_num > 0)
22c821f3 295 {
7da99ede 296 sprintf (cp, " %s ", _("and"));
22c821f3
AD
297 cp += strlen (cp);
298 }
c29240e7 299
7da99ede 300 if (rrc_num >= 1)
22c821f3 301 {
7da99ede
AD
302 sprintf (cp, ngettext ("%d reduce/reduce conflict",
303 "%d reduce/reduce conflicts", rrc_num), rrc_num);
22c821f3
AD
304 cp += strlen (cp);
305 }
ff4423cc
AD
306
307 *cp++ = '.';
308 *cp++ = '\n';
309 *cp++ = '\0';
c29240e7 310
ff4423cc 311 return res;
c29240e7
AD
312}
313
314
0df87bb6
AD
315/*-----------------------------------------------------------.
316| Output the detailed description of states with conflicts. |
317`-----------------------------------------------------------*/
c29240e7
AD
318
319void
0df87bb6 320conflicts_output (FILE *out)
c29240e7 321{
d2d1b42b 322 bool printed_sth = FALSE;
c29240e7 323 int i;
0df87bb6
AD
324 for (i = 0; i < nstates; i++)
325 if (conflicts[i])
326 {
7da99ede 327 fprintf (out, _("State %d contains "), i);
065fbd27
AD
328 fputs (conflict_report (count_sr_conflicts (state_table[i]),
329 count_rr_conflicts (state_table[i])), out);
d2d1b42b 330 printed_sth = TRUE;
0df87bb6 331 }
d2d1b42b
AD
332 if (printed_sth)
333 fputs ("\n\n", out);
0df87bb6 334}
c29240e7 335
c29240e7 336
0df87bb6
AD
337/*------------------------------------------.
338| Reporting the total number of conflicts. |
339`------------------------------------------*/
0619caf0 340
0df87bb6
AD
341void
342conflicts_print (void)
343{
344 int i;
345
a034c8b8
AD
346 /* Is the number of SR conflicts OK? Either EXPECTED_CONFLICTS is
347 not set, and then we want 0 SR, or else it is specified, in which
348 case we want equality. */
349 int src_ok = 0;
350
0df87bb6
AD
351 int src_total = 0;
352 int rrc_total = 0;
353
354 /* Conflicts by state. */
355 for (i = 0; i < nstates; i++)
356 if (conflicts[i])
357 {
065fbd27
AD
358 src_total += count_sr_conflicts (state_table[i]);
359 rrc_total += count_rr_conflicts (state_table[i]);
0df87bb6 360 }
c29240e7 361
a034c8b8
AD
362 src_ok = src_total == (expected_conflicts == -1 ? 0 : expected_conflicts);
363
364 /* If there are no RR conflicts, and as many SR conflicts as
365 expected, then there is nothing to report. */
366 if (!rrc_total && src_ok)
367 return;
368
0619caf0 369 /* Report the total number of conflicts on STDERR. */
a034c8b8 370 if (yacc_flag)
09b503c8 371 {
a034c8b8
AD
372 /* If invoked with `--yacc', use the output format specified by
373 POSIX. */
374 fprintf (stderr, _("conflicts: "));
375 if (src_total > 0)
376 fprintf (stderr, _(" %d shift/reduce"), src_total);
377 if (src_total > 0 && rrc_total > 0)
378 fprintf (stderr, ",");
379 if (rrc_total > 0)
380 fprintf (stderr, _(" %d reduce/reduce"), rrc_total);
381 putc ('\n', stderr);
382 }
383 else
384 {
385 fprintf (stderr, _("%s contains "), infile);
386 fputs (conflict_report (src_total, rrc_total), stderr);
09b503c8 387 }
7da99ede 388
a034c8b8 389 if (expected_conflicts != -1 && !src_ok)
7da99ede
AD
390 {
391 complain_message_count++;
81e895c0
AD
392 fprintf (stderr, ngettext ("expected %d shift/reduce conflict\n",
393 "expected %d shift/reduce conflicts\n",
7da99ede
AD
394 expected_conflicts),
395 expected_conflicts);
396 }
c29240e7
AD
397}
398
399
08089d5d 400void
342b8b6e 401free_conflicts (void)
08089d5d 402{
d7913476
AD
403 XFREE (conflicts);
404 XFREE (shiftset);
405 XFREE (lookaheadset);
08089d5d 406}