4

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);
Lesmana
  • 25,663
  • 9
  • 82
  • 87
deryk
  • 131
  • 2
  • 7
  • I retagged this from Adobe Flex to Gnu-Flex. – JeffryHouser Jul 11 '11 at 19:03
  • 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. – Jonathan Leffler Jul 11 '11 at 20:11

1 Answers1

1

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.

Qix - MONICA WAS MISTREATED
  • 14,451
  • 16
  • 82
  • 145
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129