我需要解析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
,因为,因为我看到,它无法区别limit5days
从limit5hours
。