问题描述
我对野牛的YACC文件这个code座。这是从Flex /野牛code为的书。 // github上:该YACC code可以从
I have this code block in bison's yacc file. This is the code from the flex/bison code for Flex & Bison book. The yacc code can be downloaded from https://github.com/GrooveStomp/Flex-Bison/blob/master/samples/purecalc.y
%{
# include "purecalc.lex.h"
# include "purecalc.h"
#define YYLEX_PARAM pp->scaninfo
%}
问题是,我有一个编译错误(Circular依赖分辨率野牛生成的文件);他们是圆形的依赖为 purecalc.lex.h
取决于PCDATA这是purecalc.h文件,而purecalc.h取决于purecalc.lex.h的yyscan_t。
The issue is that I have a compilation error(Circular dependency resolution with bison generated file); they are circular dependent as purecalc.lex.h
depends on pcdata which is in purecalc.h file, whereas purecalc.h depends on purecalc.lex.h for yyscan_t.
yyscan_t在purecalc.lex.h defiend:
yyscan_t is defiend in purecalc.lex.h:
typedef void* yyscan_t;
PCDATA定义如下:
pcdata is defined as follows:
/* per-parse data */
struct pcdata {
yyscan_t scaninfo; /* scanner context */
struct symbol *symtab; /* symbols for this parse */
struct ast *ast; /* an AST for */
};
我怎样才能打破这种code依赖?我试图复制的typedef无效* yyscan_t;
进入yacc源,但我得到的编译错误重复的typedef。
How can I break this code dependency? I tried to copy the typedef void* yyscan_t;
into the yacc source, but I got compilation error for duplicating typedef.
推荐答案
是的,在最近的野牛历史上的一些时期,一个不兼容的变化到框架文件提出,引起 yyparse
要早得多声明,其实包含点%{%} 部分。因此, yyparse
正在前 purecalc.h
声明包含,这意味着 PCDATA
时声明 yyparse
尚未宣布。
Yes, some time in recent bison history, an incompatible change was made to the skeleton files, causing yyparse
to be declared much earlier, in fact before the inclusion point for %{ %}
sections. So yyparse
is being declared before purecalc.h
is included, which means that pcdata
is not declared yet when yyparse
is declared.
有一个简单的解决方案是将移动的#include purecalc.h
较早,但这将创建一个不同的问题。的实际定义 PCDATA
要求 yyscan_t
这是在宣布purecalc.lex.h
。 purecalc.lex.h
要求 YYSTYPE
进行#defined,这发生在 purecalc.tab .H
。和 purecalc.tab.h
声明 yyparse
,要求 PCDATA $的声明C $ C>。
A simple solution would be to move the #include purecalc.h
earlier, but this creates a different problem. The actual definition of pcdata
requires yyscan_t
which is declared in purecalc.lex.h
. purecalc.lex.h
requires that YYSTYPE
be #defined, which happens in purecalc.tab.h
. And purecalc.tab.h
declares yyparse
, which requires the declaration of pcdata
.
该圆只能通过向前声明来解决:
The circularity can only be resolved by forward declaring:
struct pcdata;
不过,有前 purecalc.tab.h
来发生的是包含(#include)。
But that has to happen before purecalc.tab.h
is #included.
所以,一种可能是把这些三线年初 purecalc.h
:
So one possibility would be to put these three lines early purecalc.h
:
struct pcdata;
#include purecalc.tab.h
#include purecalc.lex.h
然后删除冗余 purecalc.tab.h
和 purecalc.lex.h
从<$ C包括$ C> purecalc.l 和 purecalc.y
,分别为。 (另外,这将是一个很好的主意是一个包括后卫成 purecalc.h
)。
and then remove the redundant purecalc.tab.h
and purecalc.lex.h
includes from purecalc.l
and purecalc.y
, respectively. (Also, it would be a really good idea to be an include guard into purecalc.h
).
另一种可能性是将结构PCDATA;
直接进入 purecalc.y
。但它不是不够好,使用%{%}
块是什么;它具有前 yyparse
插入声明。对于这一点,你需要一个%code要求{}
块:
Another possibility is to put struct pcdata;
directly into purecalc.y
. But it's not good enough to use a %{ %}
block for that; it has to be inserted before yyparse
is declared. For that, you need a %code requires { }
block:
%code requires {
struct pcdata;
}
我试过了,并将其与野牛2.7编译。如果你使用的第一个解决方案,你需要使用%code要求为
,所以它不会看起来不同。的#include purecalc {}
块。 ^ h
I tried that, and it compiled with bison 2.7. If you were to use the first solution, you'd need to use a %code requires { }
block for the #include purecalc.h
, so it wouldn't look that different.
这篇关于循环依赖分辨率野牛生成的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!