Boost :: spirit :: qi定义了计算器的nullaries

时间:2013-03-18 20:41:37

标签: c++ boost boost-spirit boost-spirit-qi abstract-syntax-tree

我正在尝试为数学表达式编写一个解析器,其中命名变量是boost::spirit(版本1_51_0)中的nullaries,我完全是新的。我定义typedef boost::function<double()> Value,我的规则将如此声明:qi::rule<Iterator, Value()> expression, term, others, ...;

我使用此宏

在nullaries上定义二元运算符
#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_;                                         \
}; 

并拥有ADDSUB等。从我看到的示例中,我希望规则的定义如下:

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。定义这样的规则的正确语法是什么?

1 个答案:

答案 0 :(得分:5)

解析器表达式看起来没问题。

你感到困惑的是构建AST。显然,你已经决定使用Semantic Actions来做到这一点,但你的努力太过粗略,我无法看到如何(甚至决定你基于什么样的样本)。

本质上:你想用“ADD”/“SUB”实例做什么,你似乎魔法“将”进入你的规则?

现在,您只需直接使用实例作为语义操作。这会导致显示的错误消息直接告诉您实例无效作为语义操作。

我假设您确实希望使用Phoenix赋值将二进制操作分配给公开的属性。这看起来像:

expression = term
     >> *( (lit('+') >> term[ _val = phx::construct<ADD>(_val, _1)])
         | (lit('-') >> term[ _val = phx::construct<SUB>(_val, _1)])
         );

你会发现这与传统的表达式语法更为匹配。

为了好玩,我根据您的Value类型修改了一个完整的表达式解析器,并创建了这个工作演示: http://liveworkspace.org/code/3kgPJR$0

#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/karma.hpp>
#include <boost/spirit/include/phoenix.hpp>

namespace qi    = boost::spirit::qi;
namespace karma = boost::spirit::karma;
namespace phx   = boost::phoenix;

typedef std::function<double()> Value;

#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_;                                         \
}; 

BINARY_FUNCTOR(ADD, +)
BINARY_FUNCTOR(SUB, -)
BINARY_FUNCTOR(MUL, *)
BINARY_FUNCTOR(DIV, /)

struct LIT
{
  LIT(double x): x_(x) {}
  double operator()() { return x_; }
  double x_;
}; 

struct NEG
{
  NEG(Value x): x_(x) {}
  double operator()() { return -x_(); }
  Value x_;
}; 


template <typename It, typename Skipper = qi::space_type>
    struct parser : qi::grammar<It, Value(), Skipper>
{
    parser() : parser::base_type(expression)
    {
        using namespace qi;
        expression =
            term                    [_val = _1]
            >> *( ('+' >> term  [_val = phx::construct<ADD>(_val, _1)])
                | ('-' >> term  [_val = phx::construct<SUB>(_val, _1)])
                );

        term =
            factor                [_val = _1]
            >> *( ('*' >> factor  [_val = phx::construct<MUL>(_val, _1)])
                | ('/' >> factor  [_val = phx::construct<DIV>(_val, _1)])
                );

        factor =
            double_               [_val = phx::construct<LIT>(_1)]
            |   '(' >> expression [_val = _1] >> ')'
            |   ('-' >> factor    [_val = phx::construct<NEG>(_1)])
            |   ('+' >> factor    [_val = _1]);

        BOOST_SPIRIT_DEBUG_NODE(expression);
        BOOST_SPIRIT_DEBUG_NODE(term);
        BOOST_SPIRIT_DEBUG_NODE(factor);
    }

  private:
    qi::rule<It, Value(), Skipper> expression, term, factor;
};

Value doParse(const std::string& input)
{
    typedef std::string::const_iterator It;
    parser<It, qi::space_type> p;
    Value eval;

    auto f(begin(input)), l(end(input));

    if (!qi::phrase_parse(f,l,p,qi::space,eval))
        std::cerr << "parse failed: '" << std::string(f,l) << "'\n";
    if (f!=l) 
        std::cerr << "trailing unparsed: '" << std::string(f,l) << "'\n";

    return eval;
}

int main()
{
    auto area = doParse("2 * (3.1415927 * (10*10))");
    std::cout << "Area of a circle r=10: " << area() << "\n";
}

会打印

Area of a circle r=10: 628.319