简单表达的boost ::精神(Simple expression with boost::spir

2019-10-20 13:04发布

我需要解析simple_expression ::= limit int_number (days | hours | minutes) 。 我写代码的语法

struct Parser: grammar<std::string::const_iterator, boost::spirit::ascii::space_type>
{
public:
  Parser(ConditionTree& a_lTree):
    Parser::base_type(limit_expression),
    m_lTree(a_lTree)
  {
    using boost::spirit::qi::uint_;

    using boost::spirit::qi::_1;
    using boost::spirit::qi::_2;

    limit_expression = limit_days_operator | limit_hours_operator | limit_minutes_operator ;

    limit_days_operator = ( string("limit") > uint_ > string("days") )[ phoenix::bind( &ConditionTree::AddDaysLimitOperator, m_lTree, _2) ]  ;
    limit_hours_operator = ( string("limit") > uint_ > string("hours") )[ phoenix::bind( &ConditionTree::AddHoursLimitOperator, m_lTree, _2) ]  ;
    limit_minutes_operator = ( string("limit") > uint_ > string("minutes") )[ phoenix::bind( &ConditionTree::AddMinutesLimitOperator, m_lTree, _2) ] ;

    BOOST_SPIRIT_DEBUG_NODE(limit_expression);

    BOOST_SPIRIT_DEBUG_NODE(limit_days_operator);
    BOOST_SPIRIT_DEBUG_NODE(limit_hours_operator);
    BOOST_SPIRIT_DEBUG_NODE(limit_minutes_operator);
  }

  rule<std::string::const_iterator, boost::spirit::ascii::space_type> limit_expression;
  rule<std::string::const_iterator, boost::spirit::ascii::space_type> limit_days_operator;
  rule<std::string::const_iterator, boost::spirit::ascii::space_type> limit_hours_operator;
  rule<std::string::const_iterator, boost::spirit::ascii::space_type> limit_minutes_operator;

  ConditionTree& m_lTree;
}


void main()
{

  ConditionTree oTree;

  Parser parser(oTree);

  std::string strTest("limit5minutes");

  std::string::const_iterator it_begin(strTest.begin());
  std::string::const_iterator it_end(strTest.end());

  bool result = phrase_parse(it_begin, it_end, parser, space);
}

:但它不能与下面的2个错误编译/usr/include/boost/spirit/home/support/argument.hpp:103: ошибка: no matching function for call to 'assertion_failed(mpl_::failed************ (boost::spirit::result_of::get_arg<boost::fusion::vector1<unsigned int&>, 1>::index_is_out_of_bounds::************)())'

/usr/include/boost/spirit/home/phoenix/bind/detail/member_function_ptr.hpp:103: ошибка: invalid initialization of reference of type 'const unsigned int&' from expression of type 'mpl_::void_'

上线limit_days_operator = ( string("limit") > uint_ > string("days") )[ phoenix::bind( &ConditionTree::AddDaysLimitOperator, m_lTree, _2) ] ;

我tryed语义动作移动到uint_:

limit_days_operator = string("limit") > uint_ [ phoenix::bind( &ConditionTree::AddDaysLimitOperator, m_lTree, _1) ] > string("days")  ;
limit_hours_operator = string("limit") > uint_ [ phoenix::bind( &ConditionTree::AddHoursLimitOperator, m_lTree, _1) ] > string("hours")  ;

然后解析器读取正确limit5days ,但不能正确limit5minutes ,因为,因为我看到,它无法区别limit5dayslimit5hours

Answer 1:

有很多怎么回事。 然而,当时我做了固定的小东西想读它,让它完整(SSCCE),它编译过: 住在Coliru

我这里简要阐明一些个别点:

  • 在语义动作绑定表达式复制m_lTree的价值,这种方式可以发生变异的成员。 phx::ref在这里需要
  • 预期点使它不可能成功解析其他什么比“天”的限制
  • lit ,除非你真的想消耗字符串作为属性的值。 更重要的是,只写"limit" >> uint_ >> "days" ,因为超载超载不休息

正如你已经注意到有反模式的数量在这个语法,使它复杂:

  • 提高精神:“语义行为是邪恶的”?
  • 所述参考型构件 m_lTree导致距作用和紧密耦合。

然而:

这一切都表示,我会通过简化的东西,避免语义动作 ,分离分析和评价。 简单解析成一个Limit结构:

qi::rule<Iterator, ConditionTree::Limit(), Skipper> limit_expression;

limit_expression = "limit" >> qi::uint_ >> unit_;

处理而无需单独表达分支单元:

unit_.add("days",    ConditionTree::Limit::days)
         ("hours",   ConditionTree::Limit::hours)
         ("minutes", ConditionTree::Limit::minutes);

因为,现在,语法只是解析Limit对象,你可以做解析后评价原子

ConditionTree::Limit limit;
if (phrase_parse(iter, end, parser, ascii::space, limit))
{
    AddLimit(oTree, limit);
}

分离评价分析,可以添加喜欢的东西

  • 相同的表达的重复评估(不重复解析)
  • 简化评估前的表达式树
  • 评估前验证
  • 调试
  • 稍微更有趣的是,允许你写ApplyLimit在实用的风格,而不是突变的对象:

     ConditionTree ApplyLimit(ConditionTree const& ct, Limit limit) { return ct + limit; // do something here } 
  • 但最重要的是:它巨大的简化语法,并节省您的生活的时间是比较好,否则花

看到这个住在Coliru ,输出:

void AddLimit(ConditionTree&, ConditionTree::Limit): 5 minutes

清单

#include <boost/fusion/adapted/struct.hpp>
#include <boost/spirit/include/qi.hpp>

namespace qi    = boost::spirit::qi;
namespace ascii = boost::spirit::ascii;

struct ConditionTree {
    struct Limit {
        unsigned value;
        enum unit_t { days, hours, minutes } unit;
    };

    friend void AddLimit(ConditionTree& ct, Limit limit) {
        std::cout << "AddLimit: " << limit.value;
        switch (limit.unit) {
            case Limit::days:    std::cout << " days\n"; break;
            case Limit::hours:   std::cout << " hours\n"; break;
            case Limit::minutes: std::cout << " minutes\n"; break;
        }
    }
};

BOOST_FUSION_ADAPT_STRUCT(ConditionTree::Limit, (unsigned,value)(ConditionTree::Limit::unit_t,unit))

template <typename Iterator = std::string::const_iterator, typename Skipper = ascii::space_type>
struct Parser: qi::grammar<Iterator, ConditionTree::Limit(), ascii::space_type>
{
    public:
        Parser() : Parser::base_type(limit_expression)
    {
        unit_.add("days", ConditionTree::Limit::days)
                 ("hours", ConditionTree::Limit::hours)
                 ("minutes", ConditionTree::Limit::minutes);

        limit_expression = "limit" >> qi::uint_ >> unit_;
    }

    qi::symbols<char, ConditionTree::Limit::unit_t> unit_;
    qi::rule<Iterator, ConditionTree::Limit(), Skipper> limit_expression;
};

int main()
{
    ConditionTree oTree;

    Parser<> parser;

    std::string strTest("limit5minutes");

    std::string::const_iterator iter(strTest.begin()), end(strTest.end());

    ConditionTree::Limit limit;
    if (phrase_parse(iter, end, parser, ascii::space, limit))
    {
        AddLimit(oTree, limit);
    }
}


文章来源: Simple expression with boost::spirit