Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6753227
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:08:08+00:00 2026-05-26T13:08:08+00:00

I am writing an interpreter for a programming language and I am doing it

  • 0

I am writing an interpreter for a programming language and I am doing it in a C++, a language, I am frankly quite new to.

What I am trying to accomplish is to convert a specific float format in std::string
to a double (or whatever). I want it to be completely independent of the locale and as robust as possible.

I have two cases:

  • integers: they should be contiguous digits from 0-9 with or without a leading minus sign (no plus sign allowed, leading zeros allowed)
  • floating-point numbers: [whole part].[decimal part] with or without a leading minus and without any thousands separators. Either whole part or decimal part can be ommitted (for example .4 or 4.), but not both

I would like it to be the “C++ way” to do it. Is there a function I could use to specify custom number formats (kind of like date in PHP).

I will be very grateful for any pointer or code-snippet provided. Thank you!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T13:08:09+00:00Added an answer on May 26, 2026 at 1:08 pm

    I don’t know about iostreams support for strict input formatting.

    However, you can use Boost Spirit:

    standard real parsers with RealPolicy

    See http://www.boost.org/doc/libs/1_47_0/libs/spirit/doc/html/spirit/qi/reference/numeric/real.html

    This will allow you to explicitely define the format(s) accepted for exponents, signs and any (thousands) separators. This is also a quite complex approach, but it is very fast and very flexible (works for non-standard numeric types too, IIRC).

    Two phase parsing

    You can use either Spirit Qi rules to specify the exact format, and pass the raw[] input sequence of to the standard numeric parsers only if it matches your requirements.

    The more involved, but also more optimal way, would be to use a Spirit Lexer to tokenize the input – effectively doing the same but more efficiently.

    The middle ground

    The middle ground here would be to use a plain-old (Posix|Perl|C++11|Boost) regular expression to validate the input format and pass it off to any suitable conversion (like Boost Lexical cast, or just std::stringstream >> double etc.)

    A sample showing both Spirit Qi and regex pre-matching at work while parsing the number format for floats (The language is c++0x1):

    #include <boost/spirit/include/qi.hpp>
    #include <boost/spirit/include/phoenix_operator.hpp>
    #include <boost/spirit/include/phoenix.hpp>
    #include <boost/regex.hpp>
    
    namespace qi=boost::spirit::qi;
    namespace phx=boost::phoenix;
    
    bool is_ok(const std::vector<char>& raw)
    {
        static const boost::regex rx(R"(-?(\d*\.\d+|\d+\.\d*))");
        return boost::regex_match(raw.begin(), raw.end(), rx);
    }
    
    template <typename Input>
        void test(const Input& input)
    {
        auto f(std::begin(input)), l(std::end(input));
    
        double parsed = 0;
        bool ok = qi::phrase_parse(f, l, 
    
           // this is the parser expression
           &(+qi::char_)[ qi::_pass = phx::bind(is_ok, qi::_1) ]
           >> qi::double_, 
           // end parser expression
    
           qi::space, parsed);
    
        std::cout << "DEBUG: '" << input << "'\t"  << std::boolalpha << ok << "\t" << parsed << std::endl;
    }
    
    int main()
    {
        const std::string good[]  = { ".300", "300.", "-.4", "-4." };
        const std::string wrong[] = { "", ".", "1", "-1", "-1111", "3..", "+1", "+.1", "+1.", "+1.0", "+-2.", "-+2." };
    
        for (auto& input : good)
            test(input);
    
        for (auto& input : wrong)
            test(input);
    }
    


    1 using c++11 featurs:

    • range-based for
    • raw string literals for the regex specification
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am learning on my own about writing an interpreter for a programming language,
I am trying to write an interpreter for the programming language Icon. One of
I'm writing a small interpreter for a C-like language in Scheme (R5RS) and trying
I am writing an interpreter in Java for a domain-specific language with some scripting
I'm writing an interpreter for an experimental language. Three of the main constructs of
I'm writing a small interpreter for a simple BASIC like language as an exercise
I've heard of the idea of bootstrapping a language, that is, writing a compiler/interpreter
I'm writing a command line interpreter and I'm trying to setup formats for individual
I am currently writing a programming language in C/C++ as an exercise (but mostly
I'm writing a simple parser/interpreter for a language . The instructions keep mentioning 'deferred

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.