Including files from Yacc/Bison definitions header (x.tab.h)

I'm generating a scanner-parser pair with Flex and Bison (similar to Lex and Yacc respectively). My YYSTYPE is a union which, as one of its members, has a struct*.
Bison generates its definitions file 'x.tab.h' containing all of the token definitions, etc. but I have the problem that when I include it from my Flex scanner (x.l) and try to compile the resulting file, the struct* is an undefined type. Is there a way I can get Bison to a #include into the header file it generates?
I can work around this problem by including the file in x.l *before* I include x.tab.h, but I would consider that a bad practice - the header file should be complete on its own.

Anybody know of how to do this?
--Rob
LVL 6
boycyAsked:
Who is Participating?
 
Kent OlsenConnect With a Mentor Data Warehouse Architect / DBACommented:

Bison doesn't provide that capability.  And when you stop and think about it, that makes a lot of sense.  The generated header file is the controls necessary to bulid the parser.

Put your definitions in a another header file.  Then use the standard Bison protocol to include your header file in the source.

%{
#include "MyHeader.h"
}%


Good Luck,
Kent
0
 
stuartdehaanCommented:
You could move the struct definition to the header file, where type definitions belong. You can now first include the header, and then the other file. This should work.

Stuart
0
 
boycyAuthor Commented:
The struct definition is in a header file already, I need to include that in x.tab.h which is generated by Bison, so I need to know how to insert extra code into that.
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
jojuCommented:
before generating x.tab.h put the following in Bison file

%{
#include "header.h"
}%


Thanxs,
Joju.
0
 
boycyAuthor Commented:
Joju, that will put 'include "header.h" into x.tab.c, not x.tab.h which is what I need.
0
 
jojuCommented:
hope you are using [-d] --defines option with bison for generating .h file
0
 
boycyAuthor Commented:
Yes. Ok let me restate the question as everyone seems to be getting confused.
Bison is generating the header file x.tab.h there's no problem there.
I want to tell bison to insert some extra code into the header file. There is no problem getting it inserted into x.tab.c, but that isn't what I want.
Does anybody know how to tell Bison to insert a given line of code into the header, if it's possible?
0
 
boycyAuthor Commented:
Thank you Kent - that was the conclusion I had come to. The definitions file I thought was part of the bison-flex link, and was there to export Bison's token definitions for Flex to use?

--Rob
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.