I'm writing trying to learn flex / bison. I've got some basic c examples now, but I'd like to move on to doing a C++ AST tree. c++ makes this type of object oriented program easier than C. However, there seems to be an issue with the c++ generation from Flex and I'm unsure how to resolve it. I want to add some methods for warning/error reporting so I was going to inherit from yyFlexLexer and in my '.l' file call stuff like warning(const char* str) and error(const char* str) where appropriate.
However, I get a 'yyFlexLexer redefinition' error among others when I try to perform inheritance in the way I BELIEVE the documentation says to do this.
lexer.l
%option nounistd
%option noyywrap
%option c++
%option yyclass="NLexer"
%{
#include "NLexer.h"
#include <iostream>
using namespace std;
%}
%%
[ \t]+
\n { return '\n';}
[0-9]+(\.[0-9]+)? { cout << "double: " << atof(YYText()) << endl;}
. {return YYText()[0];}
%%
int main(int , char**)
{
NLexer lexer;
while(lexer.yylex() != 0) { };
return 0;
}
NLexer.h
#ifndef NLEXER_H
#define NLEXER_H
#include <FlexLexer.h>
class NLexer : public yyFlexLexer
{
public:
virtual int yylex();
};
#endif
Lots of errors:
Error 1 error C2011: 'yyFlexLexer' : 'class' type redefinition c:\users\chase_l\documents\visual studio 2013\projects\nlanguage\nlanguage\include\flexlexer.h 112 1 NLanguage
Error 2 error C2504: 'yyFlexLexer' : base class undefined c:\users\chase_l\documents\visual studio 2013\projects\nlanguage\nlanguage\nlexer.h 6 1 NLanguage
About 80 more related to identifiers inside yyFlexLexer don't exist.
I could post the generated cpp file but it is a 1500 line auto-generated mess.
Edit: Apparently it is an issue with the MacroDefinition of yyFlexLexer so it can generate different base classes xxFlexLexer and so on. If you only need 1 lexer in your project (probable) you can just do the following to get it to work. If someone has a way that is better than that let me know.
#ifndef NLEXER_H
#define NLEXER_H
#undef yyFlexLexer
#include <FlexLexer.h>
class NLexer : public yyFlexLexer
{
public:
virtual int yylex();
};
#endif