2016-06-11 3 views
0

私はBoost :: Spiritの基本を学びたいと思っています。 私は、単純な "論理式"をC++構文で書かれた構文解析しようとしています。何らかの理由で、スペースを飛ばすことができません。ここに私のコードは私のテストコードでは、これまでBoost :: Spirit:基本的な論理と表現の構文解析

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

、私が呼ん:

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

誰でも:

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

そして案の定、私は素敵なコンソール出力を取得します初心者の間違いを指摘する気に? @Richardホッジスに見られるように、テンプレートパラメータとして、以前の質問スキッパーを追加することで解決:)

+0

を私はいくつかの時間前に同様の質問をしました。これはあなたに役立つはずです:http://stackoverflow.com/questions/14548592/boost-spirit-implement-small-one-line-dsl-on-a-server-application –

+0

本当にありがとう!私はテンプレートのパラメータとしてスキッパーを追加し、それは動作します。私はそれがなければ、私の文法は何とか間違った宇宙飛行士に不履行になったと思う。 –

答えて

1

template <typename Iterator, typename Skipper = boost::spirit::standard_wide::space_type> 
struct boolGrammar : public qi::grammar<Iterator, bool, Skipper> 
関連する問題