【发布时间】:2013-03-18 20:41:36
【问题描述】:
我正在尝试为数学表达式编写一个解析器,其中命名变量是 boost::spirit(版本 1_51_0)中的空值,对此我是全新的。我定义了typedef boost::function<double()> Value,我的规则将像这样声明:qi::rule<Iterator, Value()> expression, term, others, ...;
我用这个宏在空值上定义二元运算符
#define BINARY_FUNCTOR(name, op) \
struct name \
{ \
name(Value x, Value y): x_(x), y_(y) {} \
double operator()() { return x_() op y_(); } \
Value x_, y_; \
};
并有ADD、SUB 等。从我看到的示例中,我希望规则是这样定义的:
expression = term
>> *( (lit('+') >> term[ADD(_val, _1)])
| (lit('-') >> term[SUB(_val, _1)])
);
但这似乎不是正确的语法,因为我收到错误
boost/spirit/home/support/action_dispatch.hpp:162: error: no match for call to ‘(const<unnamed>::SUB) (boost::function<double ()()>&, boost::spirit::context<boost::fusion::cons<boost::function<double ()()>&, boost::fusion::nil>, boost::fusion::vector0<void> >&, bool&)’
SRParser.cpp:38: note: candidates are: double<unnamed>::SUB::operator()()
在我看来,_1 并不是我所期望的那样,即与下一个术语相关联的 Value。定义这样的规则的正确语法是什么?
【问题讨论】:
-
这里不是关于语法,而是关于语义。实际上,语义动作。有关
[和]之间预期内容的文档,请参阅boost.org/doc/libs/1_48_0/libs/spirit/doc/html/spirit/qi/…。请参阅我对工作演示的回答 :)
标签: c++ boost boost-spirit boost-spirit-qi abstract-syntax-tree