|
Boost Users : |
Subject: Re: [Boost-users] [spirit] help parsing data file into a struct
From: OvermindDL1 (overminddl1_at_[hidden])
Date: 2009-12-18 05:33:34
On Thu, Dec 17, 2009 at 11:44 AM, Kenny Riddile <kfriddile_at_[hidden]> wrote:
> This is my first time using Spirit and I haven't messed with writing EBNF
> grammars since college, so I'm sorry if this is a simple question. Â Lets say
> I have a structure that looks like this:
>
> struct Foo
> {
> Â Â std::string someString;
> Â Â std::string someOtherString;
> Â Â std::vector<std::string> stringList;
> };
>
> BOOST_FUSION_ADAPT_STRUCT(
> Â Â Foo,
> Â Â (std::string, someString)
> Â Â (std::string, someOtherString)
> Â Â (std::vector<std::string>, stringList) )
>
> and I have a data file that looks like this:
>
> SOMESTRING hello
> SOMEOTHERSTRING goodbye
>
> STRINGLIST
> {
> Â Â string1
> Â Â string2
> Â Â string3
> }
>
> I have written the following grammar to parse such a file into the struct
> above:
>
> template< typename Iterator >
> struct FooDefinition : spirit::qi::grammar< Iterator, Foo() >
> {
> Â Â FooDefinition()
> Â Â Â Â : FooDefinition::base_type( start )
> Â Â {
> Â Â Â Â using namespace spirit::qi;
>
> Â Â Â Â start = someString >> +space >> someOtherString >> +space >>
> stringList;
> Â Â Â Â someString = lit("SOMESTRING") >> +space >> value;
> Â Â Â Â someOtherString = lit("SOMEOTHERSTRING") >> +space >> value;
> Â Â Â Â stringList = lit("STRINGLIST") >> *space >> lit('{') >> *space >>
> (value % +space) >> *space >> lit('}');
> Â Â Â Â value = +char_("a-zA-Z_0-9");
> Â Â Â Â space = lit(' ') | lit('\n') | lit('\t');
> Â Â }
>
> Â Â spirit::qi::rule< Iterator, Foo() > start;
> Â Â spirit::qi::rule< Iterator, std::string() > someString;
> Â Â spirit::qi::rule< Iterator, std::string() > someOtherString;
> Â Â spirit::qi::rule< Iterator, std::vector<std::string>() > stringList;
> Â Â spirit::qi::rule< Iterator, std::string() > value;
> Â Â spirit::qi::rule< Iterator > space;
> };
>
> However, I would like the order of the three statements in the data file
> (SOMESTRING, SOMEOTHERSTRING, and STRINGLIST) to be irrelevant. Â Right now,
> they must be in the order shown above for parsing to succeed. Â How would I
> go about accomplishing this with Spirit?
That is what the permutation operator is for ( ^ ), and it acts like,
well, a permutation. :)
This is also answered faster if asked on the Spirit mailing list.
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