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