升压::精神::齐定义nullaries计算器(Boost::spirit::qi defining

2019-07-21 21:40发布

我试图写数学表达式,其中命名变量是nullaries解析器boost::spirit (1_51_0版),对此我完全新的。 我定义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与下一期有关。 什么是这样定义的规则正确的语法?

Answer 1:

解析器表情看起来不错。

什么你感到困惑了是构建AST。 显然,你已经决定使用语义动作这样做,但你的努力过于简略,我看到的只是如何(或者甚至是决定什么样你在这个基础)。

从本质上说:它是什么,你想要的情况下 “ADD” /“子”,你似乎就神奇地 “将”到您的规则?

现在,你只需直接 使用实例作为语义动作。 这导致显示的错误信息,它告诉你,直接,该实例是不能作为语义动作有效

我假设你真的想用凤凰分配二进制运算分配给暴露的属性,来代替。 这看起来像:

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


文章来源: Boost::spirit::qi defining a calculator for nullaries