X-Git-Url: https://git.saurik.com/bison.git/blobdiff_plain/3650b4b8dddf39f6d6f5b2514d1f036548c5fe2e..273a74fa89392c33ef4912036c290ad47e535da6:/src/output.c diff --git a/src/output.c b/src/output.c index 5b8ca108..07977a5d 100644 --- a/src/output.c +++ b/src/output.c @@ -183,10 +183,10 @@ static unsigned int *conflict_list = NULL; static int conflict_list_cnt; static int conflict_list_free; -/* TABLE_SIZE is the allocated size of both TABLE and CHECK. - We start with the original hard-coded value: SHRT_MAX - (yes, not USHRT_MAX). */ -static size_t table_size = SHRT_MAX; +/* TABLE_SIZE is the allocated size of both TABLE and CHECK. We start + with more or less the original hard-coded value (which was + SHRT_MAX). */ +static size_t table_size = 32768; static base_t *table = NULL; static base_t *check = NULL; /* The value used in TABLE to denote explicit parse errors @@ -216,7 +216,7 @@ table_grow (size_t desired) while (table_size <= desired) table_size *= 2; - if (trace_flag) + if (trace_flag & trace_resource) fprintf (stderr, "growing table and check from: %d to %d\n", old_size, table_size); @@ -305,8 +305,9 @@ static void prepare_tokens (void) { muscle_insert_symbol_number_table ("translate", - token_translations, - 0, 1, max_user_token_number + 1); + token_translations, + token_translations[0], + 1, max_user_token_number + 1); { int i; @@ -493,15 +494,15 @@ conflict_row (state_t *state) | that has any such conflicts. | `------------------------------------------------------------------*/ -static rule_number_t +static rule_t * action_row (state_t *state) { int i; - rule_number_t default_rule = -1; + rule_t *default_rule = NULL; reductions_t *redp = state->reductions; transitions_t *transitions = state->transitions; errs_t *errp = state->errs; - /* set nonzero to inhibit having any default reduction */ + /* Set to nonzero to inhibit having any default reduction. */ int nodefault = 0; int conflicted = 0; @@ -530,28 +531,27 @@ action_row (state_t *state) /* 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]; + FOR_EACH_SHIFT (transitions, i) + { + symbol_number_t symbol = TRANSITION_SYMBOL (transitions, i); + state_t *shift_state = transitions->states[i]; - if (actrow[symbol] != 0) - conflicted = conflrow[symbol] = 1; - actrow[symbol] = state_number_as_int (shift_state); + if (actrow[symbol] != 0) + conflicted = conflrow[symbol] = 1; + actrow[symbol] = state_number_as_int (shift_state->number); - /* Do not use any default reduction if there is a shift for - error */ - if (symbol == errtoken->number) - nodefault = 1; - } + /* 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 ACTION_MIN as the action. */ for (i = 0; i < errp->num; i++) { - symbol_number_t symbol = errp->symbols[i]; - actrow[symbol] = ACTION_MIN; + symbol_t *symbol = errp->symbols[i]; + actrow[symbol->number] = ACTION_MIN; } /* Now find the most common reduction and make it the default action @@ -567,11 +567,11 @@ action_row (state_t *state) for (i = 0; i < state->nlookaheads; i++) { int count = 0; - rule_number_t rule = state->lookaheads_rule[i]->number; + rule_t *rule = state->lookaheads_rule[i]; symbol_number_t j; for (j = 0; j < ntokens; j++) - if (actrow[j] == rule_number_as_item_number (rule)) + if (actrow[j] == rule_number_as_item_number (rule->number)) count++; if (count > max) @@ -591,17 +591,27 @@ action_row (state_t *state) { int j; for (j = 0; j < ntokens; j++) - if (actrow[j] == rule_number_as_item_number (default_rule) + if (actrow[j] == rule_number_as_item_number (default_rule->number) && ! (glr_parser && conflrow[j])) actrow[j] = 0; } } } + /* Find the rules which are reduced. */ + if (!glr_parser) + { + for (i = 0; i < ntokens; i++) + if (actrow[i] < 0 && actrow[i] != ACTION_MIN) + rules[item_number_as_rule_number (actrow[i])].useful = TRUE; + if (default_rule) + default_rule->useful = TRUE; + } + /* If have no default rule, the default is an error. So replace any action which says "error" with "use default". */ - if (default_rule == -1) + if (!default_rule) for (i = 0; i < ntokens; i++) if (actrow[i] == ACTION_MIN) actrow[i] = 0; @@ -671,6 +681,7 @@ static void token_actions (void) { state_number_t i; + rule_number_t r; int nconflict = conflicts_total_count (); rule_number_t *yydefact = XCALLOC (rule_number_t, nstates); @@ -678,6 +689,13 @@ token_actions (void) actrow = XCALLOC (action_t, ntokens); conflrow = XCALLOC (unsigned int, ntokens); + /* Now that the parser was computed, we can find which rules are + really reduced, and which are not because of SR or RR conflicts. + */ + if (!glr_parser) + for (r = 0; r < nrules; ++r) + rules[r].useful = FALSE; + if (glr_parser) { conflict_list = XCALLOC (unsigned int, 1 + 2 * nconflict); @@ -689,12 +707,24 @@ token_actions (void) for (i = 0; i < nstates; ++i) { - yydefact[i] = action_row (states[i]) + 1; + rule_t *default_rule = action_row (states[i]); + yydefact[i] = default_rule ? default_rule->number + 1 : 0; save_row (i); } muscle_insert_rule_number_table ("defact", yydefact, yydefact[0], 1, nstates); + + if (!glr_parser) + for (r = 0; r < nrules ; ++r) + if (!rules[r].useful) + { + LOCATION_PRINT (stderr, rules[r].location); + fprintf (stderr, ": %s: %s: ", + _("warning"), _("rule never reduced because of conflicts")); + rule_print (&rules[r], stderr); + } + XFREE (actrow); XFREE (conflrow); XFREE (yydefact); @@ -1351,7 +1381,7 @@ output_skeleton (void) m4_invoke (tempfile); /* If `debugging', keep this file alive. */ - if (!trace_flag) + if (!(trace_flag & trace_tools)) unlink (tempfile); free (tempfile);