'How to access FlexLexer's input function without deriving from yyFlexLexer?
I am writing a simple compiler for a simple C like language. To do that I would like to use Flex and Bison but use them both in the context of modern C++ Right now my setup consists of a main.cpp which just instantiates the scanner and parser and starts parsing
// some includes here
#include <FlexLexer.h>
#include "parser.tab.hh"
FlexLexer* scanner;
int main(int argc, char **argv) {
yy::parser p;
scanner = new yyFlexLexer;
// boilerplate
if (!p()) std::cout << "No errors detected\n";
}
scanner.l
%option c++ noyywrap
D [0-9]
L [a-zA-Z_]
H [a-fA-F0-9]
E [Ee][+-]?{D}+
FS (f|F|l|L)
IS (u|U|l|L)*
%{
// Several includes
#include "parser.tab.hh"
using token = yy::parser::token;
void count();
void comment();
int check_type();
void add_symbol(void);
void yyerror(const char *);
extern std::map<std::string, std::vector<int>> symbol_table;
extern FlexLexer* scanner;
%}
%%
"/*" { comment(); }
// skipped for brevity
%%
void comment(void)
{
int c;
while ((c = scanner->input()) != 0)
if (c == '*')
{
while ((c = scanner->input()) == '*');
if (c == '/')
return;
if (c == 0)
break;
}
yyerror("unterminated comment");
}
and finally parser.yy which looks like the Ansi C yacc grammar with the addition of this at the top
%require "3.2"
%language "c++"
%skeleton "lalr1.cc"
%{
extern int yylex();
%}
%define api.token.constructor
%define api.value.type variant
%define parse.assert
...
This setup does not compile of course as FlexLexer
does not have a member called input()
, however instantiating yyFlexLexer is also not an option as the input()
function of that class is protected and not usable.
The Flex manual states one must use yyinput()
when generating C++ code in flex and uses it as a free standing function which is not available for some reason on my setup.
https://ftp.gnu.org/old-gnu/Manuals/flex-2.5.4/html_node/flex_19.html
My question is thus: Is it possible, and if so how, to access the default input()
functionality when also using the C++ option of Flex, without resorting to creating a separate Lexer class that derives from yyFlexLexer?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|