bug-bison
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Wrong YYSTYPE definition in generated header file


From: heikki . tauriainen
Subject: Wrong YYSTYPE definition in generated header file
Date: Fri, 5 Mar 2004 11:53:27 +0200 (EET)

When all semantic values in the grammar have the same type, the Bison
manual suggests adding a #define YYSTYPE macro in the prologue section of
the input file for Bison (the Value Type node in the Bison info file). The
documentation on the %defines directive (or the -d option) gives the
impression that this macro definition is then (effectively) copied
automatically also to the generated header file; however, this isn't the
case in practice.

Running Bison (version 1.875a) on the input file

        %defines
        %{
        #define YYSTYPE double
        %}
        %%
        rule:

generates a header file with the following contents (not including
comments):

        #if ! defined (YYSTYPE) && ! defined (YYSTYPE_IS_DECLARED)
        typedef int YYSTYPE;
        # define yystype YYSTYPE /* obsolescent; will be withdrawn */
        # define YYSTYPE_IS_DECLARED 1
        # define YYSTYPE_IS_TRIVIAL 1
        #endif

        extern YYSTYPE yylval;

This header defines YYSTYPE as an int (not double) if it isn't already
defined.  Therefore, it's not possible to use this header to define
YYSTYPE correctly by simply including it in the source file for the
lexical scanner unless the macro is explicitly defined in the file first
(otherwise YYSTYPE will be defined as int, which may lead to undesired
results if the scanner actually assigns doubles to yylval).

It seems that Bison doesn't take the YYSTYPE macro definition correctly
into account when generating the output file.  However, if this is indeed
the intended behavior (which was hinted at when apparently the same
problem surfaced already in Bison 1.28 in 2001
<http://mail.gnu.org/archive/html/bug-bison/2001-03/msg00004.html>), then
the need to repeat the macro definition in the scanner source file should
probably be pointed out also in the documentation, since the current
documentation on the %defines directive and the -d option may lead to a
confusion:

 - Directive: %defines
     Write an extra output file containing macro definitions for the
     token type names defined in the grammar and the semantic value type
     `YYSTYPE', as well as a few `extern' variable declarations.

     If the parser output file is named `NAME.c' then this file is
     named `NAME.h'.

     This output file is essential if you wish to put the definition of
     `yylex' in a separate source file, because `yylex' needs to be
     able to refer to token type codes and the variable `yylval'.
     *Note Semantic Values of Tokens: Token Values.

`-d'
`--defines'
     Pretend that `%defines' was specified, i.e., write an extra output
     file containing macro definitions for the token type names defined
     in the grammar and the semantic value type `YYSTYPE', as well as a
     few `extern' variable declarations.  *Note Decl Summary::.


-- 
Heikki Tauriainen





reply via email to

[Prev in Thread] Current Thread [Next in Thread]