[antlr-interest] RewriteEmptyStreamException when changing from quoted string to token?

David Holroyd dave at badgers-in-foil.co.uk
Mon Jan 14 13:51:29 PST 2008


On Mon, Jan 14, 2008 at 02:26:26PM -0600, Jon Schewe wrote:
> I just got time to get back to this project, been stuck on a different 
> one.  I tried your suggestion of gathering everything up in a variable 
> and that didn't help.  Still the same error. 
> 
> /**
> * @param negate if true, negate all expressions by multiplying by -1
> */   
> multExpr[boolean negate]
>    :   a+=atom (PRODUCT a+=atom)* -> {negate}? ^(PRODUCT ^(NUMBER DOUBLE["-1"]) $a)
>                                   ->           ^(PRODUCT $a)
>    ;
> 
>    [junit]     Caused an ERROR
>    [junit] token PRODUCT
>    [junit] org.antlr.runtime.tree.RewriteEmptyStreamException: token PRODUCT

I got one of those the other day as a result of a rule something like,

  r: A B C? -> B C;

ANTLR is unhappy when trying to do the C rewrite, on finding the expected
tree is missing (the input didn't actually contain C).  My fix was of
course to change to,

  r: A B C? -> B C?

You can't fix just by writing PRODUCT* on the r.h.s., as a tree with
zero-or-more root nodes doesn't make sense.

Also, do you really always want to create a tree with PRODUCT as the
root here, even if PRODUCT is not in the input?  I would have thought
that if no PRODUCT is present, you'd just want 'atom'?


How about something like (untested!),

  multExpr
      :   a+=atom
      (   (PRODUCT a+=atom)+ -> ^(PRODUCT $a)
      |                      -> $a
      )
      ;

(yes, I dropped the 'negate' stuff to make it simpler -- sorry ;)


ta,
dave

-- 
http://david.holroyd.me.uk/


More information about the antlr-interest mailing list