views:

223

answers:

1

Hej, another question:

I have written a number of very similar parsers which use a number of common rules. Can I store these rule<> objects in a place where they can be acessed by multiple parsers? It looks somehow like this:

rule<> nmeaStart = ch_p('$');
rule<> nmeaAddress = alnum_p() >> alnum_p() >> !alnum_p() >> !alnum_p();
rule<> nmeaDelim = ch_p(',');
rule<> nmeaHead = nmeaStart >> nmeaAddress >> nmeaDelim;
...
/* other rules. Different for each parser*/
...
rule<> nmeaChkSumStart = ch_p('*');
rule<> nmeaChkSum = int_parser<unsigned int,16,2,2>();
rule<> nmeaTail = nmeaChkSumStart >> nmeaChkSum >> eol_p;

I would like to put all rules named nmea... into a common place, preferably into a protected static variable of some class. I guess the thing called Grammar in the Spirit-documentation is the key but honestly I don't understand much about it yet.

Many thanks! Cheers!

+1  A: 

They're just normal variables, you don't need to do anything special. So in your header write:

class nmea {
protected:
    static rule<> start, address;
};

And in your implementation file:

rule<> nmea::start = ch_p('$');
rule<> nmea::address = ch_p('$');
// etc.

Although, I think it's recommended that you use member variables.

Daniel James