Re: Parsing data using istream_iterator

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Jeff F (not_at_anywhere.com)
Date: 10/29/04


Date: Fri, 29 Oct 2004 08:18:04 -0400

Daniel,

Parsing tasks are best done with parsers. See
http://www.boost.org/libs/spirit/index.html for an embedded C++ parser
framework. There was also an article on www.codeproject.com, IIRC.

"Daniel Lidström" <someone@microsoft.com> wrote in message
news:1svdm5o1477u4$.18g82c87qj5hg$.dlg@40tude.net...
> On Fri, 29 Oct 2004 10:51:25 +0200, Daniel Lidström wrote:
>
> > Hi,
> >
> > I'm writing routines for parsing data that comes in this form:
> >
> > string 3d {
> > z 10
> > colour "white"
> > data {
> > x-value y-value z-value
> > . . .
> > . . .
> > }
> > }

The corresponding spirit parser could be defined _approximately_ like:

    using boost::spirit;

    file_iterator beg( "mydatafile" ), end;

    point pt;

    std::vector<point> points;

    rule<> rPoint = real_p[assign(pt.x)] >> real_p[assign(pt.y)] >>
real_p[assign(pt.z)];

    rule<> rData = str_p("data") >> '{' >> +rPoint[Append(points,point)] >>
'}';

    rule<> rColor = str_p("colour") >> '"' >> +alpha_p >> '"';
    rule<> rZ = chr_p('z') >> uint_p;

    rule<> r3d = str_p("string") >> "3d" >> '{' >> rZ >> rColor >>
rData>> '}';

    parse( beg, end, r3d, space_p );

This is just meant to give you a flavor of what you can do. The details are
slightly more involved. You can read up on spirit at the links above.

Jeff F



Relevant Pages

  • Re: Instantiating objects from XML
    ... First, writing your own parser is possible, but as you have discovered, it can be intrusive to your domain classes. ... additional comments were added in for server side classes so the factory processes of the framework could generate the appropriate in/out behavior for processing SOAP request. ...
    (comp.lang.smalltalk.dolphin)
  • ANN: PyBison - high-performance Python Parser
    ... Announcing PyBison, a framework for writing high-performance, full ... Bison/Yacc-generated C parser code. ... Users write a Parser class in Python. ...
    (comp.lang.python)
  • Re: Generic protocol builder/parser
    ... and building various protocols? ... Do you know of any existing framework or tool that might help? ... Use a functional language like OCaml, Haskell or F# and write parser ... What do you mean by 'parser combinators?' ...
    (comp.programming)
  • Re: Pascal analyzer - Yacc?
    ... It contains the parser used for, ... others, the fpdoc documentation generator. ... IIRC, the first FPC compiler versions were based on a YACC generated ...
    (comp.lang.pascal.delphi.misc)
  • Re: Type of a Skip Parser?
    ... I've worked with spirit a little. ... I'm aiming to allow mutual left-recursion between ... At this point I have to ask: are you aware of the Boost::Spirit parser ... combinator framework? ...
    (comp.lang.functional)