Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to put header file to .tab.h in Bison?

I wrote bison code header:

%{
#include "foo.h"
%}

And I defined a struct named 'Foo' in header. I'd like to use it as token type in Bison.

%define api.value.type union
%token <Foo*> bar

Then I use -d option to generate bison.tab.h file.

bison -d bison.y

But there is no #include foo.h in bison.tab.h, and it use struct Foo to define the union YYSTYPE.

//bison.tab.h
union YYSTPE {
    Foo* bar;
    ...
};

It caused error when compile this program: error: ‘Foo’ does not name a type

Is there a way to include header file in bison.tab.h or another solution of this case?

like image 610
syxbyi Avatar asked Dec 09 '17 07:12

syxbyi


People also ask

How do I include H file?

You request to use a header file in your program by including it with the C preprocessing directive “#include”. All the header file have a '. h' an extension. By including a header file, we can use its contents in our program.

How do I add a header file in Visual Studio C++?

Place your caret on the first line of any C# or Visual Basic file. Press Ctrl+. to trigger the Quick Actions and Refactorings menu. Select Add file header. To apply the file header to an entire project or solution, select Project or Solution under the Fix all occurrences in: option.

Does C Sharp have header files?

Microsoft created C# to remove complexities from C++ so they removed header files.

Can you include header files in header files?

A TL;DR definition: A header file must include the header files that directly define each of the types directly used in or that directly declare each of the functions used in the header file in question, but must not include anything else.


1 Answers

For includes that should appear in both the .c and the .h file (before the definition for the %union), you should use %code requires { ... }. %{ ... } inserts code in the .c file only.

For more information on the various %code options, you can look at the "Prologue Alternatives" chapter of the Bison docs.

like image 146
sepp2k Avatar answered Sep 21 '22 12:09

sepp2k