#define BOOST_SPIRIT_USE_PHOENIX_V3
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
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;
auto parser =
double_ [_val = _1] // (1)
>> *( (lit('+') >> double_[_val += _1]) // (2)
| (lit('-') >> double_[_val -= _1]) // (3)
);
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;
}
I2RlZmluZSBCT09TVF9TUElSSVRfVVNFX1BIT0VOSVhfVjMKI2luY2x1ZGUgPGJvb3N0L3NwaXJpdC9pbmNsdWRlL3FpLmhwcD4KI2luY2x1ZGUgPGJvb3N0L3NwaXJpdC9pbmNsdWRlL3Bob2VuaXguaHBwPgoKaW50IG1haW4oKQp7CiAgICBjb25zdCBzdGQ6OnN0cmluZyBJTlBVVF9EQVRBID0gIjEyZS0xICsgMy40IC0gLjY3IjsKICAgIHR5cGVkZWYgc3RkOjpzdHJpbmc6OmNvbnN0X2l0ZXJhdG9yIGl0ZXJhdG9yX3R5cGU7CiAgICBpdGVyYXRvcl90eXBlIGJlZ2luID0gc3RkOjpiZWdpbihJTlBVVF9EQVRBKTsKICAgIGl0ZXJhdG9yX3R5cGUgZW5kICAgPSBzdGQ6OmVuZChJTlBVVF9EQVRBKTsKCiAgICBuYW1lc3BhY2UgcWkgICAgPSBib29zdDo6c3Bpcml0OjpxaTsKICAgIG5hbWVzcGFjZSBhc2NpaSA9IGJvb3N0OjpzcGlyaXQ6OnFpOjphc2NpaTsKICAgIG5hbWVzcGFjZSBwaHggICA9IGJvb3N0OjpwaG9lbml4OwoKICAgIGJvb2wgb2s7CiAg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