Boost logo

Boost Users :

From: Eric Niebler (eric_at_[hidden])
Date: 2008-02-19 13:12:05


Zachary wrote:
> Hi,
> I'm a new user of xpresssive and I'd like to first give a big thank
> you for this wonderful tool.

Thanks!

> I'm having problems with the syntax for static regexes. The cheat
> sheet in the documentation says that |(set= 'a','b','c')| and
> |as_xpr('a') | 'b' |'c'| are equivalent yet the example
>
> sregex parentheses;
>
> parentheses = '(' >> *(keep(+~(boost::xpressive::set='(',')'))|by_ref(parentheses)) >> ')';
>
> compiles while
>
> sregex parentheses;
>
> parentheses = '(' >> *(keep(+~(as_xpr('(')|')'))|by_ref(parentheses)) >> ')';
>
> fails to do so (code and abbreviated errors attached).

I see how my docs led you astray. Although they match the same strings,
there actually is a difference between (set='a','b','c') and
(as_xpr('a')|'b'|'c'). The first is a set and can have its complement
taken (e.g., operator~). The second is just a bunch of regexes in
alternate, and cannot have its complement taken.

Consider this a doc bug, which I'll fix.

> Additionally,
> combining the original lines into one construction, like so:
>
> sregex parentheses = '(' >> *(keep(+~(boost::xpressive::set='(',')'))|by_ref(parentheses)) >> ')';
>
> compiles but running the code fails with:
<snip>

This is an invalid use. It's no different than:

   int i = i + 1; // oops!

You may be surprised to find this compiles, but invokes undefined
behavior. It's a sad fact in C++ that objects are in scope before they
are initialized, leading to these sorts of nasty situations.

If you would like to put this all in one line, use xpressive::self, as:

   sregex parentheses = '(' >> *(keep(+~(set='(',')'))|self) >> ')';

HTH,

-- 
Eric Niebler
Boost Consulting
www.boost-consulting.com

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net