]> git.saurik.com Git - bison.git/blame_incremental - src/conflicts.c
Stop storing rules from 1 to nrules + 1.
[bison.git] / src / conflicts.c
... / ...
CommitLineData
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. */
36int expected_conflicts = -1;
37static char *conflicts = NULL;
38struct obstack solved_conflicts_obstack;
39
40static bitset shiftset;
41static bitset lookaheadset;
42
43\f
44
45enum 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
60static inline void
61log_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
139static void
140flush_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
159static void
160flush_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
177static void
178resolve_sr_conflict (state_t *state, int lookahead,
179 symbol_number_t *errs)
180{
181 symbol_number_t i;
182 /* Find the rule to reduce by to get precedence of reduction. */
183 rule_t *redrule = state->lookaheads_rule[lookahead];
184 int redprec = redrule->prec->prec;
185 bitset lookaheads = state->lookaheads[lookahead];
186 int nerrs = 0;
187
188 for (i = 0; i < ntokens; i++)
189 if (bitset_test (lookaheads, i)
190 && bitset_test (lookaheadset, i)
191 && symbols[i]->prec)
192 {
193 /* Shift-reduce conflict occurs for token number i
194 and it has a precedence.
195 The precedence of shifting is that of token i. */
196 if (symbols[i]->prec < redprec)
197 {
198 log_resolution (redrule, i, reduce_resolution);
199 flush_shift (state, i);
200 }
201 else if (symbols[i]->prec > redprec)
202 {
203 log_resolution (redrule, i, shift_resolution);
204 flush_reduce (lookaheads, i);
205 }
206 else
207 /* Matching precedence levels.
208 For left association, keep only the reduction.
209 For right association, keep only the shift.
210 For nonassociation, keep neither. */
211
212 switch (symbols[i]->assoc)
213 {
214 case right_assoc:
215 log_resolution (redrule, i, right_resolution);
216 flush_reduce (lookaheads, i);
217 break;
218
219 case left_assoc:
220 log_resolution (redrule, i, left_resolution);
221 flush_shift (state, i);
222 break;
223
224 case non_assoc:
225 log_resolution (redrule, i, nonassoc_resolution);
226 flush_shift (state, i);
227 flush_reduce (lookaheads, i);
228 /* Record an explicit error for this token. */
229 errs[nerrs++] = i;
230 break;
231
232 case undef_assoc:
233 assert (symbols[i]->assoc != undef_assoc);
234 break;
235 }
236 }
237
238 /* Some tokens have been explicitly made errors. Allocate a
239 permanent errs structure for this state, to record them. */
240 state_errs_set (state, nerrs, errs);
241
242 if (obstack_object_size (&solved_conflicts_obstack))
243 {
244 obstack_1grow (&solved_conflicts_obstack, '\0');
245 state->solved_conflicts = obstack_finish (&solved_conflicts_obstack);
246 }
247}
248
249
250/*-------------------------------------------------------------------.
251| Solve the S/R conflicts of STATE using the |
252| precedence/associativity, and flag it inconsistent if it still has |
253| conflicts. ERRS can be used as storage to compute the list of |
254| lookaheads on which this STATE raises a parse error (%nonassoc). |
255`-------------------------------------------------------------------*/
256
257static void
258set_conflicts (state_t *state, symbol_number_t *errs)
259{
260 int i;
261 transitions_t *transitions = state->transitions;
262
263 if (state->consistent)
264 return;
265
266 bitset_zero (lookaheadset);
267
268 for (i = 0; i < transitions->num && TRANSITION_IS_SHIFT (transitions, i); i++)
269 if (!TRANSITION_IS_DISABLED (transitions, i))
270 bitset_set (lookaheadset, TRANSITION_SYMBOL (transitions, i));
271
272 /* Loop over all rules which require lookahead in this state. First
273 check for shift-reduce conflict, and try to resolve using
274 precedence. */
275 for (i = 0; i < state->nlookaheads; ++i)
276 if (state->lookaheads_rule[i]->prec
277 && state->lookaheads_rule[i]->prec->prec
278 && !bitset_disjoint_p (state->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 < state->nlookaheads; ++i)
287 {
288 if (!bitset_disjoint_p (state->lookaheads[i], lookaheadset))
289 conflicts[state->number] = 1;
290
291 bitset_or (lookaheadset, lookaheadset, state->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
301void
302conflicts_solve (void)
303{
304 state_number_t i;
305 /* List of lookaheads on which we explicitly raise a parse error. */
306 symbol_number_t *errs = XMALLOC (symbol_number_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
331static int
332count_sr_conflicts (state_t *state)
333{
334 int i;
335 int src_count = 0;
336 transitions_t *transitions = state->transitions;
337
338 if (!transitions)
339 return 0;
340
341 bitset_zero (lookaheadset);
342 bitset_zero (shiftset);
343
344 for (i = 0; i < transitions->num && TRANSITION_IS_SHIFT (transitions, i); i++)
345 if (!TRANSITION_IS_DISABLED (transitions, i))
346 bitset_set (shiftset, TRANSITION_SYMBOL (transitions, i));
347
348 for (i = 0; i < state->nlookaheads; ++i)
349 bitset_or (lookaheadset, lookaheadset, state->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
366static int
367count_rr_conflicts (state_t *state, int one_per_token)
368{
369 int i;
370 int rrc_count = 0;
371
372 if (state->nlookaheads < 2)
373 return 0;
374
375 for (i = 0; i < ntokens; i++)
376 {
377 int count = 0;
378 int j;
379 for (j = 0; j < state->nlookaheads; ++j)
380 if (bitset_test (state->lookaheads[j], i))
381 count++;
382
383 if (count >= 2)
384 rrc_count += one_per_token ? 1 : count-1;
385 }
386
387 return rrc_count;
388}
389
390/*--------------------------------------------------------------.
391| Return a human readable string which reports shift/reduce and |
392| reduce/reduce conflict numbers (SRC_NUM, RRC_NUM). |
393`--------------------------------------------------------------*/
394
395static const char *
396conflict_report (int src_num, int rrc_num)
397{
398 static char res[4096];
399 char *cp = res;
400
401 if (src_num >= 1)
402 {
403 sprintf (cp, ngettext ("%d shift/reduce conflict",
404 "%d shift/reduce conflicts", src_num), src_num);
405 cp += strlen (cp);
406 }
407
408 if (src_num > 0 && rrc_num > 0)
409 {
410 sprintf (cp, " %s ", _("and"));
411 cp += strlen (cp);
412 }
413
414 if (rrc_num >= 1)
415 {
416 sprintf (cp, ngettext ("%d reduce/reduce conflict",
417 "%d reduce/reduce conflicts", rrc_num), rrc_num);
418 cp += strlen (cp);
419 }
420
421 *cp++ = '.';
422 *cp++ = '\n';
423 *cp++ = '\0';
424
425 return res;
426}
427
428
429/*-----------------------------------------------------------.
430| Output the detailed description of states with conflicts. |
431`-----------------------------------------------------------*/
432
433void
434conflicts_output (FILE *out)
435{
436 bool printed_sth = FALSE;
437 state_number_t i;
438 for (i = 0; i < nstates; i++)
439 if (conflicts[i])
440 {
441 fprintf (out, _("State %d contains "), i);
442 fputs (conflict_report (count_sr_conflicts (states[i]),
443 count_rr_conflicts (states[i], TRUE)), out);
444 printed_sth = TRUE;
445 }
446 if (printed_sth)
447 fputs ("\n\n", out);
448}
449
450/*--------------------------------------------------------.
451| Total the number of S/R and R/R conflicts. Unlike the |
452| code in conflicts_output, however, count EACH pair of |
453| reductions for the same state and lookahead as one |
454| conflict. |
455`--------------------------------------------------------*/
456
457int
458conflicts_total_count (void)
459{
460 state_number_t i;
461 int count;
462
463 /* Conflicts by state. */
464 count = 0;
465 for (i = 0; i < nstates; i++)
466 if (conflicts[i])
467 {
468 count += count_sr_conflicts (states[i]);
469 count += count_rr_conflicts (states[i], FALSE);
470 }
471 return count;
472}
473
474
475/*------------------------------------------.
476| Reporting the total number of conflicts. |
477`------------------------------------------*/
478
479void
480conflicts_print (void)
481{
482 /* Is the number of SR conflicts OK? Either EXPECTED_CONFLICTS is
483 not set, and then we want 0 SR, or else it is specified, in which
484 case we want equality. */
485 int src_ok = 0;
486
487 int src_total = 0;
488 int rrc_total = 0;
489
490 /* Conflicts by state. */
491 {
492 state_number_t i;
493
494 for (i = 0; i < nstates; i++)
495 if (conflicts[i])
496 {
497 src_total += count_sr_conflicts (states[i]);
498 rrc_total += count_rr_conflicts (states[i], TRUE);
499 }
500 }
501
502 src_ok = src_total == (expected_conflicts == -1 ? 0 : expected_conflicts);
503
504 /* If there are no RR conflicts, and as many SR conflicts as
505 expected, then there is nothing to report. */
506 if (!rrc_total && src_ok)
507 return;
508
509 /* Report the total number of conflicts on STDERR. */
510 if (yacc_flag)
511 {
512 /* If invoked with `--yacc', use the output format specified by
513 POSIX. */
514 fprintf (stderr, _("conflicts: "));
515 if (src_total > 0)
516 fprintf (stderr, _(" %d shift/reduce"), src_total);
517 if (src_total > 0 && rrc_total > 0)
518 fprintf (stderr, ",");
519 if (rrc_total > 0)
520 fprintf (stderr, _(" %d reduce/reduce"), rrc_total);
521 putc ('\n', stderr);
522 }
523 else
524 {
525 fprintf (stderr, _("%s contains "), infile);
526 fputs (conflict_report (src_total, rrc_total), stderr);
527 }
528
529 if (expected_conflicts != -1 && !src_ok)
530 {
531 complain_message_count++;
532 fprintf (stderr, ngettext ("expected %d shift/reduce conflict\n",
533 "expected %d shift/reduce conflicts\n",
534 expected_conflicts),
535 expected_conflicts);
536 }
537}
538
539
540void
541conflicts_free (void)
542{
543 XFREE (conflicts);
544 bitset_free (shiftset);
545 bitset_free (lookaheadset);
546 obstack_free (&solved_conflicts_obstack, NULL);
547}