boost::spirit and strict_real_policies fails to parse a too long integer












1














I've to deal with really long integers in text format -- so long that they won't fit into an 32bit int.
I need to parse such text into a



boost::variant<int, double>.


So if there is a long integer to big for an integer it needs to go into a double. See the example below. It does not parse the name value pair



MUESR1 = 411100000000000. 


How can this be fixed?



#include <boost/spirit/include/qi.hpp>
#include <boost/fusion/include/std_pair.hpp>
#include <iterator>


namespace qi = boost::spirit::qi;
namespace spirit = boost::spirit;
namespace ascii = boost::spirit::ascii;

typedef boost::variant<int, double> VALUE;
typedef std::pair<std::string, VALUE> PAIR;
typedef std::map<std::string, VALUE> PAIRS;
template<typename Iterator>
struct parameters:qi::grammar<Iterator, PAIRS(), ascii::space_type>
{
qi::rule<Iterator, std::string(), ascii::space_type> m_sName;
qi::rule<Iterator, VALUE(), ascii::space_type> m_sValue;
qi::rule<Iterator, PAIR(), ascii::space_type> m_sNameValue;
qi::rule<Iterator, PAIRS(), ascii::space_type> m_sRoot;
qi::real_parser<double, qi::strict_real_policies<double> > m_sReal;

parameters(void)
:parameters::base_type(m_sRoot)
{ m_sName %= qi::lexeme[qi::char_("a-zA-Z_") >> *qi::char_("a-zA-z_0-9")];
m_sValue %= m_sReal | spirit::int_;
m_sNameValue %= m_sName >> qi::lit('=') >> m_sValue >> -qi::lit('n');
m_sRoot %= m_sNameValue >> *m_sNameValue;
}
};


int main(int, char**)
{
static const char s_ap = "
MUEPH1 = 7.014158 MUEPHW= -0.3 MUEPWP = 0.23 MUEPHL= -0.72 MUEPLP = 3.4 MUEPHS = 2.976E-07 MUEPSP = 5 VTMP= -1.8463 WVTH0= -1.01558 MUESR0 = 0.01256478438899837 MUESR1 = 411100000000000n
MUEPHW2 = 0 MUEPWP2 = 1n";
parameters<const char*> sGrammar;
const char *pIter = s_ap;
const char *const pEnd = s_ap + sizeof s_ap - 1;
PAIRS sValues;
if (phrase_parse(pIter, pEnd, sGrammar, boost::spirit::ascii::space, sValues) && pIter == pEnd)
{ std::cerr << "parsing successful!" << std::endl;
for (const auto &r : sValues)
std::cout << r.first << "=" << std::scientific << r.second << std::endl;
}
else
{ std::cerr << "parsing failed!" << std::endl;
std::cerr << pIter << std::endl;
}
}









share|improve this question


















  • 3




    m_sReal|int_|double_?
    – llonesmiz
    Nov 20 '18 at 20:06
















1














I've to deal with really long integers in text format -- so long that they won't fit into an 32bit int.
I need to parse such text into a



boost::variant<int, double>.


So if there is a long integer to big for an integer it needs to go into a double. See the example below. It does not parse the name value pair



MUESR1 = 411100000000000. 


How can this be fixed?



#include <boost/spirit/include/qi.hpp>
#include <boost/fusion/include/std_pair.hpp>
#include <iterator>


namespace qi = boost::spirit::qi;
namespace spirit = boost::spirit;
namespace ascii = boost::spirit::ascii;

typedef boost::variant<int, double> VALUE;
typedef std::pair<std::string, VALUE> PAIR;
typedef std::map<std::string, VALUE> PAIRS;
template<typename Iterator>
struct parameters:qi::grammar<Iterator, PAIRS(), ascii::space_type>
{
qi::rule<Iterator, std::string(), ascii::space_type> m_sName;
qi::rule<Iterator, VALUE(), ascii::space_type> m_sValue;
qi::rule<Iterator, PAIR(), ascii::space_type> m_sNameValue;
qi::rule<Iterator, PAIRS(), ascii::space_type> m_sRoot;
qi::real_parser<double, qi::strict_real_policies<double> > m_sReal;

parameters(void)
:parameters::base_type(m_sRoot)
{ m_sName %= qi::lexeme[qi::char_("a-zA-Z_") >> *qi::char_("a-zA-z_0-9")];
m_sValue %= m_sReal | spirit::int_;
m_sNameValue %= m_sName >> qi::lit('=') >> m_sValue >> -qi::lit('n');
m_sRoot %= m_sNameValue >> *m_sNameValue;
}
};


