简单 Flex/Bison C++
我已经在寻找答案,但对于一个简单的例子我没有得到任何快速回复。
我想使用 g++ 编译一个 flex/bison 扫描仪+解析器,只是因为我想使用 C++ 类来创建 AST 和类似的东西。
通过互联网搜索,我发现了一些漏洞,都说唯一需要的就是在 lex 文件中使用 extern "C" 声明一些函数原型。
所以我的 shady.y 文件是
%{
#include <stdio.h>
#include "opcodes.h"
#include "utils.h"
void yyerror(const char *s)
{
fprintf(stderr, "error: %s\n", s);
}
int counter = 0;
extern "C"
{
int yyparse(void);
int yylex(void);
int yywrap()
{
return 1;
}
}
%}
%token INTEGER FLOAT
%token T_SEMICOL T_COMMA T_LPAR T_RPAR T_GRID T_LSPAR T_RSPAR
%token EOL
%token T_MOV T_NOP
%%
... GRAMMAR OMITTED ...
%%
main(int argc, char **argv)
{
yyparse();
}
,而 shady.l 文件是
%{
#include "shady.tab.h"
%}
%%
"MOV"|"mov" { return T_MOV; }
"NOP"|"nop" { return T_NOP; }
";" { return T_SEMICOL; }
"," { return T_COMMA; }
"(" { return T_LPAR; }
")" { return T_RPAR; }
"#" { return T_GRID; }
"[" { return T_LSPAR; }
"]" { return T_RSPAR; }
[1-9][0-9]? { yylval = atoi(yytext); return INTEGER;}
[0-9]+"."[0-9]+ | "."?[0-9]? { yylval.d = atof(yytext); return FLOAT; }
\n { return EOL; }
[ \t] { /* ignore whitespace */ }
. { printf("Mystery character %c\n", *yytext); }
%%
最后在 makefile 中我使用 g++ 而不是 gcc:
shady: shady.l shady.y
bison -d shady.y -o shady.tab.c
flex shady.l
g++ -o $@ shady.tab.c lex.yy.c -lfl
flex 和 bison 工作正常,但在链接时出现以下错误:
Undefined symbols:
"_yylex", referenced from:
_yyparse in ccwb57x0.o
当然,如果我尝试更改有关该函数的任何内容bison 文件显示 yylex 未在 yyparse 范围内声明。
我是否试图解决比看起来更复杂的问题?实际上,我不需要一个封闭的结构来以面向对象的方式访问解析和词法分析器,我只是想让它工作。
我只是希望能够在 bison 文件中使用 C++(以创建 AST)并从 C++ 对象调用 yyparse() ..
提前致谢
I already looked for my answer but I didn't get any quick response for a simple example.
I want to compile a flex/bison scanner+parser using g++ just because I want to use C++ classes to create AST and similar things.
Searching over internet I've found some exploits, all saying that the only needed thing is to declare some function prototypes using extern "C" in lex file.
So my shady.y file is
%{
#include <stdio.h>
#include "opcodes.h"
#include "utils.h"
void yyerror(const char *s)
{
fprintf(stderr, "error: %s\n", s);
}
int counter = 0;
extern "C"
{
int yyparse(void);
int yylex(void);
int yywrap()
{
return 1;
}
}
%}
%token INTEGER FLOAT
%token T_SEMICOL T_COMMA T_LPAR T_RPAR T_GRID T_LSPAR T_RSPAR
%token EOL
%token T_MOV T_NOP
%%
... GRAMMAR OMITTED ...
%%
main(int argc, char **argv)
{
yyparse();
}
while shady.l file is
%{
#include "shady.tab.h"
%}
%%
"MOV"|"mov" { return T_MOV; }
"NOP"|"nop" { return T_NOP; }
";" { return T_SEMICOL; }
"," { return T_COMMA; }
"(" { return T_LPAR; }
")" { return T_RPAR; }
"#" { return T_GRID; }
"[" { return T_LSPAR; }
"]" { return T_RSPAR; }
[1-9][0-9]? { yylval = atoi(yytext); return INTEGER;}
[0-9]+"."[0-9]+ | "."?[0-9]? { yylval.d = atof(yytext); return FLOAT; }
\n { return EOL; }
[ \t] { /* ignore whitespace */ }
. { printf("Mystery character %c\n", *yytext); }
%%
Finally in the makefile I use g++ instead of gcc:
shady: shady.l shady.y
bison -d shady.y -o shady.tab.c
flex shady.l
g++ -o $@ shady.tab.c lex.yy.c -lfl
flex and bison work correctly but upon linking I get the following error:
Undefined symbols:
"_yylex", referenced from:
_yyparse in ccwb57x0.o
Of course if I try to change anything about the function in bison file it says that yylex is not declared in the scope of yyparse.
Am I trying to solve simply something that is more complex than it seems? Actually I don't need a closed structure to have access to parse and lexer in a object oriented manner, I just want to make it work.
I just want to be able to use C++ in bison file (to create AST) and to call yyparse() from C++ objects..
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要将
yylex
的extern "C" {}
放在shady.l
中:此外,在添加虚拟语法规则后,我只需使用以下命令即可构建并运行它:
You need the
extern "C" {}
foryylex
to be inshady.l
:Also, after adding a dummy grammar rule, I was able to build and run this with just: