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