Re: %union in Bison

James Rauser <j.rauser@science-factory.com>
17 Jan 2003 19:46:49 -0500

          From comp.compilers

Related articles
%union in Bison belgioioso@libero.it (Davide Rizzo) (2003-01-12)
Re: %union in Bison haberg@math.su.se (2003-01-17)
Re: %union in Bison j.rauser@science-factory.com (James Rauser) (2003-01-17)
Re: %union in Bison wicklund@eskimo.com (2003-01-17)
Re: %union in Bison basireddym@rediffmail.com (2003-01-25)
| List of all articles for this month |

From: James Rauser <j.rauser@science-factory.com>
Newsgroups: comp.compilers
Date: 17 Jan 2003 19:46:49 -0500
Organization: Science Factory GmbH
References: 03-01-048
Keywords: yacc, C++
Posted-Date: 17 Jan 2003 19:46:49 EST

Davide Rizzo wrote:
> [ %union in bison with a C++ class doesn't work ]


> [Bison generates C code, not C++ code. -John]


The code produced by Bison (at least, from version 1.28) *is* compilable
as C++ code. With a little bit of preprocessor magic and an innocuous hack
to the parser skeleton in bison.simple you can even use the generated
yyparse() function as a class member function:


    -- use the %pure_parser directive in your grammar


    -- near the top of your grammar, in the user-code %{ ... %} section, write:
            #ifdef yyparse
            # undef yyparse
            #endif
            #define yyparse(parms) MYCLASS::yy_parse(parms)


    -- comment out the block in bison.simple which begins with a comment
          about preventing warnings with -Wstrict-prototypes:
            /* Prevent warning if -Wstrict-prototypes. */
            #ifdef __GNUC__
            #ifdef YYPARSE_PARAM
            int yyparse (void *);
            #else
            int yyparse (void);
            #endif
            #endif
          If you also use C *and* -Wstrict-prototypes, you can enclose this
          block in an "#ifndef __cplusplus" instead.


C++ won't allow you to place an STL string (or any other instance of
a class with a constructor) into the %union. You could use a "string *",
but then you'll have to delete the string instances.




Greetings, Jim


--
------------------------------------------------------------------------
Jim Rauser Science Factory GmbH
mailto:j.rauser@science-factory.com Unter Käster 1
Tel: +49 221 277 399 204 50667 Cologne, Germany


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.