开发者

Flex&Bison: define main function in a separate file

I am trying to make a small interpreter using Flex and Bison.

Now I 开发者_如何学编程have two files: parser.l and parser.y. Usually, main function is put in parser.y file. What I want to do is to put the main function in a different file main.cpp which makes my package look neat.

#include "y.tab.h"

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

But when I compile, I got an error:

undefined reference to `main'

So I know there is something wrong to include y.tab.h.

Could you someone to tell me how to do it?

Solution

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

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


SO noted:

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 Noted

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