From b2ed6e5826e772162719db595446b2c58e4ac5d6 Mon Sep 17 00:00:00 2001 From: Akim Demaille Date: Mon, 19 Nov 2001 10:19:20 +0000 Subject: [PATCH] * src/gram.h (rule_t): New. (rule_table): New. (rrhs, rlhs): Remove, part of state_t. * src/print_graph.c, src/closure.c, src/conflicts.c, src/derives.c, * src/lalr.c, src/nullable.c, src/output.c, src/print.c, * src/reader.c, src/reduce.c: Adjust. --- ChangeLog | 10 ++++++++++ src/closure.c | 4 ++-- src/conflicts.c | 13 +++++++------ src/derives.c | 2 +- src/gram.c | 6 +++--- src/gram.h | 32 ++++++++++++++++++++++---------- src/lalr.c | 2 +- src/nullable.c | 4 ++-- src/output.c | 31 ++++++++++++++++++++++--------- src/print.c | 22 +++++++++++----------- src/print_graph.c | 7 ++++--- src/reader.c | 7 +++---- src/reduce.c | 36 ++++++++++++++++++------------------ 13 files changed, 106 insertions(+), 70 deletions(-) diff --git a/ChangeLog b/ChangeLog index 0a9fbd54..32cbf36d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,13 @@ +2001-11-19 Akim Demaille + + * src/gram.h (rule_t): New. + (rule_table): New. + (rrhs, rlhs): Remove, part of state_t. + * src/print_graph.c, src/closure.c, src/conflicts.c, src/derives.c, + * src/lalr.c, src/nullable.c, src/output.c, src/print.c, + * src/reader.c, src/reduce.c: Adjust. + + 2001-11-19 Akim Demaille * src/reader.c (symbols_output): New, extracted from... diff --git a/src/closure.c b/src/closure.c index 03a59f0b..649d4252 100644 --- a/src/closure.c +++ b/src/closure.c @@ -130,7 +130,7 @@ set_firsts (void) sp = derives[i]; while (*sp >= 0) { - symbol = ritem[rrhs[*sp++]]; + symbol = ritem[rule_table[*sp++].rhs]; if (ISVAR (symbol)) { symbol -= ntokens; @@ -287,7 +287,7 @@ closure (short *core, int n) { if (word & (1 << b)) { - itemno = rrhs[ruleno]; + itemno = rule_table[ruleno].rhs; while (csp < csend && *csp < itemno) *itemsetend++ = *csp++; *itemsetend++ = itemno; diff --git a/src/conflicts.c b/src/conflicts.c index a0c0986d..73df3b64 100644 --- a/src/conflicts.c +++ b/src/conflicts.c @@ -587,7 +587,8 @@ print_reductions (FILE *out, int state) { if (mask & *fp3) fprintf (out, _(" %-4s\t[reduce using rule %d (%s)]\n"), - tags[i], default_rule, tags[rlhs[default_rule]]); + tags[i], default_rule, + tags[rule_table[default_rule].lhs]); mask <<= 1; if (mask == 0) @@ -598,7 +599,7 @@ print_reductions (FILE *out, int state) } fprintf (out, _(" $default\treduce using rule %d (%s)\n\n"), - default_rule, tags[rlhs[default_rule]]); + default_rule, tags[rule_table[default_rule].lhs]); } else if (n - m >= 1) { @@ -687,7 +688,7 @@ print_reductions (FILE *out, int state) rule = LAruleno[j]; fprintf (out, _(" %-4s\treduce using rule %d (%s)\n"), - tags[i], rule, tags[rlhs[rule]]); + tags[i], rule, tags[rule_table[rule].lhs]); } else defaulted = 1; @@ -701,13 +702,13 @@ print_reductions (FILE *out, int state) rule = LAruleno[default_LA]; fprintf (out, _(" %-4s\treduce using rule %d (%s)\n"), - tags[i], rule, tags[rlhs[rule]]); + tags[i], rule, tags[rule_table[rule].lhs]); defaulted = 0; } rule = LAruleno[j]; fprintf (out, _(" %-4s\t[reduce using rule %d (%s)]\n"), - tags[i], rule, tags[rlhs[rule]]); + tags[i], rule, tags[rule_table[rule].lhs]); } } @@ -727,7 +728,7 @@ print_reductions (FILE *out, int state) if (default_LA >= 0) fprintf (out, _(" $default\treduce using rule %d (%s)\n"), - default_rule, tags[rlhs[default_rule]]); + default_rule, tags[rule_table[default_rule].lhs]); } } diff --git a/src/derives.c b/src/derives.c index e9c75649..a7b86b5b 100644 --- a/src/derives.c +++ b/src/derives.c @@ -74,7 +74,7 @@ set_derives (void) p = delts; for (i = nrules; i > 0; i--) { - lhs = rlhs[i]; + lhs = rule_table[i].lhs; if (lhs >= 0) { p->next = dset[lhs]; diff --git a/src/gram.c b/src/gram.c index 7016ab91..1df89a02 100644 --- a/src/gram.c +++ b/src/gram.c @@ -1,5 +1,5 @@ /* Allocate input grammar variables for bison, - Copyright 1984, 1986, 1989 Free Software Foundation, Inc. + Copyright 1984, 1986, 1989, 2001 Free Software Foundation, Inc. This file is part of Bison, the GNU Compiler Compiler. @@ -20,6 +20,7 @@ Boston, MA 02111-1307, USA. */ #include "system.h" +#include "gram.h" void dummy PARAMS((void)); @@ -32,8 +33,7 @@ int ntokens; int nvars; short *ritem = NULL; -short *rlhs = NULL; -short *rrhs = NULL; +rule_t *rule_table = NULL; short *rprec = NULL; short *rprecsym = NULL; short *sprec = NULL; diff --git a/src/gram.h b/src/gram.h index 1691c858..66031cc3 100644 --- a/src/gram.h +++ b/src/gram.h @@ -1,5 +1,5 @@ /* Data definitions for internal representation of bison's input, - Copyright 1984, 1986, 1989, 1992 Free Software Foundation, Inc. + Copyright 1984, 1986, 1989, 1992, 2001 Free Software Foundation, Inc. This file is part of Bison, the GNU Compiler Compiler. @@ -38,21 +38,27 @@ The rules receive rule numbers 1 to nrules in the order they are written. Actions and guards are accessed via the rule number. - The rules themselves are described by three arrays: rrhs, rlhs and - ritem. rlhs[R] is the symbol number of the left hand side of rule - R. The right hand side is stored as symbol numbers in a portion of - ritem. rrhs[R] contains the index in ritem of the beginning of the + The rules themselves are described by several arrays: amongst which + RITEM, and RULE_TABLE. + + RULE_TABLE is an array of struct rule_s, which members are: + + RULE_TABLE[R].lhs -- the symbol number of the left hand side of + rule R. If -1, the rule has been thrown out by reduce.c and should + be ignored. + + RULE_TABLE[R].rhs -- the index in RITEM of the beginning of the portion for rule R. - If rlhs[R] is -1, the rule has been thrown out by reduce.c and - should be ignored. + The right hand side is stored as symbol numbers in a portion of + RITEM. The length of the portion is one greater than the number of symbols in the rule's right hand side. The last element in the portion contains minus R, which identifies it as the end of a portion and says which rule it is for. - The portions of ritem come in order of increasing rule number and + The portions of RITEM come in order of increasing rule number and are followed by an element which is zero to mark the end. nitems is the total length of ritem, not counting the final zero. Each element of ritem is called an "item" and its index in ritem is an @@ -84,8 +90,7 @@ extern int ntokens; extern int nvars; extern short *ritem; -extern short *rlhs; -extern short *rrhs; + extern short *rprec; extern short *rprecsym; extern short *sprec; @@ -95,6 +100,13 @@ extern short *rline; /* Source line number of each rule */ extern int start_symbol; +typedef struct rule_s +{ + short lhs; + short rhs; +} rule_t; + +extern struct rule_s *rule_table; /* associativity values in elements of rassoc, sassoc. */ typedef enum diff --git a/src/lalr.c b/src/lalr.c index c6d3f5c3..46e1a6d5 100644 --- a/src/lalr.c +++ b/src/lalr.c @@ -561,7 +561,7 @@ build_relations (void) states[0] = state1; stateno = state1; - for (rp = ritem + rrhs[*rulep]; *rp > 0; rp++) + for (rp = ritem + rule_table[*rulep].rhs; *rp > 0; rp++) { symbol2 = *rp; sp = state_table[stateno].shift_table; diff --git a/src/nullable.c b/src/nullable.c index 33654918..6c60f0fd 100644 --- a/src/nullable.c +++ b/src/nullable.c @@ -69,7 +69,7 @@ set_nullable (void) { if (*r < 0) { - symbol = rlhs[-(*r++)]; + symbol = rule_table[-(*r++)].lhs; if (symbol >= 0 && !nullable[symbol]) { nullable[symbol] = 1; @@ -111,7 +111,7 @@ set_nullable (void) p = p->next; if (--rcount[ruleno] == 0) { - symbol = rlhs[ruleno]; + symbol = rule_table[ruleno].lhs; if (symbol >= 0 && !nullable[symbol]) { nullable[symbol] = 1; diff --git a/src/output.c b/src/output.c index 8ae1cff9..dabcba15 100644 --- a/src/output.c +++ b/src/output.c @@ -167,8 +167,16 @@ output_token_translations (void) static void output_gram (void) { - output_table_data (&output_obstack, rrhs, - 0, 1, nrules + 1); + { + int i; + short *values = XCALLOC (short, nrules + 1); + for (i = 0; i < nrules + 1; ++i) + values[i] = rule_table[i].rhs; + output_table_data (&output_obstack, values, + 0, 1, nrules + 1); + XFREE (values); + } + muscle_insert ("prhs", obstack_finish (&output_obstack)); { @@ -279,22 +287,27 @@ output_rule_data (void) muscle_insert ("toknum", obstack_finish (&output_obstack)); /* Output YYR1. */ - output_table_data (&output_obstack, rlhs, - 0, 1, nrules + 1); - muscle_insert ("r1", obstack_finish (&output_obstack)); - XFREE (rlhs + 1); + { + short *values = XCALLOC (short, nrules + 1); + for (i = 0; i < nrules + 1; ++i) + values[i] = rule_table[i].lhs; + output_table_data (&output_obstack, values, + 0, 1, nrules + 1); + muscle_insert ("r1", obstack_finish (&output_obstack)); + XFREE (values); + } /* Output YYR2. */ short_tab = XMALLOC (short, nrules + 1); for (i = 1; i < nrules; i++) - short_tab[i] = rrhs[i + 1] - rrhs[i] - 1; - short_tab[nrules] = nitems - rrhs[nrules] - 1; + short_tab[i] = rule_table[i + 1].rhs - rule_table[i].rhs - 1; + short_tab[nrules] = nitems - rule_table[nrules].rhs - 1; output_table_data (&output_obstack, short_tab, 0, 1, nrules + 1); muscle_insert ("r2", obstack_finish (&output_obstack)); XFREE (short_tab); - XFREE (rrhs + 1); + XFREE (rule_table + 1); } /*------------------------------------------------------------------. diff --git a/src/print.c b/src/print.c index d676f7f3..96b64e88 100644 --- a/src/print.c +++ b/src/print.c @@ -68,9 +68,9 @@ print_core (FILE *out, int state) sp++; rule = -(*sp); - fprintf (out, " %s -> ", tags[rlhs[rule]]); + fprintf (out, " %s -> ", tags[rule_table[rule].lhs]); - for (sp = ritem + rrhs[rule]; sp < sp1; sp++) + for (sp = ritem + rule_table[rule].rhs; sp < sp1; sp++) { fprintf (out, "%s ", tags[*sp]); } @@ -168,7 +168,7 @@ print_actions (FILE *out, int state) if (state_table[state].consistent && redp) { rule = redp->rules[0]; - symbol = rlhs[rule]; + symbol = rule_table[rule].lhs; fprintf (out, _(" $default\treduce using rule %d (%s)\n\n"), rule, tags[symbol]); } @@ -230,10 +230,10 @@ print_grammar (FILE *out) fprintf (out, "\n%s\n\n", _("Grammar")); for (i = 1; i <= nrules; i++) /* Don't print rules disabled in reduce_grammar_tables. */ - if (rlhs[i] >= 0) + if (rule_table[i].lhs >= 0) { - fprintf (out, _("rule %-4d %s ->"), i, tags[rlhs[i]]); - rule = &ritem[rrhs[i]]; + fprintf (out, _("rule %-4d %s ->"), i, tags[rule_table[i].lhs]); + rule = &ritem[rule_table[i].rhs]; if (*rule > 0) while (*rule > 0) fprintf (out, " %s", tags[*rule++]); @@ -256,7 +256,7 @@ print_grammar (FILE *out) sprintf (buffer, " (%d)", i); for (j = 1; j <= nrules; j++) - for (rule = &ritem[rrhs[j]]; *rule > 0; rule++) + for (rule = &ritem[rule_table[j].rhs]; *rule > 0; rule++) if (*rule == token_translations[i]) { END_TEST (65); @@ -274,9 +274,9 @@ print_grammar (FILE *out) for (j = 1; j <= nrules; j++) { - if (rlhs[j] == i) + if (rule_table[j].lhs == i) left_count++; - for (rule = &ritem[rrhs[j]]; *rule > 0; rule++) + for (rule = &ritem[rule_table[j].rhs]; *rule > 0; rule++) if (*rule == i) { right_count++; @@ -298,7 +298,7 @@ print_grammar (FILE *out) for (j = 1; j <= nrules; j++) { END_TEST (65); - if (rlhs[j] == i) + if (rule_table[j].lhs == i) sprintf (buffer + strlen (buffer), " %d", j); } } @@ -311,7 +311,7 @@ print_grammar (FILE *out) sprintf (buffer + strlen (buffer), _(" on right:")); for (j = 1; j <= nrules; j++) { - for (rule = &ritem[rrhs[j]]; *rule > 0; rule++) + for (rule = &ritem[rule_table[j].rhs]; *rule > 0; rule++) if (*rule == i) { END_TEST (65); diff --git a/src/print_graph.c b/src/print_graph.c index 2ce8e451..4266375a 100644 --- a/src/print_graph.c +++ b/src/print_graph.c @@ -76,9 +76,10 @@ print_core (int state, struct obstack *node_obstack) rule = -(*sp); obstack_fgrow1 (node_obstack, "%d: ", rule); - obstack_fgrow1 (node_obstack, " %s -> ", quote (tags[rlhs[rule]])); + obstack_fgrow1 (node_obstack, " %s -> ", + quote (tags[rule_table[rule].lhs])); - for (sp = ritem + rrhs[rule]; sp < sp1; sp++) + for (sp = ritem + rule_table[rule].rhs; sp < sp1; sp++) obstack_fgrow1 (node_obstack, "%s ", quote (tags[*sp])); obstack_1grow (node_obstack, '.'); @@ -181,7 +182,7 @@ print_actions (int state, const char *node_name, struct obstack *node_obstack) if (state_table[state].consistent && redp) { rule = redp->rules[0]; - symbol = rlhs[rule]; + symbol = rule_table[rule].lhs; if (obstack_object_size (node_obstack) > node_output_size) obstack_sgrow (node_obstack, "\\n"); obstack_fgrow2 (node_obstack, _("$default\treduce using rule %d (%s)"), diff --git a/src/reader.c b/src/reader.c index eaa2d533..93024ea1 100644 --- a/src/reader.c +++ b/src/reader.c @@ -1922,8 +1922,7 @@ packgram (void) bucket *ruleprec; ritem = XCALLOC (short, nitems + 1); - rlhs = XCALLOC (short, nrules) - 1; - rrhs = XCALLOC (short, nrules) - 1; + rule_table = XCALLOC (rule_t, nrules) - 1; rprec = XCALLOC (short, nrules) - 1; rprecsym = XCALLOC (short, nrules) - 1; rassoc = XCALLOC (short, nrules) - 1; @@ -1934,8 +1933,8 @@ packgram (void) p = grammar; while (p) { - rlhs[ruleno] = p->sym->value; - rrhs[ruleno] = itemno; + rule_table[ruleno].lhs = p->sym->value; + rule_table[ruleno].rhs = itemno; ruleprec = p->ruleprec; p = p->next; diff --git a/src/reduce.c b/src/reduce.c index 991034d1..71448410 100644 --- a/src/reduce.c +++ b/src/reduce.c @@ -100,7 +100,7 @@ useful_production (int i, BSet N0) /* A production is useful if all of the nonterminals in its appear in the set of useful nonterminals. */ - for (r = &ritem[rrhs[i]]; *r > 0; r++) + for (r = &ritem[rule_table[i].rhs]; *r > 0; r++) if (ISVAR (n = *r)) if (!BITISSET (N0, n - ntokens)) return FALSE; @@ -149,7 +149,7 @@ useless_nonterminals (void) { if (useful_production (i, N)) { - SETBIT (Np, rlhs[i] - ntokens); + SETBIT (Np, rule_table[i].lhs - ntokens); SETBIT (P, i); } } @@ -211,9 +211,9 @@ inaccessable_symbols (void) Vp[i] = V[i]; for (i = 1; i <= nrules; i++) { - if (!BITISSET (Pp, i) && BITISSET (P, i) && BITISSET (V, rlhs[i])) + if (!BITISSET (Pp, i) && BITISSET (P, i) && BITISSET (V, rule_table[i].lhs)) { - for (r = &ritem[rrhs[i]]; *r >= 0; r++) + for (r = &ritem[rule_table[i].rhs]; *r >= 0; r++) { if (ISTOKEN (t = *r) || BITISSET (N, t - ntokens)) { @@ -279,15 +279,15 @@ reduce_grammar_tables (void) np++; if (pn != np) { - rlhs[np] = rlhs[pn]; + rule_table[np].lhs = rule_table[pn].lhs; rline[np] = rline[pn]; rprec[np] = rprec[pn]; rassoc[np] = rassoc[pn]; - rrhs[np] = rrhs[pn]; - if (rrhs[np] != ni) + rule_table[np].rhs = rule_table[pn].rhs; + if (rule_table[np].rhs != ni) { - pi = rrhs[np]; - rrhs[np] = ni; + pi = rule_table[np].rhs; + rule_table[np].rhs = ni; while (ritem[pi] >= 0) ritem[ni++] = ritem[pi++]; ritem[ni++] = -np; @@ -319,7 +319,7 @@ reduce_grammar_tables (void) { if (!BITISSET (P, pn)) { - rlhs[pn] = -1; + rule_table[pn].lhs = -1; } } } @@ -368,8 +368,8 @@ reduce_grammar_tables (void) for (i = 1; i <= nrules; i++) { /* Ignore the rules disabled above. */ - if (rlhs[i] >= 0) - rlhs[i] = nontermmap[rlhs[i]]; + if (rule_table[i].lhs >= 0) + rule_table[i].lhs = nontermmap[rule_table[i].lhs]; if (ISVAR (rprecsym[i])) /* Can this happen? */ rprecsym[i] = nontermmap[rprecsym[i]]; @@ -434,8 +434,8 @@ reduce_output (FILE *out) if (!BITISSET (P, i)) { fprintf (out, "#%-4d ", i); - fprintf (out, "%s :\t", tags[rlhs[i]]); - for (r = &ritem[rrhs[i]]; *r >= 0; r++) + fprintf (out, "%s :\t", tags[rule_table[i].lhs]); + for (r = &ritem[rule_table[i].rhs]; *r >= 0; r++) fprintf (out, " %s", tags[*r]); fprintf (out, ";\n"); } @@ -464,8 +464,8 @@ dump_grammar (FILE *out) for (i = 1; i <= nrules; i++) { fprintf (out, "%-5d(%5d%5d)%5d : (@%-5d)", - i, rprec[i], rassoc[i], rlhs[i], rrhs[i]); - for (r = &ritem[rrhs[i]]; *r > 0; r++) + i, rprec[i], rassoc[i], rule_table[i].lhs, rule_table[i].rhs); + for (r = &ritem[rule_table[i].rhs]; *r > 0; r++) fprintf (out, "%5d", *r); fprintf (out, " [%d]\n", -(*r)); } @@ -473,8 +473,8 @@ dump_grammar (FILE *out) fprintf (out, _("Rules interpreted\n-----------------\n\n")); for (i = 1; i <= nrules; i++) { - fprintf (out, "%-5d %s :", i, tags[rlhs[i]]); - for (r = &ritem[rrhs[i]]; *r > 0; r++) + fprintf (out, "%-5d %s :", i, tags[rule_table[i].lhs]); + for (r = &ritem[rule_table[i].rhs]; *r > 0; r++) fprintf (out, " %s", tags[*r]); fputc ('\n', out); } -- 2.45.2