我需要解析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) ] ;
我试图将语义动作移到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
不同。
答案 0 :(得分:3)
这里有很多事情要发生。然而,当我完成一些小东西试图阅读并完成它(SSCCE)时,它也被编译: Live On Coliru
我在这里简要阐述一些个别观点:
m_lTree
中的绑定表达式的值,这样您就无法改变成员。此处需要phx::ref
lit
,除非您确实想要将字符串的值作为属性使用。更好的是,只需编写"limit" >> uint_ >> "days"
,因为重载重载完成剩下的工作正如您已经注意到这种语法中存在多种反模式,使其变得复杂:
所有人都说,我通过 avoiding semantic actions 来简化事情,并将解析和评估分开。
只需解析为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
}
但最重要的是:它极大地简化了语法并节省了你的生活时间,而不是花费更多时间
请参阅 Live On 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);
}
}