1 /* Find and resolve or report lookahead conflicts for bison,
3 Copyright (C) 1984, 1989, 1992, 2000-2014 Free Software Foundation,
6 This file is part of Bison, the GNU Compiler Compiler.
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.
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.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
28 #include "conflicts.h"
33 #include "print-xml.h"
38 /* -1 stands for not specified. */
39 int expected_sr_conflicts
= -1;
40 int expected_rr_conflicts
= -1;
41 static char *conflicts
;
42 static struct obstack solved_conflicts_obstack
;
43 static struct obstack solved_conflicts_xml_obstack
;
45 static bitset shift_set
;
46 static bitset lookahead_set
;
50 enum conflict_resolution
60 /*----------------------------------------------------------------.
61 | Explain how an SR conflict between TOKEN and RULE was resolved: |
63 `----------------------------------------------------------------*/
66 log_resolution (rule
*r
, symbol_number token
,
67 enum conflict_resolution resolution
)
69 if (report_flag
& report_solved_conflicts
)
71 /* The description of the resolution. */
74 case shift_resolution
:
75 case right_resolution
:
76 obstack_printf (&solved_conflicts_obstack
,
77 _(" Conflict between rule %d and token %s"
78 " resolved as shift"),
83 case reduce_resolution
:
85 obstack_printf (&solved_conflicts_obstack
,
86 _(" Conflict between rule %d and token %s"
87 " resolved as reduce"),
92 case nonassoc_resolution
:
93 obstack_printf (&solved_conflicts_obstack
,
94 _(" Conflict between rule %d and token %s"
95 " resolved as an error"),
104 case shift_resolution
:
105 obstack_printf (&solved_conflicts_obstack
,
108 symbols
[token
]->tag
);
111 case reduce_resolution
:
112 obstack_printf (&solved_conflicts_obstack
,
118 case left_resolution
:
119 obstack_printf (&solved_conflicts_obstack
,
121 symbols
[token
]->tag
);
124 case right_resolution
:
125 obstack_printf (&solved_conflicts_obstack
,
127 symbols
[token
]->tag
);
130 case nonassoc_resolution
:
131 obstack_printf (&solved_conflicts_obstack
,
133 symbols
[token
]->tag
);
137 obstack_sgrow (&solved_conflicts_obstack
, ".\n");
143 /* The description of the resolution. */
146 case shift_resolution
:
147 case right_resolution
:
148 obstack_printf (&solved_conflicts_xml_obstack
,
149 " <resolution rule=\"%d\" symbol=\"%s\""
152 xml_escape (symbols
[token
]->tag
));
155 case reduce_resolution
:
156 case left_resolution
:
157 obstack_printf (&solved_conflicts_xml_obstack
,
158 " <resolution rule=\"%d\" symbol=\"%s\""
161 xml_escape (symbols
[token
]->tag
));
164 case nonassoc_resolution
:
165 obstack_printf (&solved_conflicts_xml_obstack
,
166 " <resolution rule=\"%d\" symbol=\"%s\""
169 xml_escape (symbols
[token
]->tag
));
176 case shift_resolution
:
177 obstack_printf (&solved_conflicts_xml_obstack
,
179 xml_escape_n (0, r
->prec
->tag
),
180 xml_escape_n (1, symbols
[token
]->tag
));
183 case reduce_resolution
:
184 obstack_printf (&solved_conflicts_xml_obstack
,
186 xml_escape_n (0, symbols
[token
]->tag
),
187 xml_escape_n (1, r
->prec
->tag
));
190 case left_resolution
:
191 obstack_printf (&solved_conflicts_xml_obstack
,
193 xml_escape (symbols
[token
]->tag
));
196 case right_resolution
:
197 obstack_printf (&solved_conflicts_xml_obstack
,
199 xml_escape (symbols
[token
]->tag
));
202 case nonassoc_resolution
:
203 obstack_printf (&solved_conflicts_xml_obstack
,
205 xml_escape (symbols
[token
]->tag
));
209 obstack_sgrow (&solved_conflicts_xml_obstack
, "</resolution>\n");
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 |
217 | favor of the reduction or as an error (%nonassoc). |
218 `------------------------------------------------------------------*/
221 flush_shift (state
*s
, int token
)
223 transitions
*trans
= s
->transitions
;
226 bitset_reset (lookahead_set
, token
);
227 for (i
= 0; i
< trans
->num
; i
++)
228 if (!TRANSITION_IS_DISABLED (trans
, i
)
229 && TRANSITION_SYMBOL (trans
, i
) == token
)
230 TRANSITION_DISABLE (trans
, i
);
234 /*--------------------------------------------------------------------.
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). |
238 `--------------------------------------------------------------------*/
241 flush_reduce (bitset lookahead_tokens
, int token
)
243 bitset_reset (lookahead_tokens
, token
);
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. |
253 | RULENO is the number of the lookahead bitset to consider. |
255 | ERRORS and NERRS can be used to store discovered explicit |
257 `------------------------------------------------------------------*/
260 resolve_sr_conflict (state
*s
, int ruleno
, symbol
**errors
, int *nerrs
)
263 reductions
*reds
= s
->reductions
;
264 /* Find the rule to reduce by to get precedence of reduction. */
265 rule
*redrule
= reds
->rules
[ruleno
];
266 int redprec
= redrule
->prec
->prec
;
267 bitset lookahead_tokens
= reds
->lookahead_tokens
[ruleno
];
269 for (i
= 0; i
< ntokens
; i
++)
270 if (bitset_test (lookahead_tokens
, i
)
271 && bitset_test (lookahead_set
, i
)
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
)
279 register_precedence (redrule
->prec
->number
, i
);
280 log_resolution (redrule
, i
, reduce_resolution
);
283 else if (symbols
[i
]->prec
> redprec
)
285 register_precedence (i
, redrule
->prec
->number
);
286 log_resolution (redrule
, i
, shift_resolution
);
287 flush_reduce (lookahead_tokens
, i
);
290 /* Matching precedence levels.
291 For non-defined associativity, keep both: unexpected
292 associativity conflict.
293 For left associativity, keep only the reduction.
294 For right associativity, keep only the shift.
295 For nonassociativity, keep neither. */
297 switch (symbols
[i
]->assoc
)
302 case precedence_assoc
:
306 register_assoc (i
, redrule
->prec
->number
);
307 log_resolution (redrule
, i
, right_resolution
);
308 flush_reduce (lookahead_tokens
, i
);
312 register_assoc (i
, redrule
->prec
->number
);
313 log_resolution (redrule
, i
, left_resolution
);
318 register_assoc (i
, redrule
->prec
->number
);
319 log_resolution (redrule
, i
, nonassoc_resolution
);
321 flush_reduce (lookahead_tokens
, i
);
322 /* Record an explicit error for this token. */
323 errors
[(*nerrs
)++] = symbols
[i
];
330 /*-------------------------------------------------------------------.
331 | Solve the S/R conflicts of state S using the |
332 | precedence/associativity, and flag it inconsistent if it still has |
333 | conflicts. ERRORS can be used as storage to compute the list of |
334 | lookahead tokens on which S raises a syntax error (%nonassoc). |
335 `-------------------------------------------------------------------*/
338 set_conflicts (state
*s
, symbol
**errors
)
341 transitions
*trans
= s
->transitions
;
342 reductions
*reds
= s
->reductions
;
348 bitset_zero (lookahead_set
);
350 FOR_EACH_SHIFT (trans
, i
)
351 bitset_set (lookahead_set
, TRANSITION_SYMBOL (trans
, i
));
353 /* Loop over all rules which require lookahead in this state. First
354 check for shift-reduce conflict, and try to resolve using
356 for (i
= 0; i
< reds
->num
; ++i
)
357 if (reds
->rules
[i
]->prec
&& reds
->rules
[i
]->prec
->prec
358 && !bitset_disjoint_p (reds
->lookahead_tokens
[i
], lookahead_set
))
359 resolve_sr_conflict (s
, i
, errors
, &nerrs
);
363 /* Some tokens have been explicitly made errors. Allocate a
364 permanent errs structure for this state, to record them. */
365 state_errs_set (s
, nerrs
, errors
);
367 if (obstack_object_size (&solved_conflicts_obstack
))
368 s
->solved_conflicts
= obstack_finish0 (&solved_conflicts_obstack
);
369 if (obstack_object_size (&solved_conflicts_xml_obstack
))
370 s
->solved_conflicts_xml
= obstack_finish0 (&solved_conflicts_xml_obstack
);
372 /* Loop over all rules which require lookahead in this state. Check
373 for conflicts not resolved above. */
374 for (i
= 0; i
< reds
->num
; ++i
)
376 if (!bitset_disjoint_p (reds
->lookahead_tokens
[i
], lookahead_set
))
377 conflicts
[s
->number
] = 1;
378 bitset_or (lookahead_set
, lookahead_set
, reds
->lookahead_tokens
[i
]);
383 /*----------------------------------------------------------------.
384 | Solve all the S/R conflicts using the precedence/associativity, |
385 | and flag as inconsistent the states that still have conflicts. |
386 `----------------------------------------------------------------*/
389 conflicts_solve (void)
392 /* List of lookahead tokens on which we explicitly raise a syntax error. */
393 symbol
**errors
= xnmalloc (ntokens
+ 1, sizeof *errors
);
395 conflicts
= xcalloc (nstates
, sizeof *conflicts
);
396 shift_set
= bitset_create (ntokens
, BITSET_FIXED
);
397 lookahead_set
= bitset_create (ntokens
, BITSET_FIXED
);
398 obstack_init (&solved_conflicts_obstack
);
399 obstack_init (&solved_conflicts_xml_obstack
);
401 for (i
= 0; i
< nstates
; i
++)
403 set_conflicts (states
[i
], errors
);
405 /* For uniformity of the code, make sure all the states have a valid
407 if (!states
[i
]->errs
)
408 states
[i
]->errs
= errs_new (0, 0);
416 conflicts_update_state_numbers (state_number old_to_new
[],
417 state_number nstates_old
)
420 for (i
= 0; i
< nstates_old
; ++i
)
421 if (old_to_new
[i
] != nstates_old
)
422 conflicts
[old_to_new
[i
]] = conflicts
[i
];
426 /*---------------------------------------------.
427 | Count the number of shift/reduce conflicts. |
428 `---------------------------------------------*/
431 count_state_sr_conflicts (state
*s
)
434 transitions
*trans
= s
->transitions
;
435 reductions
*reds
= s
->reductions
;
440 bitset_zero (lookahead_set
);
441 bitset_zero (shift_set
);
443 FOR_EACH_SHIFT (trans
, i
)
444 bitset_set (shift_set
, TRANSITION_SYMBOL (trans
, i
));
446 for (i
= 0; i
< reds
->num
; ++i
)
447 bitset_or (lookahead_set
, lookahead_set
, reds
->lookahead_tokens
[i
]);
449 bitset_and (lookahead_set
, lookahead_set
, shift_set
);
451 return bitset_count (lookahead_set
);
454 /*---------------------------------------------.
455 | The total number of shift/reduce conflicts. |
456 `---------------------------------------------*/
459 count_sr_conflicts (void)
464 /* Conflicts by state. */
465 for (i
= 0; i
< nstates
; i
++)
467 res
+= count_state_sr_conflicts (states
[i
]);
473 /*----------------------------------------------------------------.
474 | Count the number of reduce/reduce conflicts. If ONE_PER_TOKEN, |
475 | count one conflict for each token that has any reduce/reduce |
476 | conflicts. Otherwise, count one conflict for each pair of |
477 | conflicting reductions. |
478 `----------------------------------------------------------------*/
481 count_state_rr_conflicts (state
*s
, bool one_per_token
)
484 reductions
*reds
= s
->reductions
;
487 for (i
= 0; i
< ntokens
; i
++)
491 for (j
= 0; j
< reds
->num
; ++j
)
492 count
+= bitset_test (reds
->lookahead_tokens
[j
], i
);
494 res
+= one_per_token
? 1 : count
-1;
501 count_rr_conflicts (bool one_per_token
)
506 /* Conflicts by state. */
507 for (i
= 0; i
< nstates
; i
++)
509 res
+= count_state_rr_conflicts (states
[i
], one_per_token
);
514 /*-----------------------------------------------------------.
515 | Output the detailed description of states with conflicts. |
516 `-----------------------------------------------------------*/
519 conflicts_output (FILE *out
)
521 bool printed_sth
= false;
523 for (i
= 0; i
< nstates
; i
++)
525 state
*s
= states
[i
];
528 int src
= count_state_sr_conflicts (s
);
529 int rrc
= count_state_rr_conflicts (s
, true);
530 fprintf (out
, _("State %d "), i
);
533 _("conflicts: %d shift/reduce, %d reduce/reduce\n"),
536 fprintf (out
, _("conflicts: %d shift/reduce\n"), src
);
538 fprintf (out
, _("conflicts: %d reduce/reduce\n"), rrc
);
546 /*--------------------------------------------------------.
547 | Total the number of S/R and R/R conflicts. Unlike the |
548 | code in conflicts_output, however, count EACH pair of |
549 | reductions for the same state and lookahead as one |
551 `--------------------------------------------------------*/
554 conflicts_total_count (void)
556 return count_sr_conflicts () + count_rr_conflicts (false);
560 /*------------------------------------------.
561 | Reporting the total number of conflicts. |
562 `------------------------------------------*/
565 conflicts_print (void)
567 if (! glr_parser
&& expected_rr_conflicts
!= -1)
569 complain (NULL
, Wother
, _("%%expect-rr applies only to GLR parsers"));
570 expected_rr_conflicts
= -1;
573 /* Screams for factoring, but almost useless because of the
574 different strings to translate. */
576 int total
= count_sr_conflicts ();
577 /* If %expect is not used, but %expect-rr is, then expect 0 sr. */
579 (expected_sr_conflicts
== -1 && expected_rr_conflicts
!= -1)
581 : expected_sr_conflicts
;
584 if (expected
!= total
)
585 complain (NULL
, complaint
,
586 _("shift/reduce conflicts: %d found, %d expected"),
590 complain (NULL
, Wconflicts_sr
,
591 ngettext ("%d shift/reduce conflict",
592 "%d shift/reduce conflicts",
598 int total
= count_rr_conflicts (true);
599 /* If %expect-rr is not used, but %expect is, then expect 0 rr. */
601 (expected_rr_conflicts
== -1 && expected_sr_conflicts
!= -1)
603 : expected_rr_conflicts
;
606 if (expected
!= total
)
607 complain (NULL
, complaint
,
608 _("reduce/reduce conflicts: %d found, %d expected"),
612 complain (NULL
, Wconflicts_rr
,
613 ngettext ("%d reduce/reduce conflict",
614 "%d reduce/reduce conflicts",
622 conflicts_free (void)
625 bitset_free (shift_set
);
626 bitset_free (lookahead_set
);
627 obstack_free (&solved_conflicts_obstack
, NULL
);
628 obstack_free (&solved_conflicts_xml_obstack
, NULL
);