[antlr-interest] Rewind one token?

Ric Klaren klaren at cs.utwente.nl
Thu Aug 19 04:56:09 PDT 2004


On Thu, Aug 19, 2004 at 09:55:05AM -0400, Ingo Maier wrote:
> On Mon, 2004-08-16 at 11:40, Monty Zukowski wrote:
> > Giving different rules a try is exactly what syntactic predicates are
> > for.  Perhaps if you could post a small example that shows the problem
> > we can help out.
> >
> > Monty
>
> I want to merge two parsers with each having its own lexer. Let's take
> OCL and Java as an example. The following input:
>
> context: Foo
> inv: 42==42
>
> class Foo {}
>
> Give OCL a try, it parses til (including) "class" because a boolean
> operator and additional expressions may follow 42==42. But at "class"
> the OCL parser fails. Now, I want to start the Java parser. However, it
> needs the "class" token. Not as a token, but as a character sequence
> because the Java lexer may produce a different token out of it.
>
> So I assume this has to be done on the character level. Does ANTLR
> supports me with my problem in any way?

As I said you might be able to use mark/rewind. E.g. when you make your
parsers/lexers make sure both lexers share the same InputBuffer (CharBuffer
in java) else things won't work.

When you start a 'trial' run mark the position run the lexer/parser then if
it throws rewind to the stored position. If it succeeds call commit and try
for the next chunk of input. The whole use is not that different as for
syntactic predicates.

My guess is that you can do this with the vanilla CharBuffer. There's a
slight chance that you have to make a custom version that allows a better
internal state reset.

Cheers,

Ric
--
-----+++++*****************************************************+++++++++-------
    ---- Ric Klaren ----- j.klaren at utwente.nl ----- +31 53 4893755  ----
-----+++++*****************************************************+++++++++-------
  Quidquid latine dictum sit, altum viditur.
                 (Whatever is said in Latin sounds profound.)



 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/antlr-interest/

<*> To unsubscribe from this group, send an email to:
    antlr-interest-unsubscribe at yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 



More information about the antlr-interest mailing list