Boost logo

Boost Users :

From: brockpeabody (brock.peabody_at_[hidden])
Date: 2003-04-04 14:39:10


Does anyone know the best way to return a rule from a function by
value? I know that the following does not work, but what's the best
workaround?

    struct calculator : grammar<calculator> {

        template <typename ScannerT> struct definition {

            definition(const calculator&) {
 
                 r = make_complex_rule('A') | make_complex_rule('B');
            }

            typedef rule<ScannerT> rule_type;

            rule_type const& start() { return r;}

            private:
 
            rule_type make_complex_rule(char c) {

                return c >> real_p; //boom
            }

            rule_type r;
        };
    };

my current workaround is the following nastiness:

        //inside struct calculator::definition<>

        rule_type& anonymous_rule() {

            anonymous_rules.push_back(
                boost::shared_ptr<rule_type>(new rule_type));

            return *anonymous_rules.back();
        }

        rule_type& make_complex_rule(char c) {

            return anonymous_rule() = c >> real_p;
        }

        std::vector<boost::shared_ptr<rule_type> > anonymous_rules;

Is there a better way?

Also, I've got an easy one... Why is a grammar passed by const
reference to its definition? Does Spirit make copies of the grammars
internally? I ask because otherwise the grammar class seems like the
ideal place to store state information (like numbers on a calculator's
stack).

Thanks ahead of time!

Brock Peabody
brock.peabody_at_[hidden]


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