int main(int, char**)
{
static const char s_ap = "
MUEPH1 = 7.014158 MUEPHW= -0.3 MUEPWP = 0.23 MUEPHL= -0.72 MUEPLP = 3.4 MUEPHS = 2.976E-07 MUEPSP = 5 VTMP= -1.8463 WVTH0= -1.01558 MUESR0 = 0.01256478438899837 MUESR1 = 411100000000000n
MUEPHW2 = 0 MUEPWP2 = 1n";
parameters<const char*> sGrammar;
const char *pIter = s_ap;
const char *const pEnd = s_ap + sizeof s_ap - 1;
PAIRS sValues;
if (phrase_parse(pIter, pEnd, sGrammar, boost::spirit::ascii::space, sValues) && pIter == pEnd)
{ std::cerr << "parsing successful!" << std::endl;
for (const auto &r : sValues)
std::cout << r.first << "=" << std::scientific << r.second << std::endl;
}
else
{ std::cerr << "parsing failed!" << std::endl;
std::cerr << pIter << std::endl;
}
}









share|improve this question


















  • 3




    m_sReal|int_|double_?
    – llonesmiz
    Nov 20 '18 at 20:06














1












1








1







I've to deal with really long integers in text format -- so long that they won't fit into an 32bit int.
I need to parse such text into a



boost::variant<int, double>.


So if there is a long integer to big for an integer it needs to go into a double. See the example below. It does not parse the name value pair



MUESR1 = 411100000000000. 


How can this be fixed?



#include <boost/spirit/include/qi.hpp>
#include <boost/fusion/include/std_pair.hpp>
#include <iterator>


namespace qi = boost::spirit::qi;
namespace spirit = boost::spirit;
namespace ascii = boost::spirit::ascii;

typedef boost::variant<int, double> VALUE;
typedef std::pair<std::string, VALUE> PAIR;
typedef std::map<std::string, VALUE> PAIRS;
template<typename Iterator>
struct parameters:qi::grammar<Iterator, PAIRS(), ascii::space_type>
{
qi::rule<Iterator, std::string(), ascii::space_type> m_sName;
qi::rule<Iterator, VALUE(), ascii::space_type> m_sValue;
qi::rule<Iterator, PAIR(), ascii::space_type> m_sNameValue;
qi::rule<Iterator, PAIRS(), ascii::space_type> m_sRoot;
qi::real_parser<double, qi::strict_real_policies<double> > m_sReal;

parameters(void)
:parameters::base_type(m_sRoot)
{ m_sName %= qi::lexeme[qi::char_("a-zA-Z_") >> *qi::char_("a-zA-z_0-9")];
m_sValue %= m_sReal | spirit::int_;
m_sNameValue %= m_sName >> qi::lit('=') >> m_sValue >> -qi::lit('n');
m_sRoot %= m_sNameValue >> *m_sNameValue;
}
};


int main(int, char**)
{
static const char s_ap = "
MUEPH1 = 7.014158 MUEPHW= -0.3 MUEPWP = 0.23 MUEPHL= -0.72 MUEPLP = 3.4 MUEPHS = 2.976E-07 MUEPSP = 5 VTMP= -1.8463 WVTH0= -1.01558 MUESR0 = 0.01256478438899837 MUESR1 = 411100000000000n
MUEPHW2 = 0 MUEPWP2 = 1n";
parameters<const char*> sGrammar;
const char *pIter = s_ap;
const char *const pEnd = s_ap + sizeof s_ap - 1;
PAIRS sValues;
if (phrase_parse(pIter, pEnd, sGrammar, boost::spirit::ascii::space, sValues) && pIter == pEnd)
{ std::cerr << "parsing successful!" << std::endl;
for (const auto &r : sValues)
std::cout << r.first << "=" << std::scientific << r.second << std::endl;
}
else
{ std::cerr << "parsing failed!" << std::endl;
std::cerr << pIter << std::endl;
}
}









