2012-07-31 16 views
0

我嘗試編寫一個Boost.Spirit解析器,解析應該表示像「print foo.txt」這樣的簡單命令的字符串。每次輸入滿足語法時,都應該調用語義操作。Boost.Spirit語義行爲解析字符串不起作用

下面是代碼:

template<class Iterator> 
struct my_parser : qi::grammar<Iterator, void(), qi::ascii::space_type> 
{ 
    void test(std::string const & s) const 
    { 
     std::cerr << s << std::endl; 
    } 

    my_parser() : my_parser::base_type(print) 
    { 
     using qi::_1; 
     using qi::char_; 

     filename = 
      *qi::char_("a-zA-Z_0-9./ ") 
      ; 

     print = 
      qi::lit("print") 
      >> filename [ boost::bind(&my_parser::test, this, _1) ] 
      ; 
    } 

    qi::rule<Iterator, std::string()> filename; 
    qi::rule<Iterator, void(), qi::ascii::space_type> print; 
}; 

如果我嘗試編譯此我得到的東西,如:

no match for call to ‘(const boost::_mfi::cmf1<void, parser::my_parser<__gnu_cxx::__normal_iterator<const char*, std::basic_string<char> > >, const std::basic_string<char>&>) (parser::my_parser<__gnu_cxx::__normal_iterator<const char*, std::basic_string<char> > >* const&, const boost::phoenix::actor<boost::spirit::argument<0> >&)’ 

如果我取代_1「ABC」例如代碼編譯,但對於輸入「print foo.txt」,phrase_parse()返回false。如果我註釋掉[boost:bind(...)] phrase_parse()返回true。

有誰知道我做錯了什麼?謝謝。

回答

2

我相信你的問題是,你要一個精神佔位符傳遞到boost::bind,一般只能用其內置的佔位符的工作(你已經隱藏與using qi::_1)。我會嘗試添加定義BOOST_SPIRIT_USE_PHOENIX_V3,添加#include "boost/phoenix.hpp",然後替換爲boost::phoenix::bind(&my_parser::test, this, _1)

+0

+1這將工作。我相信,簡單地刪除'using qi :: _ 1;'也可以工作,因爲它是[這裏]使用的方式(http://www.boost.org/doc/libs/1_50_0/libs/spirit/example/qi/actions。 CPP)。您可以在底部的「重要」部分找到[here](http://www.boost.org/doc/libs/1_50_0/libs/spirit/doc/html/spirit/qi/tutorials/semantic_actions.html)的頁面描述了問題。 – 2012-07-31 15:18:34