[antlr-interest] Rewrite Problem; Rule terminates without exception.

Gavin Lambert antlr at mirality.co.nz
Sun Mar 16 11:44:40 PDT 2008


At 04:09 17/03/2008, Michael Ernst wrote:
 >expression
 >	: ...
 >	| (orExpression ADDITION_ASSIGNMENT) => a=orExpression
 >ADDITION_ASSIGNMENT expression -> ^(ASSIGNMENT $a ^(PLUS $a
 >expression))
 >	...
 >
 >
 >Parsing "a += b;" results in following AST: (ASSIGNMENT a (PLUS
 >b))

Ok, best guess at what's happening here is that it's a runtime 
issue; $a is a tree node object and it's trying to insert the 
exact same object into the tree twice, which will end up adding it 
in one place and then moving it to the other (since the 
parent/child links will get altered).  (It could even corrupt the 
tree if it wasn't implemented carefully; I haven't checked.)

While I don't really know anything about the Java implementation, 
is it possible to insert an explicit duplication request in 
there?  Something like:

expression
@init { CommonTree b; }
   : ...
   | (orExpression ADDITION_ASSIGNMENT) => a=orExpression
     ADDITION_ASSIGNMENT expression { $b = $a.clone(); }
       -> ^(ASSIGNMENT $a ^(PLUS $b expression))
   ...
   ;



More information about the antlr-interest mailing list