#include #include #include #include #include #include #include #include struct ExtractDouble { ExtractDouble(void) {} template struct result { typedef double type; }; template double operator() (IteratorType a, IteratorType b) const { std::string tmp; tmp.reserve(std::distance(a, b)); // We have to replace 'd' and 'D' by 'e' for (IteratorType i = a; i != b; ++i) { switch(*i) { case 'd': case 'D': tmp.append(1, 'e'); break; default: tmp.append(1, *i); } } std::stringstream s(tmp); double value; s >> value; return (value); } }; struct __declspec(dllexport) ExtRealParserClosure : boost::spirit::closure { boost::spirit::closure::member1 val; }; struct __declspec(dllexport) ExtRealParser : public boost::spirit::grammar { template struct definition { boost::spirit::rule top; definition(const ExtRealParser& self) { using namespace boost::spirit; using namespace phoenix; chset<> sign("+-"); chset<> exponentMarker("eEdD"); function extractDouble; top = (!sign >> ((+digit_p >> !(ch_p<>('.') >> *digit_p) >> !(exponentMarker >> !sign >> +digit_p)) | (ch_p<>('.') >> +digit_p >> !(exponentMarker >> !sign >> +digit_p)))) [ self.val = extractDouble(arg1, arg2) ]; } // Tell Spirit to start parsing with "top" boost::spirit::rule const& start() const { return top; } }; }; const ExtRealParser ext_real_p; int main(void) { double result; boost::spirit::parse("3.1415", ext_real_p [phoenix::var(result) = phoenix::arg1], boost::spirit::space_p); std::cout << "Result is " << result << "\n"; }