【发布时间】:2014-10-17 06:51:27
【问题描述】:
我试图在语法的继承参数中传递语义动作。
在下面的非常基本的示例中,语法解析两个数字,我将语义操作(以 c++ lambda 的形式)传递给它,我希望在解析第一个数字时调用此操作。但是它没有调用,而是默默地忽略了,我想知道为什么会这样以及做这些事情的正确方法是什么。
#include <iostream>
#include <boost/spirit/include/qi.hpp>
using namespace std;
using namespace boost;
namespace qi = spirit::qi;
namespace phx = phoenix;
template <typename Iterator, typename Action>
struct two_numbers : qi::grammar<Iterator, void (Action const&)>
{
two_numbers() : two_numbers::base_type(start)
{
using namespace qi;
start = int_ [ _r1 ] >> ' ' >> int_;
}
qi::rule<Iterator, void (Action const&)> start;
};
int main ()
{
string input { "42 21" };
auto first=std::begin (input), last=std::end(input);
static const auto my_action = [] (auto&& p) {
cout << "the meaning of life is " << p << "\n";
};
static const two_numbers <decltype(first), decltype (my_action)> p;
if (qi::parse (first, last, p(phx::ref(my_action))))
cout << "parse ok\n";
}
预期的输出是:
the meaning of life is 42
parse ok
而真正的输出是:
parse ok
【问题讨论】:
标签: c++ parsing boost grammar boost-spirit