本文介绍了如何将头文件放入Bison中的.tab.h?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了野牛代码标题:

%{
#include "foo.h"
%}

然后我在标头中定义了一个名为"Foo"的结构.我想将其用作Bison中的令牌类型.

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

然后我使用-d选项生成bison.tab.h文件.

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

bison -d bison.y

但是bison.tab.h中没有#include foo.h,它使用struct Foo定义联合YYSTYPE.

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;
    ...
};

编译该程序时导致错误:error: ‘Foo’ does not name a type

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

是否可以在bison.tab.h中包含头文件或这种情况的另一种解决方案?

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

推荐答案

对于包含在.c和.h文件中(在%union的定义之前)同时出现的包含,应使用%code requires { ... }. %{ ... }仅在.c文件中插入代码.

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.

有关各种%code选项的详细信息,请查看 Bison文档的序言替代方案"一章.

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

这篇关于如何将头文件放入Bison中的.tab.h?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 18:01