Parsing statements from ascii files in arbitrary order into hierarchies of structs using boost spirit

Hello!
I'd like to parse one-line statements from an ascii file using boost spirit
into structs. Statements may be quite different and may occur in
arbitrary order. I'm wondering how I can achieve something like the
following:
/qi::rule

Here is an example parser that I wrote based on the tutorials that you should be able to adapt... http://softobjects.co.uk/mediawiki-1.16.0/index.php/Boost::Spirit_Config_Fil... hope it helps -- View this message in context: http://boost.2283326.n4.nabble.com/Parsing-statements-from-ascii-files-in-ar... Sent from the Boost - Users mailing list archive at Nabble.com.

Hi, thanks for the example. Unfortunately, I believe my problem is somewhat more complicated. With your example, statements are all of the same type (except for comments, but these are skipped anyway). I'd like to parse more general statements. These may be specified in any order. I thought I could accomplish the 'any order' thing by using an abstract base type 'Statement' to derive specialized statements from. By parsing statement blocks into std::vectors using something like r_block %= *r_statement; // rule is declared to parse into std::vector<Statement> r_statement = r_statementType0 | r_statementType1 | .. | r_statementTypeN; I would like to retrieve a list of these statements. Problem is that I don't no how to 'BOOST_FUSION_ADAPT_STRUCT()/'/ the hierarchy. Cheers, Stefan On 12/15/2011 06:14 PM, mark pashley wrote:
Here is an example parser that I wrote based on the tutorials that you should be able to adapt...
http://softobjects.co.uk/mediawiki-1.16.0/index.php/Boost::Spirit_Config_Fil...
hope it helps
-- View this message in context: http://boost.2283326.n4.nabble.com/Parsing-statements-from-ascii-files-in-ar... Sent from the Boost - Users mailing list archive at Nabble.com. _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

I have a second example here , this uses BOOST_FUSION_ADAPT_STRUCT: http://www.softobjects.co.uk/mediawiki-1.16.0/index.php/Boost::Spirit_JSON_F... The key bit of interest to you is: struct JsonMember; ///< Forward declaration of JsonObject /** * Declare JsonValue as a bopost variant. * * Note: boost::recursive_wrapper is used to allow the JsonValue to * include a vector of JsonMember which has been forward * declared. * * Note: boost::make_recursive_variant is used to allow the JsonValue to * include a vector of JsonValue. * */ typedef boost::make_recursive_variant< bool , double , std::string , boost::recursive_wrapper< std::vector< JsonMember >
, std::vector< boost::recursive_variant_ > >::type JsonValue; /** * Structure for parse Json members. */ struct JsonMember { std::string name; ///< the name of the member JsonValue value; ///< the value of the member /** constructor */ JsonMember() : name() , value() {} /** * Convenience constructor. * * \param nm the name of the member. * \param val the value for the member. */ JsonMember( const std::string& nm, const JsonValue& val ) : name(nm) , value(val) {} }; /** * Adapt the JsonMember structure using boost::fusion so that it can * be used with boost::spirit::qi. */ BOOST_FUSION_ADAPT_STRUCT( JsonMember, ( std::string, name ) ( JsonValue, value ) ) in this case there is no inheritance hierarchy, but I believe you should be able to simply us BOOST_FUSION_ADAPT_STRUCT in the following way: BOOST_FUSION_ADAPT_STRUCT( DerivedStatement0, ( Type_of_member1, name_of_member1 ) // .... ( Type_of_memberN, name_of_memberN ) ) -- View this message in context: http://boost.2283326.n4.nabble.com/Parsing-statements-from-ascii-files-in-ar... Sent from the Boost - Users mailing list archive at Nabble.com.
participants (2)
-
mark pashley
-
Stefan Zellmann