c++regexboost-spiritboost-spirit-lex

Boost::Spirit::Lex; How do I specify the token "||"?


So my question is quite simple, in my lexer class (extending lex::lexer<T>), I have the following;

this->self.add
    ...
    ("&&", AND_AND)
    ("||", OR_OR)
    ...

Inside my constructor, which compiles just fine, but triggers a SegFault on runtime. The issue is quite obvious | is the 'or' operator in the regex system, how do I get past this issue?


Solution

  • From http://www.boost.org/doc/libs/1_54_0/libs/spirit/doc/html/spirit/lex/quick_reference/lexer.html

    \X
    

    If X is a, b, e, n, r, f, t, v then the ANSI-C interpretation of \x. Otherwise a literal X (used to escape operators such as *)

    So you would use

     ("\\|\\|", OR_OR)
    

    The first backslash in each pair is treated as an escape character by the C++ string parser, causing the second one to be placed into the string content. That backslash in the string content then is seen by Spirit::Lex and acts to escape the regex operator.