开发者

Flex&Bison: define main function in a separate file

开发者 https://www.devze.com 2023-03-19 17:14 出处:网络
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 pu

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

精彩评论

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