]> git.saurik.com Git - bison.git/blob - src/conflicts.c
* src/conflicts.c (log_resolution): Accept the rule involved in
[bison.git] / src / conflicts.c
1 /* Find and resolve or report look-ahead conflicts for bison,
2 Copyright (C) 1984, 1989, 1992, 2000, 2001, 2002
3 Free Software Foundation, Inc.
4
5 This file is part of Bison, the GNU Compiler Compiler.
6
7 Bison is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 Bison is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Bison; see the file COPYING. If not, write to the Free
19 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
21
22 #include "system.h"
23 #include "bitset.h"
24 #include "complain.h"
25 #include "getargs.h"
26 #include "symtab.h"
27 #include "files.h"
28 #include "gram.h"
29 #include "state.h"
30 #include "lalr.h"
31 #include "conflicts.h"
32 #include "reader.h"
33 #include "LR0.h"
34
35 /* -1 stands for not specified. */
36 int expected_conflicts = -1;
37 static char *conflicts = NULL;
38 struct obstack solved_conflicts_obstack;
39
40 static bitset shiftset;
41 static bitset lookaheadset;
42
43 \f
44
45 enum conflict_resolution_e
46 {
47 shift_resolution,
48 reduce_resolution,
49 left_resolution,
50 right_resolution,
51 nonassoc_resolution
52 };
53
54
55 /*----------------------------------------------------------------.
56 | Explain how an SR conflict between TOKEN and RULE was resolved: |
57 | RESOLUTION. |
58 `----------------------------------------------------------------*/
59
60 static inline void
61 log_resolution (rule_t *rule, int token,
62 enum conflict_resolution_e resolution)
63 {
64 if (report_flag & report_solved_conflicts)
65 {
66 /* The description of the resolution. */
67 switch (resolution)
68 {
69 case shift_resolution:
70 case left_resolution:
71 obstack_fgrow2 (&solved_conflicts_obstack,
72 _("\
73 Conflict between rule %d and token %s resolved as shift"),
74 rule->number,
75 symbols[token]->tag);
76 break;
77 case reduce_resolution:
78 case right_resolution:
79 obstack_fgrow2 (&solved_conflicts_obstack,
80 _("\
81 Conflict between rule %d and token %s resolved as reduce"),
82 rule->number,
83 symbols[token]->tag);
84 break;
85 case nonassoc_resolution:
86 obstack_fgrow2 (&solved_conflicts_obstack,
87 _("\
88 Conflict between rule %d and token %s resolved as an error"),
89 rule->number,
90 symbols[token]->tag);
91 break;
92 }
93
94 /* The reason. */
95 switch (resolution)
96 {
97 case shift_resolution:
98 obstack_fgrow2 (&solved_conflicts_obstack,
99 " (%s < %s)",
100 rule->prec->tag,
101 symbols[token]->tag);
102 break;
103
104 case reduce_resolution:
105 obstack_fgrow2 (&solved_conflicts_obstack,
106 " (%s < %s)",
107 symbols[token]->tag,
108 rule->prec->tag);
109 break;
110
111 case left_resolution:
112 obstack_fgrow1 (&solved_conflicts_obstack,
113 " (%%left %s)",
114 symbols[token]->tag);
115 break;
116
117 case right_resolution:
118 obstack_fgrow1 (&solved_conflicts_obstack,
119 " (%%right %s)",
120 symbols[token]->tag);
121 break;
122 case nonassoc_resolution:
123 obstack_fgrow1 (&solved_conflicts_obstack,
124 " (%%nonassoc %s)",
125 symbols[token]->tag);
126 break;
127 }
128 obstack_sgrow (&solved_conflicts_obstack, ".\n");
129 }
130 }
131
132
133 /*------------------------------------------------------------------.
134 | Turn off the shift recorded for the specified token in the |
135 | specified state. Used when we resolve a shift-reduce conflict in |
136 | favor of the reduction. |
137 `------------------------------------------------------------------*/
138
139 static void
140 flush_shift (state_t *state, int token)
141 {
142 shifts *shiftp = state->shifts;
143 int i;
144
145 bitset_reset (lookaheadset, token);
146 for (i = 0; i < shiftp->nshifts; i++)
147 if (!SHIFT_IS_DISABLED (shiftp, i) && SHIFT_SYMBOL (shiftp, i) == token)
148 SHIFT_DISABLE (shiftp, i);
149 }
150
151
152 /*-------------------------------------------------------------------.
153 | Turn off the reduce recorded for the specified token for the |
154 | specified lookahead. Used when we resolve a shift-reduce conflict |
155 | in favor of the shift. |
156 `-------------------------------------------------------------------*/
157
158 static void
159 flush_reduce (bitset lookaheads, int token)
160 {
161 bitset_reset (lookaheads, token);
162 }
163
164
165 /*------------------------------------------------------------------.
166 | Attempt to resolve shift-reduce conflict for one rule by means of |
167 | precedence declarations. It has already been checked that the |
168 | rule has a precedence. A conflict is resolved by modifying the |
169 | shift or reduce tables so that there is no longer a conflict. |
170 | |
171 | LOOKAHEAD is the number of the lookahead bitset to consider. |
172 `------------------------------------------------------------------*/
173
174 static void
175 resolve_sr_conflict (state_t *state, int lookahead)
176 {
177 int i;
178 /* Find the rule to reduce by to get precedence of reduction. */
179 rule_t *redrule = state->lookaheads_rule[lookahead];
180 int redprec = redrule->prec->prec;
181 bitset lookaheads = state->lookaheads[lookahead];
182 errs *errp = errs_new (ntokens + 1);
183 errp->nerrs = 0;
184
185 for (i = 0; i < ntokens; i++)
186 if (bitset_test (lookaheads, i)
187 && bitset_test (lookaheadset, i)
188 && symbols[i]->prec)
189 {
190 /* Shift-reduce conflict occurs for token number i
191 and it has a precedence.
192 The precedence of shifting is that of token i. */
193 if (symbols[i]->prec < redprec)
194 {
195 log_resolution (redrule, i, reduce_resolution);
196 flush_shift (state, i);
197 }
198 else if (symbols[i]->prec > redprec)
199 {
200 log_resolution (redrule, i, shift_resolution);
201 flush_reduce (lookaheads, i);
202 }
203 else
204 /* Matching precedence levels.
205 For left association, keep only the reduction.
206 For right association, keep only the shift.
207 For nonassociation, keep neither. */
208
209 switch (symbols[i]->assoc)
210 {
211 case right_assoc:
212 log_resolution (redrule, i, right_resolution);
213 flush_reduce (lookaheads, i);
214 break;
215
216 case left_assoc:
217 log_resolution (redrule, i, left_resolution);
218 flush_shift (state, i);
219 break;
220
221 case non_assoc:
222 log_resolution (redrule, i, nonassoc_resolution);
223 flush_shift (state, i);
224 flush_reduce (lookaheads, i);
225 /* Record an explicit error for this token. */
226 errp->errs[errp->nerrs++] = i;
227 break;
228
229 case undef_assoc:
230 assert (symbols[i]->assoc != undef_assoc);
231 break;
232 }
233 }
234
235 /* Some tokens have been explicitly made errors. Allocate a
236 permanent errs structure for this state, to record them. */
237 state->errs = errs_dup (errp);
238 free (errp);
239
240 if (obstack_object_size (&solved_conflicts_obstack))
241 {
242 obstack_1grow (&solved_conflicts_obstack, '\0');
243 state->solved_conflicts = obstack_finish (&solved_conflicts_obstack);
244 }
245 }
246
247
248 static void
249 set_conflicts (state_t *state)
250 {
251 int i;
252 shifts *shiftp;
253
254 if (state->consistent)
255 return;
256
257 bitset_zero (lookaheadset);
258
259 shiftp = state->shifts;
260 for (i = 0; i < shiftp->nshifts && SHIFT_IS_SHIFT (shiftp, i); i++)
261 if (!SHIFT_IS_DISABLED (shiftp, i))
262 bitset_set (lookaheadset, SHIFT_SYMBOL (shiftp, i));
263
264 /* Loop over all rules which require lookahead in this state. First
265 check for shift-reduce conflict, and try to resolve using
266 precedence. */
267 for (i = 0; i < state->nlookaheads; ++i)
268 if (state->lookaheads_rule[i]->prec
269 && state->lookaheads_rule[i]->prec->prec
270 && !bitset_disjoint_p (state->lookaheads[i], lookaheadset))
271 {
272 resolve_sr_conflict (state, i);
273 break;
274 }
275
276 /* Loop over all rules which require lookahead in this state. Check
277 for conflicts not resolved above. */
278 for (i = 0; i < state->nlookaheads; ++i)
279 {
280 if (!bitset_disjoint_p (state->lookaheads[i], lookaheadset))
281 conflicts[state->number] = 1;
282
283 bitset_or (lookaheadset, lookaheadset, state->lookaheads[i]);
284 }
285 }
286
287 void
288 conflicts_solve (void)
289 {
290 size_t i;
291
292 conflicts = XCALLOC (char, nstates);
293 shiftset = bitset_create (ntokens, BITSET_FIXED);
294 lookaheadset = bitset_create (ntokens, BITSET_FIXED);
295 obstack_init (&solved_conflicts_obstack);
296
297 for (i = 0; i < nstates; i++)
298 set_conflicts (states[i]);
299 }
300
301
302 /*---------------------------------------------.
303 | Count the number of shift/reduce conflicts. |
304 `---------------------------------------------*/
305
306 static int
307 count_sr_conflicts (state_t *state)
308 {
309 int i;
310 int src_count = 0;
311 shifts *shiftp = state->shifts;
312
313 if (!shiftp)
314 return 0;
315
316 bitset_zero (lookaheadset);
317 bitset_zero (shiftset);
318
319 for (i = 0; i < shiftp->nshifts && SHIFT_IS_SHIFT (shiftp, i); i++)
320 if (!SHIFT_IS_DISABLED (shiftp, i))
321 bitset_set (shiftset, SHIFT_SYMBOL (shiftp, i));
322
323 for (i = 0; i < state->nlookaheads; ++i)
324 bitset_or (lookaheadset, lookaheadset, state->lookaheads[i]);
325
326 bitset_and (lookaheadset, lookaheadset, shiftset);
327
328 src_count = bitset_count (lookaheadset);
329
330 return src_count;
331 }
332
333
334 /*----------------------------------------------.
335 | Count the number of reduce/reduce conflicts. |
336 `----------------------------------------------*/
337
338 static int
339 count_rr_conflicts (state_t *state)
340 {
341 int i;
342 int rrc_count = 0;
343
344 if (state->nlookaheads < 2)
345 return 0;
346
347 for (i = 0; i < ntokens; i++)
348 {
349 int count = 0;
350 int j;
351 for (j = 0; j < state->nlookaheads; ++j)
352 if (bitset_test (state->lookaheads[j], i))
353 count++;
354
355 if (count >= 2)
356 rrc_count++;
357 }
358
359 return rrc_count;
360 }
361
362 /*--------------------------------------------------------------.
363 | Return a human readable string which reports shift/reduce and |
364 | reduce/reduce conflict numbers (SRC_NUM, RRC_NUM). |
365 `--------------------------------------------------------------*/
366
367 static const char *
368 conflict_report (int src_num, int rrc_num)
369 {
370 static char res[4096];
371 char *cp = res;
372
373 if (src_num >= 1)
374 {
375 sprintf (cp, ngettext ("%d shift/reduce conflict",
376 "%d shift/reduce conflicts", src_num), src_num);
377 cp += strlen (cp);
378 }
379
380 if (src_num > 0 && rrc_num > 0)
381 {
382 sprintf (cp, " %s ", _("and"));
383 cp += strlen (cp);
384 }
385
386 if (rrc_num >= 1)
387 {
388 sprintf (cp, ngettext ("%d reduce/reduce conflict",
389 "%d reduce/reduce conflicts", rrc_num), rrc_num);
390 cp += strlen (cp);
391 }
392
393 *cp++ = '.';
394 *cp++ = '\n';
395 *cp++ = '\0';
396
397 return res;
398 }
399
400
401 /*-----------------------------------------------------------.
402 | Output the detailed description of states with conflicts. |
403 `-----------------------------------------------------------*/
404
405 void
406 conflicts_output (FILE *out)
407 {
408 bool printed_sth = FALSE;
409 size_t i;
410 for (i = 0; i < nstates; i++)
411 if (conflicts[i])
412 {
413 fprintf (out, _("State %d contains "), i);
414 fputs (conflict_report (count_sr_conflicts (states[i]),
415 count_rr_conflicts (states[i])), out);
416 printed_sth = TRUE;
417 }
418 if (printed_sth)
419 fputs ("\n\n", out);
420 }
421
422
423 /*------------------------------------------.
424 | Reporting the total number of conflicts. |
425 `------------------------------------------*/
426
427 void
428 conflicts_print (void)
429 {
430 size_t i;
431
432 /* Is the number of SR conflicts OK? Either EXPECTED_CONFLICTS is
433 not set, and then we want 0 SR, or else it is specified, in which
434 case we want equality. */
435 int src_ok = 0;
436
437 int src_total = 0;
438 int rrc_total = 0;
439
440 /* Conflicts by state. */
441 for (i = 0; i < nstates; i++)
442 if (conflicts[i])
443 {
444 src_total += count_sr_conflicts (states[i]);
445 rrc_total += count_rr_conflicts (states[i]);
446 }
447
448 src_ok = src_total == (expected_conflicts == -1 ? 0 : expected_conflicts);
449
450 /* If there are no RR conflicts, and as many SR conflicts as
451 expected, then there is nothing to report. */
452 if (!rrc_total && src_ok)
453 return;
454
455 /* Report the total number of conflicts on STDERR. */
456 if (yacc_flag)
457 {
458 /* If invoked with `--yacc', use the output format specified by
459 POSIX. */
460 fprintf (stderr, _("conflicts: "));
461 if (src_total > 0)
462 fprintf (stderr, _(" %d shift/reduce"), src_total);
463 if (src_total > 0 && rrc_total > 0)
464 fprintf (stderr, ",");
465 if (rrc_total > 0)
466 fprintf (stderr, _(" %d reduce/reduce"), rrc_total);
467 putc ('\n', stderr);
468 }
469 else
470 {
471 fprintf (stderr, _("%s contains "), infile);
472 fputs (conflict_report (src_total, rrc_total), stderr);
473 }
474
475 if (expected_conflicts != -1 && !src_ok)
476 {
477 complain_message_count++;
478 fprintf (stderr, ngettext ("expected %d shift/reduce conflict\n",
479 "expected %d shift/reduce conflicts\n",
480 expected_conflicts),
481 expected_conflicts);
482 }
483 }
484
485
486 void
487 conflicts_free (void)
488 {
489 XFREE (conflicts);
490 bitset_free (shiftset);
491 bitset_free (lookaheadset);
492 obstack_free (&solved_conflicts_obstack, NULL);
493 }