gpt4 book ai didi

c++ - Boost Spirit:无法让替代运算符(operator)按我的预期工作

转载 作者:行者123 更新时间:2023-12-03 07:15:22 26 4
gpt4 key购买 nike

我无法让精神替代解析器正常工作(或者让我们做我期望的事情)。
这是我的MCVE

#include <string>
#include <iostream>

#include <boost/variant.hpp>
#include <boost/spirit/include/qi_parse.hpp>
#include <boost/spirit/include/qi.hpp>

using namespace boost::spirit;
namespace charset = boost::spirit::qi::standard_wide;

using Expr = boost::variant<std::string, int >;

int main(int argc, char ** argv)
{
std::string intToParse = "{int: 45}";
std::string stringToParse = "{string: \"foo\"}";

using It = std::string::const_iterator;
using Sk = qi::space_type;
qi::rule<It, std::string(), Sk> parseString;
qi::rule<It, int(), Sk> parseInt;
qi::rule<It, std::string(),Sk> string_;

qi::rule<It,Expr(),Sk > orParse;


string_ =
qi::lit('"')
> *(charset::char_ - '"')
> '"';

parseString = qi::omit[ qi::string("string")] > qi::lit(':') > string_;

parseInt = qi::omit[ qi::string("int")] > qi::lit(':') > qi::uint_ ;

orParse = qi::omit[qi::lit('{')] > parseString | parseInt > '}';

orParse.name_ = "alternative parser";
parseString.name_ = "string parser";
parseInt.name_= "int parser";

qi::debug(orParse);
qi::debug(parseString);
qi::debug(parseInt);

Expr res2;
qi::phrase_parse(stringToParse.cbegin(), stringToParse.cend(), orParse, qi::space, res2);
std::cout << res2<< std::endl;


Expr res;
qi::phrase_parse(intToParse.cbegin(), intToParse.cend(), orParse, qi::space, res);
std::cout << res << std::endl;

return 0;
}
当第一个选择失败时,解析停止,不尝试第二个规则!我怎么了?
这是显示我的问题的输出,对于stringToParse,第一个选择失败,但随后我希望执行int解析器,情况并非如此!
<alternative parser>
<try>{string: "foo"}</try>
<string parser>
<try>string: "foo"}</try>
<success>}</success>
<attributes>[[f, o, o]]</attributes>
</string parser>
<success>}</success>
<attributes>[[f, o, o]]</attributes>
</alternative parser>
foo
<alternative parser>
<try>{int: 45}</try>
<string parser>
<try>int: 45}</try>
<fail/>
</string parser>
<fail/>
</alternative parser>
terminate called after throwing an instance of 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::spirit::qi::expectation_failure<__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > >'
what(): boost::spirit::qi::expectation_failure
Aborted (core dumped)

最佳答案

C++ Operator Precedence表中的operator >operator |高,因此需要括号:

orParse =  qi::omit[qi::lit('{')] >  (parseString | parseInt) >  '}';
// here ^ and here ^
结果:
<alternative parser>
<try>{string: "foo"}</try>
<string parser>
<try>string: "foo"}</try>
<success>}</success>
<attributes>[[f, o, o]]</attributes>
</string parser>
<success></success>
<attributes>[[f, o, o]]</attributes>
</alternative parser>
foo
<alternative parser>
<try>{int: 45}</try>
<string parser>
<try>int: 45}</try>
<fail/>
</string parser>
<int parser>
<try>int: 45}</try>
<success>}</success>
<attributes>[45]</attributes>
</int parser>
<success></success>
<attributes>[45]</attributes>
</alternative parser>
45

关于c++ - Boost Spirit:无法让替代运算符(operator)按我的预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64719130/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com