加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Flex&Bison:在单独的文件中定义主要功能

发布时间:2020-12-15 01:45:11 所属栏目:百科 来源:网络整理
导读:我正在尝试使用Flex和Bison创建一个小型翻译. 现在我有两个文件:parser.l和parser.y.通常,main函数放在parser.y文件中.我想要做的是将main函数放在另一个main.cpp文件中,这使得我的包看起来很整洁. #include "y.tab.h"int main(){ yyparse(); return 0;} 但
我正在尝试使用Flex和Bison创建一个小型翻译.

现在我有两个文件:parser.l和parser.y.通常,main函数放在parser.y文件中.我想要做的是将main函数放在另一个main.cpp文件中,这使得我的包看起来很整洁.

#include "y.tab.h"

int main()
{
   yyparse();
   return 0;
}

但是当我编译时,我收到了一个错误:

undefined reference to `main'

所以我知道包含y.tab.h有什么问题.

你能告诉我怎么做吗?

我只是想通了:
将以下内容添加到main.c文件中:

extern FILE *yyin;
    extern FILE *yyout;
    extern int yyparse(void);

解决方法

所以注意到:

I just figured it out: add the following to your main.c file:

extern FILE *yyin;
extern FILE *yyout;
extern int yyparse(void);

@Jonathan Leffler注意到

You don’t really need yyin or yyout since you don’t (yet) reference
them from the file containing main(). However,if you end up doing
work such as reading from files specified on the command line instead
of standard input,you may need them. It would be nice if Bison
generated a header with the appropriate declarations in it. The
y.tab.h file is not,however,the place for that information; it is used to convey information between the parser and the lexical analyzer,not between the application and the parser.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读