Boost logo

Boost :

Subject: Re: [boost] Parsing commands with Spirit
From: Stewart, Robert (Robert.Stewart_at_[hidden])
Date: 2010-02-02 10:49:55

edouard_at_[hidden] wrote:
> On Tue, 2 Feb 2010 07:13:27 -0600, "Hartmut Kaiser"
> <hartmut.kaiser_at_[hidden]> wrote:
> > typedef iterator_range<char const*> range_type;
> > typedef std::pair<range_type, range_type> result_type;
> > rule<char const*, result_type()> r =
> > "command1" >> raw[+(!lit("separator") >> char_)] >>
> "separator"
> >>
> > "command2" >> raw[+alnum];
> >
> Then I have an additional question, if you mind, as I said the second
> problem we have is that my command parameter may contain the
> separator, we get around this by parsing right to left. This is not
> something we can do
> much about it, unless we encode all the commands parameters which is a
> thing we cannot realistically do.
> Let me give you an example:
> command1/aaa/bbb/ccc/ddd
> We currently extract "aaa/bbb/ccc" and "ddd".
> The way I want to do this in Spirit is extract "aaa/bbb/ccc/ddd" and
> manually extract "ddd" from it. The other possibility is to
> extract "aaa", "bbb", "ccc" and "ddd" and since I have a pair of
> pointers just merge the pair "aaa" => "ccc".

If those delimiters appear as shown, why not parse for them?

   component = +(~ch_p('/'))
   command1 =
>> component // aaa
>> '/'
>> component // bbb
>> '/'
>> component // ccc
>> '/'
>> component // ddd

I've not attached any semantic actions, but that should be a fairly easy application of what Hartmut showed already. You'd only need the start of what matches the first component (sub)rule and the end of what matches the third one, to span "aaa/bbb/ccc," of course.

Another approach is to allow for the delimiter in the middle, but not the end, though you need to match EOL or something to signal the end of the command:

   "command1/" >> +(alpha_p | '/') >> '/' >> +alpha_p >> eol_p

Rob Stewart robert.stewart_at_[hidden]
Software Engineer, Core Software using std::disclaimer;
Susquehanna International Group, LLP

IMPORTANT: The information contained in this email and/or its attachments is confidential. If you are not the intended recipient, please notify the sender immediately by reply and immediately delete this message and all its attachments. Any review, use, reproduction, disclosure or dissemination of this message or any attachment by an unintended recipient is strictly prohibited. Neither this message nor any attachment is intended as or should be construed as an offer, solicitation or recommendation to buy or sell any security or other financial instrument. Neither the sender, his or her employer nor any of their respective affiliates makes any warranties as to the completeness or accuracy of any of the information contained herein or that this message or any of its attachments is free of viruses.

Boost list run by bdawes at, gregod at, cpdaniel at, john at