share|improve this question













I've to deal with really long integers in text format -- so long that they won't fit into an 32bit int.
I need to parse such text into a



boost::variant<int, double>.


So if there is a long integer to big for an integer it needs to go into a double. See the example below. It does not parse the name value pair



MUESR1 = 411100000000000. 


How can this be fixed?



#include <boost/spirit/include/qi.hpp>
#include <boost/fusion/include/std_pair.hpp>
#include <iterator>


namespace qi = boost::spirit::qi;
namespace spirit = boost::spirit;
namespace ascii = boost::spirit::ascii;

typedef boost::variant<int, double> VALUE;
typedef std::pair<std::string, VALUE> PAIR;
typedef std::map<std::string, VALUE> PAIRS;
template<typename Iterator>
struct parameters:qi::grammar<Iterator, PAIRS(), ascii::space_type>
{
qi::rule<Iterator, std::string(), ascii::space_type> m_sName;
qi::rule<Iterator, VALUE(), ascii::space_type> m_sValue;
qi::rule<Iterator, PAIR(), ascii::space_type> m_sNameValue;
qi::rule<Iterator, PAIRS(), ascii::space_type> m_sRoot;
qi::real_parser<double, qi::strict_real_policies<double> > m_sReal;

parameters(void)
:parameters::base_type(m_sRoot)
{ m_sName %= qi::lexeme[qi::char_("a-zA-Z_") >> *qi::char_("a-zA-z_0-9")];
m_sValue %= m_sReal | spirit::int_;
m_sNameValue %= m_sName >> qi::lit('=') >> m_sValue >> -qi::lit('n');
m_sRoot %= m_sNameValue >> *m_sNameValue;
}
};


int main(int, char**)
{
static const char s_ap = "
MUEPH1 = 7.014158 MUEPHW= -0.3 MUEPWP = 0.23 MUEPHL= -0.72 MUEPLP = 3.4 MUEPHS = 2.976E-07 MUEPSP = 5 VTMP= -1.8463 WVTH0= -1.01558 MUESR0 = 0.01256478438899837 MUESR1 = 411100000000000n
MUEPHW2 = 0 MUEPWP2 = 1n";
parameters<const char*> sGrammar;
const char *pIter = s_ap;
const char *const pEnd = s_ap + sizeof s_ap - 1;
PAIRS sValues;
if (phrase_parse(pIter, pEnd, sGrammar, boost::spirit::ascii::space, sValues) && pIter == pEnd)
{ std::cerr << "parsing successful!" << std::endl;
for (const auto &r : sValues)
std::cout << r.first << "=" << std::scientific << r.second << std::endl;
}
else
{ std::cerr << "parsing failed!" << std::endl;
std::cerr << pIter << std::endl;
}
}






c++ boost-spirit






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 20 '18 at 19:20









Frank Puck

12310




12310








  • 3




    m_sReal|int_|double_?
    – llonesmiz
    Nov 20 '18 at 20:06














  • 3




    m_sReal|int_|double_?
    – llonesmiz
    Nov 20 '18 at 20:06








3




3




m_sReal|int_|double_?
– llonesmiz
Nov 20 '18 at 20:06




m_sReal|int_|double_?
– llonesmiz
Nov 20 '18 at 20:06












1 Answer
1






active

oldest

votes


















1














Yeah, the grammar asks for ONLY strict reals to be parsed. If you don't want that, you'll need to accept other reals. @llonesmiz's comment is one way to do it.




Alternatively, it seems like you could just parse doubles. Though binary real representation can be "lossy" that doesn't happen for the integral part of the mantissa until you exceed the 52/53 bits of significand (https://en.wikipedia.org/wiki/Double-precision_floating-point_format). By comparison, popular compilers have int at 32 bits.




Live On Coliru



