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? -- Cory Nelson http://www.int64.org
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
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?
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
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
-- Cory Nelson http://www.int64.org
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
Jeffrey Holle wrote:
Its because the State object is held by reference. The const parameter forces the grammar object to be stateless. This is by design.
(I'm new to this, too.) If the grammar object is stateless, how was I able to add a closure to it? Or is it just the base class (grammar) part of it that has to be stateless? -- Dick Hadsell 914-259-6320 Fax: 914-259-6499 Reply-to: hadsell@blueskystudios.com Blue Sky Studios http://www.blueskystudios.com 44 South Broadway, White Plains, NY 10601
Richard Hadsell wrote:
Jeffrey Holle wrote:
Its because the State object is held by reference. The const parameter forces the grammar object to be stateless. This is by design.
(I'm new to this, too.) If the grammar object is stateless, how was I able to add a closure to it? Or is it just the base class (grammar) part of it that has to be stateless?
I almost missed this thread. I'd suggest you continue this thread in Spirit's mailing list: Spirit-general@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/spirit-general It's the proper venue. Thanks, -- Joel de Guzman http://www.boost-consulting.com http://spirit.sf.net
participants (4)
-
Cory Nelson
-
Jeffrey Holle
-
Joel de Guzman
-
Richard Hadsell