Search code examples
c++flex-lexer

How do I get reflex to do backtracking when generating a flex scanner?


I've come up with a backtracking regex that works out fine on https://regex101.com. When I compile a simple flex(ish) program with reflex -u, the expression doesn't capture the backtracking bit -- everything else works just fine.

The documentation for reflex indicates that it will allow backtracking out of the box and I can't find anything in the documentation that tells me how to allow backtracking when using reflex as a replacement for flex.

As far as I can tell from the documentation, I have to make reflex use std::regex, since that's the only engine that supports backtracking and it seems trivial to do in a regular C++ program.

(*) Regular old flex is not an option, since it doesn't support unicode classes; specifically it doesn't support recognizing uppercase letters.


Solution

  • Backtracking reflex scanners require Boost in Perl mode. Use reflex -m=boost-perl to switch to the Boost Perl regex engine. See the RE/flex documentation section on POSIX versus Perl matching: https://www.genivia.com/doc/reflex/html/index.html#reflex-posix-perl.