#define BOOST_SPIRIT_USE_PHOENIX_V3
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <boost/phoenix/function/adapt_function.hpp>
struct add_operand {
template<typename...> struct result { typedef void type; };
template<typename L, typename R>
void operator()(L& lhs, R rhs) const { lhs += rhs; }
};
struct sub_operand {
template<typename...> struct result { typedef void type; };
template<typename L, typename R>
void operator()(L& lhs, R rhs) const { lhs -= rhs; }
};
int main()
{
const std::string INPUT_DATA = "12e-1 + 3.4 - .67";
typedef std::string::const_iterator iterator_type;
iterator_type begin = std::begin(INPUT_DATA);
iterator_type end = std::end(INPUT_DATA);
namespace qi = boost::spirit::qi;
namespace ascii = boost::spirit::qi::ascii;
namespace phx = boost::phoenix;
bool ok;
double result;
{
using namespace qi;
phx::function<add_operand> add_;
phx::function<sub_operand> sub_;
auto parser =
double_ [_val = _1]
>> *( (lit('+') >> double_[add_(_val, _1)])
| (lit('-') >> double_[sub_(_val, _1)])
);
ok = phrase_parse(begin, end, parser, ascii::space, result);
}
if (ok && begin == end)
std::cout << "parsed, result = " << result << std::endl;
else
std::cout << "not parsed" << std::endl;
}
I2RlZmluZSBCT09TVF9TUElSSVRfVVNFX1BIT0VOSVhfVjMKI2luY2x1ZGUgPGJvb3N0L3NwaXJpdC9pbmNsdWRlL3FpLmhwcD4KI2luY2x1ZGUgPGJvb3N0L3NwaXJpdC9pbmNsdWRlL3Bob2VuaXguaHBwPgojaW5jbHVkZSA8Ym9vc3QvcGhvZW5peC9mdW5jdGlvbi9hZGFwdF9mdW5jdGlvbi5ocHA+CgpzdHJ1Y3QgYWRkX29wZXJhbmQgeyAKICAgIHRlbXBsYXRlPHR5cGVuYW1lLi4uPiBzdHJ1Y3QgcmVzdWx0IHsgdHlwZWRlZiB2b2lkIHR5cGU7IH07CiAgICB0ZW1wbGF0ZTx0eXBlbmFtZSBMLCB0eXBlbmFtZSBSPgogICAgdm9pZCBvcGVyYXRvcigpKEwmIGxocywgUiByaHMpIGNvbnN0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