Boost logo

Boost :

From: Jia Pu (jiapu79_at_[hidden])
Date: 2004-04-05 00:24:57


I am trying to write a parser which skipps white space and C, Java style
comments. To get familiar with spirit, I did some experiments, here is a
question.

First, I write a simple grammar to accept some non-white space chars.
////////////////////////////////////////////////////////////////////////////////
struct FOO_GRAMMAR : public grammar<FOO_GRAMMAR>
{
        template <typename ScannerT>
        struct definition
        {
                typedef rule<ScannerT> rule_t;

                definition(ABNF_GRAMMAR const & self)
                {
                        r_LETTER =
                                range_p('a','z')
                                | range_p('A','Z')
                                | '.';
                        r = (+r_LETTER);
                }

                rule_t r, r_LETTER;
                rule_t const& start() const { return r; }

        };
};
////////////////////////////////////////////////////////////////////////////////

I used FOO_GRAMMAR with space_p as skip parser:
//////////////////////////////////////////////////////////////////////////////
FOO_GRAMMAR g;
std::string line = "This is a test parser.";
parse_info<> info = parse(line.begin(),line.end(), g, space_p);
////////////////////////////////////////////////////////////////////////////////
This works perfectly.

Then, I tried to wrap the skip parser in a skip grammar, since eventually I
need a more complicated parser. So I wrote the skip grammar as:
///////////////////////////////////////////////////////////////////////////////
struct SKIP_GRAMMAR : public grammar<SKIP_GRAMMAR>
{
        template <typename ScannerT>
        struct definition
        {
                typedef rule<ScannerT> rule_t;

                definition(SKIP_GRAMMAR const & self)
                {
                        rule_t r_SKIP = space_p;
                }

                rule_t r_SKIP;
                rule_t const& start() const { return r_SKIP; }
        };
};
/////////////////////////////////////////////////////////////////////////

I tried to parse the same sentence as before:
///////////////////////////////////////////////////////////////////////////
FOO_GRAMMAR g;
SKIP_GRAMMAR skip;
std::string line = "This is a test parser.";
parse_info<> info = parse(line.begin(),line.end(), g, skip);
//////////////////////////////////////////////////////////////////////////

This time the parsing failed.

Can anyone tell me why? I thought grammars are just like rules.

And any suggestion on building a parser skipping certain pattern? I just can
figure it out by reading the doc.

Thank you.

_________________________________________________________________
Persistent heartburn? Check out Digestive Health & Wellness for information
and advice. http://gerd.msn.com/default.asp


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