Boost logo

Boost Users :

Subject: Re: [Boost-users] Simple fast parsing
From: Hartmut Kaiser (hartmut.kaiser_at_[hidden])
Date: 2009-06-21 17:18:59


> I am currently using in a project a custom-writen recursive-decent
> parser, which uses an filtering_istream so I can support compressed
> input.
>
> The only functionality used is peekchar, getchar and integer
> extraction with 'stream >> i'.
>
> I am finding that C++ streams, particularly when it comes to integer
> extraction, are giving terrible performance. In a small test,
> uncompressing to a buffer and then writing a simple custom reader on
> that memory buffer gives 10x performance.
>
> I would perfer not to write and support such a tool if possible. Is
> there a boost library which easily supports a parser with 1 character
> lookahead and simple things like integer extraction?
>
> Looking at the documentation, Boost::Spirit seems like a very big
> hammer to crack this quite small nut, and it is unclear to me how well
> it would fit into an existing recursive decent parser. Has anyone ever
> used it as such? Is there a simple alternative?

Spirit (which is recursive decent) is definitely your friend. Especially the
new version (V2.1), which is to be released with Boost V1.41, but in SVN
trunk now and quite mature already. Parsing an integer from a buffer of
characters is as easy as:

    std::string buffer("1234");
    int value = 0;

    using namespace boost::spirit;
    bool r = qi::parse(buffer.begin(), buffer.end(), int_, value);
    assert(r && value == 1234);

And BTW: measurements show that the Spirit int_ parser is faster than atoi
(gcc/vc using their respective crtlib)!

Regards Hartmut


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net