From 643a5994714b21dfde1a44a52ccffd0a300e5131 Mon Sep 17 00:00:00 2001 From: Akim Demaille Date: Sun, 7 Apr 2002 17:40:16 +0000 Subject: [PATCH] * src/LR0.c (new_state): Display `nstates' as the name of the newly created state. Adjust to initialize first_state and last_state if needed. Be sure to distinguish the initial from the final state. (new_states): Create the itemset of the initial state, and use new_state. * src/closure.c (closure): Now that the initial state has its items properly set, there is no need for a special case when creating `ruleset'. As a result, now the rule 0, reducing to $axiom, is visible in the outputs. Adjust the test suite. * tests/conflicts.at (Solved SR Conflicts) (Unresolved SR Conflicts): Adjust. * tests/regression.at (Web2c Report, Rule Line Numbers): Idem. * tests/conflicts.at (S/R in initial): New. --- ChangeLog | 20 ++++++++++++++++++++ NEWS | 9 +++++++++ src/LR0.c | 25 ++++++++++++++++--------- src/closure.c | 15 ++++----------- tests/Makefile.am | 2 +- tests/conflicts.at | 28 +++++++++++++++++++++++++++- tests/regression.at | 3 +++ 7 files changed, 80 insertions(+), 22 deletions(-) diff --git a/ChangeLog b/ChangeLog index 8a8721a6..ecd36cbb 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,23 @@ +2002-04-07 Akim Demaille + + * src/LR0.c (new_state): Display `nstates' as the name of the + newly created state. + Adjust to initialize first_state and last_state if needed. + Be sure to distinguish the initial from the final state. + (new_states): Create the itemset of the initial state, and use + new_state. + * src/closure.c (closure): Now that the initial state has its + items properly set, there is no need for a special case when + creating `ruleset'. + + As a result, now the rule 0, reducing to $axiom, is visible in the + outputs. Adjust the test suite. + + * tests/conflicts.at (Solved SR Conflicts) + (Unresolved SR Conflicts): Adjust. + * tests/regression.at (Web2c Report, Rule Line Numbers): Idem. + * tests/conflicts.at (S/R in initial): New. + 2002-04-07 Akim Demaille * src/LR0.c (allocate_itemsets): Don't loop over ritem: loop over diff --git a/NEWS b/NEWS index 6d86faf3..4f1f0751 100644 --- a/NEWS +++ b/NEWS @@ -3,6 +3,15 @@ Bison News Changes in version 1.49a: +* The initial rule is explicit. + Bison used to play hacks with the initial rule, which the user does + not write. It is now explicit, and visible in the reports and + graphs as rule 0. + +* Useless rules are actually removed. + Before, Bison reported the useless rules, but, although not used, + included them in the parsers. + * False `Token not used' report fixed. On a grammar such as diff --git a/src/LR0.c b/src/LR0.c index 15b29f30..318055c2 100644 --- a/src/LR0.c +++ b/src/LR0.c @@ -35,7 +35,7 @@ #include "lalr.h" #include "reduce.h" -unsigned int nstates; +unsigned int nstates = 0; /* Initialize the final state to -1, otherwise, it might be set to 0 by default, and since we don't compute the reductions of the final state, we end up not computing the reductions of the initial state, @@ -190,7 +190,7 @@ new_state (int symbol) if (trace_flag) fprintf (stderr, "Entering new_state, state = %d, symbol = %d (%s)\n", - this_state->number, symbol, symbols[symbol]->tag); + nstates, symbol, symbols[symbol]->tag); if (nstates >= MAXSHORT) fatal (_("too many states (max %d)"), MAXSHORT); @@ -202,13 +202,18 @@ new_state (int symbol) shortcpy (p->items, kernel_base[symbol], kernel_size[symbol]); - last_state->next = p; + /* If this is the eoftoken, and this is not the initial state, then + this is the final state. */ + if (symbol == 0 && first_state) + final_state = p->number; + + if (!first_state) + first_state = p; + if (last_state) + last_state->next = p; last_state = p; - nstates++; - /* If this is the eoftoken, then this is the final state. */ - if (symbol == 0) - final_state = p->number; + nstates++; return p; } @@ -317,8 +322,10 @@ append_states (void) static void new_states (void) { - first_state = last_state = this_state = STATE_ALLOC (0); - nstates = 1; + /* The 0 at the lhs is the index of the item of this initial rule. */ + kernel_base[0][0] = 0; + kernel_size[0] = 1; + this_state = new_state (0); } diff --git a/src/closure.c b/src/closure.c index bdddfb99..9e595c81 100644 --- a/src/closure.c +++ b/src/closure.c @@ -244,18 +244,11 @@ closure (short *core, int n) if (trace_flag) print_closure ("input", core, n); - if (n == 0) - { - bitset_copy (ruleset, FDERIVES (start_symbol)); - } - else - { - bitset_zero (ruleset); + bitset_zero (ruleset); - for (c = 0; c < n; ++c) - if (ISVAR (ritem[core[c]])) - bitset_or (ruleset, ruleset, FDERIVES (ritem[core[c]])); - } + for (c = 0; c < n; ++c) + if (ISVAR (ritem[core[c]])) + bitset_or (ruleset, ruleset, FDERIVES (ritem[core[c]])); nitemset = 0; c = 0; diff --git a/tests/Makefile.am b/tests/Makefile.am index e7f48e26..1eb996af 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -55,7 +55,7 @@ TESTSUITE_AT = \ TESTSUITE = $(srcdir)/testsuite AUTOTEST = $(AUTOM4TE) --language=autotest -$(srcdir)/$(TESTSUITE): package.m4 $(TESTSUITE_AT) +$(TESTSUITE): package.m4 $(TESTSUITE_AT) $(AUTOTEST) -I $(srcdir) testsuite.at -o $@.tmp mv $@.tmp $@ diff --git a/tests/conflicts.at b/tests/conflicts.at index 2c017876..46add71b 100644 --- a/tests/conflicts.at +++ b/tests/conflicts.at @@ -1,5 +1,5 @@ # Exercising Bison on conflicts. -*- Autotest -*- -# Copyright 2002 Free Software Foundation, Inc. +# Copyright (C) 2002 Free Software Foundation, Inc. # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -19,6 +19,28 @@ AT_BANNER([[Conflicts.]]) +## ---------------- ## +## S/R in initial. ## +## ---------------- ## + +# I once hacked Bison in such a way that it lost its reductions on the +# initial state (because it was confusing it with the last state). It +# took me a while to strip down my failures to this simple case. So +# make sure it finds the s/r conflict below. + +AT_SETUP([S/R in initial]) + +AT_DATA([[input.y]], +[[%expect 1 +%% +exp: e 'e'; +e: 'e' | /* Nothing. */; +]]) + +AT_CHECK([bison input.y -o input.c]) + +AT_CLEANUP + ## ------------------- ## ## %nonassoc and eof. ## ## ------------------- ## @@ -143,6 +165,8 @@ exp (6) state 0 + $axiom -> . exp $ (rule 0) + NUM shift, and go to state 1 exp go to state 2 @@ -247,6 +271,8 @@ exp (6) state 0 + $axiom -> . exp $ (rule 0) + NUM shift, and go to state 1 exp go to state 2 diff --git a/tests/regression.at b/tests/regression.at index acd249bc..a5e82ff0 100644 --- a/tests/regression.at +++ b/tests/regression.at @@ -139,6 +139,8 @@ expr (7) state 0 + $axiom -> . expr $ (rule 0) + 'a' shift, and go to state 1 $default reduce using rule 3 (@2) @@ -348,6 +350,7 @@ CONST_DEC (10) @1 (11) on left: 4, on right: 5 state 0 + $axiom -> . CONST_DEC_PART $ (rule 0) $default reduce using rule 4 (@1) CONST_DEC_PART go to state 1 CONST_DEC_LIST go to state 2 -- 2.45.2