我試圖在語法的繼承參數中傳遞語義動作。Boost :: spirit在繼承屬性中傳遞語義動作
在下面這個非常基本的例子中,語法分析兩個數字,並且將語義動作(以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
強制鏈接:[Boost Spirit:「語義動作是邪惡的嗎?」](http://stackoverflow.com/questions/8259440/boost-spirit-semantic-actions-are -vil) – sehe 2014-10-16 22:28:07