[antlr-interest] Tree construction rewrite rule error
Stephen Tuttlebee
themightystephen at googlemail.com
Sat Mar 5 16:08:26 PST 2011
Hi there,
I'm currently using the Java.g parser grammar (by Yang Jiang) from
http://openjdk.java.net/projects/compiler-grammar/ and am trying to add
tree construction rewrite rules and operators to build an AST. That's
quite a job in itself (I'm trying roughly to base it on one I found in
the mercurial repository on that site that contained actions that
created a javac-style AST).
Anyway, my specific question relates to the LAST ALTERNATIVE in the
following rule (from the "expression hierarchy"):
unaryExpressionNotPlusMinus
: TILDE unaryExpression -> ^(BITWISE_COMPLEMENT unaryExpression)
| BANG unaryExpression -> ^(LOGICAL_COMPLEMENT unaryExpression)
| castExpression
| {inHandlerDeclaration||inRunMethodDeclaration}?=>
inSynchronizationExpression // only allowed within handler declarations
(which in turn are inside component definitions)
| primary -> primary
(selector -> selector[$unaryExpressionNotPlusMinus.tree])*
( PLUSPLUS -> ^(POSTINC $unaryExpressionNotPlusMinus)
| SUBSUB -> ^(POSTDEC $unaryExpressionNotPlusMinus)
)?
;
The last alternative does look a little complicated but it is basically
creating a single tree from the 'primary' in case nothing else is
matched (due to the * and ? that mean we could not match anything),
while the subsequent rewrite rules (->) within the alternative are
successively building up the tree by making the tree constructed thus
far a child of a newly created tree (this is done by referencing the
rule itself -- referencing $unaryExpressionNotPlusMinus in the context
of a rewrite rule means "take the current value of the tree of
unaryExpressionNotPlusMinus").
However, when I run the grammar through ANTLR (3.3) I get the following
errors:
error(100): /JavaBTranslator/src/JavaBPhase1SynSem1.g:1026:19: syntax
error: antlr: expecting RPAREN, found '->'
|---> (selector -> selector[$unaryExpressionNotPlusMinus.tree])*
error(100): /JavaBTranslator/src/JavaBPhase1SynSem1.g:1027:22: syntax
error: antlr: expecting RPAREN, found '->'
|---> ( PLUSPLUS -> ^(POSTINC $unaryExpressionNotPlusMinus)
error(10): internal error: /JavaBTranslator/src/JavaBPhase1SynSem1.g :
java.lang.NullPointerException
3 errors
It seems that ANTLR does not like the -> that appear in the middle of
the alternative for some reason. The problem could be a simple ANTLR
syntax thing which I'm not clear about.
Anyone have any ideas about why I'm getting these errors.
Thanks,
Stephen
PS. also, it seems that coming up with the tree grammar rules to
recognise the tree might be a little harder than constructing the tree
in the parser, at least for the more complicated tree rewrite rules like
this. But I'll have to cross that bridge when I come to it.
More information about the antlr-interest
mailing list