Wow ! Excellent :)
> > > Is there a simple math expression parser in boost ?
> > > I only need to parse those kind of expression: "1+2/3".
> > I believe Spirit is exactly what you're looking for. It has almost a dozen
> > different calculator examples to choose from as a starting point for your
> > needs.
> >
> > > I have been thinking on boost::spirit, but it is quite hard for what I
> > > need.
> > Why?
>
> I very enjoy playing with spirit, but I was looking for a 5-line code thatWell, not exactly five lines, but here is a simple calculator grammar written in Spirit:
> only boost have the secret...
namespace qi = boost::spirit::qi;
template <typename Iterator>
struct calculator : qi::grammar<Iterator, qi::space_type>
{
calculator() : calculator::base_type(expression)
{
expression = term >> *( ('+' >> term) | ('-' >> term) ) ;
term = factor >> *( ('*' >> factor) | ('/' >> factor) ) ;
factor = qi::uint_ | '(' >> expression >> ')' |
('-' >> factor) | ('+' >> factor) ;
}
qi::rule<Iterator, qi::space_type> expression, term, factor;
};
And here is how to use it:
std::string input("1+2/3");
calculator<std::string::const_iterator> calc;
if (phrase_parse(input.begin(), input.end(), calc, qi::space))
std::cout << "successfully parsed: " << input << std::endl;
Regards Hartmut
---------------
http://boost-spirit.com
_______________________________________________
Boost-users mailing list
Boost-users@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users