Using Boost Spirit to parse a text file while skip

2019-05-11 15:53发布

问题:

I have the following std::string:

<lots of text not including "label A" or "label B">    
label A: 34
<lots of text not including "label A" or "label B">
label B: 45
<lots of text not including "label A" or "label B">
...

I want extract single integral numbers following all occurrences of label A or label B and place them in corresponding vector<int> a, b. A simple, but not elegant way of doing it is using find("label A") and find("label B") and parsing whichever is first. Is there a succinct way of expressing it using Spirit? How do you skip everything but label A or label B?

回答1:

You can just

omit [ eol >> *char_ - ("\nlabel A:") ] >> eol

Example: Live On Coliru

There's also the seek[] directive in the repository. The following is equivalent to the above:

 repo::seek [ eol >> &lit("int main") ] 

Here's a sample that parses your original sample:

*repo::seek [ eol >> "label" >> char_("A-Z") >> ':' >> int_ ],

This will parse into std::vector<std::pair<char, int> > without anything else.

On Coliru Too:

#if 0
<lots of text not including "label A" or "label B">    
label A: 34
<lots of text not including "label A" or "label B">
label B: 45
<lots of text not including "label A" or "label B">
...
#endif
#include <boost/fusion/adapted/std_pair.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <boost/spirit/repository/include/qi_seek.hpp>
#include <fstream>

namespace qi   = boost::spirit::qi;
namespace repo = boost::spirit::repository::qi;

int main()
{
    std::ifstream ifs("main.cpp");
    ifs >> std::noskipws;

    boost::spirit::istream_iterator f(ifs), l;

    std::vector<std::pair<char, int> > parsed;
    using namespace qi;
    bool ok = phrase_parse(
            f, l, 
            *repo::seek [ eol >> "label" >> char_("A-Z") >> ':' >> int_ ],
            blank,
            parsed
        );

    if (ok)
    {
        std::cout << "Found:\n";
        for (auto& p : parsed)
            std::cout << "'" << p.first << "' has value " << p.second << "\n";
    }
    else
        std::cout << "Fail at: '" << std::string(f,l) << "'\n";
}

Notes:

  • seek does expose the attribute matched, which is pretty powerful:

    repo::seek [ eol >> "label" >> char_("ABCD") >> ':' ] 
    

    will 'eat' the label, but expose the label letter ('A', 'B', 'C', or 'D') as the attribute.

  • Performance when skipping can be pretty surprising, read the warning in the documentation http://www.boost.org/doc/libs/1_55_0/libs/spirit/repository/doc/html/spirit_repository/qi_components/directives/seek.html

Output is

Found:
'A' has value 34
'B' has value 45