Boost logo

Boost Users :

From: Jia Pu (JiaPu_at_[hidden])
Date: 2004-04-05 12:24:52


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.

Jia (Jay) Pu
Software Engineer
LumenVox LLC
----------------------------------------------------------------------------
------------
Anytime a linguist leaves the group the recognition rate goes up.
--Fred Jelinek (then of the IBM speech group) (1988)


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