我试图获得字符串、int和float的直接规则,以便解析以下测试
//strings
"\"hello\"",
" \" hello \" ",
" \" hello \"\"stranger\"\" \" ",
//ints
"1",
"23",
"456",
//floats
"3.3",
"34.35"在线试用:http://coliru.stacked-crooked.com/a/26fbd691876d9a8f
使用
qi::rule<std::string::const_iterator, std::string()>
double_quoted_string = '"' >> *("\"\"" >> qi::attr('"') | ~qi::char_('"')) >> '"';
qi::rule<std::string::const_iterator, std::string()>
number = (+qi::ascii::digit >> *(qi::char_('.') >> +qi::ascii::digit));
qi::rule<std::string::const_iterator, std::string()>
immediate = double_quoted_string | number;给出正确的结果--但我需要使用double_解析,因为我想支持附属式表示法、NaN等等。
但使用
qi::rule<std::string::const_iterator, std::string()>
immediate = double_quoted_string | qi::uint_ | qi::double_;整数值的打印
"1" OK: ''
----
"23" OK: ''
----
"456" OK: '�'而双数不能完全解析
在Coliru,Win7x64 VS2017最新,LLVM clang下进行测试
有时Colliru发出太多警告,编译就会停止。
知道这里发生了什么吗?
精神上的警告通常意味着-什么东西坏了?
UPDATE:如果我只使用double_,在测试它之前,以及随/不使用uint_解析器而更改的行为:https://wandbox.org/permlink/UqgItWkfC2I8tkNF,也会发生这种情况。
发布于 2020-03-24 16:30:57
在整数和双浮点分析器上使用qi::raw,以便对数字进行词汇转换:qi::raw[qi::uint_]和qi::raw[qi::double_]。
但解析的顺序也很重要。如果uint_解析器在double_之前,如下所示:
immediate = double_quoted_string | qi::raw[qi::uint_] | qi::raw[qi::double_];
BOOST_SPIRIT_DEBUG_NODES((immediate)); // for debug output然后,uint_解析器将部分使用双浮点数,然后整个解析将失败:
<immediate>
<try>34.35</try>
<success>.35</success> //<----- this is what is left after uint_ parsed
<attributes>[[3, 4]]</attributes> // <---- what uint_ parser successfully parsed
</immediate>
"34.35" Failed
Remaining unparsed: "34.35"uint_与double_交换顺序后
immediate = double_quoted_string | qi::raw[qi::double_] | qi::raw[qi::uint_];
结果:
"\"hello\"" OK: 'hello'
----
" \" hello \" " OK: ' hello '
----
" \" hello \"\"stranger\"\" \" " OK: ' hello "stranger" '
----
"1" OK: '1'
----
"64" OK: '64'
----
"456" OK: '456'
----
"3.3" OK: '3.3'
----
"34.35" OK: '34.35'
----https://stackoverflow.com/questions/60832051
复制相似问题