Ich versuche, das folgende Ergebnis zu verstehen. Der Testfall-Code istboost spirit qi numerisches syntaktisches Parsing von Integer- und Gleitkommazahlen
#include <boost/config/warning_disable.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/spirit/include/phoenix_fusion.hpp>
#include <boost/spirit/include/phoenix_stl.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/variant/recursive_variant.hpp>
#include <boost/spirit/home/support/context.hpp>
#include <boost/spirit/home/phoenix.hpp>
#include <boost/foreach.hpp>
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <vector>
namespace sp = boost::spirit;
namespace qi = boost::spirit::qi;
using namespace boost::spirit::ascii;
namespace fusion = boost::fusion;
namespace phoenix = boost::phoenix;
using phoenix::at_c;
using phoenix::push_back;
using phoenix::bind;
template <typename P>
void test_parser(
char const* input, P const& p, bool full_match = true)
{
using boost::spirit::qi::parse;
char const* f(input);
char const* l(f + strlen(f));
if (parse(f, l, p) && (!full_match || (f == l)))
std::cout << "ok" << std::endl;
else
std::cout << "fail" << std::endl;
}
int main() {
test_parser("+12345", qi::int_); //Ok
test_parser("+12345", qi::double_ - qi::int_); //failed, as expected
test_parser("+12345.34", qi::int_); // failed, as expected
test_parser("+12345.34", qi::double_ - qi::int_); //failed but it should be Ok!
};
die Motivation hier ist, dass ich Zahlen übereinstimmen ‚12345‘ als ganze Zahlen will und nie als floating Punkte. '12345.34' wird double_ und niemals int_ entsprechen, aber der umgekehrte Fall ist nicht wahr; '12345' passt sowohl zu ganzen Zahlen (int_) als auch zu Fließkommazahlen (double_). Ich habe versucht double_ - int_ und es konnte nicht erfolgreich '12345' übereinstimmen. Ich hoffe jedoch, dass der letzte Testfall '12345.34' positiv mit double_ - int_ übereinstimmt, aber das Ergebnis, das ich bekomme, stimmt nicht überein.
Warum dies so ist, und wie bekomme ich einen Parser, der nur ganze Zahlen und anderen Spiele, die nur Gleitpunkte entsprechen (wie in c, würde 5,0 als Fließkomma interpretiert werden)
+1 Dies ist eine bessere Lösung. – academicRobot
danke, das funktioniert perfekt – lurscher