2010-08-23 41 views
1

在我的解析器,我有問題的typedef與野牛和彎曲

%union { 
    SYMTABLE *p_entry ; 
    QUAD *p_quad ; 
} ; 

現在,SYMTABLE是一個結構typedef的。 struct和typedef位於包含文件中。這沒有問題。

QUAD是一個struct的typedef(typedef struct quad QUAD)。 struct和typedef位於包含文件中。

是沒有問題做:

bison -d parser.y 
gcc parser.tab.c -c 

我的詞法需要的yylval,所以在聲明部分我有

#include "parser.tab.h" /* bison generated header file */ 
extern YYSTYPE yylval ; 

當我做

flex scanner.lex 
gcc lex.yy.c -c 

GCC笙歌

In file included from scanner.lex:16: 
parser.y:30: error: syntax error before "QUAD" 
parser.y:30: warning: no semicolon at end of struct or union 
parser.y:32: error: syntax error before '}' token 
parser.y:32: warning: data definition has no type or storage class 
parser.tab.h:121: error: syntax error before "yylval" 
parser.tab.h:121: warning: data definition has no type or storage class 

如果我回到我的parser.y文件中,只用yylval%union替換QUAD與struct quad,問題就消失了。我想說這是一個愚蠢的typedef錯誤,但野牛生成的文件編譯就好。我在我的掃描儀中包含了QUAD typedef和struct quad的頭文件。

看來這是問題發生的唯一地方,所以我可以用struct quad替換QUAD,但這與SYMTABLE不一致。

回答

3

我test.l:

%{ 
#include "bla.h" 
#include "test.tab.h" /* bison generated header file */ 
extern YYSTYPE yylval ; 
%} 

%% 
\n  printf("nl"); 
.  printf("c"); 
%% 

我test.y:

%{ 
#include "bla.h" 
%} 

%union { 
     SYMTABLE *p_entry ; 
     QUAD *p_quad ; 
}; 

%% 

input: 
| input; 

%% 

我bla.h:

typedef void *SYMTABLE; 
typedef void *QUAD; 

我的構建:

[email protected]:pts/21:~/temp> bison -d test.y 
test.y: conflicts: 1 shift/reduce 
test.y:13.3-7: warning: rule useless in parser due to conflicts: input: input 
[email protected]:pts/21:~/temp> flex test.l  
[email protected]:pts/21:~/temp> icc lex.yy.c -c 
[email protected]:pts/21:~/temp> 
+0

我知道這是一個錯誤在我的結尾。原來在我的掃描儀中,我在#include「quad.h」之前#include「parser.tab.h」 我認爲parser.tab.h會包含來自quad.h的include,但它似乎不是。 – Kizaru 2010-08-24 00:28:30