help-bison
[Top][All Lists]
Advanced

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

Re: %union problems


From: Joel E. Denny
Subject: Re: %union problems
Date: Fri, 15 May 2009 12:41:00 -0400 (EDT)

On Fri, 15 May 2009, [ISO-8859-1] Varga-H?li D?niel wrote:

> I have defined a struct in the grammar file similar to this:
> 
> typedef struct {
>   char *mem1;
>   char *mem2;
> } where_cls;
> 
> The union includes it:
> %union{
>   ...
>   where_cls aswhere;
> }

> After looking at the tab.h file I
> noticed that the union is also defined there but this time my structs
> and such were not there. So after manually editing the tab.h file it
> worked like a charm.

If you're using Bison 2.3b or later, you should place your where_cls 
definition and any other requirements of the union in a %code requires 
block:

  %code requires {
    typedef struct {
      char *mem1;
      char *mem2;
    } where_cls;
  }

Bison will insert that code into both the parser.tab.c and parser.tab.h 
files before the union.  See the %code entries in the section "Bison 
Declaration Summary" in the Bison manual for a summary of the %code 
functionality.  See the section "Prologue Alternatives" for a detailed 
discussion including the advantages of %code over the traditional Yacc 
prologue directive, %{...%}.

If you're using a version earlier than Bison 2.3b (or expect that your 
users will), the best approach is to write a parser-wrap.tab.h file as 
follows:

  typedef struct {
    char *mem1;
    char *mem2;
  } where_cls;

  #include "parser.tab.h"

Include parser-wrap.tab.h everywhere you would normally include 
parser.tab.h.  You'll also need to keep your where_cls definition in a 
%{...%} before the %union in your parser.y file.  Of course, you could 
write a where_cls.h file to avoid maintaining the where_cls definition in 
two places.

reply via email to

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