c++ - Boost::spirit::qi 定义一个 nullaries 的计算器

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

我正在尝试为数学表达式编写一个解析器,其中命名变量是 boost::spirit 中的 nullaries| (版本 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_;                                         \
}; 

并且有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与下一个术语相关联。定义这样的规则的正确语法是什么?

最佳答案

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

您对构建 AST 感到困惑。显然,您已决定使用语义操作来执行此操作,但您的努力太粗略了,我看不出究竟如何(甚至无法决定您基于哪个样本)。

本质上:您想对“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

关于c++ - Boost::spirit::qi 定义一个 nullaries 的计算器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15486531/

相关文章:

c++ - 如何确保此 qi 解析器不允许点运算符之间有空格?

c++ - 将 Base1 指针转换为 Base2 指针(boost shared_ptr)

c++ - 宽字符串 Visual Studio 2005 与 2017?

c++ - 如何使用 boost::any_cast 转换为基本类型?

c++ - 路径变得太长的解决方法?

c++ - boost xml_parser 以格式化空标签

c++ - 从 python 中提取类

boost-spirit - 使用 Spirit.Qi 消除语法糖

c++ - 如何编写 boost::spirit::qi 解析器来执行 '?' 在正则表达式中所做的事情?

c++ - C 错误 "s1, s2 are used uninitialised in this function"