Boost logo

Boost :

From: Hartmut Kaiser (hartmutkaiser_at_[hidden])
Date: 2003-08-21 14:07:27


Paul Hamilton wrote:

> I want to parse "," separated lists of strings.
>
> I have:
>
> char const* plist_wo_item = "x, y, z";
> rule<> list_wo_item;
> std::vector<std::string> vec_list;
>
> list_wo_item =
> (*anychar_p) >> *( ',' >>
> (*anychar_p)[append (vec_list)])
> ;
>
> parse_info<> result = parse (plist_wo_item, list_wo_item);
> if (!result.hit)
> return false;
>
> This only places 1 item into vec_list.

The first *anychar_p eats up all your input (it matches zero or more
occurences of any char :-). There are several ways to circumvent this
problem:

    list_wo_item = (*(anychar_p - ','))[append (vec_list)]
>> *( ',' >> (*(anychar_p - ','))[append (vec_list)] )
        ;

which is very verbose but functional.
To simplify the expression, try this:

    list_wo_item = list_p((*anychar_p)[append(vec_list)], ',');

which does the required refactorings (parser transformations) behind the
scene.

BTW, to skip the whitespace in between the list items, you may want to
use a skip parser while calling the parse function:

    parse_info<> result = parse (plist_wo_item, list_wo_item, space_p);

HTH
Regards Hartmut


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk