简单Flex / Bison C ++ [英] Simple Flex/Bison C++

查看:295
本文介绍了简单Flex / Bison C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经找到了我的答案,但我没有得到任何快速反应一个简单的例子。

I already looked for my answer but I didn't get any quick response for a simple example.

我想编译flex / bison scanner + g ++只是因为我想使用C ++类创建AST和类似的东西。

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.

搜索互联网我发现了一些漏洞,所有说,唯一需要的是在lex文件中使用externC声明一些函数原型。

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.

所以我的shady.y文件是

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();
}

而shady.l文件是

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); }

%%



最后在makefile中使用g ++代替gcc :

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和bison可以正常工作,但是在链接时会出现以下错误:

flex and bison work correctly but upon linking I get the following error:

Undefined symbols:
  "_yylex", referenced from:
  _yyparse in ccwb57x0.o

如果我尝试改变任何关于函数的bison文件,它说,yylex没有在yyparse的范围内声明。

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.

我只是想能够在bison文件中使用C ++(创建AST)和从C ++对象调用yyparse()。

I just want to be able to use C++ in bison file (to create AST) and to call yyparse() from C++ objects..

提前感谢

推荐答案

您需要 externC{} for yylex 位于 shady.l

You need the extern "C" {} for yylex to be in shady.l:

%{
    extern "C"
    {
        int yylex(void);
    }

    #include "shady.tab.h"
%}

%%

"MOV"|"mov" { return T_MOV; }
"NOP"|"nop" { return T_NOP; }

...etc...

虚拟语法规则,我能够构建和运行这个只是:

Also, after adding a dummy grammar rule, I was able to build and run this with just:

  559  flex shady.l
  560  bison -d shady.y
  561  g++ shady.tab.c lex.yy.c

这篇关于简单Flex / Bison C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