]>
Commit | Line | Data |
---|---|---|
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 | ||
39 | static bitset shiftset; | |
40 | static bitset lookaheadset; | |
41 | \f | |
42 | ||
43 | static inline void | |
44 | log_resolution (state_t *state, int LAno, int token, const char *resolution) | |
45 | { | |
46 | if (verbose_flag) | |
47 | obstack_fgrow4 (&output_obstack, | |
48 | _("\ | |
49 | Conflict in state %d between rule %d and token %s resolved as %s.\n"), | |
50 | state->number, | |
51 | LArule[LAno]->number, | |
52 | symbols[token]->tag, | |
53 | resolution); | |
54 | } | |
55 | ||
56 | ||
57 | /*------------------------------------------------------------------. | |
58 | | Turn off the shift recorded for the specified token in the | | |
59 | | specified state. Used when we resolve a shift-reduce conflict in | | |
60 | | favor of the reduction. | | |
61 | `------------------------------------------------------------------*/ | |
62 | ||
63 | static void | |
64 | flush_shift (state_t *state, int token) | |
65 | { | |
66 | shifts *shiftp = state->shifts; | |
67 | int i; | |
68 | ||
69 | bitset_reset (lookaheadset, token); | |
70 | for (i = 0; i < shiftp->nshifts; i++) | |
71 | if (!SHIFT_IS_DISABLED (shiftp, i) && SHIFT_SYMBOL (shiftp, i) == token) | |
72 | SHIFT_DISABLE (shiftp, i); | |
73 | } | |
74 | ||
75 | ||
76 | /*-------------------------------------------------------------------. | |
77 | | Turn off the reduce recorded for the specified token for the | | |
78 | | specified lookahead. Used when we resolve a shift-reduce conflict | | |
79 | | in favor of the shift. | | |
80 | `-------------------------------------------------------------------*/ | |
81 | ||
82 | static void | |
83 | flush_reduce (int lookahead, int token) | |
84 | { | |
85 | bitset_reset (LA[lookahead], token); | |
86 | } | |
87 | ||
88 | ||
89 | /*------------------------------------------------------------------. | |
90 | | Attempt to resolve shift-reduce conflict for one rule by means of | | |
91 | | precedence declarations. It has already been checked that the | | |
92 | | rule has a precedence. A conflict is resolved by modifying the | | |
93 | | shift or reduce tables so that there is no longer a conflict. | | |
94 | `------------------------------------------------------------------*/ | |
95 | ||
96 | static void | |
97 | resolve_sr_conflict (state_t *state, int lookahead) | |
98 | { | |
99 | int i; | |
100 | /* find the rule to reduce by to get precedence of reduction */ | |
101 | int redprec = LArule[lookahead]->prec->prec; | |
102 | errs *errp = errs_new (ntokens + 1); | |
103 | errp->nerrs = 0; | |
104 | ||
105 | for (i = 0; i < ntokens; i++) | |
106 | if (bitset_test (LA[lookahead], i) | |
107 | && bitset_test (lookaheadset, i) | |
108 | && symbols[i]->prec) | |
109 | { | |
110 | /* Shift-reduce conflict occurs for token number i | |
111 | and it has a precedence. | |
112 | The precedence of shifting is that of token i. */ | |
113 | if (symbols[i]->prec < redprec) | |
114 | { | |
115 | log_resolution (state, lookahead, i, _("reduce")); | |
116 | flush_shift (state, i); | |
117 | } | |
118 | else if (symbols[i]->prec > redprec) | |
119 | { | |
120 | log_resolution (state, lookahead, i, _("shift")); | |
121 | flush_reduce (lookahead, i); | |
122 | } | |
123 | else | |
124 | /* Matching precedence levels. | |
125 | For left association, keep only the reduction. | |
126 | For right association, keep only the shift. | |
127 | For nonassociation, keep neither. */ | |
128 | ||
129 | switch (symbols[i]->assoc) | |
130 | { | |
131 | case right_assoc: | |
132 | log_resolution (state, lookahead, i, _("shift")); | |
133 | flush_reduce (lookahead, i); | |
134 | break; | |
135 | ||
136 | case left_assoc: | |
137 | log_resolution (state, lookahead, i, _("reduce")); | |
138 | flush_shift (state, i); | |
139 | break; | |
140 | ||
141 | case non_assoc: | |
142 | log_resolution (state, lookahead, i, _("an error")); | |
143 | flush_shift (state, i); | |
144 | flush_reduce (lookahead, i); | |
145 | /* Record an explicit error for this token. */ | |
146 | errp->errs[errp->nerrs++] = i; | |
147 | break; | |
148 | } | |
149 | } | |
150 | ||
151 | /* Some tokens have been explicitly made errors. Allocate a | |
152 | permanent errs structure for this state, to record them. */ | |
153 | state->errs = errs_dup (errp); | |
154 | free (errp); | |
155 | } | |
156 | ||
157 | ||
158 | static void | |
159 | set_conflicts (state_t *state) | |
160 | { | |
161 | int i; | |
162 | shifts *shiftp; | |
163 | ||
164 | if (state->consistent) | |
165 | return; | |
166 | ||
167 | bitset_zero (lookaheadset); | |
168 | ||
169 | shiftp = state->shifts; | |
170 | for (i = 0; i < shiftp->nshifts && SHIFT_IS_SHIFT (shiftp, i); i++) | |
171 | if (!SHIFT_IS_DISABLED (shiftp, i)) | |
172 | bitset_set (lookaheadset, SHIFT_SYMBOL (shiftp, i)); | |
173 | ||
174 | /* Loop over all rules which require lookahead in this state. First | |
175 | check for shift-reduce conflict, and try to resolve using | |
176 | precedence */ | |
177 | for (i = 0; i < state->nlookaheads; ++i) | |
178 | if (LArule[state->lookaheadsp + i]->prec | |
179 | && LArule[state->lookaheadsp + i]->prec->prec | |
180 | && !bitset_disjoint_p (LA[state->lookaheadsp + i], lookaheadset)) | |
181 | { | |
182 | resolve_sr_conflict (state, state->lookaheadsp + i); | |
183 | break; | |
184 | } | |
185 | ||
186 | /* Loop over all rules which require lookahead in this state. Check | |
187 | for conflicts not resolved above. */ | |
188 | for (i = 0; i < state->nlookaheads; ++i) | |
189 | { | |
190 | if (!bitset_disjoint_p (LA[state->lookaheadsp + i], lookaheadset)) | |
191 | conflicts[state->number] = 1; | |
192 | ||
193 | bitset_or (lookaheadset, lookaheadset, LA[state->lookaheadsp + i]); | |
194 | } | |
195 | } | |
196 | ||
197 | void | |
198 | solve_conflicts (void) | |
199 | { | |
200 | size_t i; | |
201 | ||
202 | conflicts = XCALLOC (char, nstates); | |
203 | shiftset = bitset_create (ntokens, BITSET_FIXED); | |
204 | lookaheadset = bitset_create (ntokens, BITSET_FIXED); | |
205 | ||
206 | for (i = 0; i < nstates; i++) | |
207 | set_conflicts (states[i]); | |
208 | } | |
209 | ||
210 | ||
211 | /*---------------------------------------------. | |
212 | | Count the number of shift/reduce conflicts. | | |
213 | `---------------------------------------------*/ | |
214 | ||
215 | static int | |
216 | count_sr_conflicts (state_t *state) | |
217 | { | |
218 | int i; | |
219 | int src_count = 0; | |
220 | shifts *shiftp = state->shifts; | |
221 | ||
222 | if (!shiftp) | |
223 | return 0; | |
224 | ||
225 | bitset_zero (lookaheadset); | |
226 | bitset_zero (shiftset); | |
227 | ||
228 | for (i = 0; i < shiftp->nshifts && SHIFT_IS_SHIFT (shiftp, i); i++) | |
229 | if (!SHIFT_IS_DISABLED (shiftp, i)) | |
230 | bitset_set (shiftset, SHIFT_SYMBOL (shiftp, i)); | |
231 | ||
232 | for (i = 0; i < state->nlookaheads; ++i) | |
233 | bitset_or (lookaheadset, lookaheadset, LA[state->lookaheadsp + i]); | |
234 | ||
235 | bitset_and (lookaheadset, lookaheadset, shiftset); | |
236 | ||
237 | src_count = bitset_count (lookaheadset); | |
238 | ||
239 | return src_count; | |
240 | } | |
241 | ||
242 | ||
243 | /*----------------------------------------------. | |
244 | | Count the number of reduce/reduce conflicts. | | |
245 | `----------------------------------------------*/ | |
246 | ||
247 | static int | |
248 | count_rr_conflicts (state_t *state) | |
249 | { | |
250 | int i; | |
251 | int rrc_count = 0; | |
252 | ||
253 | if (state->nlookaheads < 2) | |
254 | return 0; | |
255 | ||
256 | for (i = 0; i < ntokens; i++) | |
257 | { | |
258 | int count = 0; | |
259 | int j; | |
260 | for (j = 0; j < state->nlookaheads; ++j) | |
261 | if (bitset_test (LA[state->lookaheadsp + j], i)) | |
262 | count++; | |
263 | ||
264 | if (count >= 2) | |
265 | rrc_count++; | |
266 | } | |
267 | ||
268 | return rrc_count; | |
269 | } | |
270 | ||
271 | /*--------------------------------------------------------------. | |
272 | | Return a human readable string which reports shift/reduce and | | |
273 | | reduce/reduce conflict numbers (SRC_NUM, RRC_NUM). | | |
274 | `--------------------------------------------------------------*/ | |
275 | ||
276 | static const char * | |
277 | conflict_report (int src_num, int rrc_num) | |
278 | { | |
279 | static char res[4096]; | |
280 | char *cp = res; | |
281 | ||
282 | if (src_num >= 1) | |
283 | { | |
284 | sprintf (cp, ngettext ("%d shift/reduce conflict", | |
285 | "%d shift/reduce conflicts", src_num), src_num); | |
286 | cp += strlen (cp); | |
287 | } | |
288 | ||
289 | if (src_num > 0 && rrc_num > 0) | |
290 | { | |
291 | sprintf (cp, " %s ", _("and")); | |
292 | cp += strlen (cp); | |
293 | } | |
294 | ||
295 | if (rrc_num >= 1) | |
296 | { | |
297 | sprintf (cp, ngettext ("%d reduce/reduce conflict", | |
298 | "%d reduce/reduce conflicts", rrc_num), rrc_num); | |
299 | cp += strlen (cp); | |
300 | } | |
301 | ||
302 | *cp++ = '.'; | |
303 | *cp++ = '\n'; | |
304 | *cp++ = '\0'; | |
305 | ||
306 | return res; | |
307 | } | |
308 | ||
309 | ||
310 | /*-----------------------------------------------------------. | |
311 | | Output the detailed description of states with conflicts. | | |
312 | `-----------------------------------------------------------*/ | |
313 | ||
314 | void | |
315 | conflicts_output (FILE *out) | |
316 | { | |
317 | bool printed_sth = FALSE; | |
318 | size_t i; | |
319 | for (i = 0; i < nstates; i++) | |
320 | if (conflicts[i]) | |
321 | { | |
322 | fprintf (out, _("State %d contains "), i); | |
323 | fputs (conflict_report (count_sr_conflicts (states[i]), | |
324 | count_rr_conflicts (states[i])), out); | |
325 | printed_sth = TRUE; | |
326 | } | |
327 | if (printed_sth) | |
328 | fputs ("\n\n", out); | |
329 | } | |
330 | ||
331 | ||
332 | /*------------------------------------------. | |
333 | | Reporting the total number of conflicts. | | |
334 | `------------------------------------------*/ | |
335 | ||
336 | void | |
337 | conflicts_print (void) | |
338 | { | |
339 | size_t i; | |
340 | ||
341 | /* Is the number of SR conflicts OK? Either EXPECTED_CONFLICTS is | |
342 | not set, and then we want 0 SR, or else it is specified, in which | |
343 | case we want equality. */ | |
344 | int src_ok = 0; | |
345 | ||
346 | int src_total = 0; | |
347 | int rrc_total = 0; | |
348 | ||
349 | /* Conflicts by state. */ | |
350 | for (i = 0; i < nstates; i++) | |
351 | if (conflicts[i]) | |
352 | { | |
353 | src_total += count_sr_conflicts (states[i]); | |
354 | rrc_total += count_rr_conflicts (states[i]); | |
355 | } | |
356 | ||
357 | src_ok = src_total == (expected_conflicts == -1 ? 0 : expected_conflicts); | |
358 | ||
359 | /* If there are no RR conflicts, and as many SR conflicts as | |
360 | expected, then there is nothing to report. */ | |
361 | if (!rrc_total && src_ok) | |
362 | return; | |
363 | ||
364 | /* Report the total number of conflicts on STDERR. */ | |
365 | if (yacc_flag) | |
366 | { | |
367 | /* If invoked with `--yacc', use the output format specified by | |
368 | POSIX. */ | |
369 | fprintf (stderr, _("conflicts: ")); | |
370 | if (src_total > 0) | |
371 | fprintf (stderr, _(" %d shift/reduce"), src_total); | |
372 | if (src_total > 0 && rrc_total > 0) | |
373 | fprintf (stderr, ","); | |
374 | if (rrc_total > 0) | |
375 | fprintf (stderr, _(" %d reduce/reduce"), rrc_total); | |
376 | putc ('\n', stderr); | |
377 | } | |
378 | else | |
379 | { | |
380 | fprintf (stderr, _("%s contains "), infile); | |
381 | fputs (conflict_report (src_total, rrc_total), stderr); | |
382 | } | |
383 | ||
384 | if (expected_conflicts != -1 && !src_ok) | |
385 | { | |
386 | complain_message_count++; | |
387 | fprintf (stderr, ngettext ("expected %d shift/reduce conflict\n", | |
388 | "expected %d shift/reduce conflicts\n", | |
389 | expected_conflicts), | |
390 | expected_conflicts); | |
391 | } | |
392 | } | |
393 | ||
394 | ||
395 | void | |
396 | free_conflicts (void) | |
397 | { | |
398 | XFREE (conflicts); | |
399 | bitset_free (shiftset); | |
400 | bitset_free (lookaheadset); | |
401 | } |