如何在Bison中将头文件放到.tab.h中?

问题描述 投票:4回答:1

我写了野牛代码标题:

%{
#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++ compiler-construction bison
1个回答
5
投票

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

有关各种%code选项的更多信息,您可以查看"Prologue Alternatives" chapter of the Bison docs

© www.soinside.com 2019 - 2024. All rights reserved.