1

I'm trying to learn the basics of Boost::Spirit, and its not going well. I'm trying to parse a simple "logical and" expression written in c++ syntax. 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? :)

  • I asked a similar question some time ago. This should be helpful to you: http://stackoverflow.com/questions/14548592/boost-spirit-implement-small-one-line-dsl-on-a-server-application – Richard Hodges Jun 11 '16 at 16:52
  • Thanks a lot, it really was! I added the skipper as template parameter, and it works. I guess without it, my grammar somehow defaulted to an incorrect space skipper. – Milan Irigoyen Jun 11 '16 at 17:18

1 Answers1

2

Solved by adding the skipper as template parameter, as seen in @Richard Hodges earlier question :

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