「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"に置き換えると、コードはコンパイルされますが、phrase_parse() は入力"print foo.txt"に対して false を返します。[boost:bind(...)] phrase_parse() をコメントアウトすると、true が返されます。
誰かが私が間違っていることを知っていますか? ありがとう。