| George Neuner 2007-05-16, 4:13 am |
| On 15 May 2007 11:02:08 -0700, Alex Rubinsteyn
<alex.rubinsteyn@gmail.com> wrote:
>I am implementing a parser combinator library in C++. I know C++ isn't
>a popular language in this group, but my question is about getting the
>type signatures of parsers to match, which is a problem some here may
>have dealt with in other languages. Specifically, I can't figure out
>how implement the skip parser (which consumes input but returns
>nothing).
I'm not sure you can implement this using just associativity with the
stream operators (though if I think about it some more I might come up
with something). To do what you want a composed parser really needs
to be a container so it can control application of and catch results
of sub-parsers.
I would recursively chain the parsers, arranging for results to be
passed from the current parser to the next. Chaining forward this can
be done by passing the result of the current parse as a parameter to
the match function of the next parser. Chaining backward, you can
just use the return value of the previous parser (assuming it is a
(pointer/reference to a) structure containing all the information).
The casualty is your nice stream syntax ... composition becomes a
recursive process of constructing and associating parser objects.
However, the match function of the outer container could still be
called in a sequence using the stream syntax.
Thant's suggestion is equally good although I think passing results
rather than continuations is a bit easier to implement and understand
given the intended application.
>Now, if I wanted to write something like:
>Parser<int, string>* int_in_parens = skip(ch('(')) >> integer >>
>skip(ch(')'))
>
>I encounter all sorts of difficulties. int_in_parens recognizes string
>like "(394)" and should return the enclosed integer.
In your example the parentheses are not really being "skipped" but are
required syntax for the match. This is a semantically different
situation from, say, a parser matching a comment where the matched
text is irrelevant. I do understand why you want to refer to it as
skipping however.
George
--
for email reply remove "/" from address
|