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 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(), ascii::space_type>
{
qi::rule(), ascii::space_type> list;
qi::rule 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 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.
Thanks in advance for help.
Regards,
&rzej