[antlr-interest] Antlr and Java Generics

Sam Barnett-Cormack s.barnett-cormack at lancaster.ac.uk
Tue Mar 24 05:01:59 PDT 2009


Anders Hessellund wrote:
> Hi,
> 
> I've been googling and reading FAQs for some information about how Antlr
> and Java Generics relate but nothing significant has shown up.
> 
> - Does anyone here know of a good tutorial or perhaps just an example
> grammar?
> 
> - One of the thing that I would like was to use generic methods in my
> parser, such as
> 
>     public final <T extends Number<T>> T arithmeticExpr() throws
>     RecognitionException {
>     ..
>     }
> 
> 
> How can I achieve that (if possible)?

Is that even a valid generic declaration? For starters, Number isn't a
generic type, so Number<T> isn't valid. Then, Java can't specify a
generic solely by return type - it has to be specified partly through a
formal parameter, or through a type parameter to the enclosing class.
Finally, why would you want this to be generic? With the flexibility of
Number, why not just make the return type Number?

To answer the ANTLR question, though, as far as I understand it ANTLR
does not generate generic code. At all. However, as members{} is passed
straight through, you could create generic methods there. That wouldn't
relate to parse rules, though.

Sam


More information about the antlr-interest mailing list