bug-bison
[Top][All Lists]
Advanced

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

Re: yacc.c does not prototype yyparse in the header


From: Akim Demaille
Subject: Re: yacc.c does not prototype yyparse in the header
Date: Wed, 13 Jun 2012 09:03:38 +0200

Le 12 juin 2012 à 18:41, Paul Eggert a écrit :

>> Second, I never really paid attention to this before,
>> but how on earth can it be really useful to rename the
>> global variables (--name-prefix) such as yylval, global
>> functions, such as yyparse, but not the global types, such as
>> YYSTYPE?
> 
> This "works" because different modules can include different
> parser.h files.  So long as no single module includes both
> parser.h files, there is no clash at compile-time; and there
> is no clash at link-time because types are invisible to the
> linker.

Yes, that's what I had written below :)

> But it clearly would be better to do as you suggest
> and to apply the prefix to the types as well -- that
> would support programs where a single module wants to include
> two parser.h files (something that does not work now).

I'm afraid that changing this now would break existing
code.  People might be using YYSTYPE currently to define
their own yylex for instance.  The best course is probably
to define both YYSTYPE and <PREFIX>STYPE for a while.

Instead of

> #if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED
> typedef union YYSTYPE
> {
>  int ival;
> } YYSTYPE;
> # define YYSTYPE_IS_TRIVIAL 1
> # define yystype YYSTYPE /* obsolescent; will be withdrawn */
> # define YYSTYPE_IS_DECLARED 1
> #endif
> 
> extern YYSTYPE foolval;

we could issue something as follows.  First, the very same
declaration, but for %name-prefix "foo":

> #if ! defined FOOSTYPE && ! defined FOOSTYPE_IS_DECLARED
> typedef union FOOSTYPE
> {
>  int ival;
> } FOOSTYPE;
> # define FOOSTYPE_IS_TRIVIAL 1
> # define FOOSTYPE_IS_DECLARED 1
> #endif
> 
> extern FOOSTYPE foolval;

then a compatibility section (with all the needed provisions for
__attribute__):

> # if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED
> /* YYSTYPE will be removed in a future release of Bison, use FOOSTYPE. */
> typedef FOOSTYPE YYSTYPE __attribute__ ((deprecated));
> #  if defined FOOSTYPE_IS_TRIVIAL && FOOSTYPE_IS_TRIVIAL
> #   define YYSTYPE_IS_TRIVIAL 1
> #  endif
> #  define YYSTYPE_IS_DECLARED 1
> # endif

There is one problem though.  Unless you rely on %union to define
the types, the documentation promotes "#define YYSTYPE" as a means
to define the semantic type.

Another, completely different, course of action would be
to deprecate %name-prefix and leave it as is, and promote
something like

  %define api.prefix "foo"

which, this time, would also cover the types.  I like this
option, as it is easier to see whether you use the modern
style or the older one, and it is more consistent with our
use of %define today.




reply via email to

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