From 5fbb0954b861b5bff653e1b87a1bd5e0b328445d Mon Sep 17 00:00:00 2001 From: Akim Demaille Date: Mon, 22 Apr 2002 08:21:55 +0000 Subject: [PATCH] Propagate more token_number_t. * src/gram.h (token_number_as_item_number) (item_number_as_token_number): New. * src/output.c (GENERATE_OUTPUT_TABLE): New. Use it to create output_item_number_table and output_token_number_table. * src/LR0.c, src/derives.c, src/gram.c, src/gram.h, src/lalr.c, * src/lex.c, src/nullable.c, src/output.c, src/print.c, * src/print_graph.c, src/reader.c, src/reduce.c, src/state.h, * src/symtab.c, src/symtab.h: Use token_number_t instead of shorts. --- ChangeLog | 15 ++++++ src/LR0.c | 38 +++++++------- src/derives.c | 2 +- src/gram.c | 2 +- src/gram.h | 11 +++- src/lalr.c | 13 +++-- src/lex.c | 4 +- src/nullable.c | 6 +-- src/output.c | 125 +++++++++++++++++++--------------------------- src/print.c | 21 ++++---- src/print_graph.c | 2 +- src/reader.c | 14 +++--- src/reduce.c | 6 +-- src/state.h | 2 +- src/symtab.c | 2 +- src/symtab.h | 4 ++ 16 files changed, 138 insertions(+), 129 deletions(-) diff --git a/ChangeLog b/ChangeLog index 8238e3ca..1570a9e0 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,18 @@ +2002-04-22 Akim Demaille + + Propagate more token_number_t. + + * src/gram.h (token_number_as_item_number) + (item_number_as_token_number): New. + * src/output.c (GENERATE_OUTPUT_TABLE): New. + Use it to create output_item_number_table and + output_token_number_table. + * src/LR0.c, src/derives.c, src/gram.c, src/gram.h, src/lalr.c, + * src/lex.c, src/nullable.c, src/output.c, src/print.c, + * src/print_graph.c, src/reader.c, src/reduce.c, src/state.h, + * src/symtab.c, src/symtab.h: Use token_number_t instead of shorts. + + 2002-04-22 Akim Demaille * src/output.h, src/output.c (get_lines_number): Remove. diff --git a/src/LR0.c b/src/LR0.c index 65c9ec07..a2a07881 100644 --- a/src/LR0.c +++ b/src/LR0.c @@ -26,6 +26,7 @@ #include "bitset.h" #include "quotearg.h" #include "symtab.h" +#include "gram.h" #include "getargs.h" #include "reader.h" #include "gram.h" @@ -51,7 +52,7 @@ static state_t *this_state = NULL; static state_t *last_state = NULL; static int nshifts; -static short *shift_symbol = NULL; +static token_number_t *shift_symbol = NULL; static short *redset = NULL; static short *shiftset = NULL; @@ -116,7 +117,7 @@ allocate_storage (void) shiftset = XCALLOC (short, nsyms); redset = XCALLOC (short, nrules + 1); state_hash = XCALLOC (state_t *, STATE_HASH_SIZE); - shift_symbol = XCALLOC (short, nsyms); + shift_symbol = XCALLOC (token_number_t, nsyms); } @@ -161,20 +162,19 @@ new_itemsets (void) nshifts = 0; for (i = 0; i < nritemset; ++i) - { - int symbol = ritem[itemset[i]]; - if (symbol >= 0) - { - if (!kernel_size[symbol]) - { - shift_symbol[nshifts] = symbol; - nshifts++; - } - - kernel_base[symbol][kernel_size[symbol]] = itemset[i] + 1; - kernel_size[symbol]++; - } - } + if (ritem[itemset[i]] >= 0) + { + token_number_t symbol + = item_number_as_token_number (ritem[itemset[i]]); + if (!kernel_size[symbol]) + { + shift_symbol[nshifts] = symbol; + nshifts++; + } + + kernel_base[symbol][kernel_size[symbol]] = itemset[i] + 1; + kernel_size[symbol]++; + } } @@ -185,7 +185,7 @@ new_itemsets (void) `-----------------------------------------------------------------*/ static state_t * -new_state (int symbol) +new_state (token_number_t symbol) { state_t *p; @@ -229,7 +229,7 @@ new_state (int symbol) `--------------------------------------------------------------*/ static int -get_state (int symbol) +get_state (token_number_t symbol) { int key; int i; @@ -298,7 +298,7 @@ append_states (void) { int i; int j; - int symbol; + token_number_t symbol; if (trace_flag) fprintf (stderr, "Entering append_states, state = %d\n", diff --git a/src/derives.c b/src/derives.c index 297e6384..d3443602 100644 --- a/src/derives.c +++ b/src/derives.c @@ -69,7 +69,7 @@ set_derives (void) p = delts; for (i = nrules; i > 0; i--) { - int lhs = rules[i].lhs->number; + token_number_t lhs = rules[i].lhs->number; p->next = dset[lhs]; p->value = i; dset[lhs] = p; diff --git a/src/gram.c b/src/gram.c index 9189a1cf..1120b897 100644 --- a/src/gram.c +++ b/src/gram.c @@ -40,7 +40,7 @@ int nvars = 0; token_number_t *token_translations = NULL; -int start_symbol = 0; +token_number_t start_symbol = 0; int max_user_token_number = 256; diff --git a/src/gram.h b/src/gram.h index d7b9df4e..cf21e48f 100644 --- a/src/gram.h +++ b/src/gram.h @@ -114,7 +114,16 @@ typedef int item_number_t; extern item_number_t *ritem; extern int nritems; -extern int start_symbol; +/* There is weird relationship between item_number_t and + token_number_t: we store token_number_t in item_number_t, but in + the latter we also store, as negative numbers, the rule numbers. + + Therefore, an token_number_t must be a valid item_number_t, and we + sometimes have to perform the converse transformation. */ +#define token_number_as_item_number(Tok) ((item_number_t) (Tok)) +#define item_number_as_token_number(Ite) ((token_number_t) (Ite)) + +extern token_number_t start_symbol; typedef struct rule_s diff --git a/src/lalr.c b/src/lalr.c index cf5733bb..643f8fd9 100644 --- a/src/lalr.c +++ b/src/lalr.c @@ -217,7 +217,7 @@ set_goto_map (void) `----------------------------------------------------------*/ static int -map_goto (int state, int symbol) +map_goto (int state, token_number_t symbol) { int high; int low; @@ -267,7 +267,7 @@ initialize_F (void) for (; j < sp->nshifts; j++) { - int symbol = SHIFT_SYMBOL (sp, j); + token_number_t symbol = SHIFT_SYMBOL (sp, j); if (nullable[symbol]) edge[nedges++] = map_goto (stateno, symbol); } @@ -408,7 +408,7 @@ build_relations (void) for (i = 0; i < ngotos; i++) { int nedges = 0; - int symbol1 = states[to_state[i]]->accessing_symbol; + token_number_t symbol1 = states[to_state[i]]->accessing_symbol; short *rulep; for (rulep = derives[symbol1]; *rulep > 0; rulep++) @@ -426,7 +426,8 @@ build_relations (void) for (j = 0; j < sp->nshifts; j++) { state = states[sp->shifts[j]]; - if (state->accessing_symbol == *rp) + if (state->accessing_symbol + == item_number_as_token_number (*rp)) break; } @@ -445,7 +446,9 @@ build_relations (void) /* JF added rp>=ritem && I hope to god its right! */ if (rp >= ritem && ISVAR (*rp)) { - edge[nedges++] = map_goto (states1[--length], *rp); + /* Downcasting from item_number_t to token_number_t. */ + edge[nedges++] = map_goto (states1[--length], + item_number_as_token_number (*rp)); if (nullable[*rp]) done = 0; } diff --git a/src/lex.c b/src/lex.c index 2d5caef1..516f7997 100644 --- a/src/lex.c +++ b/src/lex.c @@ -364,7 +364,7 @@ lex (void) obstack_1grow (&token_obstack, '\0'); token_buffer = obstack_finish (&token_obstack); symval = getsym (token_buffer); - if (symval->number == -1) + if (symval->number == NUMBER_UNDEFINED) { symval->number = ntokens++; symval->class = token_sym; @@ -392,7 +392,7 @@ lex (void) token_buffer = obstack_finish (&token_obstack); symval = getsym (token_buffer); - if (symval->number == -1) + if (symval->number == NUMBER_UNDEFINED) { symval->number = ntokens++; symval->class = token_sym; diff --git a/src/nullable.c b/src/nullable.c index 6708313b..ae02db75 100644 --- a/src/nullable.c +++ b/src/nullable.c @@ -47,11 +47,11 @@ void set_nullable (void) { int ruleno; - short *s1; - short *s2; + token_number_t *s1; + token_number_t *s2; shorts *p; - short *squeue = XCALLOC (short, nvars); + token_number_t *squeue = XCALLOC (token_number_t, nvars); short *rcount = XCALLOC (short, nrules + 1); /* RITEM contains all the rules, including useless productions. Hence we must allocate room for useless nonterminals too. */ diff --git a/src/output.c b/src/output.c index 28a7992e..ef7e6716 100644 --- a/src/output.c +++ b/src/output.c @@ -133,77 +133,50 @@ static struct obstack format_obstack; int error_verbose = 0; -/*----------------------------------------------------------------. -| Format the FIRST and then TABLE_DATA[BEGIN..END[ into OOUT, and | -| return the number of bits needed for its longuest value. | -`----------------------------------------------------------------*/ - -static inline long int -output_short_table (struct obstack *oout, - short *table_data, - short first, - int begin, - int end) -{ - long int max = first; - int i; - int j = 1; +/*------------------------------------------------------------------. +| Create a function NAME which Format the FIRST and then | +| TABLE_DATA[BEGIN..END[ (of TYPE) into OOUT, and return the number | +| of bits needed for its longuest value. | +`------------------------------------------------------------------*/ - obstack_fgrow1 (oout, "%6d", first); - for (i = begin; i < end; ++i) - { - obstack_1grow (oout, ','); - if (j >= 10) - { - obstack_sgrow (oout, "\n "); - j = 1; - } - else - ++j; - obstack_fgrow1 (oout, "%6d", table_data[i]); - if (table_data[i] > max) - max = table_data[i]; - } - obstack_1grow (oout, 0); - return max; +#define GENERATE_OUTPUT_TABLE(Name, Type) \ + \ +static inline long int \ +Name (struct obstack *oout, \ + Type *table_data, \ + Type first, \ + int begin, \ + int end) \ +{ \ + long int max = first; \ + int i; \ + int j = 1; \ + \ + obstack_fgrow1 (oout, "%6d", first); \ + for (i = begin; i < end; ++i) \ + { \ + obstack_1grow (oout, ','); \ + if (j >= 10) \ + { \ + obstack_sgrow (oout, "\n "); \ + j = 1; \ + } \ + else \ + ++j; \ + obstack_fgrow1 (oout, "%6d", table_data[i]); \ + if (table_data[i] > max) \ + max = table_data[i]; \ + } \ + obstack_1grow (oout, 0); \ + \ + return max; \ } - -/*--------------------. -| Similar, for ints. | -`--------------------*/ - -static inline long int -output_int_table (struct obstack *oout, - int *table_data, - int first, - int begin, - int end) -{ - long int max = first; - int i; - int j = 1; - - obstack_fgrow1 (oout, "%6d", first); - for (i = begin; i < end; ++i) - { - obstack_1grow (oout, ','); - if (j >= 10) - { - obstack_sgrow (oout, "\n "); - j = 1; - } - else - ++j; - obstack_fgrow1 (oout, "%6d", table_data[i]); - if (table_data[i] > max) - max = table_data[i]; - } - obstack_1grow (oout, 0); - - return max; -} +GENERATE_OUTPUT_TABLE(output_int_table, int) +GENERATE_OUTPUT_TABLE(output_short_table, short) +GENERATE_OUTPUT_TABLE(output_token_number_table, token_number_t) +GENERATE_OUTPUT_TABLE(output_item_number_table, item_number_t) /*-----------------------------------------------------------------. @@ -214,8 +187,9 @@ output_int_table (struct obstack *oout, static void prepare_tokens (void) { - long int max = output_short_table (&format_obstack, token_translations, - 0, 1, max_user_token_number + 1); + long int max = output_token_number_table (&format_obstack, + token_translations, + 0, 1, max_user_token_number + 1); muscle_insert ("translate", obstack_finish (&format_obstack)); MUSCLE_INSERT_LONG_INT ("token_number_max", max); XFREE (token_translations); @@ -281,7 +255,7 @@ prepare_rules (void) int i = 0; item_number_t *rhs = XMALLOC (item_number_t, nritems); short *prhs = XMALLOC (short, nrules + 1); - short *r1 = XMALLOC (short, nrules + 1); + token_number_t *r1 = XMALLOC (token_number_t, nrules + 1); short *r2 = XMALLOC (short, nrules + 1); short *rline = XMALLOC (short, nrules + 1); @@ -313,7 +287,7 @@ prepare_rules (void) output_short_table (&format_obstack, rline, 0, 1, nrules + 1); muscle_insert ("rline", obstack_finish (&format_obstack)); - output_short_table (&format_obstack, r1, 0, 1, nrules + 1); + output_token_number_table (&format_obstack, r1, 0, 1, nrules + 1); muscle_insert ("r1", obstack_finish (&format_obstack)); output_short_table (&format_obstack, r2, 0, 1, nrules + 1); @@ -332,11 +306,12 @@ static void prepare_states (void) { size_t i; - short *values = (short *) alloca (sizeof (short) * nstates); + token_number_t *values = + (token_number_t *) alloca (sizeof (token_number_t) * nstates); for (i = 0; i < nstates; ++i) values[i] = states[i]->accessing_symbol; - output_short_table (&format_obstack, values, - 0, 1, nstates); + output_token_number_table (&format_obstack, values, + 0, 1, nstates); muscle_insert ("stos", obstack_finish (&format_obstack)); } @@ -389,7 +364,7 @@ action_row (state_t *state) to reduce. */ for (i = 0; i < shiftp->nshifts; i++) { - int symbol; + token_number_t symbol; int shift_state = shiftp->shifts[i]; if (!shift_state) continue; diff --git a/src/print.c b/src/print.c index e8c4bd07..ec5a12ad 100644 --- a/src/print.c +++ b/src/print.c @@ -124,7 +124,7 @@ print_shifts (FILE *out, state_t *state) if (!SHIFT_IS_DISABLED (shiftp, i)) { int state1 = shiftp->shifts[i]; - int symbol = states[state1]->accessing_symbol; + token_number_t symbol = states[state1]->accessing_symbol; fprintf (out, _(" %-4s\tshift, and go to state %d\n"), escape (symbols[symbol]->tag), state1); @@ -166,7 +166,7 @@ print_gotos (FILE *out, state_t *state) if (!SHIFT_IS_DISABLED (shiftp, i)) { int state1 = shiftp->shifts[i]; - int symbol = states[state1]->accessing_symbol; + token_number_t symbol = states[state1]->accessing_symbol; fprintf (out, _(" %-4s\tgo to state %d\n"), escape (symbols[symbol]->tag), state1); } @@ -190,7 +190,7 @@ print_reductions (FILE *out, state_t *state) if (state->consistent) { int rule = redp->rules[0]; - int symbol = rules[rule].lhs->number; + token_number_t symbol = rules[rule].lhs->number; fprintf (out, _(" $default\treduce using rule %d (%s)\n\n"), rule - 1, escape (symbols[symbol]->tag)); return; @@ -360,7 +360,8 @@ do { \ static void print_grammar (FILE *out) { - int i, j; + token_number_t i; + int j; item_number_t *rule; char buffer[90]; int column = 0; @@ -368,11 +369,11 @@ print_grammar (FILE *out) /* rule # : LHS -> RHS */ fprintf (out, "%s\n\n", _("Grammar")); fprintf (out, " %s\n", _("Number, Line, Rule")); - for (i = 1; i < nrules + 1; i++) + for (j = 1; j < nrules + 1; j++) { fprintf (out, _(" %3d %3d %s ->"), - i - 1, rules[i].line, escape (rules[i].lhs->tag)); - rule = rules[i].rhs; + j - 1, rules[j].line, escape (rules[j].lhs->tag)); + rule = rules[j].rhs; if (*rule >= 0) while (*rule >= 0) fprintf (out, " %s", escape (symbols[*rule++]->tag)); @@ -396,7 +397,7 @@ print_grammar (FILE *out) for (j = 1; j < nrules + 1; j++) for (rule = rules[j].rhs; *rule >= 0; rule++) - if (*rule == token_translations[i]) + if (item_number_as_token_number (*rule) == token_translations[i]) { END_TEST (65); sprintf (buffer + strlen (buffer), " %d", j - 1); @@ -417,7 +418,7 @@ print_grammar (FILE *out) if (rules[j].lhs->number == i) left_count++; for (rule = rules[j].rhs; *rule >= 0; rule++) - if (*rule == i) + if (item_number_as_token_number (*rule) == i) { right_count++; break; @@ -452,7 +453,7 @@ print_grammar (FILE *out) for (j = 1; j < nrules + 1; j++) { for (rule = rules[j].rhs; *rule >= 0; rule++) - if (*rule == i) + if (item_number_as_token_number (*rule) == i) { END_TEST (65); sprintf (buffer + strlen (buffer), " %d", j - 1); diff --git a/src/print_graph.c b/src/print_graph.c index 6de5b34d..15861f35 100644 --- a/src/print_graph.c +++ b/src/print_graph.c @@ -114,7 +114,7 @@ print_actions (state_t *state, const char *node_name) if (!SHIFT_IS_DISABLED (shiftp, i)) { int state1 = shiftp->shifts[i]; - int symbol = states[state1]->accessing_symbol; + token_number_t symbol = states[state1]->accessing_symbol; new_edge (&edge); diff --git a/src/reader.c b/src/reader.c index f76f5c84..da0744e5 100644 --- a/src/reader.c +++ b/src/reader.c @@ -199,13 +199,13 @@ symbol_pack (symbol_t *this) /* This symbol and its alias are a single token defn. Allocate a tokno, and assign to both check agreement of prec and assoc fields and make both the same */ - if (this->number == -1) + if (this->number == NUMBER_UNDEFINED) { if (this == eoftoken || this->alias == eoftoken) this->number = this->alias->number = 0; else { - assert (this->alias->number != -1); + assert (this->alias->number != NUMBER_UNDEFINED); this->number = this->alias->number; } } @@ -215,7 +215,7 @@ symbol_pack (symbol_t *this) } else /* this->class == token_sym */ { - assert (this->number != -1); + assert (this->number != NUMBER_UNDEFINED); } symbols[this->number] = this; @@ -705,7 +705,7 @@ parse_token_decl (symbol_class what_is, symbol_class what_is_not) symbol->class = what_is; if (what_is == nterm_sym && oldclass != nterm_sym) symbol->number = nvars++; - if (what_is == token_sym && symbol->number == -1) + if (what_is == token_sym && symbol->number == NUMBER_UNDEFINED) symbol->number = ntokens++; if (typename) @@ -857,7 +857,7 @@ parse_assoc_decl (associativity assoc) symval->assoc = assoc; if (symval->class == nterm_sym) complain (_("symbol %s redefined"), symval->tag); - if (symval->number == -1) + if (symval->number == NUMBER_UNDEFINED) { symval->number = ntokens++; symval->class = token_sym; @@ -1763,7 +1763,9 @@ packgram (void) p = p->next; while (p && p->sym) { - ritem[itemno++] = p->sym->number; + /* item_number_t = token_number_t. + But the former needs to contain more: negative rule numbers. */ + ritem[itemno++] = token_number_as_item_number (p->sym->number); /* A rule gets by default the precedence and associativity of the last token in it. */ if (p->sym->class == token_sym) diff --git a/src/reduce.c b/src/reduce.c index e1c42a06..c8a8fe71 100644 --- a/src/reduce.c +++ b/src/reduce.c @@ -278,12 +278,12 @@ reduce_grammar_tables (void) static void nonterminals_reduce (void) { - int i, n; + token_number_t i, n; /* Map the nonterminals to their new index: useful first, useless afterwards. Kept for later report. */ - short *nontermmap = XCALLOC (short, nvars) - ntokens; + token_number_t *nontermmap = XCALLOC (token_number_t, nvars) - ntokens; n = ntokens; for (i = ntokens; i < nsyms; i++) if (bitset_test (V, i)) @@ -308,7 +308,7 @@ nonterminals_reduce (void) for (i = 0; i < nritems; ++i) if (ISVAR (ritem[i])) - ritem[i] = nontermmap[ritem[i]]; + ritem[i] = token_number_as_item_number (nontermmap[ritem[i]]); start_symbol = nontermmap[start_symbol]; diff --git a/src/state.h b/src/state.h index 3dc74acf..dc093fea 100644 --- a/src/state.h +++ b/src/state.h @@ -171,7 +171,7 @@ typedef struct state_s struct state_s *link; short number; - short accessing_symbol; + token_number_t accessing_symbol; shifts *shifts; reductions *reductions; errs *errs; diff --git a/src/symtab.c b/src/symtab.c index 01cf9202..598f8ee2 100644 --- a/src/symtab.c +++ b/src/symtab.c @@ -35,7 +35,7 @@ symbol_new (const char *tag) res->tag = xstrdup (tag); res->type_name = NULL; - res->number = -1; + res->number = NUMBER_UNDEFINED; res->prec = 0; res->assoc = right_assoc; res->user_token_number = SUNDEF; diff --git a/src/symtab.h b/src/symtab.h index df566eb5..591c6b8e 100644 --- a/src/symtab.h +++ b/src/symtab.h @@ -43,15 +43,19 @@ typedef enum /* Internal token numbers. */ typedef short token_number_t; + #define SUNDEF -1 /* For undefined user number. */ #define SALIAS -9991 /* for symbol generated with an alias */ +#define NUMBER_UNDEFINED ((token_number_t) -1) + struct symbol_s { /* The key, name of the symbol. */ char *tag; /* Its type. */ char *type_name; + token_number_t number; short prec; associativity assoc; -- 2.45.2