X-Git-Url: https://git.saurik.com/bison.git/blobdiff_plain/e0e5bf84b0e5494553cc1c1c6db599c4033df4df..8b752b00fdaaa3802e4ab05a468f6cda4a9fa500:/src/output.c diff --git a/src/output.c b/src/output.c index a15570a7..4a7a7265 100644 --- a/src/output.c +++ b/src/output.c @@ -225,6 +225,7 @@ GENERATE_MUSCLE_INSERT_TABLE(muscle_insert_unsigned_int_table, unsigned int) GENERATE_MUSCLE_INSERT_TABLE(muscle_insert_short_table, short) GENERATE_MUSCLE_INSERT_TABLE(muscle_insert_symbol_number_table, symbol_number_t) GENERATE_MUSCLE_INSERT_TABLE(muscle_insert_item_number_table, item_number_t) +GENERATE_MUSCLE_INSERT_TABLE(muscle_insert_state_number_table, state_number_t) /*-----------------------------------------------------------------. @@ -248,7 +249,7 @@ prepare_tokens (void) SYMBOL_TAG_GET uses slot 0. */ const char *cp = quotearg_n_style (1, c_quoting_style, - symbol_tag_get (symbols[i])); + symbols[i]->tag); /* Width of the next token, including the two quotes, the coma and the space. */ int strsize = strlen (cp) + 2; @@ -293,7 +294,7 @@ prepare_tokens (void) static void prepare_rules (void) { - int r; + rule_number_t r; unsigned int i = 0; item_number_t *rhs = XMALLOC (item_number_t, nritems); unsigned int *prhs = XMALLOC (unsigned int, nrules + 1); @@ -305,7 +306,7 @@ prepare_rules (void) for (r = 1; r < nrules + 1; ++r) { - item_number_t *rhsp; + item_number_t *rhsp = NULL; /* Index of rule R in RHS. */ prhs[r] = i; /* RHS of the rule R. */ @@ -350,13 +351,13 @@ prepare_rules (void) static void prepare_states (void) { - size_t i; + state_number_t i; symbol_number_t *values = (symbol_number_t *) alloca (sizeof (symbol_number_t) * nstates); for (i = 0; i < nstates; ++i) values[i] = states[i]->accessing_symbol; muscle_insert_symbol_number_table ("stos", values, - 0, 1, nstates); + 0, 1, nstates); } @@ -427,10 +428,10 @@ static int action_row (state_t *state) { int i; - int default_rule = 0; - reductions *redp = state->reductions; - shifts *shiftp = state->shifts; - errs *errp = state->errs; + rule_number_t default_rule = 0; + reductions_t *redp = state->reductions; + transitions_t *transitions = state->transitions; + errs_t *errp = state->errs; /* set nonzero to inhibit having any default reduction */ int nodefault = 0; int conflicted = 0; @@ -438,62 +439,56 @@ action_row (state_t *state) for (i = 0; i < ntokens; i++) actrow[i] = conflrow[i] = 0; - if (redp->nreds >= 1) + if (redp->num >= 1) { int j; + bitset_iterator biter; /* 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 */ - for (j = 0; j < ntokens; j++) + BITSET_FOR_EACH (biter, state->lookaheads[i], j, 0) + { /* and record this rule as the rule to use if that token follows. */ - if (bitset_test (state->lookaheads[i], j)) - { - if (actrow[j] != 0) - conflicted = conflrow[j] = 1; - actrow[j] = -state->lookaheads_rule[i]->number; - } + 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 < shiftp->nshifts; i++) - { - symbol_number_t symbol; - int shift_state = shiftp->shifts[i]; - if (!shift_state) - continue; - - symbol = states[shift_state]->accessing_symbol; - - if (ISVAR (symbol)) - break; + 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] = shift_state; + 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; - } + /* 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->nerrs; i++) + for (i = 0; i < errp->num; i++) { - int symbol = errp->errs[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->nreds >= 1 && !nodefault) + if (redp->num >= 1 && !nodefault) { if (state->consistent) default_rule = redp->rules[0]; @@ -503,11 +498,11 @@ action_row (state_t *state) for (i = 0; i < state->nlookaheads; i++) { int count = 0; - int rule = -state->lookaheads_rule[i]->number; - int j; + rule_number_t rule = state->lookaheads_rule[i]->number; + symbol_number_t j; for (j = 0; j < ntokens; j++) - if (actrow[j] == rule) + if (actrow[j] == -rule) count++; if (count > max) @@ -527,10 +522,10 @@ action_row (state_t *state) { int j; for (j = 0; j < ntokens; j++) - if (actrow[j] == default_rule && ! (glr_parser && conflrow[j])) + if (actrow[j] == -default_rule + && ! (glr_parser && conflrow[j])) actrow[j] = 0; } - default_rule = -default_rule; } } @@ -550,9 +545,9 @@ action_row (state_t *state) static void -save_row (int state) +save_row (state_number_t state) { - int i; + symbol_number_t i; int count; short *sp = NULL; short *sp1 = NULL; @@ -599,7 +594,7 @@ save_row (int state) static void token_actions (void) { - size_t i; + state_number_t i; int nconflict = conflicts_total_count (); short *yydefact = XCALLOC (short, nstates); @@ -637,21 +632,21 @@ token_actions (void) void actions_output (FILE *out) { - int rule; + rule_number_t r; fputs ("m4_define([b4_actions], \n[[", out); - for (rule = 1; rule < nrules + 1; ++rule) - if (rules[rule].action) + for (r = 1; r < nrules + 1; ++r) + if (rules[r].action) { - fprintf (out, " case %d:\n", rule); + fprintf (out, " case %d:\n", r); if (!no_lines_flag) fprintf (out, muscle_find ("linef"), - rules[rule].action_location.first_line, + rules[r].action_location.first_line, quotearg_style (c_quoting_style, muscle_find ("filename"))); fprintf (out, " %s\n break;\n\n", - rules[rule].action); + rules[r].action); } fputs ("]])\n\n", out); } @@ -660,7 +655,7 @@ actions_output (FILE *out) | Output the merge functions to OUT. | `--------------------------------------*/ -void +static void merger_output (FILE *out) { int n; @@ -751,7 +746,7 @@ symbol_destructors_output (FILE *out) fprintf (out, "%s[[[%s]], [[%d]], [[%s]], [[%d]], [[%s]], [[%s]]]", first ? "" : ",\n", infile, symbol->destructor_location.first_line, - symbol_tag_get (symbol), + symbol->tag, symbol->number, symbol->destructor, symbol->type_name); @@ -784,7 +779,7 @@ symbol_printers_output (FILE *out) fprintf (out, "%s[[[%s]], [[%d]], [[%s]], [[%d]], [[%s]], [[%s]]]", first ? "" : ",\n", infile, symbol->printer_location.first_line, - symbol_tag_get (symbol), + symbol->tag, symbol->number, symbol->printer, symbol->type_name); @@ -796,17 +791,17 @@ symbol_printers_output (FILE *out) static void -save_column (int symbol, int default_state) +save_column (symbol_number_t symbol, state_number_t default_state) { int i; short *sp; short *sp1; short *sp2; int count; - int symno = symbol - ntokens + nstates; + int symno = symbol - ntokens + state_number_as_int (nstates); - short begin = goto_map[symbol]; - short end = goto_map[symbol + 1]; + int begin = goto_map[symbol]; + int end = goto_map[symbol + 1]; count = 0; for (i = begin; i < end; i++) @@ -830,29 +825,31 @@ save_column (int symbol, int default_state) width[symno] = sp1[-1] - sp[0] + 1; } -static int -default_goto (int symbol) + +static state_number_t +default_goto (symbol_number_t symbol) { - size_t i; - size_t m = goto_map[symbol]; - size_t n = goto_map[symbol + 1]; - int default_state = -1; + state_number_t s; + int i; + int m = goto_map[symbol]; + int n = goto_map[symbol + 1]; + state_number_t default_state = (state_number_t) -1; int max = 0; if (m == n) - return -1; + return (state_number_t) -1; - for (i = 0; i < nstates; i++) - state_count[i] = 0; + for (s = 0; s < nstates; s++) + state_count[s] = 0; for (i = m; i < n; i++) state_count[to_state[i]]++; - for (i = 0; i < nstates; i++) - if (state_count[i] > max) + for (s = 0; s < nstates; s++) + if (state_count[s] > max) { - max = state_count[i]; - default_state = i; + max = state_count[s]; + default_state = s; } return default_state; @@ -871,19 +868,19 @@ default_goto (int symbol) static void goto_actions (void) { - int i; - short *yydefgoto = XMALLOC (short, nsyms - ntokens); + symbol_number_t i; + state_number_t *yydefgoto = XMALLOC (state_number_t, nsyms - ntokens); state_count = XCALLOC (short, nstates); for (i = ntokens; i < nsyms; ++i) { - int default_state = default_goto (i); + state_number_t default_state = default_goto (i); save_column (i, default_state); yydefgoto[i - ntokens] = default_state; } - muscle_insert_short_table ("defgoto", yydefgoto, - yydefgoto[0], 1, nsyms - ntokens); + muscle_insert_state_number_table ("defgoto", yydefgoto, + yydefgoto[0], 1, nsyms - ntokens); XFREE (state_count); XFREE (yydefgoto); } @@ -978,7 +975,7 @@ pack_vector (int vector) for (k = 0; ok && k < t; k++) { - loc = j + from[k]; + loc = j + state_number_as_int (from[k]); if (loc > (int) table_size) table_grow (loc); @@ -994,11 +991,11 @@ pack_vector (int vector) { for (k = 0; k < t; k++) { - loc = j + from[k]; - table[loc] = to[k]; + loc = j + state_number_as_int (from[k]); + table[loc] = state_number_as_int (to[k]); if (glr_parser && conflict_to != NULL) conflict_table[loc] = conflict_to[k]; - check[loc] = from[k]; + check[loc] = state_number_as_int (from[k]); } while (table[lowzero] != 0) @@ -1129,8 +1126,13 @@ output_check (void) static void output_actions (void) { - size_t i; - nvectors = nstates + nvars; + /* That's a poor way to make sure the sizes are properly corelated, + in particular the signedness is not taking into account, but it's + not useless. */ + assert (sizeof (nvectors) >= sizeof (nstates)); + assert (sizeof (nvectors) >= sizeof (nvars)); + + nvectors = state_number_as_int (nstates) + nvars; froms = XCALLOC (short *, nvectors); tos = XCALLOC (short *, nvectors); @@ -1155,15 +1157,6 @@ output_actions (void) output_conflicts (); output_check (); - - for (i = 0; i < nstates; ++i) - { - free (states[i]->shifts); - XFREE (states[i]->reductions); - free (states[i]->errs); - free (states[i]); - } - XFREE (states); } @@ -1204,9 +1197,9 @@ m4_invoke (const char *definitions) if (!skel_in) error (EXIT_FAILURE, errno, "cannot run m4"); skel_lex (); - } + /*---------------------------. | Call the skeleton parser. | `---------------------------*/ @@ -1266,7 +1259,7 @@ prepare (void) MUSCLE_INSERT_INT ("pure", pure_parser); MUSCLE_INSERT_INT ("nsym", nsyms); MUSCLE_INSERT_INT ("debug", debug_flag); - MUSCLE_INSERT_INT ("final", final_state); + MUSCLE_INSERT_INT ("final", final_state->number); MUSCLE_INSERT_INT ("undef_token_number", undeftoken->number); MUSCLE_INSERT_INT ("user_token_number_max", max_user_token_number); MUSCLE_INSERT_INT ("error_verbose", error_verbose);