static int count_sr_conflicts (state *s) { int i; int src_count = 0; transitions *trans = s->transitions; reductions *reds = s->reductions; if (!trans) return 0; bitset_zero (lookahead_set); bitset_zero (shift_set); FOR_EACH_SHIFT (trans, i) bitset_set (shift_set, TRANSITION_SYMBOL (trans, i)); for (i = 0; i < reds->num; ++i) bitset_or (lookahead_set, lookahead_set, reds->lookahead_tokens[i]); bitset_and (lookahead_set, lookahead_set, shift_set); src_count = bitset_count (lookahead_set); return src_count; }
static void no_reduce_bitset_init (state const *s, bitset *no_reduce_set) { int n; *no_reduce_set = bitset_create (ntokens, BITSET_FIXED); bitset_zero (*no_reduce_set); FOR_EACH_SHIFT (s->transitions, n) bitset_set (*no_reduce_set, TRANSITION_SYMBOL (s->transitions, n)); for (n = 0; n < s->errs->num; ++n) if (s->errs->symbols[n]) bitset_set (*no_reduce_set, s->errs->symbols[n]->content->number); }
static void set_conflicts (state *s, symbol **errors) { int i; transitions *trans = s->transitions; reductions *reds = s->reductions; int nerrs = 0; if (s->consistent) return; bitset_zero (lookahead_set); FOR_EACH_SHIFT (trans, i) bitset_set (lookahead_set, TRANSITION_SYMBOL (trans, i)); /* Loop over all rules which require lookahead in this state. First check for shift-reduce conflict, and try to resolve using precedence. */ for (i = 0; i < reds->num; ++i) if (reds->rules[i]->prec && reds->rules[i]->prec->prec && !bitset_disjoint_p (reds->lookahead_tokens[i], lookahead_set)) resolve_sr_conflict (s, i, errors, &nerrs); if (nerrs) { /* Some tokens have been explicitly made errors. Allocate a permanent errs structure for this state, to record them. */ state_errs_set (s, nerrs, errors); } if (obstack_object_size (&solved_conflicts_obstack)) { obstack_1grow (&solved_conflicts_obstack, '\0'); s->solved_conflicts = obstack_finish (&solved_conflicts_obstack); } if (obstack_object_size (&solved_conflicts_xml_obstack)) { obstack_1grow (&solved_conflicts_xml_obstack, '\0'); s->solved_conflicts_xml = obstack_finish (&solved_conflicts_xml_obstack); } /* Loop over all rules which require lookahead in this state. Check for conflicts not resolved above. */ for (i = 0; i < reds->num; ++i) { if (!bitset_disjoint_p (reds->lookahead_tokens[i], lookahead_set)) conflicts[s->number] = 1; bitset_or (lookahead_set, lookahead_set, reds->lookahead_tokens[i]); } }
/** * \pre * - \c *edgesp and \c edge_counts were computed by * \c ielr_compute_internal_follow_edges. * \post * - \c *always_followsp is a new \c bitsetv with \c ngotos rows and * \c ntokens columns. * - <tt>(*always_followsp)[i][j]</tt> is set iff token \c j is an always * follow (that is, it's computed by internal and successor edges) of goto * \c i. * - Rows of \c *edgesp have been realloc'ed and extended to include * successor follow edges. \c edge_counts has not been updated. */ static void ielr_compute_always_follows (goto_number ***edgesp, int const edge_counts[], bitsetv *always_followsp) { *always_followsp = bitsetv_create (ngotos, ntokens, BITSET_FIXED); { goto_number *edge_array = xnmalloc (ngotos, sizeof *edge_array); goto_number i; for (i = 0; i < ngotos; ++i) { goto_number nedges = edge_counts[i]; { int j; transitions *trans = states[to_state[i]]->transitions; FOR_EACH_SHIFT (trans, j) bitset_set ((*always_followsp)[i], TRANSITION_SYMBOL (trans, j)); for (; j < trans->num; ++j) { symbol_number sym = TRANSITION_SYMBOL (trans, j); if (nullable[sym - ntokens]) edge_array[nedges++] = map_goto (to_state[i], sym); } } if (nedges - edge_counts[i]) { (*edgesp)[i] = xnrealloc ((*edgesp)[i], nedges + 1, sizeof *(*edgesp)[i]); memcpy ((*edgesp)[i] + edge_counts[i], edge_array + edge_counts[i], (nedges - edge_counts[i]) * sizeof *(*edgesp)[i]); (*edgesp)[i][nedges] = END_NODE; } } free (edge_array); } relation_digraph (*edgesp, ngotos, always_followsp); if (trace_flag & trace_ielr) { fprintf (stderr, "always follow edges:\n"); relation_print (*edgesp, ngotos, stderr); fprintf (stderr, "always_follows:\n"); debug_bitsetv (*always_followsp); } }
static void print_reductions (FILE *out, int level, state *s) { transitions *trans = s->transitions; reductions *reds = s->reductions; rule *default_reduction = NULL; int report = false; int i, j; if (reds->num == 0) { xml_puts (out, level, "<reductions/>"); return; } if (yydefact[s->number] != 0) default_reduction = &rules[yydefact[s->number] - 1]; bitset_zero (no_reduce_set); FOR_EACH_SHIFT (trans, i) bitset_set (no_reduce_set, TRANSITION_SYMBOL (trans, i)); for (i = 0; i < s->errs->num; ++i) if (s->errs->symbols[i]) bitset_set (no_reduce_set, s->errs->symbols[i]->number); if (default_reduction) report = true; if (reds->lookahead_tokens) for (i = 0; i < ntokens; i++) { bool count = bitset_test (no_reduce_set, i); for (j = 0; j < reds->num; ++j) if (bitset_test (reds->lookahead_tokens[j], i)) { if (! count) { if (reds->rules[j] != default_reduction) report = true; count = true; } else { report = true; } } } /* Nothing to report. */ if (!report) { xml_puts (out, level, "<reductions/>"); return; } xml_puts (out, level, "<reductions>"); /* Report lookahead tokens (or $default) and reductions. */ if (reds->lookahead_tokens) for (i = 0; i < ntokens; i++) { bool defaulted = false; bool count = bitset_test (no_reduce_set, i); for (j = 0; j < reds->num; ++j) if (bitset_test (reds->lookahead_tokens[j], i)) { if (! count) { if (reds->rules[j] != default_reduction) print_reduction (out, level + 1, symbols[i]->tag, reds->rules[j], true); else defaulted = true; count = true; } else { if (defaulted) print_reduction (out, level + 1, symbols[i]->tag, default_reduction, true); defaulted = false; print_reduction (out, level + 1, symbols[i]->tag, reds->rules[j], false); } } } if (default_reduction) print_reduction (out, level + 1, "$default", default_reduction, true); xml_puts (out, level, "</reductions>"); }
static void print_reductions (FILE *out, state *s) { transitions *trans = s->transitions; reductions *reds = s->reductions; rule *default_rule = NULL; size_t width = 0; int i, j; if (reds->num == 0) return; default_rule = state_default_rule (s); bitset_zero (shift_set); FOR_EACH_SHIFT (trans, i) bitset_set (shift_set, TRANSITION_SYMBOL (trans, i)); /* Compute the width of the look-ahead token column. */ if (default_rule) width = strlen (_("$default")); if (reds->look_ahead_tokens) for (i = 0; i < ntokens; i++) { bool count = bitset_test (shift_set, i); for (j = 0; j < reds->num; ++j) if (bitset_test (reds->look_ahead_tokens[j], i)) { if (! count) { if (reds->rules[j] != default_rule) max_length (&width, symbols[i]->tag); count = true; } else { max_length (&width, symbols[i]->tag); } } } /* Nothing to report. */ if (!width) return; fputc ('\n', out); width += 2; /* Report look-ahead tokens (or $default) and reductions. */ if (reds->look_ahead_tokens) for (i = 0; i < ntokens; i++) { bool defaulted = false; bool count = bitset_test (shift_set, i); for (j = 0; j < reds->num; ++j) if (bitset_test (reds->look_ahead_tokens[j], i)) { if (! count) { if (reds->rules[j] != default_rule) print_reduction (out, width, symbols[i]->tag, reds->rules[j], true); else defaulted = true; count = true; } else { if (defaulted) print_reduction (out, width, symbols[i]->tag, default_rule, true); defaulted = false; print_reduction (out, width, symbols[i]->tag, reds->rules[j], false); } } } if (default_rule) print_reduction (out, width, _("$default"), default_rule, true); }
static void print_reductions (FILE *out, state *s) { transitions *trans = s->transitions; reductions *reds = s->reductions; rule *default_reduction = NULL; size_t width = 0; int i, j; bool default_reduction_only = true; if (reds->num == 0) return; if (yydefact[s->number] != 0) default_reduction = &rules[yydefact[s->number] - 1]; bitset_zero (no_reduce_set); FOR_EACH_SHIFT (trans, i) bitset_set (no_reduce_set, TRANSITION_SYMBOL (trans, i)); for (i = 0; i < s->errs->num; ++i) if (s->errs->symbols[i]) bitset_set (no_reduce_set, s->errs->symbols[i]->number); /* Compute the width of the lookahead token column. */ if (default_reduction) width = strlen (_("$default")); if (reds->lookahead_tokens) for (i = 0; i < ntokens; i++) { bool count = bitset_test (no_reduce_set, i); for (j = 0; j < reds->num; ++j) if (bitset_test (reds->lookahead_tokens[j], i)) { if (! count) { if (reds->rules[j] != default_reduction) max_length (&width, symbols[i]->tag); count = true; } else { max_length (&width, symbols[i]->tag); } } } /* Nothing to report. */ if (!width) return; fputc ('\n', out); width += 2; /* Report lookahead tokens (or $default) and reductions. */ if (reds->lookahead_tokens) for (i = 0; i < ntokens; i++) { bool defaulted = false; bool count = bitset_test (no_reduce_set, i); if (count) default_reduction_only = false; for (j = 0; j < reds->num; ++j) if (bitset_test (reds->lookahead_tokens[j], i)) { if (! count) { if (reds->rules[j] != default_reduction) { default_reduction_only = false; print_reduction (out, width, symbols[i]->tag, reds->rules[j], true); } else defaulted = true; count = true; } else { default_reduction_only = false; if (defaulted) print_reduction (out, width, symbols[i]->tag, default_reduction, true); defaulted = false; print_reduction (out, width, symbols[i]->tag, reds->rules[j], false); } } } if (default_reduction) { char *default_reductions = muscle_percent_define_get ("lr.default-reductions"); print_reduction (out, width, _("$default"), default_reduction, true); aver (0 == strcmp (default_reductions, "most") || (0 == strcmp (default_reductions, "consistent") && default_reduction_only) || (reds->num == 1 && reds->rules[0]->number == 0)); free (default_reductions); } }