c++ - How to inherit from yyFlexLexer? -
i'm writing trying learn flex / bison. i've got basic c examples now, i'd move on doing c++ ast tree. c++ makes type of object oriented program easier c. however, there seems issue c++ generation flex , i'm unsure how resolve it. want add methods warning/error reporting going inherit yyflexlexer , in '.l' file call stuff warning(const char* str) , error(const char* str) appropriate.
however, 'yyflexlexer redefinition' error among others when try perform inheritance in way believe documentation says 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 identifiers inside yyflexlexer don't exist.
i post generated cpp file 1500 line auto-generated mess.
edit: apparently issue macrodefinition of yyflexlexer can generate different base classes xxflexlexer , on. if need 1 lexer in project (probable) can following work. if has way better let me know.
#ifndef nlexer_h #define nlexer_h #undef yyflexlexer #include <flexlexer.h> class nlexer : public yyflexlexer { public: virtual int yylex(); }; #endif
inside generated lexer.yy.cc
file can find old comment problem:
/* c++ scanner mess. flexlexer.h header file relies on * following macro. required in order pass c++-multiple-scanners * test in regression suite. we reports breaks inheritance. * address in future release of flex, or omit c++ scanner * altogether. */
#define yyflexlexer yyflexlexer
yyflexlexeronce
include guard used overcome it. nlexer.h
:
#ifndef nlexer_h #define nlexer_h #if !defined(yyflexlexeronce) #include <flexlexer.h> #endif class nlexer : public yyflexlexer { public: virtual int yylex(); }; #endif
Comments
Post a Comment