Boost logo

Boost Users :

Subject: Re: [Boost-users] [Boost][spirit] Confused about spaces!
From: Joel de Guzman (joel_at_[hidden])
Date: 2011-06-03 19:45:51


On 6/4/2011 12:32 AM, Germán Diago wrote:
> Hello. I'm trying to implement a very basic parser but I'm stuck at
> the beginning of it.
>
> The parser will be fed with input from a very basic minilanguage to
> execute tests.
>
> I want that the first line begins literally like this:
>
> begin TestActions
>
>
> But with my current implementation I can't:
>
> template <class FwdIterator>
> struct TestFileParser : qi::grammar<FwdIterator, Test (),
> qi::ascii::space_type> {
>
>
> TestFileParser() : TestFileParser::base_type(start_) {
> using namespace boost::spirit::qi;
>
> start_ = lit("begin") >> lit("TestActions");
>
> }
>
>
> qi::rule<FwdIterator, Test (), qi::ascii::space_type> start_;
> };
>
>
> Because this implementation matches "beginTestActions" as well. And I
> want them to be
> different words. If I put a space between them, my rule complains
> because its attribute
> must be a Test(). And if I put in the middel a ' ' it doesn't work
> anymore. What's wrong with spaces?
> I'm using phrase_parse with qi::ascii::space skipper. Thanks in advance.

Use a lexeme and a predicate. Here's an example:

  lexeme[lit("begin") >> !alnum] // make sure we have whole words
>> lit("TestActions");

The expression: >> !alnum makes sure that begin is not immediately
followed by alnum. Being a predicate (using !), its attribute is
ignored by the attribute mechanism.

HTH.

Regards,

-- 
Joel de Guzman
http://www.boostpro.com
http://boost-spirit.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