[antlr-interest] grammar that separates classpath from class name

Scott jackett_dad at yahoo.com
Mon Apr 9 13:28:11 PDT 2012


All,

I'm just starting with Antlr.  I have past experience with JavaCC, but it's been a while.

I'm parsing a fully qualified java type name, and would like to differentiate the classpath from the class name.  So if I were parsing this:

com.company.JavaType

I would like to specify an expression for which the package and class name are distinct nodes in the parse tree.  I copied a Java 5 grammar as a starting point, but that distinction is not made.  I could easily do this separation in a visitor, but since Antlr is so cool, I'd rather that it did the work for me.

The catch is, I don't want to have the last dot character be part of the package name, and I don't want the dot to be part of the class name either.  

So here is what I have so far:

genericTypeArgument : type | QUESTION_MARK (('extends' | 'super') type)?;

type            : qualifiedJavaType arrayModifier* | primitiveType arrayModifier*;

packageDecl        : (Identifier DOT)*;

qualifiedJavaType    : packageDecl? javaType;

javaType        : Identifier genericTypeArguments?;

This works almost.  The package declaration includes the last dot.  Is there a way that I can have the parser ignore(or not include) only the last dot in the package declaration?

Thanks,

Scott


More information about the antlr-interest mailing list