]>
Commit | Line | Data |
---|---|---|
742e4900 | 1 | /* Find and resolve or report lookahead conflicts for bison, |
f041e30b | 2 | |
34136e65 | 3 | Copyright (C) 1984, 1989, 1992, 2000-2012 Free Software Foundation, |
575619af | 4 | Inc. |
08089d5d | 5 | |
ceed8467 | 6 | This file is part of Bison, the GNU Compiler Compiler. |
08089d5d | 7 | |
f16b0819 PE |
8 | This program is free software: you can redistribute it and/or modify |
9 | it under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation, either version 3 of the License, or | |
11 | (at your option) any later version. | |
08089d5d | 12 | |
f16b0819 PE |
13 | This program is distributed in the hope that it will be useful, |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
08089d5d | 17 | |
ceed8467 | 18 | You should have received a copy of the GNU General Public License |
f16b0819 | 19 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
08089d5d | 20 | |
2cec9080 | 21 | #include <config.h> |
08089d5d | 22 | #include "system.h" |
f041e30b PE |
23 | |
24 | #include <bitset.h> | |
25 | ||
26 | #include "LR0.h" | |
7da99ede | 27 | #include "complain.h" |
f041e30b | 28 | #include "conflicts.h" |
08089d5d | 29 | #include "files.h" |
f041e30b | 30 | #include "getargs.h" |
08089d5d | 31 | #include "gram.h" |
720d742f | 32 | #include "lalr.h" |
41d7a5f2 | 33 | #include "print-xml.h" |
b2ca4022 | 34 | #include "reader.h" |
f041e30b PE |
35 | #include "state.h" |
36 | #include "symtab.h" | |
d2729d44 | 37 | |
7da99ede | 38 | /* -1 stands for not specified. */ |
d6328241 PH |
39 | int expected_sr_conflicts = -1; |
40 | int expected_rr_conflicts = -1; | |
da2a7671 | 41 | static char *conflicts; |
d6b771c3 | 42 | static struct obstack solved_conflicts_obstack; |
41d7a5f2 | 43 | static struct obstack solved_conflicts_xml_obstack; |
08089d5d | 44 | |
8dd162d3 | 45 | static bitset shift_set; |
742e4900 | 46 | static bitset lookahead_set; |
b408954b | 47 | |
c29240e7 | 48 | \f |
08089d5d | 49 | |
f041e30b | 50 | enum conflict_resolution |
b408954b AD |
51 | { |
52 | shift_resolution, | |
53 | reduce_resolution, | |
54 | left_resolution, | |
55 | right_resolution, | |
86eff183 | 56 | nonassoc_resolution |
b408954b AD |
57 | }; |
58 | ||
59 | ||
9801d40c AD |
60 | /*----------------------------------------------------------------. |
61 | | Explain how an SR conflict between TOKEN and RULE was resolved: | | |
62 | | RESOLUTION. | | |
63 | `----------------------------------------------------------------*/ | |
64 | ||
c29240e7 | 65 | static inline void |
f041e30b | 66 | log_resolution (rule *r, symbol_number token, |
e9690142 | 67 | enum conflict_resolution resolution) |
08089d5d | 68 | { |
b408954b AD |
69 | if (report_flag & report_solved_conflicts) |
70 | { | |
71 | /* The description of the resolution. */ | |
72 | switch (resolution) | |
e9690142 JD |
73 | { |
74 | case shift_resolution: | |
75 | case right_resolution: | |
cb9ec4fa | 76 | obstack_printf (&solved_conflicts_obstack, |
e9690142 JD |
77 | _(" Conflict between rule %d and token %s" |
78 | " resolved as shift"), | |
79 | r->number, | |
80 | symbols[token]->tag); | |
81 | break; | |
82 | ||
83 | case reduce_resolution: | |
84 | case left_resolution: | |
cb9ec4fa | 85 | obstack_printf (&solved_conflicts_obstack, |
e9690142 JD |
86 | _(" Conflict between rule %d and token %s" |
87 | " resolved as reduce"), | |
88 | r->number, | |
89 | symbols[token]->tag); | |
90 | break; | |
91 | ||
92 | case nonassoc_resolution: | |
cb9ec4fa | 93 | obstack_printf (&solved_conflicts_obstack, |
e9690142 JD |
94 | _(" Conflict between rule %d and token %s" |
95 | " resolved as an error"), | |
96 | r->number, | |
97 | symbols[token]->tag); | |
98 | break; | |
99 | } | |
b408954b AD |
100 | |
101 | /* The reason. */ | |
102 | switch (resolution) | |
e9690142 JD |
103 | { |
104 | case shift_resolution: | |
cb9ec4fa | 105 | obstack_printf (&solved_conflicts_obstack, |
e9690142 JD |
106 | " (%s < %s)", |
107 | r->prec->tag, | |
108 | symbols[token]->tag); | |
109 | break; | |
110 | ||
111 | case reduce_resolution: | |
cb9ec4fa | 112 | obstack_printf (&solved_conflicts_obstack, |
e9690142 JD |
113 | " (%s < %s)", |
114 | symbols[token]->tag, | |
115 | r->prec->tag); | |
116 | break; | |
117 | ||
118 | case left_resolution: | |
cb9ec4fa | 119 | obstack_printf (&solved_conflicts_obstack, |
e9690142 JD |
120 | " (%%left %s)", |
121 | symbols[token]->tag); | |
122 | break; | |
123 | ||
124 | case right_resolution: | |
cb9ec4fa | 125 | obstack_printf (&solved_conflicts_obstack, |
e9690142 JD |
126 | " (%%right %s)", |
127 | symbols[token]->tag); | |
128 | break; | |
129 | ||
130 | case nonassoc_resolution: | |
cb9ec4fa | 131 | obstack_printf (&solved_conflicts_obstack, |
e9690142 JD |
132 | " (%%nonassoc %s)", |
133 | symbols[token]->tag); | |
134 | break; | |
135 | } | |
41d7a5f2 | 136 | |
b408954b | 137 | obstack_sgrow (&solved_conflicts_obstack, ".\n"); |
ef1b4273 | 138 | } |
41d7a5f2 | 139 | |
ef1b4273 JD |
140 | /* XML report */ |
141 | if (xml_flag) | |
142 | { | |
143 | /* The description of the resolution. */ | |
144 | switch (resolution) | |
145 | { | |
146 | case shift_resolution: | |
147 | case right_resolution: | |
aaf63e45 | 148 | obstack_printf (&solved_conflicts_xml_obstack, |
ef1b4273 JD |
149 | " <resolution rule=\"%d\" symbol=\"%s\"" |
150 | " type=\"shift\">", | |
151 | r->number, | |
152 | xml_escape (symbols[token]->tag)); | |
153 | break; | |
154 | ||
155 | case reduce_resolution: | |
156 | case left_resolution: | |
aaf63e45 | 157 | obstack_printf (&solved_conflicts_xml_obstack, |
ef1b4273 JD |
158 | " <resolution rule=\"%d\" symbol=\"%s\"" |
159 | " type=\"reduce\">", | |
160 | r->number, | |
161 | xml_escape (symbols[token]->tag)); | |
162 | break; | |
163 | ||
164 | case nonassoc_resolution: | |
aaf63e45 | 165 | obstack_printf (&solved_conflicts_xml_obstack, |
ef1b4273 JD |
166 | " <resolution rule=\"%d\" symbol=\"%s\"" |
167 | " type=\"error\">", | |
168 | r->number, | |
169 | xml_escape (symbols[token]->tag)); | |
170 | break; | |
171 | } | |
41d7a5f2 | 172 | |
ef1b4273 JD |
173 | /* The reason. */ |
174 | switch (resolution) | |
175 | { | |
176 | case shift_resolution: | |
aaf63e45 | 177 | obstack_printf (&solved_conflicts_xml_obstack, |
ef1b4273 JD |
178 | "%s < %s", |
179 | xml_escape_n (0, r->prec->tag), | |
180 | xml_escape_n (1, symbols[token]->tag)); | |
181 | break; | |
182 | ||
183 | case reduce_resolution: | |
aaf63e45 | 184 | obstack_printf (&solved_conflicts_xml_obstack, |
ef1b4273 JD |
185 | "%s < %s", |
186 | xml_escape_n (0, symbols[token]->tag), | |
187 | xml_escape_n (1, r->prec->tag)); | |
188 | break; | |
189 | ||
190 | case left_resolution: | |
aaf63e45 | 191 | obstack_printf (&solved_conflicts_xml_obstack, |
ef1b4273 JD |
192 | "%%left %s", |
193 | xml_escape (symbols[token]->tag)); | |
194 | break; | |
195 | ||
196 | case right_resolution: | |
aaf63e45 | 197 | obstack_printf (&solved_conflicts_xml_obstack, |
ef1b4273 JD |
198 | "%%right %s", |
199 | xml_escape (symbols[token]->tag)); | |
200 | break; | |
201 | ||
202 | case nonassoc_resolution: | |
aaf63e45 | 203 | obstack_printf (&solved_conflicts_xml_obstack, |
ef1b4273 JD |
204 | "%%nonassoc %s", |
205 | xml_escape (symbols[token]->tag)); | |
206 | break; | |
207 | } | |
208 | ||
209 | obstack_sgrow (&solved_conflicts_xml_obstack, "</resolution>\n"); | |
b408954b | 210 | } |
08089d5d DM |
211 | } |
212 | ||
213 | ||
c29240e7 AD |
214 | /*------------------------------------------------------------------. |
215 | | Turn off the shift recorded for the specified token in the | | |
216 | | specified state. Used when we resolve a shift-reduce conflict in | | |
9d774aff | 217 | | favor of the reduction or as an error (%nonassoc). | |
c29240e7 AD |
218 | `------------------------------------------------------------------*/ |
219 | ||
4a120d45 | 220 | static void |
f041e30b | 221 | flush_shift (state *s, int token) |
08089d5d | 222 | { |
f041e30b | 223 | transitions *trans = s->transitions; |
9f136c07 | 224 | int i; |
c29240e7 | 225 | |
742e4900 | 226 | bitset_reset (lookahead_set, token); |
f041e30b PE |
227 | for (i = 0; i < trans->num; i++) |
228 | if (!TRANSITION_IS_DISABLED (trans, i) | |
e9690142 | 229 | && TRANSITION_SYMBOL (trans, i) == token) |
f041e30b | 230 | TRANSITION_DISABLE (trans, i); |
08089d5d DM |
231 | } |
232 | ||
233 | ||
8dd162d3 | 234 | /*--------------------------------------------------------------------. |
9d774aff JD |
235 | | Turn off the reduce recorded for the specified token in the | |
236 | | specified lookahead set. Used when we resolve a shift-reduce | | |
237 | | conflict in favor of the shift or as an error (%nonassoc). | | |
8dd162d3 | 238 | `--------------------------------------------------------------------*/ |
709ae8c6 AD |
239 | |
240 | static void | |
742e4900 | 241 | flush_reduce (bitset lookahead_tokens, int token) |
709ae8c6 | 242 | { |
742e4900 | 243 | bitset_reset (lookahead_tokens, token); |
709ae8c6 AD |
244 | } |
245 | ||
246 | ||
c29240e7 AD |
247 | /*------------------------------------------------------------------. |
248 | | Attempt to resolve shift-reduce conflict for one rule by means of | | |
249 | | precedence declarations. It has already been checked that the | | |
250 | | rule has a precedence. A conflict is resolved by modifying the | | |
251 | | shift or reduce tables so that there is no longer a conflict. | | |
9801d40c | 252 | | | |
742e4900 | 253 | | RULENO is the number of the lookahead bitset to consider. | |
8b752b00 | 254 | | | |
9d774aff JD |
255 | | ERRORS and NERRS can be used to store discovered explicit | |
256 | | errors. | | |
c29240e7 | 257 | `------------------------------------------------------------------*/ |
08089d5d | 258 | |
4a120d45 | 259 | static void |
9d774aff | 260 | resolve_sr_conflict (state *s, int ruleno, symbol **errors, int *nerrs) |
08089d5d | 261 | { |
f041e30b PE |
262 | symbol_number i; |
263 | reductions *reds = s->reductions; | |
9801d40c | 264 | /* Find the rule to reduce by to get precedence of reduction. */ |
f041e30b | 265 | rule *redrule = reds->rules[ruleno]; |
9801d40c | 266 | int redprec = redrule->prec->prec; |
742e4900 | 267 | bitset lookahead_tokens = reds->lookahead_tokens[ruleno]; |
08089d5d | 268 | |
08089d5d | 269 | for (i = 0; i < ntokens; i++) |
742e4900 | 270 | if (bitset_test (lookahead_tokens, i) |
e9690142 JD |
271 | && bitset_test (lookahead_set, i) |
272 | && symbols[i]->prec) | |
92b16366 | 273 | { |
e9690142 JD |
274 | /* Shift-reduce conflict occurs for token number i |
275 | and it has a precedence. | |
276 | The precedence of shifting is that of token i. */ | |
277 | if (symbols[i]->prec < redprec) | |
278 | { | |
279 | log_resolution (redrule, i, reduce_resolution); | |
280 | flush_shift (s, i); | |
281 | } | |
282 | else if (symbols[i]->prec > redprec) | |
283 | { | |
284 | log_resolution (redrule, i, shift_resolution); | |
285 | flush_reduce (lookahead_tokens, i); | |
286 | } | |
287 | else | |
d78f0ac9 AD |
288 | /* Matching precedence levels. |
289 | For non-defined associativity, keep both: unexpected | |
290 | associativity conflict. | |
291 | For left associativity, keep only the reduction. | |
292 | For right associativity, keep only the shift. | |
293 | For nonassociativity, keep neither. */ | |
709ae8c6 | 294 | |
e9690142 JD |
295 | switch (symbols[i]->assoc) |
296 | { | |
d78f0ac9 | 297 | case undef_assoc: |
e9690142 | 298 | abort (); |
68cae94e | 299 | |
d78f0ac9 AD |
300 | case precedence_assoc: |
301 | break; | |
302 | ||
e9690142 JD |
303 | case right_assoc: |
304 | log_resolution (redrule, i, right_resolution); | |
305 | flush_reduce (lookahead_tokens, i); | |
306 | break; | |
307 | ||
308 | case left_assoc: | |
309 | log_resolution (redrule, i, left_resolution); | |
310 | flush_shift (s, i); | |
311 | break; | |
312 | ||
313 | case non_assoc: | |
314 | log_resolution (redrule, i, nonassoc_resolution); | |
315 | flush_shift (s, i); | |
316 | flush_reduce (lookahead_tokens, i); | |
317 | /* Record an explicit error for this token. */ | |
318 | errors[(*nerrs)++] = symbols[i]; | |
319 | break; | |
320 | } | |
92b16366 | 321 | } |
08089d5d DM |
322 | } |
323 | ||
324 | ||
8b752b00 | 325 | /*-------------------------------------------------------------------. |
f041e30b | 326 | | Solve the S/R conflicts of state S using the | |
8b752b00 | 327 | | precedence/associativity, and flag it inconsistent if it still has | |
f041e30b | 328 | | conflicts. ERRORS can be used as storage to compute the list of | |
742e4900 | 329 | | lookahead tokens on which S raises a syntax error (%nonassoc). | |
8b752b00 AD |
330 | `-------------------------------------------------------------------*/ |
331 | ||
4a120d45 | 332 | static void |
f041e30b | 333 | set_conflicts (state *s, symbol **errors) |
08089d5d | 334 | { |
914feea9 | 335 | int i; |
f041e30b PE |
336 | transitions *trans = s->transitions; |
337 | reductions *reds = s->reductions; | |
9d774aff | 338 | int nerrs = 0; |
c29240e7 | 339 | |
f041e30b | 340 | if (s->consistent) |
c29240e7 | 341 | return; |
08089d5d | 342 | |
742e4900 | 343 | bitset_zero (lookahead_set); |
08089d5d | 344 | |
f041e30b | 345 | FOR_EACH_SHIFT (trans, i) |
742e4900 | 346 | bitset_set (lookahead_set, TRANSITION_SYMBOL (trans, i)); |
08089d5d | 347 | |
742e4900 | 348 | /* Loop over all rules which require lookahead in this state. First |
c29240e7 | 349 | check for shift-reduce conflict, and try to resolve using |
9801d40c | 350 | precedence. */ |
cd08e51e AD |
351 | for (i = 0; i < reds->num; ++i) |
352 | if (reds->rules[i]->prec && reds->rules[i]->prec->prec | |
e9690142 | 353 | && !bitset_disjoint_p (reds->lookahead_tokens[i], lookahead_set)) |
9d774aff JD |
354 | resolve_sr_conflict (s, i, errors, &nerrs); |
355 | ||
356 | if (nerrs) | |
357 | { | |
358 | /* Some tokens have been explicitly made errors. Allocate a | |
359 | permanent errs structure for this state, to record them. */ | |
360 | state_errs_set (s, nerrs, errors); | |
361 | } | |
362 | if (obstack_object_size (&solved_conflicts_obstack)) | |
6fbe73b6 | 363 | s->solved_conflicts = obstack_finish0 (&solved_conflicts_obstack); |
41d7a5f2 | 364 | if (obstack_object_size (&solved_conflicts_xml_obstack)) |
6fbe73b6 | 365 | s->solved_conflicts_xml = obstack_finish0 (&solved_conflicts_xml_obstack); |
08089d5d | 366 | |
742e4900 | 367 | /* Loop over all rules which require lookahead in this state. Check |
c29240e7 | 368 | for conflicts not resolved above. */ |
cd08e51e | 369 | for (i = 0; i < reds->num; ++i) |
08089d5d | 370 | { |
742e4900 | 371 | if (!bitset_disjoint_p (reds->lookahead_tokens[i], lookahead_set)) |
e9690142 | 372 | conflicts[s->number] = 1; |
742e4900 | 373 | bitset_or (lookahead_set, lookahead_set, reds->lookahead_tokens[i]); |
c29240e7 AD |
374 | } |
375 | } | |
08089d5d | 376 | |
8b752b00 AD |
377 | |
378 | /*----------------------------------------------------------------. | |
379 | | Solve all the S/R conflicts using the precedence/associativity, | | |
380 | | and flag as inconsistent the states that still have conflicts. | | |
381 | `----------------------------------------------------------------*/ | |
382 | ||
08089d5d | 383 | void |
b408954b | 384 | conflicts_solve (void) |
08089d5d | 385 | { |
f041e30b | 386 | state_number i; |
742e4900 | 387 | /* List of lookahead tokens on which we explicitly raise a syntax error. */ |
da2a7671 | 388 | symbol **errors = xnmalloc (ntokens + 1, sizeof *errors); |
08089d5d | 389 | |
da2a7671 | 390 | conflicts = xcalloc (nstates, sizeof *conflicts); |
8dd162d3 | 391 | shift_set = bitset_create (ntokens, BITSET_FIXED); |
742e4900 | 392 | lookahead_set = bitset_create (ntokens, BITSET_FIXED); |
b408954b | 393 | obstack_init (&solved_conflicts_obstack); |
41d7a5f2 | 394 | obstack_init (&solved_conflicts_xml_obstack); |
08089d5d | 395 | |
c29240e7 | 396 | for (i = 0; i < nstates; i++) |
8b752b00 | 397 | { |
f041e30b | 398 | set_conflicts (states[i], errors); |
8b752b00 AD |
399 | |
400 | /* For uniformity of the code, make sure all the states have a valid | |
e9690142 | 401 | `errs' member. */ |
8b752b00 | 402 | if (!states[i]->errs) |
e9690142 | 403 | states[i]->errs = errs_new (0, 0); |
8b752b00 AD |
404 | } |
405 | ||
f041e30b | 406 | free (errors); |
08089d5d DM |
407 | } |
408 | ||
409 | ||
5967f0cf JD |
410 | void |
411 | conflicts_update_state_numbers (state_number old_to_new[], | |
412 | state_number nstates_old) | |
413 | { | |
14462c2b JD |
414 | state_number i; |
415 | for (i = 0; i < nstates_old; ++i) | |
5967f0cf JD |
416 | if (old_to_new[i] != nstates_old) |
417 | conflicts[old_to_new[i]] = conflicts[i]; | |
418 | } | |
419 | ||
420 | ||
c29240e7 AD |
421 | /*---------------------------------------------. |
422 | | Count the number of shift/reduce conflicts. | | |
423 | `---------------------------------------------*/ | |
424 | ||
0df87bb6 | 425 | static int |
f041e30b | 426 | count_sr_conflicts (state *s) |
08089d5d | 427 | { |
f9abaa2c | 428 | int i; |
0df87bb6 | 429 | int src_count = 0; |
f041e30b PE |
430 | transitions *trans = s->transitions; |
431 | reductions *reds = s->reductions; | |
08089d5d | 432 | |
f041e30b | 433 | if (!trans) |
0df87bb6 | 434 | return 0; |
08089d5d | 435 | |
742e4900 | 436 | bitset_zero (lookahead_set); |
8dd162d3 | 437 | bitset_zero (shift_set); |
08089d5d | 438 | |
f041e30b | 439 | FOR_EACH_SHIFT (trans, i) |
8dd162d3 | 440 | bitset_set (shift_set, TRANSITION_SYMBOL (trans, i)); |
08089d5d | 441 | |
cd08e51e | 442 | for (i = 0; i < reds->num; ++i) |
742e4900 | 443 | bitset_or (lookahead_set, lookahead_set, reds->lookahead_tokens[i]); |
08089d5d | 444 | |
742e4900 | 445 | bitset_and (lookahead_set, lookahead_set, shift_set); |
08089d5d | 446 | |
742e4900 | 447 | src_count = bitset_count (lookahead_set); |
0df87bb6 AD |
448 | |
449 | return src_count; | |
08089d5d DM |
450 | } |
451 | ||
452 | ||
676385e2 PH |
453 | /*----------------------------------------------------------------. |
454 | | Count the number of reduce/reduce conflicts. If ONE_PER_TOKEN, | | |
455 | | count one conflict for each token that has any reduce/reduce | | |
456 | | conflicts. Otherwise, count one conflict for each pair of | | |
457 | | conflicting reductions. | | |
458 | +`----------------------------------------------------------------*/ | |
c29240e7 | 459 | |
0df87bb6 | 460 | static int |
d0829076 | 461 | count_rr_conflicts (state *s, bool one_per_token) |
08089d5d | 462 | { |
c29240e7 | 463 | int i; |
f041e30b | 464 | reductions *reds = s->reductions; |
0df87bb6 | 465 | int rrc_count = 0; |
08089d5d | 466 | |
08089d5d DM |
467 | for (i = 0; i < ntokens; i++) |
468 | { | |
0df87bb6 AD |
469 | int count = 0; |
470 | int j; | |
cd08e51e | 471 | for (j = 0; j < reds->num; ++j) |
e9690142 JD |
472 | if (bitset_test (reds->lookahead_tokens[j], i)) |
473 | count++; | |
08089d5d | 474 | |
c29240e7 | 475 | if (count >= 2) |
e9690142 | 476 | rrc_count += one_per_token ? 1 : count-1; |
08089d5d | 477 | } |
0df87bb6 AD |
478 | |
479 | return rrc_count; | |
08089d5d DM |
480 | } |
481 | ||
52489d44 | 482 | |
be728048 PE |
483 | /*--------------------------------------------------------. |
484 | | Report the number of conflicts, using the Yacc format. | | |
485 | `--------------------------------------------------------*/ | |
52489d44 AD |
486 | |
487 | static void | |
be728048 | 488 | conflict_report (FILE *out, int src_num, int rrc_num) |
52489d44 | 489 | { |
be728048 PE |
490 | if (src_num && rrc_num) |
491 | fprintf (out, _("conflicts: %d shift/reduce, %d reduce/reduce\n"), | |
e9690142 | 492 | src_num, rrc_num); |
be728048 PE |
493 | else if (src_num) |
494 | fprintf (out, _("conflicts: %d shift/reduce\n"), src_num); | |
495 | else if (rrc_num) | |
496 | fprintf (out, _("conflicts: %d reduce/reduce\n"), rrc_num); | |
52489d44 AD |
497 | } |
498 | ||
499 | ||
0df87bb6 AD |
500 | /*-----------------------------------------------------------. |
501 | | Output the detailed description of states with conflicts. | | |
502 | `-----------------------------------------------------------*/ | |
c29240e7 AD |
503 | |
504 | void | |
0df87bb6 | 505 | conflicts_output (FILE *out) |
c29240e7 | 506 | { |
8307162d | 507 | bool printed_sth = false; |
f041e30b | 508 | state_number i; |
0df87bb6 | 509 | for (i = 0; i < nstates; i++) |
640748ee | 510 | { |
f041e30b | 511 | state *s = states[i]; |
640748ee | 512 | if (conflicts[i]) |
e9690142 JD |
513 | { |
514 | fprintf (out, _("State %d "), i); | |
515 | conflict_report (out, count_sr_conflicts (s), | |
516 | count_rr_conflicts (s, true)); | |
517 | printed_sth = true; | |
518 | } | |
640748ee | 519 | } |
d2d1b42b AD |
520 | if (printed_sth) |
521 | fputs ("\n\n", out); | |
0df87bb6 | 522 | } |
c29240e7 | 523 | |
676385e2 PH |
524 | /*--------------------------------------------------------. |
525 | | Total the number of S/R and R/R conflicts. Unlike the | | |
526 | | code in conflicts_output, however, count EACH pair of | | |
742e4900 | 527 | | reductions for the same state and lookahead as one | |
e9690142 | 528 | | conflict. | |
676385e2 PH |
529 | `--------------------------------------------------------*/ |
530 | ||
531 | int | |
532 | conflicts_total_count (void) | |
533 | { | |
f041e30b | 534 | state_number i; |
676385e2 PH |
535 | int count; |
536 | ||
537 | /* Conflicts by state. */ | |
538 | count = 0; | |
539 | for (i = 0; i < nstates; i++) | |
540 | if (conflicts[i]) | |
541 | { | |
e9690142 JD |
542 | count += count_sr_conflicts (states[i]); |
543 | count += count_rr_conflicts (states[i], false); | |
676385e2 PH |
544 | } |
545 | return count; | |
546 | } | |
e0e5bf84 | 547 | |
c29240e7 | 548 | |
0df87bb6 AD |
549 | /*------------------------------------------. |
550 | | Reporting the total number of conflicts. | | |
551 | `------------------------------------------*/ | |
0619caf0 | 552 | |
0df87bb6 AD |
553 | void |
554 | conflicts_print (void) | |
555 | { | |
a034c8b8 AD |
556 | /* Is the number of SR conflicts OK? Either EXPECTED_CONFLICTS is |
557 | not set, and then we want 0 SR, or else it is specified, in which | |
558 | case we want equality. */ | |
035aa4a0 PE |
559 | bool src_ok; |
560 | bool rrc_ok; | |
a034c8b8 | 561 | |
0df87bb6 AD |
562 | int src_total = 0; |
563 | int rrc_total = 0; | |
035aa4a0 PE |
564 | int src_expected; |
565 | int rrc_expected; | |
0df87bb6 AD |
566 | |
567 | /* Conflicts by state. */ | |
d57650a5 | 568 | { |
f041e30b | 569 | state_number i; |
d57650a5 AD |
570 | |
571 | for (i = 0; i < nstates; i++) | |
572 | if (conflicts[i]) | |
e9690142 JD |
573 | { |
574 | src_total += count_sr_conflicts (states[i]); | |
575 | rrc_total += count_rr_conflicts (states[i], true); | |
576 | } | |
d57650a5 | 577 | } |
c29240e7 | 578 | |
d6328241 PH |
579 | if (! glr_parser && rrc_total > 0 && expected_rr_conflicts != -1) |
580 | { | |
6fb8b256 | 581 | complain (Wother, _("%%expect-rr applies only to GLR parsers")); |
d6328241 PH |
582 | expected_rr_conflicts = -1; |
583 | } | |
584 | ||
035aa4a0 PE |
585 | src_expected = expected_sr_conflicts == -1 ? 0 : expected_sr_conflicts; |
586 | rrc_expected = expected_rr_conflicts == -1 ? 0 : expected_rr_conflicts; | |
587 | src_ok = src_total == src_expected; | |
588 | rrc_ok = rrc_total == rrc_expected; | |
a034c8b8 | 589 | |
d6328241 | 590 | /* If there are as many RR conflicts and SR conflicts as |
a034c8b8 | 591 | expected, then there is nothing to report. */ |
035aa4a0 | 592 | if (rrc_ok & src_ok) |
a034c8b8 AD |
593 | return; |
594 | ||
0619caf0 | 595 | /* Report the total number of conflicts on STDERR. */ |
786743d5 JD |
596 | if (expected_sr_conflicts == -1 && expected_rr_conflicts == -1) |
597 | { | |
327db05b | 598 | if (!(warnings_flag & Wconflicts_sr)) |
786743d5 | 599 | src_total = 0; |
327db05b | 600 | if (!(warnings_flag & Wconflicts_rr)) |
786743d5 JD |
601 | rrc_total = 0; |
602 | } | |
035aa4a0 PE |
603 | if (src_total | rrc_total) |
604 | { | |
0d43e605 AD |
605 | if (expected_sr_conflicts == -1 && expected_rr_conflicts == -1) |
606 | set_warning_issued (); | |
035aa4a0 | 607 | if (! yacc_flag) |
e9690142 | 608 | fprintf (stderr, "%s: ", current_file); |
035aa4a0 PE |
609 | conflict_report (stderr, src_total, rrc_total); |
610 | } | |
7da99ede | 611 | |
d6328241 | 612 | if (expected_sr_conflicts != -1 || expected_rr_conflicts != -1) |
76be9271 PE |
613 | { |
614 | if (! src_ok) | |
6fb8b256 VS |
615 | complain (complaint, ngettext ("expected %d shift/reduce conflict", |
616 | "expected %d shift/reduce conflicts", | |
617 | src_expected), | |
e9690142 | 618 | src_expected); |
d6328241 | 619 | if (! rrc_ok) |
6fb8b256 VS |
620 | complain (complaint, ngettext ("expected %d reduce/reduce conflict", |
621 | "expected %d reduce/reduce conflicts", | |
622 | rrc_expected), | |
e9690142 | 623 | rrc_expected); |
76be9271 | 624 | } |
c29240e7 AD |
625 | } |
626 | ||
627 | ||
08089d5d | 628 | void |
b408954b | 629 | conflicts_free (void) |
08089d5d | 630 | { |
afbb696d | 631 | free (conflicts); |
8dd162d3 | 632 | bitset_free (shift_set); |
742e4900 | 633 | bitset_free (lookahead_set); |
b408954b | 634 | obstack_free (&solved_conflicts_obstack, NULL); |
41d7a5f2 | 635 | obstack_free (&solved_conflicts_xml_obstack, NULL); |
08089d5d | 636 | } |