问题
I need to parse simple_expression ::= limit int_number (days | hours | minutes)
. I wrote code for grammar
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);
}
But it can't compile with following 2 errors:
/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_'
on line
limit_days_operator = ( string("limit") > uint_ > string("days") )[ phoenix::bind( &ConditionTree::AddDaysLimitOperator, m_lTree, _2) ] ;
I tryed to move semantic action to 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") ;
Then the parser correctly read limit5days
, but not correctly limit5minutes
, because, as I see, it can't differ limit5days
from limit5hours
.
回答1:
There's a lot going on here. However, by the time I was done fixing up little things trying to read it and make it complete (SSCCE), it compiled too: Live On Coliru
I briefly illuminate some individual points here:
- the bind expressions in the semantic action copy
m_lTree
by value, this way you can't mutate the member.phx::ref
was required here - the expectation points made it impossible to parse anything other than "days" limits successfully
- use
lit
unless you actually want to consume the value of the string as an attribute. Better yet, just write"limit" >> uint_ >> "days"
because overload overloading does the rest
As you've noticed there are number of anti-patterns in this grammar, making it complex:
- Boost Spirit: "Semantic actions are evil"?
- the reference-type member
m_lTree
leads to Action At A Distance and tight coupling.
However:
That all said, I'd simplify things by avoiding semantic actions, and separating parsing and evaluation.
Simply parsing into a Limit
struct:
qi::rule<Iterator, ConditionTree::Limit(), Skipper> limit_expression;
limit_expression = "limit" >> qi::uint_ >> unit_;
Handles the units without separate expression branches:
unit_.add("days", ConditionTree::Limit::days)
("hours", ConditionTree::Limit::hours)
("minutes", ConditionTree::Limit::minutes);
Because, now, the grammar just parses a Limit
object you can do the evaluation atomically after the parse:
ConditionTree::Limit limit;
if (phrase_parse(iter, end, parser, ascii::space, limit))
{
AddLimit(oTree, limit);
}
Separating parsing from evaluation enables you to add things like
- repeated evaluation of the same expression (without repeated parsing)
- simplifying the expression tree before evaluation
- validating before evaluation
- debugging
slightly more interestingly, allows you to write
ApplyLimit
in a functional style, not mutating an object:ConditionTree ApplyLimit(ConditionTree const& ct, Limit limit) { return ct + limit; // do something here }
but most importantly: it hugely simplifies the grammar and saves you hours of your life that were better spent otherwise
See this Live On Coliru, outputting:
void AddLimit(ConditionTree&, ConditionTree::Limit): 5 minutes
Listing
#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);
}
}
来源:https://stackoverflow.com/questions/24933455/simple-expression-with-boostspirit