From 002389585a71dfe28525fc0cdf5100c550e40b63 Mon Sep 17 00:00:00 2001 From: Akim Demaille Date: Fri, 30 Nov 2001 10:47:56 +0000 Subject: [PATCH] * src/reduce.c: Various comment/formatting changes. (nonterminals_reduce): New, extracted from... (reduce_grammar_tables): here. (reduce_grammar): Call nonterminals_reduce. --- ChangeLog | 7 ++++ src/main.c | 3 +- src/reduce.c | 112 ++++++++++++++++++++++++++++----------------------- 3 files changed, 69 insertions(+), 53 deletions(-) diff --git a/ChangeLog b/ChangeLog index 4a8ddbcb..1606c8dd 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +2001-11-30 Akim Demaille + + * src/reduce.c: Various comment/formatting changes. + (nonterminals_reduce): New, extracted from... + (reduce_grammar_tables): here. + (reduce_grammar): Call nonterminals_reduce. + 2001-11-29 Paul Eggert * src/bison.simple (YYSTACK_REALLOC): Remove. diff --git a/src/main.c b/src/main.c index 80704466..76caf7a0 100644 --- a/src/main.c +++ b/src/main.c @@ -64,8 +64,7 @@ main (int argc, char *argv[]) if (complain_message_count) exit (1); - /* Find useless nonterminals and productions and reduce the grammar. - In file reduce.c. */ + /* Find useless nonterminals and productions and reduce the grammar. */ reduce_grammar (); /* Record other info about the grammar. In files derives and diff --git a/src/reduce.c b/src/reduce.c index 02dff622..911fb929 100644 --- a/src/reduce.c +++ b/src/reduce.c @@ -38,11 +38,18 @@ typedef unsigned *BSet; typedef short *rule; -/* N is set of all nonterminals which are not useless. P is set of - all rules which have no useless nonterminals in their RHS. V is - the set of all accessible symbols. */ +/* Set of all nonterminals which are not useless. */ +static BSet N; -static BSet N, P, V, V1; +/* Set of all rules which have no useless nonterminals in their RHS. */ +static BSet P; + +/* Set of all accessible symbols. */ +static BSet V; + +/* Set of symbols used to define rule precedence (so they are + `useless', but no warning should be issued). */ +static BSet V1; static int nuseful_productions; static int nuseless_productions; @@ -318,71 +325,72 @@ reduce_grammar_tables (void) } } } +} - /* remove useless symbols */ - if (nuseless_nonterminals > 0) - { - int i, n; -/* short j; JF unused */ - short *nontermmap; - rule r; +/*------------------------------. +| Remove useless nonterminals. | +`------------------------------*/ - /* Create a map of nonterminal number to new nonterminal - number. -1 in the map means it was useless and is being - eliminated. */ +static void +nonterminals_reduce (void) +{ + int i, n; + rule r; - nontermmap = XCALLOC (short, nvars) - ntokens; - for (i = ntokens; i < nsyms; i++) - nontermmap[i] = -1; + /* Create a map of nonterminal number to new nonterminal number. -1 + in the map means it was useless and is being eliminated. */ - n = ntokens; - for (i = ntokens; i < nsyms; i++) - if (BITISSET (V, i)) - nontermmap[i] = n++; + short *nontermmap = XCALLOC (short, nvars) - ntokens; + for (i = ntokens; i < nsyms; i++) + nontermmap[i] = -1; - /* Shuffle elements of tables indexed by symbol number. */ + n = ntokens; + for (i = ntokens; i < nsyms; i++) + if (BITISSET (V, i)) + nontermmap[i] = n++; - for (i = ntokens; i < nsyms; i++) + /* Shuffle elements of tables indexed by symbol number. */ + + for (i = ntokens; i < nsyms; i++) + { + n = nontermmap[i]; + if (n >= 0) { - n = nontermmap[i]; - if (n >= 0) - { - sassoc[n] = sassoc[i]; - sprec[n] = sprec[i]; - tags[n] = tags[i]; - } + sassoc[n] = sassoc[i]; + sprec[n] = sprec[i]; + tags[n] = tags[i]; } + } - /* Replace all symbol numbers in valid data structures. */ + /* Replace all symbol numbers in valid data structures. */ - for (i = 1; i <= nrules; i++) - { - /* Ignore the rules disabled above. */ - if (rule_table[i].lhs >= 0) - rule_table[i].lhs = nontermmap[rule_table[i].lhs]; - if (ISVAR (rule_table[i].precsym)) - /* Can this happen? */ - rule_table[i].precsym = nontermmap[rule_table[i].precsym]; - } + for (i = 1; i <= nrules; i++) + { + /* Ignore the rules disabled above. */ + if (rule_table[i].lhs >= 0) + rule_table[i].lhs = nontermmap[rule_table[i].lhs]; + if (ISVAR (rule_table[i].precsym)) + /* Can this happen? */ + rule_table[i].precsym = nontermmap[rule_table[i].precsym]; + } - for (r = ritem; *r; r++) - if (ISVAR (*r)) - *r = nontermmap[*r]; + for (r = ritem; *r; r++) + if (ISVAR (*r)) + *r = nontermmap[*r]; - start_symbol = nontermmap[start_symbol]; + start_symbol = nontermmap[start_symbol]; - nsyms -= nuseless_nonterminals; - nvars -= nuseless_nonterminals; + nsyms -= nuseless_nonterminals; + nvars -= nuseless_nonterminals; - free (&nontermmap[ntokens]); - } + free (&nontermmap[ntokens]); } -/*-----------------------------------------------------------------. -| Ouput the detailed results of the reductions. For FILE.output. | -`-----------------------------------------------------------------*/ +/*------------------------------------------------------------------. +| Output the detailed results of the reductions. For FILE.output. | +`------------------------------------------------------------------*/ void reduce_output (FILE *out) @@ -537,6 +545,8 @@ reduce_grammar (void) tags[start_symbol]); reduce_grammar_tables (); + if (nuseless_nonterminals > 0) + nonterminals_reduce (); if (trace_flag) { -- 2.45.2