//#define BOOST_SPIRIT_DEBUG
#include <boost/fusion/include/std_pair.hpp>
#include <boost/spirit/include/qi.hpp>
#include <iterator>
#include <iomanip>

namespace qi = boost::spirit::qi;
namespace ascii = boost::spirit::ascii;

typedef boost::variant<int, double> VALUE;
typedef std::pair<std::string, VALUE> PAIR;
typedef std::map<std::string, VALUE> PAIRS;

template <typename Iterator> struct parameters : qi::grammar<Iterator, PAIRS(), ascii::space_type> {

parameters(void) : parameters::base_type(m_sRoot) {
m_sName = qi::lexeme[qi::char_("a-zA-Z_") >> *qi::char_("a-zA-z_0-9")];
m_sValue = m_sReal | qi::int_ | qi::double_;
m_sNameValue = m_sName >> '=' >> m_sValue >> -qi::lit('n');
m_sRoot = m_sNameValue >> *m_sNameValue;

BOOST_SPIRIT_DEBUG_NODES((m_sName)(m_sValue)(m_sNameValue)(m_sRoot))
}

private:
qi::rule<Iterator, std::string(), ascii::space_type> m_sName;
qi::rule<Iterator, VALUE(), ascii::space_type> m_sValue;
qi::rule<Iterator, PAIR(), ascii::space_type> m_sNameValue;
qi::rule<Iterator, PAIRS(), ascii::space_type> m_sRoot;
qi::real_parser<double, qi::strict_real_policies<double> > m_sReal;
};

int main(int, char **) {
static const char s_ap = R"(
MUEPH1 = 7.014158 MUEPHW= -0.3 MUEPWP = 0.23 MUEPHL= -0.72 MUEPLP = 3.4 MUEPHS = 2.976E-07 MUEPSP = 5 VTMP= -1.8463 WVTH0= -1.01558 MUESR0 = 0.01256478438899837 MUESR1 = 411100000000000
MUEPHW2 = 0 MUEPWP2 = 1
)";

parameters<const char*> sGrammar;

const char *pIter = std::begin(s_ap);
const char *const pEnd = std::end(s_ap) - 1;
PAIRS sValues;

if (phrase_parse(pIter, pEnd, sGrammar, boost::spirit::ascii::space, sValues) && pIter == pEnd) {
std::cerr << "parsing successful!" << std::endl;
for (const auto &r : sValues)
std::cout << r.first << "=" << std::scientific << std::setprecision(2) << r.second << std::endl;
} else {
std::cerr << "parsing failed!" << std::endl;
std::cerr << std::quoted(std::string(pIter, pEnd)) << std::endl;
}
}


