Question

I want to create a lex::token_def<> that contains character sequences like '[' or ']' or '&>'

I tried escaping the necessary characters:

namespace lex = boost::spirit::lex;

enum LexerIDs { ID_IDENTIFIER, ID_WHITESPACE, ID_INTEGER, ID_FLOAT, ID_PUNCTUATOR };

template <typename Lexer>
struct my_lexer : lex::lexer<Lexer>
{
    my_lexer() : punctuator("\[|\]|\(|\)|\.|&>|\*\*|\*|\+|-|~|!|/|%|<<|>>|<|>|<=|>=|==|!=")
    {
        this->self.add(punctuator, ID_PUNCTUATOR);
    }
    lex::token_def<> punctuator;
};

but this gives me some warning about unrecognized escaped characters & lexing a string with it fails. How can I do this correctly?

Was it helpful?

Solution

You need an extra escaping level:

my_lexer() : punctuator("\\[|\\]|\\(|\\)|\\.|&>|\\*\\*|\\*|\\+|-|~|!|/|%|<<|>>|<|>|<=|>=|==|!=")

"\\" is a string literal containing one backslash, which the lexer constructor then parses.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top