]> git.saurik.com Git - bison.git/blob - src/conflicts.c
(CHARACTER): Remove unused token. All uses removed.
[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, symbol_number_t 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 right_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 left_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 transitions_t *transitions = state->transitions;
143 int i;
144
145 bitset_reset (lookaheadset, token);
146 for (i = 0; i < transitions->num; i++)
147 if (!TRANSITION_IS_DISABLED (transitions, i)
148 && TRANSITION_SYMBOL (transitions, i) == token)
149 TRANSITION_DISABLE (transitions, i);
150 }
151
152
153 /*-------------------------------------------------------------------.
154 | Turn off the reduce recorded for the specified token for the |
155 | specified lookahead. Used when we resolve a shift-reduce conflict |
156 | in favor of the shift. |
157 `-------------------------------------------------------------------*/
158
159 static void
160 flush_reduce (bitset lookaheads, int token)
161 {
162 bitset_reset (lookaheads, token);
163 }
164
165
166 /*------------------------------------------------------------------.
167 | Attempt to resolve shift-reduce conflict for one rule by means of |
168 | precedence declarations. It has already been checked that the |
169 | rule has a precedence. A conflict is resolved by modifying the |
170 | shift or reduce tables so that there is no longer a conflict. |
171 | |
172 | LOOKAHEAD is the number of the lookahead bitset to consider. |
173 | |
174 | ERRS can be used to store discovered explicit errors. |
175 `------------------------------------------------------------------*/
176
177 static void
178 resolve_sr_conflict (state_t *state, int ruleno,
179 symbol_t **errs)
180 {
181 symbol_number_t i;
182 reductions_t *reds = state->reductions;
183 /* Find the rule to reduce by to get precedence of reduction. */
184 rule_t *redrule = reds->rules[ruleno];
185 int redprec = redrule->prec->prec;
186 bitset lookaheads = reds->lookaheads[ruleno];
187 int nerrs = 0;
188
189 for (i = 0; i < ntokens; i++)
190 if (bitset_test (lookaheads, i)
191 && bitset_test (lookaheadset, i)
192 && symbols[i]->prec)
193 {
194 /* Shift-reduce conflict occurs for token number i
195 and it has a precedence.
196 The precedence of shifting is that of token i. */
197 if (symbols[i]->prec < redprec)
198 {
199 log_resolution (redrule, i, reduce_resolution);
200 flush_shift (state, i);
201 }
202 else if (symbols[i]->prec > redprec)
203 {
204 log_resolution (redrule, i, shift_resolution);
205 flush_reduce (lookaheads, i);
206 }
207 else
208 /* Matching precedence levels.
209 For left association, keep only the reduction.
210 For right association, keep only the shift.
211 For nonassociation, keep neither. */
212
213 switch (symbols[i]->assoc)
214 {
215 case right_assoc:
216 log_resolution (redrule, i, right_resolution);
217 flush_reduce (lookaheads, i);
218 break;
219
220 case left_assoc:
221 log_resolution (redrule, i, left_resolution);
222 flush_shift (state, i);
223 break;
224
225 case non_assoc:
226 log_resolution (redrule, i, nonassoc_resolution);
227 flush_shift (state, i);
228 flush_reduce (lookaheads, i);
229 /* Record an explicit error for this token. */
230 errs[nerrs++] = symbols[i];
231 break;
232
233 case undef_assoc:
234 assert (symbols[i]->assoc != undef_assoc);
235 break;
236 }
237 }
238
239 /* Some tokens have been explicitly made errors. Allocate a
240 permanent errs structure for this state, to record them. */
241 state_errs_set (state, nerrs, errs);
242
243 if (obstack_object_size (&solved_conflicts_obstack))
244 {
245 obstack_1grow (&solved_conflicts_obstack, '\0');
246 state->solved_conflicts = obstack_finish (&solved_conflicts_obstack);
247 }
248 }
249
250
251 /*-------------------------------------------------------------------.
252 | Solve the S/R conflicts of STATE using the |
253 | precedence/associativity, and flag it inconsistent if it still has |
254 | conflicts. ERRS can be used as storage to compute the list of |
255 | lookaheads on which this STATE raises a parse error (%nonassoc). |
256 `-------------------------------------------------------------------*/
257
258 static void
259 set_conflicts (state_t *state, symbol_t **errs)
260 {
261 int i;
262 transitions_t *transitions = state->transitions;
263 reductions_t *reds = state->reductions;
264
265 if (state->consistent)
266 return;
267
268 bitset_zero (lookaheadset);
269
270 FOR_EACH_SHIFT (transitions, i)
271 bitset_set (lookaheadset, TRANSITION_SYMBOL (transitions, i));
272
273 /* Loop over all rules which require lookahead in this state. First
274 check for shift-reduce conflict, and try to resolve using
275 precedence. */
276 for (i = 0; i < reds->num; ++i)
277 if (reds->rules[i]->prec && reds->rules[i]->prec->prec
278 && !bitset_disjoint_p (reds->lookaheads[i], lookaheadset))
279 {
280 resolve_sr_conflict (state, i, errs);
281 break;
282 }
283
284 /* Loop over all rules which require lookahead in this state. Check
285 for conflicts not resolved above. */
286 for (i = 0; i < reds->num; ++i)
287 {
288 if (!bitset_disjoint_p (reds->lookaheads[i], lookaheadset))
289 conflicts[state->number] = 1;
290
291 bitset_or (lookaheadset, lookaheadset, reds->lookaheads[i]);
292 }
293 }
294
295
296 /*----------------------------------------------------------------.
297 | Solve all the S/R conflicts using the precedence/associativity, |
298 | and flag as inconsistent the states that still have conflicts. |
299 `----------------------------------------------------------------*/
300
301 void
302 conflicts_solve (void)
303 {
304 state_number_t i;
305 /* List of lookaheads on which we explicitly raise a parse error. */
306 symbol_t **errs = XMALLOC (symbol_t *, ntokens + 1);
307
308 conflicts = XCALLOC (char, nstates);
309 shiftset = bitset_create (ntokens, BITSET_FIXED);
310 lookaheadset = bitset_create (ntokens, BITSET_FIXED);
311 obstack_init (&solved_conflicts_obstack);
312
313 for (i = 0; i < nstates; i++)
314 {
315 set_conflicts (states[i], errs);
316
317 /* For uniformity of the code, make sure all the states have a valid
318 `errs' member. */
319 if (!states[i]->errs)
320 states[i]->errs = errs_new (0, 0);
321 }
322
323 free (errs);
324 }
325
326
327 /*---------------------------------------------.
328 | Count the number of shift/reduce conflicts. |
329 `---------------------------------------------*/
330
331 static int
332 count_sr_conflicts (state_t *state)
333 {
334 int i;
335 int src_count = 0;
336 transitions_t *transitions = state->transitions;
337 reductions_t *reds = state->reductions;
338
339 if (!transitions)
340 return 0;
341
342 bitset_zero (lookaheadset);
343 bitset_zero (shiftset);
344
345 FOR_EACH_SHIFT (transitions, i)
346 bitset_set (shiftset, TRANSITION_SYMBOL (transitions, i));
347
348 for (i = 0; i < reds->num; ++i)
349 bitset_or (lookaheadset, lookaheadset, reds->lookaheads[i]);
350
351 bitset_and (lookaheadset, lookaheadset, shiftset);
352
353 src_count = bitset_count (lookaheadset);
354
355 return src_count;
356 }
357
358
359 /*----------------------------------------------------------------.
360 | Count the number of reduce/reduce conflicts. If ONE_PER_TOKEN, |
361 | count one conflict for each token that has any reduce/reduce |
362 | conflicts. Otherwise, count one conflict for each pair of |
363 | conflicting reductions. |
364 +`----------------------------------------------------------------*/
365
366 static int
367 count_rr_conflicts (state_t *state, int one_per_token)
368 {
369 int i;
370 reductions_t *reds = state->reductions;
371 int rrc_count = 0;
372
373 for (i = 0; i < ntokens; i++)
374 {
375 int count = 0;
376 int j;
377 for (j = 0; j < reds->num; ++j)
378 if (bitset_test (reds->lookaheads[j], i))
379 count++;
380
381 if (count >= 2)
382 rrc_count += one_per_token ? 1 : count-1;
383 }
384
385 return rrc_count;
386 }
387
388
389 /*--------------------------------------------------------------.
390 | Return a human readable string which reports shift/reduce and |
391 | reduce/reduce conflict numbers (SRC_NUM, RRC_NUM). |
392 `--------------------------------------------------------------*/
393
394 static const char *
395 conflict_report (int src_num, int rrc_num)
396 {
397 static char res[4096];
398 char *cp = res;
399
400 if (src_num >= 1)
401 {
402 sprintf (cp, ngettext ("%d shift/reduce conflict",
403 "%d shift/reduce conflicts", src_num), src_num);
404 cp += strlen (cp);
405 }
406
407 if (src_num > 0 && rrc_num > 0)
408 {
409 sprintf (cp, " %s ", _("and"));
410 cp += strlen (cp);
411 }
412
413 if (rrc_num >= 1)
414 {
415 sprintf (cp, ngettext ("%d reduce/reduce conflict",
416 "%d reduce/reduce conflicts", rrc_num), rrc_num);
417 cp += strlen (cp);
418 }
419
420 *cp++ = '\0';
421
422 return res;
423 }
424
425
426 /*----------------------------------------------------------------.
427 | Same as above, but report the number of conflicts a` la POSIX. |
428 `----------------------------------------------------------------*/
429
430 static void
431 conflict_report_yacc (int src_num, int rrc_num)
432 {
433 /* If invoked with `--yacc', use the output format specified by
434 POSIX. */
435 fprintf (stderr, _("conflicts: "));
436 if (src_num > 0)
437 fprintf (stderr, _(" %d shift/reduce"), src_num);
438 if (src_num > 0 && rrc_num > 0)
439 fprintf (stderr, ",");
440 if (rrc_num > 0)
441 fprintf (stderr, _(" %d reduce/reduce"), rrc_num);
442 putc ('\n', stderr);
443 }
444
445
446 /*-----------------------------------------------------------.
447 | Output the detailed description of states with conflicts. |
448 `-----------------------------------------------------------*/
449
450 void
451 conflicts_output (FILE *out)
452 {
453 bool printed_sth = false;
454 state_number_t i;
455 for (i = 0; i < nstates; i++)
456 {
457 state_t *s = states[i];
458 if (conflicts[i])
459 {
460 fprintf (out, _("State %d contains "), i);
461 fprintf (out, "%s.\n",
462 conflict_report (count_sr_conflicts (s),
463 count_rr_conflicts (s, true)));
464 printed_sth = true;
465 }
466 }
467 if (printed_sth)
468 fputs ("\n\n", out);
469 }
470
471 /*--------------------------------------------------------.
472 | Total the number of S/R and R/R conflicts. Unlike the |
473 | code in conflicts_output, however, count EACH pair of |
474 | reductions for the same state and lookahead as one |
475 | conflict. |
476 `--------------------------------------------------------*/
477
478 int
479 conflicts_total_count (void)
480 {
481 state_number_t i;
482 int count;
483
484 /* Conflicts by state. */
485 count = 0;
486 for (i = 0; i < nstates; i++)
487 if (conflicts[i])
488 {
489 count += count_sr_conflicts (states[i]);
490 count += count_rr_conflicts (states[i], false);
491 }
492 return count;
493 }
494
495
496 /*------------------------------------------.
497 | Reporting the total number of conflicts. |
498 `------------------------------------------*/
499
500 void
501 conflicts_print (void)
502 {
503 /* Is the number of SR conflicts OK? Either EXPECTED_CONFLICTS is
504 not set, and then we want 0 SR, or else it is specified, in which
505 case we want equality. */
506 int src_ok = 0;
507
508 int src_total = 0;
509 int rrc_total = 0;
510
511 /* Conflicts by state. */
512 {
513 state_number_t i;
514
515 for (i = 0; i < nstates; i++)
516 if (conflicts[i])
517 {
518 src_total += count_sr_conflicts (states[i]);
519 rrc_total += count_rr_conflicts (states[i], true);
520 }
521 }
522
523 src_ok = src_total == (expected_conflicts == -1 ? 0 : expected_conflicts);
524
525 /* If there are no RR conflicts, and as many SR conflicts as
526 expected, then there is nothing to report. */
527 if (!rrc_total && src_ok)
528 return;
529
530 /* Report the total number of conflicts on STDERR. */
531 if (yacc_flag)
532 conflict_report_yacc (src_total, rrc_total);
533 else
534 warn ("%s", conflict_report (src_total, rrc_total));
535
536 if (expected_conflicts != -1 && !src_ok)
537 complain (ngettext ("expected %d shift/reduce conflict",
538 "expected %d shift/reduce conflicts",
539 expected_conflicts),
540 expected_conflicts);
541 }
542
543
544 void
545 conflicts_free (void)
546 {
547 XFREE (conflicts);
548 bitset_free (shiftset);
549 bitset_free (lookaheadset);
550 obstack_free (&solved_conflicts_obstack, NULL);
551 }