Re: Possible bug or simple nuisance compiler warning

Akim Demaille <[email protected]>
Newsgroups gmane.comp.parsers.bison.bugs
Message-ID <[email protected]>
Hi all,

> Le 30 nov. 2020 à 07:19, Akim Demaille <[email protected]> a écrit :
> 
> Hi Jot,
> 
> [...]
> Therefore:
> 1. the compiler is right, a cast would be nice to avoid this warning.
> 2. it's actually a pity to use -1 here to mean "invalid value".
> 
> I won't address (1), I think it's better to address (2) by using 0 instead of -1, which in this precise case would result in the use of "unsigned char yydefgoto", i.e., the very same type as state_type.

I will install this following commit.  Cheers!

commit 24233748ecd1dfaae212224fb269531eca50ad37
Author: Akim Demaille <[email protected]>
Date:   Wed Dec 2 21:39:26 2020 +0100

    tables: avoid warnings and save bits
    
    The yydefgoto table uses -1 as an invalid for an impossible case (we
    never use yydefgoto[0], since it corresponds to the reduction to
    $accept, which never happens).  Since yydefgoto is a table of state
    numbers, this -1 forces a signed type uselessly, which (1) might
    trigger compiler warnings when storing a value from yydefgoto into a
    state number (nonnegative), and (2) wastes bits which might result in
    using a int16 where a uint8 suffices.
    
    Reported by Jot Dot <[email protected]>.
    https://lists.gnu.org/r/bug-bison/2020-11/msg00027.html
    
    * src/tables.c (default_goto): Use 0 rather than -1 as invalid value.
    * tests/regression.at: Adjust.

diff --git a/src/parse-gram.c b/src/parse-gram.c
index adda86af..83f7d4a9 100644
--- a/src/parse-gram.c
+++ b/src/parse-gram.c
@@ -1,4 +1,4 @@
-/* A Bison parser, made by GNU Bison 3.7.3.118-d0ea7-dirty.  */
+/* A Bison parser, made by GNU Bison 3.7.4.133-f40c-dirty.  */
 
 /* Bison implementation for Yacc-like parsers in C
 
@@ -46,10 +46,10 @@
    USER NAME SPACE" below.  */
 
 /* Identify Bison output, and Bison version.  */
-#define YYBISON 30703
+#define YYBISON 30704
 
 /* Bison version string.  */
-#define YYBISON_VERSION "3.7.3.118-d0ea7-dirty"
+#define YYBISON_VERSION "3.7.4.133-f40c-dirty"
 
 /* Skeleton name.  */
 #define YYSKELETON_NAME "yacc.c"
@@ -799,9 +799,9 @@ static const yytype_int16 yypgoto[] =
 };
 
   /* YYDEFGOTO[NTERM-NUM].  */
-static const yytype_int16 yydefgoto[] =
+static const yytype_uint8 yydefgoto[] =
 {
-      -1,     1,     2,    43,    81,   115,    76,    45,    83,    46,
+       0,     1,     2,    43,    81,   115,    76,    45,    83,    46,
       50,    49,    51,    47,    60,   158,   120,   121,   122,    96,
       92,    93,    94,   128,   142,    86,    87,    88,    99,    70,
       77,    78,    79,   136,   146,   147,   113,    55,   105,    71,
diff --git a/src/parse-gram.h b/src/parse-gram.h
index b679374e..02e352e2 100644
--- a/src/parse-gram.h
+++ b/src/parse-gram.h
@@ -1,4 +1,4 @@
-/* A Bison parser, made by GNU Bison 3.7.3.118-d0ea7-dirty.  */
+/* A Bison parser, made by GNU Bison 3.7.4.133-f40c-dirty.  */
 
 /* Bison interface for Yacc-like parsers in C
 
diff --git a/src/tables.c b/src/tables.c
index 052e96e6..d26a4a97 100644
--- a/src/tables.c
+++ b/src/tables.c
@@ -512,7 +512,14 @@ default_goto (symbol_number sym, size_t state_count[])
 {
   const goto_number begin = goto_map[sym - ntokens];
   const goto_number end = goto_map[sym - ntokens + 1];
-  state_number res = -1;
+
+  /* In the case this symbol is never reduced to ($accept), use state
+     0.  We used to use -1, but as a result the yydefgoto table must
+     be signed, which (1) might trigger compiler warnings when storing
+     a value from yydefgoto into a state number (nonnegative), and (2)
+     wastes bits which might result in using a int16 where a uint8
+     suffices. */
+  state_number res = 0;
 
   if (begin != end)
     {
diff --git a/tests/regression.at b/tests/regression.at
index 3e57e38a..5bb05ae9 100644
--- a/tests/regression.at
+++ b/tests/regression.at
@@ -737,7 +737,7 @@ static const yytype_int8 yypgoto[] =
 };
 static const yytype_int8 yydefgoto[] =
 {
-      -1,     2,     3,     4,     8
+       0,     2,     3,     4,     8
 };
 static const yytype_int8 yytable[] =
 {
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.