Boost logo

Boost Users :

Subject: [Boost-users] [spirit] help parsing data file into a struct
From: Kenny Riddile (kfriddile_at_[hidden])
Date: 2009-12-17 13:44:09


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?


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