简体   繁体   English

升压::精神::莱克斯; 如何指定令牌“||”?

[英]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; 所以我的问题很简单,在我的lexer类(扩展lex::lexer<T> )中,我有以下内容;

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

Inside my constructor, which compiles just fine, but triggers a SegFault on runtime. 在我的构造函数中,编译得很好,但在运行时触发SegFault。 The issue is quite obvious | 这个问题是很明显| is the 'or' operator in the regex system, how do I get past this issue? 是正则表达式系统中的'或'运算符,我该如何解决这个问题?

From http://www.boost.org/doc/libs/1_54_0/libs/spirit/doc/html/spirit/lex/quick_reference/lexer.html 来自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. 如果X是a,b,e,n,r,f,t,v则是\\ x的ANSI-C解释。 Otherwise a literal X (used to escape operators such as *) 否则为文字X(用于转义运算符,如*)

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. C ++字符串解析器将每对中的第一个反斜杠视为转义字符,从而将第二个反斜杠放入字符串内容中。 That backslash in the string content then is seen by Spirit::Lex and acts to escape the regex operator. 然后,字符串内容中的反斜杠被Spirit :: Lex看到并且用于逃避正则表达式运算符。

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

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