(Spirit) Semantic Actions with member functions?
I am playing around with Spirit and I seem to run into an architecture problem with semantic actions. Here's a small example. class MyGrammar : public grammar<MyGrammar> { public: template <typename ScannerT> struct definition { definition(MyGrammar const& self) { TestRule = str_p("test")[&my_action]; } rule<ScannerT> const& start() const { return TestRulet; }; private: rule<ScannerT> TestRule; }; }; My major problem is related to the fact that I cannot pass any custom data to my_action function. Since my_action is a global function, it has no knowledge of MyGrammar and must rely on global variables. Ideally, I would pass self to my_action to be able to hold state. What is a general way to get around this problem? The documention mentions a way to specify member functions as semantic action actors using boost's bind facilities but I was unable to get it to work. Thanks in advance. P.S. Sorry for a fairly newbie question, I was unable to find the answer online or in the docs. P.P.S. I might have accidently double posted this. Sorry about that.
Hi, Please see the spirit_bind.cpp sample in the libs\spirit\example\fundamental directory. BTW, Spirit has its own mailing list. Please send future posts there. Thanks, -- Joel de Guzman joel at boost-consulting.com http://www.boost-consulting.com http://spirit.sf.net Slava Akhmechet wrote:
I am playing around with Spirit and I seem to run into an architecture problem with semantic actions. Here's a small example.
class MyGrammar : public grammar<MyGrammar> { public: template <typename ScannerT> struct definition { definition(MyGrammar const& self) { TestRule = str_p("test")[&my_action]; }
rule<ScannerT> const& start() const { return TestRulet; };
private: rule<ScannerT> TestRule; }; };
My major problem is related to the fact that I cannot pass any custom data to my_action function. Since my_action is a global function, it has no knowledge of MyGrammar and must rely on global variables. Ideally, I would pass self to my_action to be able to hold state. What is a general way to get around this problem? The documention mentions a way to specify member functions as semantic action actors using boost's bind facilities but I was unable to get it to work. Thanks in advance.
P.S. Sorry for a fairly newbie question, I was unable to find the answer online or in the docs. P.P.S. I might have accidently double posted this. Sorry about that.
Its time to switch your global function "myaction" into a functor. Functors are classes that have a function call operator with the same synopsis as your function. The advantage is that one can pass custom data to it via a non-default constructor. Functors should be and sometimes must be stateless, so care must be employeed to use references in member data. Slava Akhmechet wrote:
I am playing around with Spirit and I seem to run into an architecture problem with semantic actions. Here's a small example.
class MyGrammar : public grammar<MyGrammar> { public: template <typename ScannerT> struct definition { definition(MyGrammar const& self) { TestRule = str_p("test")[&my_action]; }
rule<ScannerT> const& start() const { return TestRulet; };
private: rule<ScannerT> TestRule; }; };
My major problem is related to the fact that I cannot pass any custom data to my_action function. Since my_action is a global function, it has no knowledge of MyGrammar and must rely on global variables. Ideally, I would pass self to my_action to be able to hold state. What is a general way to get around this problem? The documention mentions a way to specify member functions as semantic action actors using boost's bind facilities but I was unable to get it to work. Thanks in advance.
P.S. Sorry for a fairly newbie question, I was unable to find the answer online or in the docs. P.P.S. I might have accidently double posted this. Sorry about that.
*Yahoo! Groups Sponsor*
http://rd.yahoo.com/M=247865.3269369.4566997.1261774/D=egroupweb/S=170500678...
Info: http://www.boost.org Wiki: http://www.crystalclearsoftware.com/cgi-bin/boost_wiki/wiki.pl Unsubscribe: mailto:boost-users-unsubscribe@yahoogroups.com
Your use of Yahoo! Groups is subject to the Yahoo! Terms of Service http://docs.yahoo.com/info/terms/.
participants (3)
-
Jeff Holle
-
Joel de Guzman
-
Slava Akhmechet