c++ - 升压::精神::莱克斯;如何指定 token "||"?

标签 c++ regex boost-spirit boost-spirit-lex

所以我的问题很简单,在我的词法分析器类(扩展 lex::lexer<T> )中,我有以下内容;

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

在我的构造函数中,它编译得很好,但在运行时触发了 SegFault。问题很明显|是正则表达式系统中的“或”运算符,我该如何解决这个问题?

最佳答案

来自 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 *)

所以你会用

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

每对中的第一个反斜杠被 C++ 字符串解析器视为转义字符,导致将第二个反斜杠放入字符串内容中。字符串内容中的反斜杠随后会被 Spirit::Lex 看到,并用来逃避正则表达式运算符。

关于c++ - 升压::精神::莱克斯;如何指定 token "||"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18346489/

相关文章:

python - 从列表中删除空项 (Python)

regex - 非常复杂的正则表达式

java - 为什么捕获组中的标志会删除该组?

c++ - 使用函数属性构造 qi::rule

c++ - 使用 boost Spirit x3 解析为集合

c++ - 实现 graph_coloring - m 着色问题的问题

c++ - 通过引用 C++ 初始化指针数组

c++ - std 或 boost 在某处有 tag<T> 或 type_t<T> 吗?

c++ - 显示QList内容

c++ - Boost.Spirit X3 解析器 "no type named type in(...)"