I have trouble with boost spirit to parse a file like that :
int [int, int, int] [ int, int]
...
Nothing really hard, the following grammar works for that:
template<typename Iterator>
struct parser_expression : qi::grammar<Iterator,ascii::space_type>
{
parser_expression() : parser_expression::base_type(start) {
using qi::double_;
using qi::int_;
using boost::spirit::qi::char_;
using qi::alpha;
using qi::alnum;
using qi::digit;
using qi::eps;
using qi::_val;
using boost::phoenix::bind;
start = int_ >> list1 >> list2 >> char_('=');
list1 = ('[' >> int_ >> *(char_(',') >> int_ ) >> char_(']')) | (char_('[') >> char_(']'));
list2 = ('[' >> int_ >> *(char_(',') >> int_ ) >> char_(']')) | -(char_('[') >> char_(']'));
}
qi::rule<Iterator,ascii::space_type> start;
qi::rule<Iterator,ascii::space_type> list1;
qi::rule<Iterator,ascii::space_type> list2;
};
My problem is that I need to save the result of parsing. For example, I need to save the list1 and list2 of int to a custom list template :
template <typename T>
class SimpleLinkList {
private:
ChainLink<T>* head;
...
}
where ChainLink is :
template<typename T>
class ChainLink {
private:
T object;
ChainLink* next;
...
}
I have a method pushback in SimpleLinkList just like vector but I don't understand how to parse int, save it to a ChainLink and add it to a SimpleLinkList.
I've already seen how to adapt a template structure to a fusion sequence at http://boost-spirit.com/home/2010/02/08/how-to-adapt-templates-as-a-fusion-sequence/.
I need a custom LinkList to be able to delete and add items while looping on it.
I need help to understand how I can arrange all of that to successfully parse my file.
Thx for your help.
You're probably looking for the container attribute customization points:
For your type, it would look like:
A simple demonstration (using a dummy implementation of
SimpleLinkList
):Note
qi::char_
by (implicit)qi::lit
where possible, because you don't actually want to parse the punctuation characters into the attribute (right?)%
instead of the verbose alternative-
to make the element list optional (allow zero elements)list >> -list
to make the second list optional alltogether.The following testcases:
Print the output:
See it all integrated: http://ideone.com/odqhBz. Preventing linkrot: