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
oryyout
since you don't (yet) reference them from the file containingmain()
. 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. They.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.
精彩评论