[antlr-interest] ANTLR 3: Problem with static DFA class generation

Oliver Zeigermann oliver.zeigermann at gmail.com
Mon Sep 12 03:04:47 PDT 2005


2005/9/11, Terence Parr <parrt at cs.usfca.edu>:
> 
> On Sep 10, 2005, at 3:16 PM, Oliver Zeigermann wrote:
> 
> > 2005/9/10, Terence Parr <parrt at cs.usfca.edu>:
> >
> >> No matter what, if you have a ref to a parameter in a predicate that
> >> will end up in a cyclic DFA, it will not be visible!  There is
> >> absolutely no way around this.  You can generate an arbitrary DFA in
> >> java code without using ptrs to objects.  Many people have try to
> >> show me some try-finally stuff that will do arbitrary gotos but I
> >> always show a backwards jump that isn't possible.
> >>
> >
> > Maybe I am missing something obvious, but why not making the inner
> > classes non-static? This way you have the implied this pointer and can
> > simply access the fields of the enclosing class.
> >
> > This actually worked for me!!!
> >
> > Turning something like
> >
> >         alt10 = DFA10.predict(input);
> > to
> >         alt10 = new DFA10().predict(input);
> 
> Hi Oliver,
> 
> Yep, I was opposed to creating that memory on each invocation, but
> now that I think about it, it would be not that bad for parsers as
> cyclic DFAs are uncommon.  However, the lexer would get crushed.  Can
> u imagine creating and extra object for every token?

No, you are right, creating an extra object would be a bad idea.
Instead one should rather have a sinle instance for each DFA

like 
class XMLLexer extends Lexer {
...
private dfa10 = new DFA10();
...

alt10 = dfa10.predict(input);
 
and things will work.

> There was a reason that I simply didn't pass in the "this" pointer as
> a parameter.  can't remember why.  Maybe it was because I couldn't
> see inside the code and figure out what variables had to have "this."
> prepended.  There was some really good reason I didn't do this.

Sorry, I guess I haven't made myself quite clear. There is no need to
prepend "this" to any variable or explicitly passing any pointers if
the DFA classes are non-static inner classes. You can simply access
all variables of the enclosing class. It simply works.

I will try if I can adjust the templats to show what I mean.

Oliver


More information about the antlr-interest mailing list