|
Boost Users : |
Subject: Re: [Boost-users] [spirit] need help with the parser
From: OvermindDL1 (overminddl1_at_[hidden])
Date: 2010-05-05 22:05:36
On Wed, May 5, 2010 at 2:08 PM, Andrzej Krzemienski <akrzemi1_at_[hidden]> wrote:
> Hi,
> Could someone help me understand why my fairly simple spirit::qi
> parser does not work as I expect?
> I try to parse string "2 c" into the structure:
>
> Â struct Data {
> Â Â char val;
> Â };
>
> Â std::map<int, Data> map;
>
> such that the map contains one element with an int 2 as key and char
> "c" wrapped into structure Data as the value. I am using Boost 1.42.
> The rules for the grammar w/o semantic actions are as follows.
>
> Â value = qi::char_;
> Â list = (qi::int_ >> value); // the start rule
>
> I know the grammar could be expressed more simply; my real life
> example is more complex and makes sense, but I tried to reduce it as
> much as possible.
> The full grammar definition reads:
>
> Â typedef std::string::const_iterator Iter;
> Â BOOST_FUSION_ADAPT_STRUCT(Data,(char, val));
>
> Â struct TestGrammar
> Â : qi::grammar<Iter, std::map<int,Data>(), ascii::space_type>
> Â {
> Â Â qi::rule<Iter, std::map<int,Data>(), ascii::space_type> list;
> Â Â qi::rule<Iter, Data(), ascii::space_type> value;
>
> Â Â TestGrammar() : TestGrammar::base_type(list) {
> Â Â Â value = qi::char_[ at_c<0>(_val) = _1 ];
> Â Â Â list = (qi::int_ >> value)[ _val[_1] = _2 ];
> Â Â }
> Â };
>
> The code that does the parsing reads
>
> Â int main( ) {
> Â Â std::string input = "2 c";
> Â Â Iter iter = input.begin(), end = input.end();
> Â Â TestGrammar grammar;
> Â Â std::map<int,Data> map;
> Â Â bool ans = phrase_parse( iter, end, grammar, space, map );
>
> Â Â assert( ans );
> Â Â assert( iter == end );
> Â Â BOOST_AUTO( it, map.find(2) );
> Â Â assert( it != map.end() );
> Â Â assert( it->second.val == 'c' ); // FAILS!
> Â }
>
> I marked my expectations with assertions. The parsing does succeed,
> i.e. phrase_parse returns true and the entire input is consumed, the
> key of the map's element is right, but its value is somehow lost. Can
> anyone tell what I am missing? I enclose the entire program code in
> the attachment.
What version are you using? Boost 1.42? 1.43? Trunk?
This looks like it should work in 1.43, or at least the trunk, but
there was a known bug with single element structs before that.
This is usually better to ask 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