gpt4 book ai didi

c++ - Boost::Spirit:基本 "logical and"表达式解析

转载 作者:行者123 更新时间:2023-11-30 03:40:53 25 4
gpt4 key购买 nike

我正在尝试学习 Boost::Spirit 的基础知识,但进展并不顺利。我正在尝试解析用 C++ 语法编写的简单“逻辑与”表达式。出于某种原因,我无法让空格跳转工作。

到目前为止,这是我的代码

template <typename Iterator>
struct boolGrammar : public qi::grammar<Iterator, bool>
{
public:
boolGrammar() : boolGrammar::base_type(expression)
{
andExpr = (qi::lit(L"1") >> qi::lit(L"&&") >> qi::lit(L"1"))[qi::_val = true];
}
qi::rule<Iterator, bool> andExpr;
};

bool conditionEvalAndParse(std::wstring condition)
{
boolGrammar<std::wstring::iterator> g;
bool result = false;
std::wstring::iterator it = condition.begin();
bool parseResult = qi::phrase_parse(it, condition.end(), g, boost::spirit::standard_wide::space , result);

if (parseResult) {
return result;
}
else
{
std::wcout << L"Failed to parse condition " << condition << L". The following wasn't parsed : " << std::wstring(condition, it - condition.begin(), std::wstring::npos) << std::endl;
return false;
}
}

在我的测试代码中,我调用:

conditionEvalAndParse(L"1&&1");
conditionEvalAndParse(L"1 && 1");

果然,我得到了一个可爱的控制台输出:

"Failed to parse condition 1 && 1. The following wasn't parsed : 1 && 1"

有人愿意指出新手的错误吗? :)

最佳答案

通过添加 skipper 作为模板参数解决,如@Richard Hodges 先前的问题所示:

template <typename Iterator, typename Skipper = boost::spirit::standard_wide::space_type>
struct boolGrammar : public qi::grammar<Iterator, bool, Skipper>

关于c++ - Boost::Spirit:基本 "logical and"表达式解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37766187/

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