简体   繁体   English

boost::spirit::lex 问题 - 标点符号

[英]Trouble with boost::spirit::lex - punctuation characters

I want to create a lex::token_def<> that contains character sequences like '[' or ']' or '&>'我想创建一个 lex::token_def<> 包含像 '[' 或 ']' 或 '&>' 这样的字符序列

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?我怎样才能正确地做到这一点?

You need an extra escaping level:您需要一个额外的转义级别:

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

"\\\\" is a string literal containing one backslash, which the lexer constructor then parses. "\\\\"是一个包含一个反斜杠的字符串文字,词法分析器构造函数然后解析它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM