I'm trying to use flex
and bison
in my project to generate a parser code for a file structure. Main programming language is C++ and project is on an OO design mainly running in parallel.
I heard that flex
and bison
generated parsers are C codes and they're not reenterant. Googling, I found flex++
and bisonc++
. Unfortunately there is no simple tutorial to get started. Most examples are based on bison/flex
. Some people somehow integrated bison/flex
parsers in their C++ code. They supposed to be "tricky"...
Documentation of flex++
and bisonc++
doesn't help me and. Tutorials and examples, they all get input from stdin and print some messages on stdout.
I need these features in my parser:
- Parser should be a C++ class, defined in normal manner (a header and a cpp file)
- Parser receives data from either an
std::string
orstd::stringstream
or a null-terminatedchar*
.
I feel so confused. Should I use flex++/bisonc++
or flex/bison
? And how to do that, satisfying above conditions?
The LRSTAR product (LR(k) parser and DFA lexer generator) is C++ based. Runs on Widowns and has six Visual Studio projects. The code also compiles with "gcc" and other compilers. There are classes for lexer and parser, symbol-table, AST. Complete source code is available. It gets good reviews. I should know. I am the author.
There are flex/bison, flex++/bison++ and flexc++/bisonc++. I think it's best to pick one of these three pairs, instead of mixing/matching flex++ and bisonc++. Here are the user guides for Flexc++ and Bisonc++.
From the Flexc++ website:
From the Bisonc++ website:
So flexc++/bisonc++ are more than just wrappers around the old flex/bison utilities. They generate complete C++ classes to be used for re-entrant scanning / parsing.
Flex can generate a reentrant C scanner. See Section 19 Reentrant C scanners in the Flex manual.
Similarly, Bison can generate a reentrant C parser. See Section 3.8.11 A Pure (Reentrant) Parser in the Bison manual for details.
Do you absolutely need to have a C++ parser and std::string/stringstream based parser data?
Have you looked at Boost.Spirit as an alternative?