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
56 log_resolution (int lookahead
, int token
,
57 enum conflict_resolution_e resolution
)
59 if (report_flag
& report_solved_conflicts
)
61 /* The description of the resolution. */
64 case shift_resolution
:
66 obstack_fgrow2 (&solved_conflicts_obstack
,
68 Conflict between rule %d and token %s resolved as shift"),
69 LArule
[lookahead
]->number
,
72 case reduce_resolution
:
73 case right_resolution
:
74 obstack_fgrow2 (&solved_conflicts_obstack
,
76 Conflict between rule %d and token %s resolved as reduce"),
77 LArule
[lookahead
]->number
,
80 case nonassoc_resolution
:
81 obstack_fgrow2 (&solved_conflicts_obstack
,
83 Conflict between rule %d and token %s resolved as an error"),
84 LArule
[lookahead
]->number
,
92 case shift_resolution
:
93 obstack_fgrow2 (&solved_conflicts_obstack
,
95 LArule
[lookahead
]->prec
->tag
,
99 case reduce_resolution
:
100 obstack_fgrow2 (&solved_conflicts_obstack
,
103 LArule
[lookahead
]->prec
->tag
);
106 case left_resolution
:
107 obstack_fgrow1 (&solved_conflicts_obstack
,
109 symbols
[token
]->tag
);
112 case right_resolution
:
113 obstack_fgrow1 (&solved_conflicts_obstack
,
115 symbols
[token
]->tag
);
117 case nonassoc_resolution
:
118 obstack_fgrow1 (&solved_conflicts_obstack
,
120 symbols
[token
]->tag
);
123 obstack_sgrow (&solved_conflicts_obstack
, ".\n");
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 `------------------------------------------------------------------*/
135 flush_shift (state_t
*state
, int token
)
137 shifts
*shiftp
= state
->shifts
;
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
);
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 `-------------------------------------------------------------------*/
154 flush_reduce (int lookahead
, int token
)
156 bitset_reset (LA
[lookahead
], token
);
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 `------------------------------------------------------------------*/
168 resolve_sr_conflict (state_t
*state
, int lookahead
)
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);
176 for (i
= 0; i
< ntokens
; i
++)
177 if (bitset_test (LA
[lookahead
], i
)
178 && bitset_test (lookaheadset
, i
)
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
)
186 log_resolution (lookahead
, i
, reduce_resolution
);
187 flush_shift (state
, i
);
189 else if (symbols
[i
]->prec
> redprec
)
191 log_resolution (lookahead
, i
, shift_resolution
);
192 flush_reduce (lookahead
, i
);
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. */
200 switch (symbols
[i
]->assoc
)
203 log_resolution (lookahead
, i
, right_resolution
);
204 flush_reduce (lookahead
, i
);
208 log_resolution (lookahead
, i
, left_resolution
);
209 flush_shift (state
, i
);
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
;
222 /* Some tokens have been explicitly made errors. Allocate a
223 permanent errs structure for this state, to record them. */
224 state
->errs
= errs_dup (errp
);
227 if (obstack_object_size (&solved_conflicts_obstack
))
229 obstack_1grow (&solved_conflicts_obstack
, '\0');
230 state
->solved_conflicts
= obstack_finish (&solved_conflicts_obstack
);
236 set_conflicts (state_t
*state
)
241 if (state
->consistent
)
244 bitset_zero (lookaheadset
);
246 shiftp
= state
->shifts
;
247 for (i
= 0; i
< shiftp
->nshifts
&& SHIFT_IS_SHIFT (shiftp
, i
); i
++)
248 if (!SHIFT_IS_DISABLED (shiftp
, i
))
249 bitset_set (lookaheadset
, SHIFT_SYMBOL (shiftp
, i
));
251 /* Loop over all rules which require lookahead in this state. First
252 check for shift-reduce conflict, and try to resolve using
254 for (i
= 0; i
< state
->nlookaheads
; ++i
)
255 if (LArule
[state
->lookaheadsp
+ i
]->prec
256 && LArule
[state
->lookaheadsp
+ i
]->prec
->prec
257 && !bitset_disjoint_p (LA
[state
->lookaheadsp
+ i
], lookaheadset
))
259 resolve_sr_conflict (state
, state
->lookaheadsp
+ i
);
263 /* Loop over all rules which require lookahead in this state. Check
264 for conflicts not resolved above. */
265 for (i
= 0; i
< state
->nlookaheads
; ++i
)
267 if (!bitset_disjoint_p (LA
[state
->lookaheadsp
+ i
], lookaheadset
))
268 conflicts
[state
->number
] = 1;
270 bitset_or (lookaheadset
, lookaheadset
, LA
[state
->lookaheadsp
+ i
]);
275 conflicts_solve (void)
279 conflicts
= XCALLOC (char, nstates
);
280 shiftset
= bitset_create (ntokens
, BITSET_FIXED
);
281 lookaheadset
= bitset_create (ntokens
, BITSET_FIXED
);
282 obstack_init (&solved_conflicts_obstack
);
284 for (i
= 0; i
< nstates
; i
++)
285 set_conflicts (states
[i
]);
289 /*---------------------------------------------.
290 | Count the number of shift/reduce conflicts. |
291 `---------------------------------------------*/
294 count_sr_conflicts (state_t
*state
)
298 shifts
*shiftp
= state
->shifts
;
303 bitset_zero (lookaheadset
);
304 bitset_zero (shiftset
);
306 for (i
= 0; i
< shiftp
->nshifts
&& SHIFT_IS_SHIFT (shiftp
, i
); i
++)
307 if (!SHIFT_IS_DISABLED (shiftp
, i
))
308 bitset_set (shiftset
, SHIFT_SYMBOL (shiftp
, i
));
310 for (i
= 0; i
< state
->nlookaheads
; ++i
)
311 bitset_or (lookaheadset
, lookaheadset
, LA
[state
->lookaheadsp
+ i
]);
313 bitset_and (lookaheadset
, lookaheadset
, shiftset
);
315 src_count
= bitset_count (lookaheadset
);
321 /*----------------------------------------------.
322 | Count the number of reduce/reduce conflicts. |
323 `----------------------------------------------*/
326 count_rr_conflicts (state_t
*state
)
331 if (state
->nlookaheads
< 2)
334 for (i
= 0; i
< ntokens
; i
++)
338 for (j
= 0; j
< state
->nlookaheads
; ++j
)
339 if (bitset_test (LA
[state
->lookaheadsp
+ j
], i
))
349 /*--------------------------------------------------------------.
350 | Return a human readable string which reports shift/reduce and |
351 | reduce/reduce conflict numbers (SRC_NUM, RRC_NUM). |
352 `--------------------------------------------------------------*/
355 conflict_report (int src_num
, int rrc_num
)
357 static char res
[4096];
362 sprintf (cp
, ngettext ("%d shift/reduce conflict",
363 "%d shift/reduce conflicts", src_num
), src_num
);
367 if (src_num
> 0 && rrc_num
> 0)
369 sprintf (cp
, " %s ", _("and"));
375 sprintf (cp
, ngettext ("%d reduce/reduce conflict",
376 "%d reduce/reduce conflicts", rrc_num
), rrc_num
);
388 /*-----------------------------------------------------------.
389 | Output the detailed description of states with conflicts. |
390 `-----------------------------------------------------------*/
393 conflicts_output (FILE *out
)
395 bool printed_sth
= FALSE
;
397 for (i
= 0; i
< nstates
; i
++)
400 fprintf (out
, _("State %d contains "), i
);
401 fputs (conflict_report (count_sr_conflicts (states
[i
]),
402 count_rr_conflicts (states
[i
])), out
);
410 /*------------------------------------------.
411 | Reporting the total number of conflicts. |
412 `------------------------------------------*/
415 conflicts_print (void)
419 /* Is the number of SR conflicts OK? Either EXPECTED_CONFLICTS is
420 not set, and then we want 0 SR, or else it is specified, in which
421 case we want equality. */
427 /* Conflicts by state. */
428 for (i
= 0; i
< nstates
; i
++)
431 src_total
+= count_sr_conflicts (states
[i
]);
432 rrc_total
+= count_rr_conflicts (states
[i
]);
435 src_ok
= src_total
== (expected_conflicts
== -1 ? 0 : expected_conflicts
);
437 /* If there are no RR conflicts, and as many SR conflicts as
438 expected, then there is nothing to report. */
439 if (!rrc_total
&& src_ok
)
442 /* Report the total number of conflicts on STDERR. */
445 /* If invoked with `--yacc', use the output format specified by
447 fprintf (stderr
, _("conflicts: "));
449 fprintf (stderr
, _(" %d shift/reduce"), src_total
);
450 if (src_total
> 0 && rrc_total
> 0)
451 fprintf (stderr
, ",");
453 fprintf (stderr
, _(" %d reduce/reduce"), rrc_total
);
458 fprintf (stderr
, _("%s contains "), infile
);
459 fputs (conflict_report (src_total
, rrc_total
), stderr
);
462 if (expected_conflicts
!= -1 && !src_ok
)
464 complain_message_count
++;
465 fprintf (stderr
, ngettext ("expected %d shift/reduce conflict\n",
466 "expected %d shift/reduce conflicts\n",
474 conflicts_free (void)
477 bitset_free (shiftset
);
478 bitset_free (lookaheadset
);
479 obstack_free (&solved_conflicts_obstack
, NULL
);