- int i;
- rule_number_t default_rule = 0;
- reductions_t *redp = state->reductions;
- transitions_t *transitions = state->shifts;
- errs_t *errp = state->errs;
- /* set nonzero to inhibit having any default reduction */
- int nodefault = 0;
- int conflicted = 0;
-
- for (i = 0; i < ntokens; i++)
- actrow[i] = conflrow[i] = 0;
-
- if (redp->num >= 1)
- {
- int j;
- /* loop over all the rules available here which require
- lookahead */
- for (i = state->nlookaheads - 1; i >= 0; --i)
- /* and find each token which the rule finds acceptable
- to come next */
- BITSET_EXECUTE (state->lookaheads[i], 0, j,
- {
- /* and record this rule as the rule to use if that
- token follows. */
- if (actrow[j] != 0)
- conflicted = conflrow[j] = 1;
- actrow[j] = -state->lookaheads_rule[i]->number;
- });
- }
-
- /* Now see which tokens are allowed for shifts in this state. For
- them, record the shift as the thing to do. So shift is preferred
- to reduce. */
- for (i = 0; i < transitions->num && TRANSITION_IS_SHIFT (transitions, i); i++)
- if (!TRANSITION_IS_DISABLED (transitions, i))
- {
- symbol_number_t symbol = TRANSITION_SYMBOL (transitions, i);
- state_number_t shift_state = transitions->states[i];
-
- if (actrow[symbol] != 0)
- conflicted = conflrow[symbol] = 1;
- actrow[symbol] = state_number_as_int (shift_state);
-
- /* Do not use any default reduction if there is a shift for
- error */
- if (symbol == errtoken->number)
- nodefault = 1;
- }
-
- /* See which tokens are an explicit error in this state (due to
- %nonassoc). For them, record SHRT_MIN as the action. */
- for (i = 0; i < errp->num; i++)
- {
- symbol_number_t symbol = errp->symbols[i];
- actrow[symbol] = SHRT_MIN;
- }
-
- /* Now find the most common reduction and make it the default action
- for this state. */
-
- if (redp->num >= 1 && !nodefault)
- {
- if (state->consistent)
- default_rule = redp->rules[0];
- else
- {
- int max = 0;
- for (i = 0; i < state->nlookaheads; i++)
- {
- int count = 0;
- rule_number_t rule = state->lookaheads_rule[i]->number;
- symbol_number_t j;
-
- for (j = 0; j < ntokens; j++)
- if (actrow[j] == -rule)
- count++;
-
- if (count > max)
- {
- max = count;
- default_rule = rule;
- }
- }
-
- /* GLR parsers need space for conflict lists, so we can't
- default conflicted entries. For non-conflicted entries
- or as long as we are not building a GLR parser,
- actions that match the default are replaced with zero,
- which means "use the default". */
-
- if (max > 0)
- {
- int j;
- for (j = 0; j < ntokens; j++)
- if (actrow[j] == -default_rule
- && ! (glr_parser && conflrow[j]))
- actrow[j] = 0;
- }
- }
- }
-
- /* If have no default rule, the default is an error.
- So replace any action which says "error" with "use default". */
-
- if (default_rule == 0)
- for (i = 0; i < ntokens; i++)
- if (actrow[i] == SHRT_MIN)
- actrow[i] = 0;
-
- if (conflicted)
- conflict_row (state);
-
- return default_rule;