[antlr-interest] ANTLR 3.1 gives RewriteEmptyStreamException, ANTLR3.0.1 does not ?

David Pearce David.Pearce at mcs.vuw.ac.nz
Mon Sep 29 13:44:20 PDT 2008


Hi Terence,

> Yes.  you must use
> 
>> list : '(' listBody ')' -> ^(LIST listBody?)
> 
> not
> 
>> list : '(' listBody ')' -> ^(LIST listBody)
> 
> clearly listBody can be optional, right?

Yes, in this case it is definitely optional.  But, in the Java grammar 
case it is not:

> classDeclaration
>     :    'class' Identifier (typeParameters)?
>         ('extends' type)?         ('implements' typeList)?
>         classBody
>     ;
> 
> ...
> 
> classBody
>     :    '{' classBodyDeclaration* '}'
>     ;
> 
> ...
> 
> classBodyDeclaration
>     :    ';'
>     |    ... [other productions]
>     ; 

I can't make classBody here optional, like e.g.:

 > classDeclaration
 >     :    'class' Identifier (typeParameters)?
 >         ('extends' type)?         ('implements' typeList)?
 >         classBody?
 >         -> ^(Class ...)
 >     ;

Otherwise, I'm accepting Java files which are not correct.  It seems the 
only option is to refactor giving this:

 > classDeclaration
 >     :    'class' Identifier (typeParameters)?
 >         ('extends' type)?         ('implements' typeList)?
 >         '{' (classBodyDeclaration | ';')* '}'
 >         -> ^(Class ...)
 >     ;
 >
 > ...
 >
 > classBodyDeclaration
 >     :    ... [other productions]
 >     ;

But, this doesn't seem ideal to me?

Cheers,

Dave

>>> This seems clumsy, since neither the X or Y tokens are actually needed
> 
> just use ? :)
> 
> Ter
> 

-- 
Lecturer in Computer Science,
School of Mathematics, Statistics and Computer Science,
Victoria University of Wellington,
PO Box 600,
Wellington,
New Zealand.

Office: Cotton 231
Telephone: +64 (0)4 463 5833
URL: http://www.mcs.vuw.ac.nz/~djp


More information about the antlr-interest mailing list