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