c++ - boost spirit: how to instruct the parser to read whole input and then report the result -


i have minimal example of expression grammar (arithmetic expressions only) should succeed when complete input valid arithmetic expression. seems me parser reporting success when subsequence results start symbol of parser.

  template <typename iterator>     struct expparser : qi::grammar<iterator, expression_ast(),ascii::space_type>     {         expparser() : expparser::base_type(aexp)         {             using qi::_val;             using qi::_1;             using qi::_2;             using qi::_3;             using qi::uint_;             using qi::lit;             using qi::alnum;               aexp = tm >> +((lit("+") >> aexp))                 | (tm >> +(lit("-") >> aexp))                 | (tm >> +(lit("*") >> aexp))                 | (tm >> +(lit("/") >> aexp))                 | tm;              tm = (uint_)[_val=_1];         }          qi::rule<iterator, expression_ast(),ascii::space_type  >         aexp,tm;     }; }   int main() {      std::string input("3+5*}{%%"); //initial part junk @ end    using boost::spirit::ascii::space;          typedef client::expparser<std::string::const_iterator> parser;          parser par; // our grammar          std::string::const_iterator beg = input.begin();          std::string::const_iterator end = input.end(); //       std::cout<<"about parse expression "<<input;          bool r = phrase_parse(beg, end, par, space, ast);          if(!r)          {              boost_assert_msg(false,"not valid expression parse");          }else          {              std::cout<<"parsed successfully"<<std::endl;          }     } 

when run program succeeds because 3 matches tm , gives aexp initial symbol. how can ensure formedness of complete expression?

phrase_parse returns true if match found, if match doesn't encompass whole string. modifies front iterator given point end of match found, means if whole string matched, front iterator equal end iterator when phrase_parse finished.

if want test whether match found , encompasses whole string, use

     if(!r || beg != end)      {          boost_assert_msg(false,"not valid expression parse");      }else      {          std::cout<<"parsed successfully"<<std::endl;      } 

Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -