Search code examples
c++parsingboost-spirit-qi

Parsing a list of doubles with boost::spirit::qi


I'm getting familiar with boost::spirit and want to parse strings like below:

double_1 | double_2 | ... | double_n | double_1% | double_2% ... | double_m%

Where m>=0, n>=0.

For example, all lines below should parse ok:

91.3 | 44 | 5e-3 | 12% | 11%

91.3 | 44 | 5e-3

12% | 11%

I want to use boost::spirit::qi.

So, I've written two parsers like below:

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

    template <typename Iterator>
    bool parse_numbers(Iterator& first, Iterator last, std::vector<double>& v)
    {
        using qi::double_;
        using qi::phrase_parse;        
        using ascii::space;

        bool r = phrase_parse(first, last,

            //  Begin grammar
            (
                // double_ % '|'
                double_ >> *('|' >> double_ >> '|')
            )
            ,
            //  End grammar

            space, v);
        return r;
    }
    
    template <typename Iterator>
    bool parse_numbersWithPercents(Iterator& first, Iterator last, std::vector<double>& v)
    {
        using qi::double_;
        using qi::phrase_parse;
        using ascii::space;

        bool r = phrase_parse(first, last,

            //  Begin grammar
            (
                (double_ >> '%') % '|'
            )
            ,
            //  End grammar

            space, v);

        if (first != last) // fail if we did not get a full match
            return false;
        return r;
    }
}

And, then, I'm calling them in main like below:

int main()
{
    std::cout << "Give me a list of numbers in a format  double_1 | double_2 | ... | double_n | double_1% | double_2% ... | double_m%\n";
    std::cout << "The numbers will be inserted in a vector of numbers\n";
    std::cout << "Type [q or Q] to quit\n\n";

    std::string str;
    while (getline(std::cin, str))
    {
        if (str.empty() || str[0] == 'q' || str[0] == 'Q')
            break;

        std::vector<double> v;
        std::string::iterator begin = str.begin(), end = str.end();
        if (client::parse_numbers(begin, end, v))
        {
            std::cout << "-------------------------\n";
            std::cout << "First Part Parsing succeeded\n";

            for (std::vector<double>::size_type i = 0; i < v.size(); ++i)
                std::cout << i << ": " << v[i] << std::endl;

            std::cout << "\n-------------------------\n";
            if(begin != end) {
                if('|' == *begin) ++begin;
                if(begin != end) {
                    std::cout << "Parsing second part: " << std::string(begin, end) << std::endl;
                    std::vector<double> v1;
                     if (client::parse_numbersWithPercents(begin, end, v1))
                    {
                        std::cout << "-------------------------\n";
                        std::cout << "Second Part Parsing succeeded\n";

                        for (std::vector<double>::size_type i = 0; i < v1.size(); ++i)
                            std::cout << i << ": " << v1[i] << std::endl;

                        std::cout << "\n-------------------------\n";
                } else {
                    std::cout << "-------------------------\n";
                    std::cout << "Second Part Parsing failed\n";
                    std::cout << "-------------------------\n";
                    
                    if(begin != end) {
                        std::cout << "Remaining part is: " << std::string(begin, end) << std::endl; }
                    }
            }
        }
        }
        else
        {
            std::cout << "-------------------------\n";
            std::cout << "First Part Parsing failed\n";
            std::cout << "-------------------------\n";
            
            if(begin != end) {
                std::cout << "Remaining part is: " << std::string(begin, end) << std::endl; }
        }
    }

    std::cout << "Bye... :-) \n\n";
    return 0;
}

As you can see, this method doesn't work correct for corner cases like:

91.3 | 44 | 5e-3

12% | 11%

I'm interested is there another way to do the same in a more simple way using boost library. Or somehow correct my parsers to do right parsing of above corner cases. It would be nice to have first and second parts in a separate containers.

Thanks in advance.


Solution

  • Hah. My intuition was this should be enormously simple. However, I've come to conclude that indeed it's a bit nontrivial.

    The problem is with making the non-list-repeat separator optional. I thought long and hard about the most elegant way to make it optional and came up with this:

    Live On Coliru

    #include <boost/spirit/include/qi.hpp>
    
    namespace qi = boost::spirit::qi;
    
    namespace {
        using double_vec = std::vector<double>;
        using It         = std::string::const_iterator;
    
        static const qi::rule<It, double_vec(bool percent), qi::blank_type> doubles_
            = (qi::double_ >> (qi::eps(qi::_r1) >> '%' | !qi::lit('%'))) % '|';
    }
    
    int main() {
        std::string str;
        while (std::getline(std::cin, str)) {
            It f = str.begin(), l = str.end();
    
            double_vec v, w;
    
            bool ok = qi::phrase_parse(f, l, 
                      (doubles_(false) >> -('|' >> doubles_(true))) 
                    | qi::attr(double_vec{}) >> doubles_(true),
                    qi::blank, v, w);
    
            if (ok && f == l) {
                std::cout << "Parsed " << v.size() << "/" << w.size() << " elements\n";
            } else {
                std::istringstream iss(str);
                if (iss >> str && (str == "q" || str == "Q"))
                    break;
                std::cout << "Invalid input. Remaining '" << std::string(f,l) << "'\n";
            }
        }
    }
    

    Which produces the following result given the test inputs:

    ./test <<INPUT
    91.3 | 44 | 5e-3 | 12% | 11% 
    91.3 | 44 | 5e-3 
    12% | 11%
    q
    INPUT
    Parsed 3/2 elements
    Parsed 3/0 elements
    Parsed 0/2 elements
    

    Depending on what you're trying to /actually/ achieve here, things could be more elegant

    UPDATE In response to the comments, here's how I'd actually improve this by relaxing the grammar. Note how we shift ignoring '|' to the skipper:

    Live On Coliru

    qi::phrase_parse(
            f, l, *(qi::double_>>!qi::lit('%')) >> *(qi::double_>>'%'),
            qi::blank | '|', v, w);