'YYS类型'没有会员
在我的 YACC 文件中,我有这个:
%union {
Node *node;
FuncParamList *fParam;
CallParamList *cParam;
char *str;
struct {
char *name;
Node *node;
} nameNodePair;
}
这是我的 Lex 文件(请注意,它包括 YACC 生成的头文件):
%{
#include "yacc_parser.hh"
%}
%%
if return IF;
ei return ELSEIF;
else return ELSE;
endif return ENDIF;
while return WHILE;
loop return LOOP;
func return FUNC;
end return END;
:= return ASSIGN;
\. return DOT;
, return COMMA;
: return COLON;
\( return OPAREN;
\) return CPAREN;
(\n|\r\n?) { /* increment line count */ return LF; }
; return LF;
[!?A-Za-z][!?A-Za-z0-9] { yylval.str = yytext; return NAME; }
[0-9]+ { yylval.str = yytext; return INTEGER; }
%%
但编译时出现此错误:
/home/michael/Projects/lang/lib/lex_lexer.l:26:9: error: ‘YYSTYPE’ has no member named ‘str’
/home/michael/Projects/lang/lib/lex_lexer.l:27:9: error: ‘YYSTYPE’ has no member named ‘str’
我确保 YACC 头文件包含 YYSTYPE 定义,并且Lex 输出文件在使用 YYSTYPE 之前确实包含它。我应该怎么办?
In my YACC file, I have this:
%union {
Node *node;
FuncParamList *fParam;
CallParamList *cParam;
char *str;
struct {
char *name;
Node *node;
} nameNodePair;
}
This is my Lex file (note that it includes the header file generated by YACC):
%{
#include "yacc_parser.hh"
%}
%%
if return IF;
ei return ELSEIF;
else return ELSE;
endif return ENDIF;
while return WHILE;
loop return LOOP;
func return FUNC;
end return END;
:= return ASSIGN;
\. return DOT;
, return COMMA;
: return COLON;
\( return OPAREN;
\) return CPAREN;
(\n|\r\n?) { /* increment line count */ return LF; }
; return LF;
[!?A-Za-z][!?A-Za-z0-9] { yylval.str = yytext; return NAME; }
[0-9]+ { yylval.str = yytext; return INTEGER; }
%%
But I get this error when I compile:
/home/michael/Projects/lang/lib/lex_lexer.l:26:9: error: ‘YYSTYPE’ has no member named ‘str’
/home/michael/Projects/lang/lib/lex_lexer.l:27:9: error: ‘YYSTYPE’ has no member named ‘str’
I made sure that the YACC header file contains the YYSTYPE definition, and the Lex output file does include it before it uses YYSTYPE. What should I do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
[应该是注释,但我需要空格和空白才能可读。当一切都澄清后将进行编辑以使其成为真正的解决方案]
编辑1:新的conf
因此,让我们稍微澄清一下您应拥有的文件:
C++ 代码样式/
yacc_parser.yy
:包含 %unionC++ 代码样式/
yacc_parser.hh
和yacc_parser。 cc
:由yacc yacc_parser.yy
命令生成C 代码风格 /
lex_lexer.l
:包含yacc_parser.h
C 代码风格/
lex_lexer.c
:由lex lex_lexer.l
命令生成然后编译并链接:
gcc -Wall lex_lexer.c yacc_parser.cc
应该产生可执行文件。由于您混合了 C 和 C++ 代码,我几乎可以肯定您需要在某处使用
extern "C" { ... }
将您的联合链接为 C 类型而不是 C++。这可以解释为什么你的c代码找不到结构成员。也许
供我参考,为什么你混合使用 C 和 C++?为什么不只有一种语言?
[should be a comment, but I need spacing and blanks to be readable. Will edit to make it real solution when all will be clarify]
Edit1: new conf
So let's clarify a bit the file you shall have :
C++ code style /
yacc_parser.yy
: containing the %unionC++ code style /
yacc_parser.hh
andyacc_parser.cc
: generated by theyacc yacc_parser.yy
commandC code style /
lex_lexer.l
: includesyacc_parser.h
C code style /
lex_lexer.c
: generated bylex lex_lexer.l
commandThen you compile&link :
gcc -Wall lex_lexer.c yacc_parser.cc
that should produce the executable file.Since you mix C and C++ code, I almost sure you need to use a
extern "C" { ... }
somewhere to link your union as a C type not C++. That may explain why you c code can't find the struct member.Maybe
for my information, why have you a mix of C and C++? why not only one language?