Re: Possible bug or simple nuisance compiler warning

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

> Le 29 nov. 2020 à 09:33, Akim Demaille <[email protected]> a écrit :
> 
> Hi Jot,
> 
>> Le 29 nov. 2020 à 02:12, Jot Dot <[email protected]> a écrit :
>> 
>> Let me know if you still want that file. 
> 
> I do.  Thanks!

So Jot sent me his file privately, and the relevant parts are:

    /// Stored state numbers (used for stacks).
    typedef unsigned char state_type;

   Parser ::state_type
   Parser ::yy_lr_goto_state_ (state_type yystate, int yysym)
  {
    int yyr = yypgoto_[yysym - YYNTOKENS] + yystate;
    if (0 <= yyr && yyr <= yylast_ && yycheck_[yyr] == yystate)
      return yytable_[yyr];
    else
      return yydefgoto_[yysym - YYNTOKENS];
  }

  const signed char
   Parser ::yydefgoto_[] =
  {
      -1,     2,     3,    10,    11,    19,    20,    21,    22,    63,
      64,    65,    12,    27,    41,    75,    76,    15,    16,    24,
      25,    52,    53,    54
  };

Jot's compiler's warning is about yy_lr_goto_state_ returning a signed char (from yydefgoto_) as an unsigned_char (state_type).

One can see that, except for the first value, everything in yydefgoto fits within an unsigned char (state_type).  The semantics of yydefgoto is to map a nonterminal to a state (to implement the "goto" phase of a reduction).  The table is indexed by the nonterminal number, starting at 0 for the first nonterminal: $accept.  The special nonterminal is never actually reduced, since reducing to $accept is actually accepting the input (returning successfully from yyparse).  So we actually _never_ use slot 0 of yydefgoto.

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.

In the case of Bison's parser itself, it will even allow to use smaller int types (unsigned char instead of int16):

  /* YYDEFGOTO[NTERM-NUM].  */
static const yytype_int16 yydefgoto[] =
{
      -1,     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,
      80,    72,    73,   111
};


Thanks Jot for reporting this warning.

Cheers!
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.