176 |
|
|
177 |
static void |
static void |
178 |
resolve_sr_conflict (state_t *state, int lookahead, |
resolve_sr_conflict (state_t *state, int lookahead, |
179 |
symbol_number_t *errs) |
symbol_t **errs) |
180 |
{ |
{ |
181 |
symbol_number_t i; |
symbol_number_t i; |
182 |
/* Find the rule to reduce by to get precedence of reduction. */ |
/* Find the rule to reduce by to get precedence of reduction. */ |
226 |
flush_shift (state, i); |
flush_shift (state, i); |
227 |
flush_reduce (lookaheads, i); |
flush_reduce (lookaheads, i); |
228 |
/* Record an explicit error for this token. */ |
/* Record an explicit error for this token. */ |
229 |
errs[nerrs++] = i; |
errs[nerrs++] = symbols[i]; |
230 |
break; |
break; |
231 |
|
|
232 |
case undef_assoc: |
case undef_assoc: |
255 |
`-------------------------------------------------------------------*/ |
`-------------------------------------------------------------------*/ |
256 |
|
|
257 |
static void |
static void |
258 |
set_conflicts (state_t *state, symbol_number_t *errs) |
set_conflicts (state_t *state, symbol_t **errs) |
259 |
{ |
{ |
260 |
int i; |
int i; |
261 |
transitions_t *transitions = state->transitions; |
transitions_t *transitions = state->transitions; |
265 |
|
|
266 |
bitset_zero (lookaheadset); |
bitset_zero (lookaheadset); |
267 |
|
|
268 |
for (i = 0; i < transitions->num && TRANSITION_IS_SHIFT (transitions, i); i++) |
FOR_EACH_SHIFT (transitions, i) |
269 |
if (!TRANSITION_IS_DISABLED (transitions, i)) |
bitset_set (lookaheadset, TRANSITION_SYMBOL (transitions, i)); |
|
bitset_set (lookaheadset, TRANSITION_SYMBOL (transitions, i)); |
|
270 |
|
|
271 |
/* Loop over all rules which require lookahead in this state. First |
/* Loop over all rules which require lookahead in this state. First |
272 |
check for shift-reduce conflict, and try to resolve using |
check for shift-reduce conflict, and try to resolve using |
302 |
{ |
{ |
303 |
state_number_t i; |
state_number_t i; |
304 |
/* List of lookaheads on which we explicitly raise a parse error. */ |
/* List of lookaheads on which we explicitly raise a parse error. */ |
305 |
symbol_number_t *errs = XMALLOC (symbol_number_t, ntokens + 1); |
symbol_t **errs = XMALLOC (symbol_t *, ntokens + 1); |
306 |
|
|
307 |
conflicts = XCALLOC (char, nstates); |
conflicts = XCALLOC (char, nstates); |
308 |
shiftset = bitset_create (ntokens, BITSET_FIXED); |
shiftset = bitset_create (ntokens, BITSET_FIXED); |
340 |
bitset_zero (lookaheadset); |
bitset_zero (lookaheadset); |
341 |
bitset_zero (shiftset); |
bitset_zero (shiftset); |
342 |
|
|
343 |
for (i = 0; i < transitions->num && TRANSITION_IS_SHIFT (transitions, i); i++) |
FOR_EACH_SHIFT (transitions, i) |
344 |
if (!TRANSITION_IS_DISABLED (transitions, i)) |
bitset_set (shiftset, TRANSITION_SYMBOL (transitions, i)); |
|
bitset_set (shiftset, TRANSITION_SYMBOL (transitions, i)); |
|
345 |
|
|
346 |
for (i = 0; i < state->nlookaheads; ++i) |
for (i = 0; i < state->nlookaheads; ++i) |
347 |
bitset_or (lookaheadset, lookaheadset, state->lookaheads[i]); |
bitset_or (lookaheadset, lookaheadset, state->lookaheads[i]); |
432 |
conflicts_output (FILE *out) |
conflicts_output (FILE *out) |
433 |
{ |
{ |
434 |
bool printed_sth = FALSE; |
bool printed_sth = FALSE; |
435 |
|
bool *used_rules = XCALLOC (bool, nrules); |
436 |
state_number_t i; |
state_number_t i; |
437 |
for (i = 0; i < nstates; i++) |
for (i = 0; i < nstates; i++) |
438 |
if (conflicts[i]) |
{ |
439 |
{ |
state_t *s = states[i]; |
440 |
fprintf (out, _("State %d contains "), i); |
int j; |
441 |
fputs (conflict_report (count_sr_conflicts (states[i]), |
for (j = 0; j < s->reductions->num; ++j) |
442 |
count_rr_conflicts (states[i], TRUE)), out); |
used_rules[s->reductions->rules[j]->number] = TRUE; |
443 |
printed_sth = TRUE; |
if (conflicts[i]) |
444 |
} |
{ |
445 |
|
fprintf (out, _("State %d contains "), i); |
446 |
|
fputs (conflict_report (count_sr_conflicts (s), |
447 |
|
count_rr_conflicts (s, TRUE)), out); |
448 |
|
printed_sth = TRUE; |
449 |
|
} |
450 |
|
} |
451 |
if (printed_sth) |
if (printed_sth) |
452 |
fputs ("\n\n", out); |
fputs ("\n\n", out); |
453 |
|
|
454 |
|
for (i = 0; i < nstates; i++) |
455 |
|
{ |
456 |
|
state_t *s = states[i]; |
457 |
|
reductions_t *r = s->reductions; |
458 |
|
int j; |
459 |
|
for (j = 0; j < r->num; ++j) |
460 |
|
if (!used_rules[r->rules[j]->number]) |
461 |
|
{ |
462 |
|
LOCATION_PRINT (stderr, r->rules[j]->location); |
463 |
|
fprintf (stderr, ": %s: %s: ", |
464 |
|
_("warning"), |
465 |
|
_("rule never reduced because of conflicts")); |
466 |
|
rule_print (r->rules[j], stderr); |
467 |
|
} |
468 |
|
} |
469 |
|
free (used_rules); |
470 |
} |
} |
471 |
|
|
472 |
/*--------------------------------------------------------. |
/*--------------------------------------------------------. |