]>
Commit | Line | Data |
---|---|---|
08089d5d | 1 | /* Find and resolve or report look-ahead conflicts for bison, |
602bbf31 | 2 | Copyright 1984, 1989, 1992, 2000, 2001, 2002 Free Software Foundation, Inc. |
08089d5d | 3 | |
ceed8467 | 4 | This file is part of Bison, the GNU Compiler Compiler. |
08089d5d | 5 | |
ceed8467 AD |
6 | Bison is free software; you can redistribute it and/or modify it |
7 | under the terms of the GNU General Public License as published by | |
8 | the Free Software Foundation; either version 2, or (at your option) | |
9 | any later version. | |
08089d5d | 10 | |
ceed8467 AD |
11 | Bison is distributed in the hope that it will be useful, but |
12 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | General Public License for more details. | |
08089d5d | 15 | |
ceed8467 AD |
16 | You should have received a copy of the GNU General Public License |
17 | along with Bison; see the file COPYING. If not, write to the Free | |
18 | Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA | |
19 | 02111-1307, USA. */ | |
08089d5d | 20 | |
08089d5d | 21 | #include "system.h" |
602bbf31 | 22 | #include "bitset.h" |
7da99ede | 23 | #include "complain.h" |
ceed8467 | 24 | #include "getargs.h" |
0e78e603 | 25 | #include "symtab.h" |
08089d5d DM |
26 | #include "files.h" |
27 | #include "gram.h" | |
28 | #include "state.h" | |
720d742f | 29 | #include "lalr.h" |
0619caf0 | 30 | #include "conflicts.h" |
b2ca4022 AD |
31 | #include "reader.h" |
32 | #include "LR0.h" | |
d2729d44 | 33 | |
7da99ede AD |
34 | /* -1 stands for not specified. */ |
35 | int expected_conflicts = -1; | |
342b8b6e | 36 | static char *conflicts = NULL; |
08089d5d | 37 | |
b86796bf AD |
38 | static bitset shiftset; |
39 | static bitset lookaheadset; | |
c29240e7 | 40 | \f |
08089d5d | 41 | |
c29240e7 | 42 | static inline void |
e53c6322 | 43 | log_resolution (state_t *state, int LAno, int token, const char *resolution) |
08089d5d | 44 | { |
64d15509 AD |
45 | if (verbose_flag) |
46 | obstack_fgrow4 (&output_obstack, | |
47 | _("\ | |
c29240e7 | 48 | Conflict in state %d between rule %d and token %s resolved as %s.\n"), |
ad949da9 AD |
49 | state->number, LAruleno[LAno], symbols[token]->tag, |
50 | resolution); | |
08089d5d DM |
51 | } |
52 | ||
53 | ||
c29240e7 AD |
54 | /*------------------------------------------------------------------. |
55 | | Turn off the shift recorded for the specified token in the | | |
56 | | specified state. Used when we resolve a shift-reduce conflict in | | |
57 | | favor of the reduction. | | |
58 | `------------------------------------------------------------------*/ | |
59 | ||
4a120d45 | 60 | static void |
065fbd27 | 61 | flush_shift (state_t *state, int token) |
08089d5d | 62 | { |
065fbd27 | 63 | shifts *shiftp = state->shifts; |
9f136c07 | 64 | int i; |
c29240e7 | 65 | |
b86796bf | 66 | bitset_reset (lookaheadset, token); |
d954473d AD |
67 | for (i = 0; i < shiftp->nshifts; i++) |
68 | if (!SHIFT_IS_DISABLED (shiftp, i) && SHIFT_SYMBOL (shiftp, i) == token) | |
69 | SHIFT_DISABLE (shiftp, i); | |
08089d5d DM |
70 | } |
71 | ||
72 | ||
709ae8c6 AD |
73 | /*-------------------------------------------------------------------. |
74 | | Turn off the reduce recorded for the specified token for the | | |
75 | | specified lookahead. Used when we resolve a shift-reduce conflict | | |
76 | | in favor of the shift. | | |
77 | `-------------------------------------------------------------------*/ | |
78 | ||
79 | static void | |
80 | flush_reduce (int lookahead, int token) | |
81 | { | |
602bbf31 | 82 | bitset_reset (LA[lookahead], token); |
709ae8c6 AD |
83 | } |
84 | ||
85 | ||
c29240e7 AD |
86 | /*------------------------------------------------------------------. |
87 | | Attempt to resolve shift-reduce conflict for one rule by means of | | |
88 | | precedence declarations. It has already been checked that the | | |
89 | | rule has a precedence. A conflict is resolved by modifying the | | |
90 | | shift or reduce tables so that there is no longer a conflict. | | |
91 | `------------------------------------------------------------------*/ | |
08089d5d | 92 | |
4a120d45 | 93 | static void |
065fbd27 | 94 | resolve_sr_conflict (state_t *state, int lookahead) |
08089d5d | 95 | { |
c29240e7 | 96 | int i; |
9f136c07 | 97 | /* find the rule to reduce by to get precedence of reduction */ |
1a2b5d37 | 98 | int redprec = rules[LAruleno[lookahead]].prec; |
2cec70b9 AD |
99 | errs *errp = errs_new (ntokens + 1); |
100 | errp->nerrs = 0; | |
08089d5d | 101 | |
08089d5d | 102 | for (i = 0; i < ntokens; i++) |
602bbf31 | 103 | if (bitset_test (LA[lookahead], i) |
b86796bf | 104 | && bitset_test (lookaheadset, i) |
0e78e603 | 105 | && symbols[i]->prec) |
92b16366 | 106 | { |
709ae8c6 AD |
107 | /* Shift-reduce conflict occurs for token number i |
108 | and it has a precedence. | |
109 | The precedence of shifting is that of token i. */ | |
0e78e603 | 110 | if (symbols[i]->prec < redprec) |
92b16366 | 111 | { |
709ae8c6 | 112 | log_resolution (state, lookahead, i, _("reduce")); |
92b16366 AD |
113 | flush_shift (state, i); |
114 | } | |
0e78e603 | 115 | else if (symbols[i]->prec > redprec) |
92b16366 | 116 | { |
709ae8c6 AD |
117 | log_resolution (state, lookahead, i, _("shift")); |
118 | flush_reduce (lookahead, i); | |
92b16366 AD |
119 | } |
120 | else | |
709ae8c6 AD |
121 | /* Matching precedence levels. |
122 | For left association, keep only the reduction. | |
123 | For right association, keep only the shift. | |
124 | For nonassociation, keep neither. */ | |
125 | ||
5a670b1e | 126 | switch (symbols[i]->assoc) |
709ae8c6 AD |
127 | { |
128 | case right_assoc: | |
129 | log_resolution (state, lookahead, i, _("shift")); | |
130 | flush_reduce (lookahead, i); | |
131 | break; | |
132 | ||
133 | case left_assoc: | |
134 | log_resolution (state, lookahead, i, _("reduce")); | |
135 | flush_shift (state, i); | |
136 | break; | |
137 | ||
138 | case non_assoc: | |
139 | log_resolution (state, lookahead, i, _("an error")); | |
140 | flush_shift (state, i); | |
141 | flush_reduce (lookahead, i); | |
142 | /* Record an explicit error for this token. */ | |
2cec70b9 | 143 | errp->errs[errp->nerrs++] = i; |
709ae8c6 AD |
144 | break; |
145 | } | |
92b16366 AD |
146 | } |
147 | ||
92b16366 AD |
148 | /* Some tokens have been explicitly made errors. Allocate a |
149 | permanent errs structure for this state, to record them. */ | |
2cec70b9 | 150 | state->errs = errs_dup (errp); |
c29240e7 | 151 | free (errp); |
08089d5d DM |
152 | } |
153 | ||
154 | ||
4a120d45 | 155 | static void |
065fbd27 | 156 | set_conflicts (state_t *state) |
08089d5d | 157 | { |
914feea9 | 158 | int i; |
c29240e7 | 159 | shifts *shiftp; |
c29240e7 | 160 | |
065fbd27 | 161 | if (state->consistent) |
c29240e7 | 162 | return; |
08089d5d | 163 | |
b86796bf | 164 | bitset_zero (lookaheadset); |
08089d5d | 165 | |
065fbd27 | 166 | shiftp = state->shifts; |
d954473d AD |
167 | for (i = 0; i < shiftp->nshifts && SHIFT_IS_SHIFT (shiftp, i); i++) |
168 | if (!SHIFT_IS_DISABLED (shiftp, i)) | |
b86796bf | 169 | bitset_set (lookaheadset, SHIFT_SYMBOL (shiftp, i)); |
08089d5d | 170 | |
c29240e7 AD |
171 | /* Loop over all rules which require lookahead in this state. First |
172 | check for shift-reduce conflict, and try to resolve using | |
173 | precedence */ | |
065fbd27 | 174 | for (i = 0; i < state->nlookaheads; ++i) |
914feea9 AD |
175 | if (rules[LAruleno[state->lookaheadsp + i]].prec |
176 | && !bitset_disjoint_p (LA[state->lookaheadsp + i], lookaheadset)) | |
177 | { | |
178 | resolve_sr_conflict (state, state->lookaheadsp + i); | |
179 | break; | |
180 | } | |
08089d5d | 181 | |
c29240e7 AD |
182 | /* Loop over all rules which require lookahead in this state. Check |
183 | for conflicts not resolved above. */ | |
065fbd27 | 184 | for (i = 0; i < state->nlookaheads; ++i) |
08089d5d | 185 | { |
914feea9 AD |
186 | if (!bitset_disjoint_p (LA[state->lookaheadsp + i], lookaheadset)) |
187 | conflicts[state->number] = 1; | |
08089d5d | 188 | |
f9abaa2c | 189 | bitset_or (lookaheadset, lookaheadset, LA[state->lookaheadsp + i]); |
c29240e7 AD |
190 | } |
191 | } | |
08089d5d DM |
192 | |
193 | void | |
342b8b6e | 194 | solve_conflicts (void) |
08089d5d | 195 | { |
602bbf31 | 196 | size_t i; |
08089d5d | 197 | |
d7913476 | 198 | conflicts = XCALLOC (char, nstates); |
b86796bf | 199 | shiftset = bitset_create (ntokens, BITSET_FIXED); |
b86796bf | 200 | lookaheadset = bitset_create (ntokens, BITSET_FIXED); |
08089d5d | 201 | |
c29240e7 | 202 | for (i = 0; i < nstates; i++) |
29e88316 | 203 | set_conflicts (states[i]); |
08089d5d DM |
204 | } |
205 | ||
206 | ||
c29240e7 AD |
207 | /*---------------------------------------------. |
208 | | Count the number of shift/reduce conflicts. | | |
209 | `---------------------------------------------*/ | |
210 | ||
0df87bb6 | 211 | static int |
065fbd27 | 212 | count_sr_conflicts (state_t *state) |
08089d5d | 213 | { |
f9abaa2c | 214 | int i; |
0df87bb6 | 215 | int src_count = 0; |
065fbd27 | 216 | shifts *shiftp = state->shifts; |
08089d5d | 217 | |
c29240e7 | 218 | if (!shiftp) |
0df87bb6 | 219 | return 0; |
08089d5d | 220 | |
b86796bf AD |
221 | bitset_zero (lookaheadset); |
222 | bitset_zero (shiftset); | |
08089d5d | 223 | |
52afa962 | 224 | for (i = 0; i < shiftp->nshifts && SHIFT_IS_SHIFT (shiftp, i); i++) |
9839bbe5 | 225 | if (!SHIFT_IS_DISABLED (shiftp, i)) |
b86796bf | 226 | bitset_set (shiftset, SHIFT_SYMBOL (shiftp, i)); |
08089d5d | 227 | |
065fbd27 | 228 | for (i = 0; i < state->nlookaheads; ++i) |
f9abaa2c | 229 | bitset_or (lookaheadset, lookaheadset, LA[state->lookaheadsp + i]); |
08089d5d | 230 | |
b86796bf | 231 | bitset_and (lookaheadset, lookaheadset, shiftset); |
08089d5d | 232 | |
914feea9 | 233 | src_count = bitset_count (lookaheadset); |
0df87bb6 AD |
234 | |
235 | return src_count; | |
08089d5d DM |
236 | } |
237 | ||
238 | ||
c29240e7 AD |
239 | /*----------------------------------------------. |
240 | | Count the number of reduce/reduce conflicts. | | |
241 | `----------------------------------------------*/ | |
242 | ||
0df87bb6 | 243 | static int |
065fbd27 | 244 | count_rr_conflicts (state_t *state) |
08089d5d | 245 | { |
c29240e7 | 246 | int i; |
0df87bb6 | 247 | int rrc_count = 0; |
08089d5d | 248 | |
065fbd27 | 249 | if (state->nlookaheads < 2) |
0df87bb6 | 250 | return 0; |
08089d5d | 251 | |
08089d5d DM |
252 | for (i = 0; i < ntokens; i++) |
253 | { | |
0df87bb6 AD |
254 | int count = 0; |
255 | int j; | |
065fbd27 | 256 | for (j = 0; j < state->nlookaheads; ++j) |
602bbf31 | 257 | if (bitset_test (LA[state->lookaheadsp + j], i)) |
52afa962 | 258 | count++; |
08089d5d | 259 | |
c29240e7 AD |
260 | if (count >= 2) |
261 | rrc_count++; | |
08089d5d | 262 | } |
0df87bb6 AD |
263 | |
264 | return rrc_count; | |
08089d5d DM |
265 | } |
266 | ||
ff4423cc AD |
267 | /*--------------------------------------------------------------. |
268 | | Return a human readable string which reports shift/reduce and | | |
269 | | reduce/reduce conflict numbers (SRC_NUM, RRC_NUM). | | |
270 | `--------------------------------------------------------------*/ | |
c29240e7 | 271 | |
ff4423cc AD |
272 | static const char * |
273 | conflict_report (int src_num, int rrc_num) | |
c29240e7 | 274 | { |
ff4423cc AD |
275 | static char res[4096]; |
276 | char *cp = res; | |
277 | ||
7da99ede | 278 | if (src_num >= 1) |
22c821f3 | 279 | { |
7da99ede AD |
280 | sprintf (cp, ngettext ("%d shift/reduce conflict", |
281 | "%d shift/reduce conflicts", src_num), src_num); | |
22c821f3 AD |
282 | cp += strlen (cp); |
283 | } | |
c29240e7 | 284 | |
0619caf0 | 285 | if (src_num > 0 && rrc_num > 0) |
22c821f3 | 286 | { |
7da99ede | 287 | sprintf (cp, " %s ", _("and")); |
22c821f3 AD |
288 | cp += strlen (cp); |
289 | } | |
c29240e7 | 290 | |
7da99ede | 291 | if (rrc_num >= 1) |
22c821f3 | 292 | { |
7da99ede AD |
293 | sprintf (cp, ngettext ("%d reduce/reduce conflict", |
294 | "%d reduce/reduce conflicts", rrc_num), rrc_num); | |
22c821f3 AD |
295 | cp += strlen (cp); |
296 | } | |
ff4423cc AD |
297 | |
298 | *cp++ = '.'; | |
299 | *cp++ = '\n'; | |
300 | *cp++ = '\0'; | |
c29240e7 | 301 | |
ff4423cc | 302 | return res; |
c29240e7 AD |
303 | } |
304 | ||
305 | ||
0df87bb6 AD |
306 | /*-----------------------------------------------------------. |
307 | | Output the detailed description of states with conflicts. | | |
308 | `-----------------------------------------------------------*/ | |
c29240e7 AD |
309 | |
310 | void | |
0df87bb6 | 311 | conflicts_output (FILE *out) |
c29240e7 | 312 | { |
d2d1b42b | 313 | bool printed_sth = FALSE; |
602bbf31 | 314 | size_t i; |
0df87bb6 AD |
315 | for (i = 0; i < nstates; i++) |
316 | if (conflicts[i]) | |
317 | { | |
7da99ede | 318 | fprintf (out, _("State %d contains "), i); |
29e88316 AD |
319 | fputs (conflict_report (count_sr_conflicts (states[i]), |
320 | count_rr_conflicts (states[i])), out); | |
d2d1b42b | 321 | printed_sth = TRUE; |
0df87bb6 | 322 | } |
d2d1b42b AD |
323 | if (printed_sth) |
324 | fputs ("\n\n", out); | |
0df87bb6 | 325 | } |
c29240e7 | 326 | |
c29240e7 | 327 | |
0df87bb6 AD |
328 | /*------------------------------------------. |
329 | | Reporting the total number of conflicts. | | |
330 | `------------------------------------------*/ | |
0619caf0 | 331 | |
0df87bb6 AD |
332 | void |
333 | conflicts_print (void) | |
334 | { | |
602bbf31 | 335 | size_t i; |
0df87bb6 | 336 | |
a034c8b8 AD |
337 | /* Is the number of SR conflicts OK? Either EXPECTED_CONFLICTS is |
338 | not set, and then we want 0 SR, or else it is specified, in which | |
339 | case we want equality. */ | |
340 | int src_ok = 0; | |
341 | ||
0df87bb6 AD |
342 | int src_total = 0; |
343 | int rrc_total = 0; | |
344 | ||
345 | /* Conflicts by state. */ | |
346 | for (i = 0; i < nstates; i++) | |
347 | if (conflicts[i]) | |
348 | { | |
29e88316 AD |
349 | src_total += count_sr_conflicts (states[i]); |
350 | rrc_total += count_rr_conflicts (states[i]); | |
0df87bb6 | 351 | } |
c29240e7 | 352 | |
a034c8b8 AD |
353 | src_ok = src_total == (expected_conflicts == -1 ? 0 : expected_conflicts); |
354 | ||
355 | /* If there are no RR conflicts, and as many SR conflicts as | |
356 | expected, then there is nothing to report. */ | |
357 | if (!rrc_total && src_ok) | |
358 | return; | |
359 | ||
0619caf0 | 360 | /* Report the total number of conflicts on STDERR. */ |
a034c8b8 | 361 | if (yacc_flag) |
09b503c8 | 362 | { |
a034c8b8 AD |
363 | /* If invoked with `--yacc', use the output format specified by |
364 | POSIX. */ | |
365 | fprintf (stderr, _("conflicts: ")); | |
366 | if (src_total > 0) | |
367 | fprintf (stderr, _(" %d shift/reduce"), src_total); | |
368 | if (src_total > 0 && rrc_total > 0) | |
369 | fprintf (stderr, ","); | |
370 | if (rrc_total > 0) | |
371 | fprintf (stderr, _(" %d reduce/reduce"), rrc_total); | |
372 | putc ('\n', stderr); | |
373 | } | |
374 | else | |
375 | { | |
376 | fprintf (stderr, _("%s contains "), infile); | |
377 | fputs (conflict_report (src_total, rrc_total), stderr); | |
09b503c8 | 378 | } |
7da99ede | 379 | |
a034c8b8 | 380 | if (expected_conflicts != -1 && !src_ok) |
7da99ede AD |
381 | { |
382 | complain_message_count++; | |
81e895c0 AD |
383 | fprintf (stderr, ngettext ("expected %d shift/reduce conflict\n", |
384 | "expected %d shift/reduce conflicts\n", | |
7da99ede AD |
385 | expected_conflicts), |
386 | expected_conflicts); | |
387 | } | |
c29240e7 AD |
388 | } |
389 | ||
390 | ||
08089d5d | 391 | void |
342b8b6e | 392 | free_conflicts (void) |
08089d5d | 393 | { |
d7913476 | 394 | XFREE (conflicts); |
b86796bf AD |
395 | bitset_free (shiftset); |
396 | bitset_free (lookaheadset); | |
08089d5d | 397 | } |