[antlr-interest] Java Grammar
Simon
cocoa at gmx.ch
Fri Nov 21 12:56:37 PST 2008
hi
I'm trying to build an AST for a Java like language. The hardest part
(if you want to built a meaningful AST) is the section of
unaryExpressionNotPlusMinus (see grammar fragments at end or the
Java.g grammar on antlr.org).
I have successfully built ASTs for the following constructs (using
semantic predicates based on a symbol table)
^(FIELD_ACCESS target Identifier)
^(INVOKE target Identifier arguments)
^(ARRAY_ACCESS target expr)
However, I'm struggling with fully qualified type names, such as those
in
java.lang.Integer.parseInt("123")
Of course, I want something like
^(INVOKE ^(TYPE_REFERENCE ...) arguments)
The problem is that I somehow have to look ahead to detect whether it
is a qualified type name (don't know how the precedence is if there is
a variable named java with a field named lang that has a field named
Integer that has method named parseInt, but that's another problem). I
could write my own semantic predicate method that looks ahead in the
input to detect a qualified type name. Is there an easier way to do
that? Or am I approaching the problem from the wrong side?
I've tried to look at the Java grammar from langtools recently posted
in this list, but didn't get any smarter (they rely heavily on the
existing javac classes).
Thanks
Simon
unaryExpressionNotPlusMinus
: ...
| primary selector* ('++'|'--')?
;
primary
: parExpression
| literal
| 'new' creator
| Identifier ('.' Identifier)* identifierSuffix? // this is
the hard / interesting part
| primitiveType ('[' ']')* '.' 'class'
| 'void' '.' 'class'
;
identifierSuffix
: ('[' ']')+ '.' 'class'
| ('[' expression ']')+ // can also be matched by selector, but
do here
| arguments
| '.' 'class'
;
selector
: '.' Identifier arguments?
| '[' expression ']'
;
More information about the antlr-interest
mailing list