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.
5 This file is part of Bison, the GNU Compiler Compiler.
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)
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.
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
31 #include "conflicts.h"
35 /* -1 stands for not specified. */
36 int expected_conflicts
= -1;
37 static char *conflicts
= NULL
;
38 struct obstack solved_conflicts_obstack
;
40 static bitset shiftset
;
41 static bitset lookaheadset
;
45 enum conflict_resolution_e
55 /*----------------------------------------------------------------.
56 | Explain how an SR conflict between TOKEN and RULE was resolved: |
58 `----------------------------------------------------------------*/
61 log_resolution (rule_t
*rule
, symbol_number_t token
,
62 enum conflict_resolution_e resolution
)
64 if (report_flag
& report_solved_conflicts
)
66 /* The description of the resolution. */
69 case shift_resolution
:
70 case right_resolution
:
71 obstack_fgrow2 (&solved_conflicts_obstack
,
73 Conflict between rule %d and token %s resolved as shift"),
77 case reduce_resolution
:
79 obstack_fgrow2 (&solved_conflicts_obstack
,
81 Conflict between rule %d and token %s resolved as reduce"),
85 case nonassoc_resolution
:
86 obstack_fgrow2 (&solved_conflicts_obstack
,
88 Conflict between rule %d and token %s resolved as an error"),
97 case shift_resolution
:
98 obstack_fgrow2 (&solved_conflicts_obstack
,
101 symbols
[token
]->tag
);
104 case reduce_resolution
:
105 obstack_fgrow2 (&solved_conflicts_obstack
,
111 case left_resolution
:
112 obstack_fgrow1 (&solved_conflicts_obstack
,
114 symbols
[token
]->tag
);
117 case right_resolution
:
118 obstack_fgrow1 (&solved_conflicts_obstack
,
120 symbols
[token
]->tag
);
122 case nonassoc_resolution
:
123 obstack_fgrow1 (&solved_conflicts_obstack
,
125 symbols
[token
]->tag
);
128 obstack_sgrow (&solved_conflicts_obstack
, ".\n");
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 `------------------------------------------------------------------*/
140 flush_shift (state_t
*state
, int token
)
142 transitions_t
*transitions
= state
->transitions
;
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
);
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 `-------------------------------------------------------------------*/
160 flush_reduce (bitset lookaheads
, int token
)
162 bitset_reset (lookaheads
, token
);
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. |
172 | LOOKAHEAD is the number of the lookahead bitset to consider. |
174 | ERRS can be used to store discovered explicit errors. |
175 `------------------------------------------------------------------*/
178 resolve_sr_conflict (state_t
*state
, int lookahead
,
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
];
188 for (i
= 0; i
< ntokens
; i
++)
189 if (bitset_test (lookaheads
, i
)
190 && bitset_test (lookaheadset
, i
)
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
)
198 log_resolution (redrule
, i
, reduce_resolution
);
199 flush_shift (state
, i
);
201 else if (symbols
[i
]->prec
> redprec
)
203 log_resolution (redrule
, i
, shift_resolution
);
204 flush_reduce (lookaheads
, i
);
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. */
212 switch (symbols
[i
]->assoc
)
215 log_resolution (redrule
, i
, right_resolution
);
216 flush_reduce (lookaheads
, i
);
220 log_resolution (redrule
, i
, left_resolution
);
221 flush_shift (state
, i
);
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
++] = symbols
[i
];
233 assert (symbols
[i
]->assoc
!= undef_assoc
);
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
);
242 if (obstack_object_size (&solved_conflicts_obstack
))
244 obstack_1grow (&solved_conflicts_obstack
, '\0');
245 state
->solved_conflicts
= obstack_finish (&solved_conflicts_obstack
);
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 `-------------------------------------------------------------------*/
258 set_conflicts (state_t
*state
, symbol_t
**errs
)
261 transitions_t
*transitions
= state
->transitions
;
263 if (state
->consistent
)
266 bitset_zero (lookaheadset
);
268 FOR_EACH_SHIFT (transitions
, i
)
269 bitset_set (lookaheadset
, TRANSITION_SYMBOL (transitions
, i
));
271 /* Loop over all rules which require lookahead in this state. First
272 check for shift-reduce conflict, and try to resolve using
274 for (i
= 0; i
< state
->nlookaheads
; ++i
)
275 if (state
->lookaheads_rule
[i
]->prec
276 && state
->lookaheads_rule
[i
]->prec
->prec
277 && !bitset_disjoint_p (state
->lookaheads
[i
], lookaheadset
))
279 resolve_sr_conflict (state
, i
, errs
);
283 /* Loop over all rules which require lookahead in this state. Check
284 for conflicts not resolved above. */
285 for (i
= 0; i
< state
->nlookaheads
; ++i
)
287 if (!bitset_disjoint_p (state
->lookaheads
[i
], lookaheadset
))
288 conflicts
[state
->number
] = 1;
290 bitset_or (lookaheadset
, lookaheadset
, state
->lookaheads
[i
]);
295 /*----------------------------------------------------------------.
296 | Solve all the S/R conflicts using the precedence/associativity, |
297 | and flag as inconsistent the states that still have conflicts. |
298 `----------------------------------------------------------------*/
301 conflicts_solve (void)
304 /* List of lookaheads on which we explicitly raise a parse error. */
305 symbol_t
**errs
= XMALLOC (symbol_t
*, ntokens
+ 1);
307 conflicts
= XCALLOC (char, nstates
);
308 shiftset
= bitset_create (ntokens
, BITSET_FIXED
);
309 lookaheadset
= bitset_create (ntokens
, BITSET_FIXED
);
310 obstack_init (&solved_conflicts_obstack
);
312 for (i
= 0; i
< nstates
; i
++)
314 set_conflicts (states
[i
], errs
);
316 /* For uniformity of the code, make sure all the states have a valid
318 if (!states
[i
]->errs
)
319 states
[i
]->errs
= errs_new (0, 0);
326 /*---------------------------------------------.
327 | Count the number of shift/reduce conflicts. |
328 `---------------------------------------------*/
331 count_sr_conflicts (state_t
*state
)
335 transitions_t
*transitions
= state
->transitions
;
340 bitset_zero (lookaheadset
);
341 bitset_zero (shiftset
);
343 FOR_EACH_SHIFT (transitions
, i
)
344 bitset_set (shiftset
, TRANSITION_SYMBOL (transitions
, i
));
346 for (i
= 0; i
< state
->nlookaheads
; ++i
)
347 bitset_or (lookaheadset
, lookaheadset
, state
->lookaheads
[i
]);
349 bitset_and (lookaheadset
, lookaheadset
, shiftset
);
351 src_count
= bitset_count (lookaheadset
);
357 /*----------------------------------------------------------------.
358 | Count the number of reduce/reduce conflicts. If ONE_PER_TOKEN, |
359 | count one conflict for each token that has any reduce/reduce |
360 | conflicts. Otherwise, count one conflict for each pair of |
361 | conflicting reductions. |
362 +`----------------------------------------------------------------*/
365 count_rr_conflicts (state_t
*state
, int one_per_token
)
370 if (state
->nlookaheads
< 2)
373 for (i
= 0; i
< ntokens
; i
++)
377 for (j
= 0; j
< state
->nlookaheads
; ++j
)
378 if (bitset_test (state
->lookaheads
[j
], i
))
382 rrc_count
+= one_per_token
? 1 : count
-1;
389 /*--------------------------------------------------------------.
390 | Return a human readable string which reports shift/reduce and |
391 | reduce/reduce conflict numbers (SRC_NUM, RRC_NUM). |
392 `--------------------------------------------------------------*/
395 conflict_report (int src_num
, int rrc_num
)
397 static char res
[4096];
402 sprintf (cp
, ngettext ("%d shift/reduce conflict",
403 "%d shift/reduce conflicts", src_num
), src_num
);
407 if (src_num
> 0 && rrc_num
> 0)
409 sprintf (cp
, " %s ", _("and"));
415 sprintf (cp
, ngettext ("%d reduce/reduce conflict",
416 "%d reduce/reduce conflicts", rrc_num
), rrc_num
);
426 /*----------------------------------------------------------------.
427 | Same as above, but report the number of conflicts a` la POSIX. |
428 `----------------------------------------------------------------*/
431 conflict_report_yacc (int src_num
, int rrc_num
)
433 /* If invoked with `--yacc', use the output format specified by
435 fprintf (stderr
, _("conflicts: "));
437 fprintf (stderr
, _(" %d shift/reduce"), src_num
);
438 if (src_num
> 0 && rrc_num
> 0)
439 fprintf (stderr
, ",");
441 fprintf (stderr
, _(" %d reduce/reduce"), rrc_num
);
446 /*-----------------------------------------------------------.
447 | Output the detailed description of states with conflicts. |
448 `-----------------------------------------------------------*/
451 conflicts_output (FILE *out
)
453 bool printed_sth
= FALSE
;
454 bool *used_rules
= XCALLOC (bool, nrules
);
456 for (i
= 0; i
< nstates
; i
++)
458 state_t
*s
= states
[i
];
460 for (j
= 0; j
< s
->reductions
->num
; ++j
)
461 used_rules
[s
->reductions
->rules
[j
]->number
] = TRUE
;
464 fprintf (out
, _("State %d contains "), i
);
465 fprintf (out
, "%s.\n",
466 conflict_report (count_sr_conflicts (s
),
467 count_rr_conflicts (s
, TRUE
)));
474 for (i
= 0; i
< nstates
; i
++)
476 state_t
*s
= states
[i
];
477 reductions_t
*r
= s
->reductions
;
479 for (j
= 0; j
< r
->num
; ++j
)
480 if (!used_rules
[r
->rules
[j
]->number
])
482 LOCATION_PRINT (stderr
, r
->rules
[j
]->location
);
483 fprintf (stderr
, ": %s: %s: ",
485 _("rule never reduced because of conflicts"));
486 rule_print (r
->rules
[j
], stderr
);
492 /*--------------------------------------------------------.
493 | Total the number of S/R and R/R conflicts. Unlike the |
494 | code in conflicts_output, however, count EACH pair of |
495 | reductions for the same state and lookahead as one |
497 `--------------------------------------------------------*/
500 conflicts_total_count (void)
505 /* Conflicts by state. */
507 for (i
= 0; i
< nstates
; i
++)
510 count
+= count_sr_conflicts (states
[i
]);
511 count
+= count_rr_conflicts (states
[i
], FALSE
);
517 /*------------------------------------------.
518 | Reporting the total number of conflicts. |
519 `------------------------------------------*/
522 conflicts_print (void)
524 /* Is the number of SR conflicts OK? Either EXPECTED_CONFLICTS is
525 not set, and then we want 0 SR, or else it is specified, in which
526 case we want equality. */
532 /* Conflicts by state. */
536 for (i
= 0; i
< nstates
; i
++)
539 src_total
+= count_sr_conflicts (states
[i
]);
540 rrc_total
+= count_rr_conflicts (states
[i
], TRUE
);
544 src_ok
= src_total
== (expected_conflicts
== -1 ? 0 : expected_conflicts
);
546 /* If there are no RR conflicts, and as many SR conflicts as
547 expected, then there is nothing to report. */
548 if (!rrc_total
&& src_ok
)
551 /* Report the total number of conflicts on STDERR. */
553 conflict_report_yacc (src_total
, rrc_total
);
555 warn ("%s", conflict_report (src_total
, rrc_total
));
557 if (expected_conflicts
!= -1 && !src_ok
)
558 complain (ngettext ("expected %d shift/reduce conflict",
559 "expected %d shift/reduce conflicts",
566 conflicts_free (void)
569 bitset_free (shiftset
);
570 bitset_free (lookaheadset
);
571 obstack_free (&solved_conflicts_obstack
, NULL
);