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