Boost logo

Boost :

From: Jorge Lodos Vigil (lodos_at_[hidden])
Date: 2007-11-23 12:43:39


Dave Jenkins wrote:
>
> > Hi
> > We are using xpressive with a grammar to match certain patterns. In
> > some cases, we have the need to ignore white spaces.
> > Using dynamic regexes, this can be achieved with the
> > ignore_white_space constant.
> > Is there a way to ignore white spaces using a grammar in xpressive
> > other than modifying the grammar itself?
>
> How about using a filter_iterator to skip spaces. Something
> like the following program:
>
> #include <iostream>
> #include <string>
> #include <boost/config.hpp>
> #include <boost/iterator/filter_iterator.hpp>
> #include <boost/range/iterator_range.hpp> #include
> <boost/xpressive/xpressive_static.hpp>
>
> struct not_space {
> inline bool operator()(char ch) const { return ' ' != ch; } };
>
> typedef boost::filter_iterator<not_space,
> std::string::const_iterator> Iter_Skip;
>
> boost::iterator_range<Iter_Skip> make_range (std::string& s) {
> return boost::make_iterator_range(
> boost::make_filter_iterator<not_space>(s.begin(), s.end()),
> boost::make_filter_iterator<not_space>(s.end(), s.end())
> );
> }
>
> int main()
> {
> using namespace boost::xpressive;
>
> std::string s = "aa bb cc";
>
> typedef basic_regex<Iter_Skip> regex_skip;
> regex_skip rx = as_xpr("aa") >> "bbcc";
> match_results<Iter_Skip> what;
>
> if(!regex_match(make_range(s), what, rx))
> std::cout << "not found\n";
> else
> std::cout << "found\n";
> return 0;
> }

Thank you for your help. Unfortunately our grammar definition have spaces, we can not remove all of them. I'll think more about your idea of modifying the way to traverse the string, perhaps we may do some sort of preprocessing.
Thanks again!

Cheers
Jorge


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