Note also the general improvements, like adding debugging to your rules.






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53400077%2fboostspirit-and-strict-real-policies-fails-to-parse-a-too-long-integer%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    Yeah, the grammar asks for ONLY strict reals to be parsed. If you don't want that, you'll need to accept other reals. @llonesmiz's comment is one way to do it.




    Alternatively, it seems like you could just parse doubles. Though binary real representation can be "lossy" that doesn't happen for the integral part of the mantissa until you exceed the 52/53 bits of significand (https://en.wikipedia.org/wiki/Double-precision_floating-point_format). By comparison, popular compilers have int at 32 bits.




    Live On Coliru



    //#define BOOST_SPIRIT_DEBUG
    #include <boost/fusion/include/std_pair.hpp>
    #include <boost/spirit/include/qi.hpp>
    #include <iterator>
    #include <iomanip>

    namespace qi = boost::spirit::qi;
    namespace ascii = boost::spirit::ascii;

    typedef boost::variant<int, double> VALUE;
    typedef std::pair<std::string, VALUE> PAIR;
    typedef std::map<std::string, VALUE> PAIRS;

    template <typename Iterator> struct parameters : qi::grammar<Iterator, PAIRS(), ascii::space_type> {

    parameters(void) : parameters::base_type(m_sRoot) {
    m_sName = qi::lexeme[qi::char_("a-zA-Z_") >> *qi::char_("a-zA-z_0-9")];
    m_sValue = m_sReal | qi::int_ | qi::double_;
    m_sNameValue = m_sName >> '=' >> m_sValue >> -qi::lit('n');
    m_sRoot = m_sNameValue >> *m_sNameValue;

    BOOST_SPIRIT_DEBUG_NODES((m_sName)(m_sValue)(m_sNameValue)(m_sRoot))
    }

    private:
    qi::rule<Iterator, std::string(), ascii::space_type> m_sName;
    qi::rule<Iterator, VALUE(), ascii::space_type> m_sValue;
    qi::rule<Iterator, PAIR(), ascii::space_type> m_sNameValue;
    qi::rule<Iterator, PAIRS(), ascii::space_type> m_sRoot;
    qi::real_parser<double, qi::strict_real_policies<double> > m_sReal;
    };

    int main(int, char **) {
    static const char s_ap = R"(
    MUEPH1 = 7.014158 MUEPHW= -0.3 MUEPWP = 0.23 MUEPHL= -0.72 MUEPLP = 3.4 MUEPHS = 2.976E-07 MUEPSP = 5 VTMP= -1.8463 WVTH0= -1.01558 MUESR0 = 0.01256478438899837 MUESR1 = 411100000000000
    MUEPHW2 = 0 MUEPWP2 = 1
    )";

    parameters<const char*> sGrammar;

    const char *pIter = std::begin(s_ap);
    const char *const pEnd = std::end(s_ap) - 1;
    PAIRS sValues;

    if (phrase_parse(pIter, pEnd, sGrammar, boost::spirit::ascii::space, sValues) && pIter == pEnd) {
    std::cerr << "parsing successful!" << std::endl;
    for (const auto &r : sValues)
    std::cout << r.first << "=" << std::scientific << std::setprecision(2) << r.second << std::endl;
    } else {
    std::cerr << "parsing failed!" << std::endl;
    std::cerr << std::quoted(std::string(pIter, pEnd)) << std::endl;
    }
    }


    Note also the general improvements, like adding debugging to your rules.






    share|improve this answer


























      1














      Yeah, the grammar asks for ONLY strict reals to be parsed. If you don't want that, you'll need to accept other reals. @llonesmiz's comment is one way to do it.




      Alternatively, it seems like you could just parse doubles. Though binary real representation can be "lossy" that doesn't happen for the integral part of the mantissa until you exceed the 52/53 bits of significand (https://en.wikipedia.org/wiki/Double-precision_floating-point_format). By comparison, popular compilers have int at 32 bits.




      Live On Coliru



      //#define BOOST_SPIRIT_DEBUG
      #include <boost/fusion/include/std_pair.hpp>
      #include <boost/spirit/include/qi.hpp>
      #include <iterator>
      #include <iomanip>

      namespace qi = boost::spirit::qi;
      namespace ascii = boost::spirit::ascii;

      typedef boost::variant<int, double> VALUE;
      typedef std::pair<std::string, VALUE> PAIR;
      typedef std::map<std::string, VALUE> PAIRS;

      template <typename Iterator> struct parameters : qi::grammar<Iterator, PAIRS(), ascii::space_type> {

      parameters(void) : parameters::base_type(m_sRoot) {
      m_sName = qi::lexeme[qi::char_("a-zA-Z_") >> *qi::char_("a-zA-z_0-9")];
      m_sValue = m_sReal | qi::int_ | qi::double_;
      m_sNameValue = m_sName >> '=' >> m_sValue >> -qi::lit('n');
      m_sRoot = m_sNameValue >> *m_sNameValue;

      BOOST_SPIRIT_DEBUG_NODES((m_sName)(m_sValue)(m_sNameValue)(m_sRoot))
      }

      private:
      qi::rule<Iterator, std::string(), ascii::space_type> m_sName;
      qi::rule<Iterator, VALUE(), ascii::space_type> m_sValue;
      qi::rule<Iterator, PAIR(), ascii::space_type> m_sNameValue;
      qi::rule<Iterator, PAIRS(), ascii::space_type> m_sRoot;
      qi::real_parser<double, qi::strict_real_policies<double> > m_sReal;
      };

      int main(int, char **) {
      static const char s_ap = R"(
      MUEPH1 = 7.014158 MUEPHW= -0.3 MUEPWP = 0.23 MUEPHL= -0.72 MUEPLP = 3.4 MUEPHS = 2.976E-07 MUEPSP = 5 VTMP= -1.8463 WVTH0= -1.01558 MUESR0 = 0.01256478438899837 MUESR1 = 411100000000000
      MUEPHW2 = 0 MUEPWP2 = 1
      )";

      parameters<const char*> sGrammar;

      const char *pIter = std::begin(s_ap);
      const char *const pEnd = std::end(s_ap) - 1;
      PAIRS sValues;

      if (phrase_parse(pIter, pEnd, sGrammar, boost::spirit::ascii::space, sValues) && pIter == pEnd) {
      std::cerr << "parsing successful!" << std::endl;
      for (const auto &r : sValues)
      std::cout << r.first << "=" << std::scientific << std::setprecision(2) << r.second << std::endl;
      } else {
      std::cerr << "parsing failed!" << std::endl;
      std::cerr << std::quoted(std::string(pIter, pEnd)) << std::endl;
      }
      }


      Note also the general improvements, like adding debugging to your rules.






      share|improve this answer
























        1












        1








        1






        Yeah, the grammar asks for ONLY strict reals to be parsed. If you don't want that, you'll need to accept other reals. @llonesmiz's comment is one way to do it.




        Alternatively, it seems like you could just parse doubles. Though binary real representation can be "lossy" that doesn't happen for the integral part of the mantissa until you exceed the 52/53 bits of significand (https://en.wikipedia.org/wiki/Double-precision_floating-point_format). By comparison, popular compilers have int at 32 bits.




        Live On Coliru



        //#define BOOST_SPIRIT_DEBUG
        #include <boost/fusion/include/std_pair.hpp>
        #include <boost/spirit/include/qi.hpp>
        #include <iterator>
        #include <iomanip>

        namespace qi = boost::spirit::qi;
        namespace ascii = boost::spirit::ascii;

        typedef boost::variant<int, double> VALUE;
        typedef std::pair<std::string, VALUE> PAIR;
        typedef std::map<std::string, VALUE> PAIRS;

        template <typename Iterator> struct parameters : qi::grammar<Iterator, PAIRS(), ascii::space_type> {

        parameters(void) : parameters::base_type(m_sRoot) {
        m_sName = qi::lexeme[qi::char_("a-zA-Z_") >> *qi::char_("a-zA-z_0-9")];
        m_sValue = m_sReal | qi::int_ | qi::double_;
        m_sNameValue = m_sName >> '=' >> m_sValue >> -qi::lit('n');
        m_sRoot = m_sNameValue >> *m_sNameValue;

        BOOST_SPIRIT_DEBUG_NODES((m_sName)(m_sValue)(m_sNameValue)(m_sRoot))
        }

        private:
        qi::rule<Iterator, std::string(), ascii::space_type> m_sName;
        qi::rule<Iterator, VALUE(), ascii::space_type> m_sValue;
        qi::rule<Iterator, PAIR(), ascii::space_type> m_sNameValue;
        qi::rule<Iterator, PAIRS(), ascii::space_type> m_sRoot;
        qi::real_parser<double, qi::strict_real_policies<double> > m_sReal;
        };

        int main(int, char **) {
        static const char s_ap = R"(
        MUEPH1 = 7.014158 MUEPHW= -0.3 MUEPWP = 0.23 MUEPHL= -0.72 MUEPLP = 3.4 MUEPHS = 2.976E-07 MUEPSP = 5 VTMP= -1.8463 WVTH0= -1.01558 MUESR0 = 0.01256478438899837 MUESR1 = 411100000000000
        MUEPHW2 = 0 MUEPWP2 = 1
        )";

        parameters<const char*> sGrammar;

        const char *pIter = std::begin(s_ap);
        const char *const pEnd = std::end(s_ap) - 1;
        PAIRS sValues;

        if (phrase_parse(pIter, pEnd, sGrammar, boost::spirit::ascii::space, sValues) && pIter == pEnd) {
        std::cerr << "parsing successful!" << std::endl;
        for (const auto &r : sValues)
        std::cout << r.first << "=" << std::scientific << std::setprecision(2) << r.second << std::endl;
        } else {
        std::cerr << "parsing failed!" << std::endl;
        std::cerr << std::quoted(std::string(pIter, pEnd)) << std::endl;
        }
        }


        Note also the general improvements, like adding debugging to your rules.






        share|improve this answer












        Yeah, the grammar asks for ONLY strict reals to be parsed. If you don't want that, you'll need to accept other reals. @llonesmiz's comment is one way to do it.




        Alternatively, it seems like you could just parse doubles. Though binary real representation can be "lossy" that doesn't happen for the integral part of the mantissa until you exceed the 52/53 bits of significand (https://en.wikipedia.org/wiki/Double-precision_floating-point_format). By comparison, popular compilers have int at 32 bits.




        Live On Coliru



        //#define BOOST_SPIRIT_DEBUG
        #include <boost/fusion/include/std_pair.hpp>
        #include <boost/spirit/include/qi.hpp>
        #include <iterator>
        #include <iomanip>

        namespace qi = boost::spirit::qi;
        namespace ascii = boost::spirit::ascii;

        typedef boost::variant<int, double> VALUE;
        typedef std::pair<std::string, VALUE> PAIR;
        typedef std::map<std::string, VALUE> PAIRS;

        template <typename Iterator> struct parameters : qi::grammar<Iterator, PAIRS(), ascii::space_type> {

        parameters(void) : parameters::base_type(m_sRoot) {
        m_sName = qi::lexeme[qi::char_("a-zA-Z_") >> *qi::char_("a-zA-z_0-9")];
        m_sValue = m_sReal | qi::int_ | qi::double_;
        m_sNameValue = m_sName >> '=' >> m_sValue >> -qi::lit('n');
        m_sRoot = m_sNameValue >> *m_sNameValue;

        BOOST_SPIRIT_DEBUG_NODES((m_sName)(m_sValue)(m_sNameValue)(m_sRoot))
        }

        private:
        qi::rule<Iterator, std::string(), ascii::space_type> m_sName;
        qi::rule<Iterator, VALUE(), ascii::space_type> m_sValue;
        qi::rule<Iterator, PAIR(), ascii::space_type> m_sNameValue;
        qi::rule<Iterator, PAIRS(), ascii::space_type> m_sRoot;
        qi::real_parser<double, qi::strict_real_policies<double> > m_sReal;
        };

        int main(int, char **) {
        static const char s_ap = R"(
        MUEPH1 = 7.014158 MUEPHW= -0.3 MUEPWP = 0.23 MUEPHL= -0.72 MUEPLP = 3.4 MUEPHS = 2.976E-07 MUEPSP = 5 VTMP= -1.8463 WVTH0= -1.01558 MUESR0 = 0.01256478438899837 MUESR1 = 411100000000000
        MUEPHW2 = 0 MUEPWP2 = 1
        )";

        parameters<const char*> sGrammar;

        const char *pIter = std::begin(s_ap);
        const char *const pEnd = std::end(s_ap) - 1;
        PAIRS sValues;

        if (phrase_parse(pIter, pEnd, sGrammar, boost::spirit::ascii::space, sValues) && pIter == pEnd) {
        std::cerr << "parsing successful!" << std::endl;
        for (const auto &r : sValues)
        std::cout << r.first << "=" << std::scientific << std::setprecision(2) << r.second << std::endl;
        } else {
        std::cerr << "parsing failed!" << std::endl;
        std::cerr << std::quoted(std::string(pIter, pEnd)) << std::endl;
        }
        }


        Note also the general improvements, like adding debugging to your rules.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 20 '18 at 23:55









        sehe

        274k33335458




        274k33335458






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53400077%2fboostspirit-and-strict-real-policies-fails-to-parse-a-too-long-integer%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Create new schema in PostgreSQL using DBeaver

            Deepest pit of an array with Javascript: test on Codility

            Costa Masnaga