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
On Wed, May 5, 2010 at 2:08 PM, Andrzej Krzemienski
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.
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. :)
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.
I am using Boost 1.42 and hence Spirit 2.2. single-elem struct is just
a coincidence my problem still appears if I use a two-elem struct
(again, the code enclosed and relevant extracts follow):
struct Data { char val1; char val2; };
BOOST_FUSION_ADAPT_STRUCT(Data,(char, val1)(char, val2));
struct TestGrammar
: qi::grammar
This is usually better to ask on the Spirit mailing list. :) Thanks for the advice, I will try that.
Regards, &rzej
participants (2)
-
Andrzej Krzemienski
-
OvermindDL1