
Hi all, Is there a simple math expression parser in boost ? I only need to parse those kind of expression: "1+2/3". I have been thinking on boost::spirit, but it is quite hard for what I need. - Eloi.

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? Regards Hartmut --------------- http://boost-spirit.com

I very enjoy playing with spirit, but I was looking for a 5-line code that
only boost have the secret...
Anyway, thank you !
2010/11/30 Hartmut Kaiser
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?
Regards Hartmut --------------- http://boost-spirit.com
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

From: boost-users-bounces@lists.boost.org on behalf of Eloi Du Bois Sent: Tue 11/30/2010 9:05 AM To: boost-users@lists.boost.org Subject: Re: [Boost-users] Parsing math expression
I very enjoy playing with spirit, but I was looking for a 5-line code that only boost have the secret...
Anyway, thank you !
Before I comment on using or not using Spirit what exactly are you trying to do? From what I can see you are trying to parse a single-digit numbers and only support addition and division. Is that correct? Stephen

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 that only boost have the secret...
Well, not exactly five lines, but here is a simple calculator grammar written in Spirit:
namespace qi = boost::spirit::qi;
template <typename Iterator>
struct calculator : qi::grammar

Wow ! Excellent :)
I thought it would be more verbose than that.
Thank you very much,
Eloi Du Bois.
2010/11/30 Hartmut Kaiser
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 that only boost have the secret...
Well, not exactly five lines, but here is a simple calculator grammar written in Spirit:
namespace qi = boost::spirit::qi;
template <typename Iterator> struct calculator : qi::grammar
{ calculator() : calculator::base_type(expression) { expression = term >> *( ('+' >> term) | ('-' >> term) ) ; term = factor >> *( ('*' >> factor) | ('/' >> factor) ) ; factor = qi::uint_ | '(' >> expression >> ')' | ('-' >> factor) | ('+' >> factor) ; } qi::rule expression, term, factor; }; And here is how to use it:
std::string input("1+2/3"); calculatorstd::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

Sorry, but I meant that it has to compute the final result.
What would you recommend to do that ?
2010/11/30 Eloi Du Bois
Wow ! Excellent :)
I thought it would be more verbose than that.
Thank you very much, Eloi Du Bois.
2010/11/30 Hartmut Kaiser
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 that only boost have the secret...
Well, not exactly five lines, but here is a simple calculator grammar written in Spirit:
namespace qi = boost::spirit::qi;
template <typename Iterator> struct calculator : qi::grammar
{ calculator() : calculator::base_type(expression) { expression = term >> *( ('+' >> term) | ('-' >> term) ) ; term = factor >> *( ('*' >> factor) | ('/' >> factor) ) ; factor = qi::uint_ | '(' >> expression >> ')' | ('-' >> factor) | ('+' >> factor) ; } qi::rule expression, term, factor; }; And here is how to use it:
std::string input("1+2/3"); calculatorstd::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

Sorry, but I meant that it has to compute the final result. What would you recommend to do that ?
Why don't you look for yourself here: $BOOST_ROOT/libs/spirit/example/qi/calc3.cpp (or any of the more complex calculator examples)? Regards Hartmut --------------- http://boost-spirit.com
2010/11/30 Eloi Du Bois
Wow ! Excellent :) I thought it would be more verbose than that.
Thank you very much, Eloi Du Bois. 2010/11/30 Hartmut Kaiser
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 that only boost have the secret... Well, not exactly five lines, but here is a simple calculator grammar written in Spirit:
namespace qi = boost::spirit::qi;
template <typename Iterator> struct calculator : qi::grammar
{ calculator() : calculator::base_type(expression) { expression = term >> *( ('+' >> term) | ('-' >> term) ) ; term = factor >> *( ('*' >> factor) | ('/' >> factor) ) ; factor = qi::uint_ | '(' >> expression >> ')' | ('-' >> factor) | ('+' >> factor) ; } qi::rule expression, term, factor; }; And here is how to use it:
std::string input("1+2/3"); calculatorstd::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

Ok, thank you for the link.
2010/11/30 Hartmut Kaiser
Sorry, but I meant that it has to compute the final result. What would you recommend to do that ?
Why don't you look for yourself here: $BOOST_ROOT/libs/spirit/example/qi/calc3.cpp (or any of the more complex calculator examples)?
Regards Hartmut --------------- http://boost-spirit.com
2010/11/30 Eloi Du Bois
Wow ! Excellent :) I thought it would be more verbose than that.
Thank you very much, Eloi Du Bois. 2010/11/30 Hartmut Kaiser
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 that only boost have the secret... Well, not exactly five lines, but here is a simple calculator grammar written in Spirit:
namespace qi = boost::spirit::qi;
template <typename Iterator> struct calculator : qi::grammar
{ calculator() : calculator::base_type(expression) { expression = term >> *( ('+' >> term) | ('-' >> term) ) ; term = factor >> *( ('*' >> factor) | ('/' >> factor) ) ; factor = qi::uint_ | '(' >> expression >> ')' | ('-' >> factor) | ('+' >> factor) ; } qi::rule expression, term, factor; }; And here is how to use it:
std::string input("1+2/3"); calculatorstd::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
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (3)
-
Eloi Du Bois
-
Hartmut Kaiser
-
Torri, Stephen CIV NSWCDD, W15