[antlr-interest] Rewriting Repeatable Prefix Operators To Nested AST Structure

Gavin Lambert antlr at mirality.co.nz
Thu Oct 2 12:39:43 PDT 2008


At 08:24 3/10/2008, Randall R Schulz wrote:
 >op350Formula
 >    :   (
 >            Not ->  ^(Not $op350Formula)
 >        ) *
 >        (primitiveFormula -> primitiveFormula)
 >    ;

The reason why you're getting the RewriteEmptyStreamException 
there is that on the first pass through you're trying to use the 
tree $op350Formula in the rewrite, but it's empty.

You can avoid the error by changing $op350Formula to 
$op350Formula?, but this still won't produce the tree you want -- 
for the input "---a", you'll get the tree "^(NOT ^(NOT ^(NOT))) a"

The right way to do it is to actually recurse:

op350Formula
   :  Not a=op350Formula -> ^(Not $a)
   |  primitiveFormula -> primitiveFormula
   ;



More information about the antlr-interest mailing list