简体   繁体   English

Boost :: Spirit:基本的“逻辑和”表达式解析

[英]Boost::Spirit : Basic “logical and” expression parsing

I'm trying to learn the basics of Boost::Spirit, and its not going well. 我正在尝试学习Boost :: Spirit的基础知识,但进展不太顺利。 I'm trying to parse a simple "logical and" expression written in c++ syntax. 我正在尝试解析一个用c ++语法编写的简单“逻辑和”表达式。 And for some reason, I can't get the space skipping to work. 由于某种原因,我无法跳过该空间。

Here's my code so far 到目前为止,这是我的代码

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;
    }
}

In my test code, I call : 在测试代​​码中,我致电:

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

And sure enough, I get a lovely console output : 确实,我得到了一个可爱的控制台输出:

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

Anyone care to point out a newbie's mistake? 有人在乎指出新手的错误吗? :) :)

Solved by adding the skipper as template parameter, as seen in @Richard Hodges earlier question : 通过将船长添加为模板参数来解决,如@Richard Hodges先前的问题所示:

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

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

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