X-Git-Url: https://git.saurik.com/bison.git/blobdiff_plain/914feea9d07b0a53ee958e8df755addb97196913..b0299a2ebb1a000b67d23d3cfecb52f699dd734a:/src/output.c diff --git a/src/output.c b/src/output.c index c2720d96..b813b537 100644 --- a/src/output.c +++ b/src/output.c @@ -176,56 +176,127 @@ output_table_data (struct obstack *oout, } +/*-----------------------------------------------------------------. +| Prepare the muscles related to the tokens: translate, tname, and | +| toknum. | +`-----------------------------------------------------------------*/ + static void -output_token_translations (void) +prepare_tokens (void) { output_table_data (&format_obstack, token_translations, 0, 1, max_user_token_number + 1); muscle_insert ("translate", obstack_finish (&format_obstack)); XFREE (token_translations); -} + { + int i; + int j = 0; + for (i = 0; i < nsyms; i++) + { + /* Be sure not to use twice the same quotearg slot. */ + const char *cp = + quotearg_n_style (1, c_quoting_style, + quotearg_style (escape_quoting_style, + symbols[i]->tag)); + /* Width of the next token, including the two quotes, the coma + and the space. */ + int strsize = strlen (cp) + 2; + + if (j + strsize > 75) + { + obstack_sgrow (&format_obstack, "\n "); + j = 2; + } + + obstack_sgrow (&format_obstack, cp); + obstack_sgrow (&format_obstack, ", "); + j += strsize; + } + /* Add a NULL entry to list of tokens (well, 0, as NULL might not be + defined). */ + obstack_sgrow (&format_obstack, "0"); -static void -output_gram (void) -{ + /* Finish table and store. */ + obstack_1grow (&format_obstack, 0); + muscle_insert ("tname", obstack_finish (&format_obstack)); + } + + /* Output YYTOKNUM. */ { int i; - short *values = XCALLOC (short, nrules + 1); - for (i = 0; i < nrules + 1; ++i) - values[i] = rules[i].rhs; + short *values = XCALLOC (short, ntokens + 1); + for (i = 0; i < ntokens + 1; ++i) + values[i] = symbols[i]->user_token_number; output_table_data (&format_obstack, values, - 0, 1, nrules + 1); - XFREE (values); + 0, 1, ntokens + 1); + muscle_insert ("toknum", obstack_finish (&format_obstack)); + free (values); } +} - muscle_insert ("prhs", obstack_finish (&format_obstack)); - { - short *yyrhs; - int i; +/*-------------------------------------------------------------. +| Prepare the muscles related to the rules: rhs, prhs, r1, r2, | +| rline. | +`-------------------------------------------------------------*/ + +static void +prepare_rules (void) +{ + short *rhsp; + int r; + int i = 0; + short *rhs = XMALLOC (short, nritems); + short *prhs = XMALLOC (short, nrules + 1); + short *r1 = XMALLOC (short, nrules + 1); + short *r2 = XMALLOC (short, nrules + 1); + short *rline = XMALLOC (short, nrules + 1); + + for (r = 1; r < nrules + 1; ++r) + { + /* Index of rule R in RHS. */ + prhs[r] = i; + /* RHS of the rule R. */ + for (rhsp = rules[r].rhs; *rhsp >= 0; ++rhsp) + rhs[i++] = *rhsp; + /* LHS of the rule R. */ + r1[r] = rules[r].lhs->number; + /* Length of rule R's RHS. */ + r2[r] = i - prhs[r]; + /* Separator in RHS. */ + rhs[i++] = -1; + /* Line where rule was defined. */ + rline[r] = rules[r].line; + } + assert (i == nritems); - yyrhs = XMALLOC (short, nritems); + output_table_data (&format_obstack, rhs, ritem[0], 1, nritems); + muscle_insert ("rhs", obstack_finish (&format_obstack)); - for (i = 1; i < nritems; ++i) - yyrhs[i] = ritem[i] >= 0 ? ritem[i] : -1; + output_table_data (&format_obstack, prhs, 0, 1, nrules + 1); + muscle_insert ("prhs", obstack_finish (&format_obstack)); - output_table_data (&format_obstack, yyrhs, - ritem[0], 1, nritems); - muscle_insert ("rhs", obstack_finish (&format_obstack)); + output_table_data (&format_obstack, rline, 0, 1, nrules + 1); + muscle_insert ("rline", obstack_finish (&format_obstack)); - XFREE (yyrhs); - } + output_table_data (&format_obstack, r1, 0, 1, nrules + 1); + muscle_insert ("r1", obstack_finish (&format_obstack)); + + output_table_data (&format_obstack, r2, 0, 1, nrules + 1); + muscle_insert ("r2", obstack_finish (&format_obstack)); -#if 0 - if (!semantic_parser) - obstack_sgrow (&table_obstack, "\n#endif\n"); -#endif + free (rhs); + free (prhs); + free (r2); } +/*--------------------------------------------. +| Prepare the muscles related to the states. | +`--------------------------------------------*/ static void -output_stos (void) +prepare_states (void) { size_t i; short *values = (short *) alloca (sizeof (short) * nstates); @@ -237,87 +308,6 @@ output_stos (void) } -static void -output_rule_data (void) -{ - int i; - int j; - short *short_tab = NULL; - - { - short *values = XCALLOC (short, nrules + 1); - for (i = 0; i < nrules + 1; ++i) - values[i] = rules[i].line; - output_table_data (&format_obstack, values, - 0, 1, nrules + 1); - muscle_insert ("rline", obstack_finish (&format_obstack)); - XFREE (values); - } - - - j = 0; - for (i = 0; i < nsyms; i++) - { - /* Be sure not to use twice the same quotearg slot. */ - const char *cp = - quotearg_n_style (1, c_quoting_style, - quotearg_style (escape_quoting_style, symbols[i]->tag)); - /* Width of the next token, including the two quotes, the coma - and the space. */ - int strsize = strlen (cp) + 2; - - if (j + strsize > 75) - { - obstack_sgrow (&format_obstack, "\n "); - j = 2; - } - - obstack_sgrow (&format_obstack, cp); - obstack_sgrow (&format_obstack, ", "); - j += strsize; - } - /* Add a NULL entry to list of tokens (well, 0, as NULL might not be - defined). */ - obstack_sgrow (&format_obstack, "0"); - - /* Finish table and store. */ - obstack_1grow (&format_obstack, 0); - muscle_insert ("tname", obstack_finish (&format_obstack)); - - /* Output YYTOKNUM. */ - { - short *values = XCALLOC (short, ntokens + 1); - for (i = 0; i < ntokens + 1; ++i) - values[i] = symbols[i]->user_token_number; - output_table_data (&format_obstack, values, - 0, 1, ntokens + 1); - muscle_insert ("toknum", obstack_finish (&format_obstack)); - XFREE (values); - } - - - /* Output YYR1. */ - { - short *values = XCALLOC (short, nrules + 1); - for (i = 0; i < nrules + 1; ++i) - values[i] = rules[i].lhs; - output_table_data (&format_obstack, values, - 0, 1, nrules + 1); - muscle_insert ("r1", obstack_finish (&format_obstack)); - XFREE (values); - } - - /* Output YYR2. */ - short_tab = XMALLOC (short, nrules + 1); - for (i = 1; i < nrules; i++) - short_tab[i] = rules[i + 1].rhs - rules[i].rhs - 1; - short_tab[nrules] = nritems - rules[nrules].rhs - 1; - output_table_data (&format_obstack, short_tab, - 0, 1, nrules + 1); - muscle_insert ("r2", obstack_finish (&format_obstack)); - XFREE (short_tab); -} - /*------------------------------------------------------------------. | Decide what to do for each type of token if seen as the lookahead | | token in specified state. The value returned is used as the | @@ -358,7 +348,7 @@ action_row (state_t *state) /* and record this rule as the rule to use if that token follows. */ if (bitset_test (LA[state->lookaheadsp + i], j)) - actrow[j] = -LAruleno[state->lookaheadsp + i]; + actrow[j] = -LArule[state->lookaheadsp + i]->number; } /* Now see which tokens are allowed for shifts in this state. For @@ -405,7 +395,7 @@ action_row (state_t *state) for (i = 0; i < state->nlookaheads; i++) { int count = 0; - int rule = -LAruleno[state->lookaheadsp + i]; + int rule = -LArule[state->lookaheadsp + i]->number; int j; for (j = 0; j < ntokens; j++) @@ -568,6 +558,7 @@ void token_definitions_output (FILE *out) { int i; + int first = 1; for (i = 0; i < ntokens; ++i) { bucket *symbol = symbols[i]; @@ -576,7 +567,7 @@ token_definitions_output (FILE *out) if (number == SALIAS) continue; /* Skip error token. */ - if (symbol->value == error_token_number) + if (symbol->number == error_token_number) continue; if (symbol->tag[0] == '\'') continue; /* skip literal character */ @@ -594,12 +585,13 @@ token_definitions_output (FILE *out) if (strchr (symbol->tag, '.') || strchr (symbol->tag, '$')) continue; - fprintf (out, "# define %s\t%d\n", - symbol->tag, number); + fprintf (out, "%s [[[%s]], [%d]]", + first ? "" : ",\n", symbol->tag, number); if (semantic_parser) /* FIXME: This is probably wrong, and should be just as above. --akim. */ - fprintf (out, "# define T%s\t%d\n", symbol->tag, symbol->value); + fprintf (out, "# define T%s\t%d\n", symbol->tag, symbol->number); + first = 0; } } @@ -908,8 +900,10 @@ output_check (void) XFREE (check); } -/* compute and output yydefact, yydefgoto, yypact, yypgoto, yytable - and yycheck. */ +/*-----------------------------------------------------------------. +| Compute and output yydefact, yydefgoto, yypact, yypgoto, yytable | +| and yycheck. | +`-----------------------------------------------------------------*/ static void output_actions (void) @@ -924,7 +918,7 @@ output_actions (void) token_actions (); bitsetv_free (LA); - XFREE (LAruleno); + free (LArule); goto_actions (); XFREE (goto_map + ntokens); @@ -958,10 +952,9 @@ static void output_skeleton (void) { /* Store the definition of all the muscles. */ - char *tempdir = getenv ("TMPDIR"); + const char *tempdir = getenv ("TMPDIR"); char *tempfile = NULL; FILE *out = NULL; - ssize_t bytes_read; int fd; if (tempdir == NULL) @@ -989,9 +982,9 @@ output_skeleton (void) guards_output (out); fputs ("]])\n\n", out); - fputs ("m4_define([b4_tokendef], \n[[", out); + fputs ("m4_define([b4_tokens], \n[", out); token_definitions_output (out); - fputs ("]])\n\n", out); + fputs ("])\n\n", out); muscles_m4_output (out); @@ -1082,12 +1075,9 @@ output (void) { obstack_init (&format_obstack); - output_token_translations (); - output_gram (); - - if (semantic_parser) - output_stos (); - output_rule_data (); + prepare_tokens (); + prepare_rules (); + prepare_states (); output_actions (); prepare ();