Question

I've coded a couple of parsers before using flex to create the lexer, but this time I wanted flex to generate a Lexer Class instead of the classic C file, i.e. I wanted flex to generate a C++ scanner class... the thing is that it is not generating the yyFlexLexer.h for me, only the lex.yy.cc

I've read the Flex manual C++ chapter... so idk what to do... is it that my flex for windows isn't not working properly?

These are the flex options I'm currently using:

%option outfile="cmdsLexer.cpp"
%option stdinit
%option case-insensitive
%option c++

%{
    #include "global-scope.h"
%}

ANDIGIT     [a-zA-Z0-9]
IDNTIFIER   [a-zA-Z_]({ANDIGIT}|_|-)*
INTEGER     [-+]?[0-9]+
STRING      \"[^\n\"]*\"
....
Was it helpful?

Solution

There is no yyFlexLexer.h file. Instead, the cmdsLexer.cpp file will contain a line

#include <FlexLexer.h>

Notice the use of angled brackets instead of double quotes. This file should be installed on your system, e.g. as /usr/include/FlexLexer.h. It is the same for every lexer. For Windows, the path will obviously be different. Simply find the file in your flex installation, and add it to the path your compiler searches for header files, e.g. using the -I option to gcc.

Also notice the last paragraph of the document you referenced for advanced use of that header:

If you want to create multiple (different) lexer classes, you use the ‘-P’ flag (or the prefix= option) to rename each yyFlexLexer to some other ‘xxFlexLexer’. You then can include <FlexLexer.h> in your other sources once per lexer class, first renaming yyFlexLexer as follows:

     #undef yyFlexLexer
     #define yyFlexLexer xxFlexLexer
     #include <FlexLexer.h>

     #undef yyFlexLexer
     #define yyFlexLexer zzFlexLexer
     #include <FlexLexer.h>

if, for example, you used %option prefix="xx" for one of your scanners and %option prefix="zz" for the other.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top