Boost: Spirit Reuse Rules

Hey, one more question:

I wrote some very similar parsers that use a number of general rules. Can I save this rule <> of objects in a place where they can be processed by several parsers? It looks something 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 the rules named nmea ... in a normal place, preferably in a protected static variable of some class. I think that the thing called Grammar in the Spirit-documentation is the key, but to be honest, I don’t understand anything about it yet.

Many thanks! Hooray!

+6
c ++ boost-spirit
source share
1 answer

These are ordinary variables, you do not need to do anything special. Therefore, in the title 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 he recommended using member variables.

+2
source share

All Articles