flex
flex copied to clipboard
yy_init in c++ mode
In the generated lexer the variable yy_init is not defined, when %option c++ is given, or when the lexer is generated with the -+ cli option. When that generated lexer is compiled, the compiler rightfully complains about yy_init used, but not declared/defined.
flex version 2.6.4, linux system
more details:
> flex -o lexer.cc src/mkninja.lex
> cat lexer.cc | grep --color -n yy_init
337:static int yy_init = 0; /* whether we need to initialize */
358:static void yy_init_buffer ( YY_BUFFER_STATE b, FILE *file );
582:static int yy_init_globals ( void );
800: if ( !(yy_init) )
802: (yy_init) = 1;
1461: yy_init_buffer( YY_CURRENT_BUFFER, input_file );
1552: yy_init_buffer( b, file );
1588: static void yy_init_buffer (YY_BUFFER_STATE b, FILE * file )
1605: /* If b is the current buffer, then yy_init_buffer was _probably_
1989:static int yy_init_globals (void)
1999: (yy_init) = 0;
2036: yy_init_globals( );
> flex -+ -o lexer.cc src/mkninja.lex
> cat lexer.cc | grep --color -n yy_init
659: if ( !(yy_init) )
661: (yy_init) = 1;
1018: yy_init = 0;
1472: yy_init_buffer( YY_CURRENT_BUFFER, input_file );
1575: yy_init_buffer( b, file );
1623: void yyFlexLexer::yy_init_buffer( YY_BUFFER_STATE b, std::istream& file )
1638: /* If b is the current buffer, then yy_init_buffer was _probably_
g++ -std=c++17 -MMD -I src -I tmp -g -ggdb3 -MF obj/lexer.o.d -c lexer.cc -o obj/lexer.o
tmp/lexer.cc: In function ‘yy::parser::symbol_type yylex(driver&)’:
tmp/lexer.cc:659:9: error: ‘yy_init’ was not declared in this scope
if ( !(yy_init) )
^~~~~~~
I just found out that if I omit the -+ option, therefore running in C mode, the C++ compiler is happy with the generated code. It seems for now that the work around is to simply omit the -+ or % option c++.
... weird...
The yy_init
references you show from the generated C++ lexer are for the yyFlexLexer
member in FlexLexer.h
. Is it possible your C++ lexer class does not have yyFlexLexer
as a base class? What flex options are in use?
I don't recall. I have since moved on to re/flex. ( https://www.genivia.com/doc/reflex/html/ ). That code is so much nicer, especially with regard to C++. Sorry.
I have run into the exact same problem. I had a compiling .y
file and after adding %option c++
the generated .cpp
does not compile anymore:
error: ‘yy_init’ was not declared in this scope
error: ‘yy_start’ was not declared in this scope
error: ‘yyin’ was not declared in this scope
error: ‘yyout’ was not declared in this scope
error: ‘yy_buffer_stack’ was not declared in this scope
I did not use any special options: %option c++ %option noyywrap
Compilation was also straightforward: flex -o file.cpp file.l
Are you simply trying to compile a C lexer as C++? In that case you should not use %option c++
- this option requires you to use yyFlexLexer
class from /usr/include/FlexLexer.h
either directly or as a base class for your C++ lexer class