Its because the State object is held by reference. The const parameter forces the grammar object to be stateless. This is by design. Cory Nelson wrote:
I'm just trying to make a simple parser like uint_p. From the source I've dug through it doesn't look like those use grammar classes though it's very possible I'm wrong.
If the object passed through definition() is const, how would state be updated?
On Tue, 16 Nov 2004 21:59:24 -0500, Jeffrey Holle
wrote: Not sure where you are stuck at, given your description, but I'll give it a try.
In order to hook semantic actions into your grammar: 1. You should create a grammar object that houses your grammar. 2. You should use functor action operators.
The first provides a way to expose application objects to the grammar in a maintainable way.
For instance:
struct My_grammar : public grammar
{ My_grammar(State& state) : state_(state) {} template <typename ScannerT> struct definition { definition(My_grammar const& self) { // define grammar here rule<ScannerT> outerRule; rule<ScannerT> const& start() const { return outerRule; } }; State& state_; } In the object example, the action objects can get mutable access to the State object to record what needs recording.
The second "should" involves the actual action operators. These can be either functions or functors. I sugguest using only the later in that they can be used like:
struct captureSA { captureSA(State *pState) : m_pState(pState) {} void operator()(iterator_t first, const iterator_t& last) const; State *m_pState; };
This functor can be attached to a rule like (note: within the body of definition above):
outerRule[captureSA(self.state)]
There is one caviot to this. The signature of the operator() method changes to: void operator()(const char& ch) when the rule matches single characters. Sometimes its necessary to define both methods in an action functor.
Hope that helps.
Cory Nelson wrote:
Okay, I'm trying to learn Spirit and I've got the grammar down. I've tried looking at the documentation but still can not find out how to make a custom parser (that will let me use the [action] operator). Could someone give an example of a pascal string parser (one byte for length, string follows) to get me started?
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users