我写了野牛代码标题:
%{
#include "foo.h"
%}
我在标题中定义了一个名为'Foo'的结构。我想在Bison中将它用作令牌类型。
%define api.value.type union
%token <Foo*> bar
然后我使用-d
选项生成bison.tab.h
文件。
bison -d bison.y
但是在#include foo.h
中没有bison.tab.h
,它使用struct Foo来定义联合YYSTYPE。
//bison.tab.h
union YYSTPE {
Foo* bar;
...
};
编译此程序时导致错误:error: ‘Foo’ does not name a type
有没有办法在bison.tab.h
中包含头文件或本案例的另一种解决方案?
对于应出现在.c和.h文件中的包含(在%union
的定义之前),您应该使用%code requires { ... }
。 %{ ... }
仅在.c文件中插入代码。
有关各种%code
选项的更多信息,您可以查看"Prologue Alternatives" chapter of the Bison docs。