[antlr-interest] parsing empty function parameters

Andy Tripp antlr at jazillian.com
Fri Oct 5 12:09:32 PDT 2007


Using ANTLR v3.0.1,
I'm trying to parse function parameters which can be empty, like this:
f(a, , c, d);
g(,,,,)

I have:
argList
   : argSpec (COMMA argSpec)*   
   ;

But then whenever I try to allow "argSpec" to have an empty alternative, 
ANTLR itself
produces an OutOfMemoryError.

Here's the original "argSpec" rule, which doesn't allow empty:

argSpec
   : argPassingType? argSpec2
   ;

I tried making the whole thing optional:
argSpec
   : (argPassingType? argSpec2)?
   ;

I tried a syntactic predicate:
argSpec
   : (','|')') =>                // if next token is comma or rparen, 
nothing to consume.
   | argPassingType? argSpec2
   ;

I tried a semantic predicate:
argSpec
   : {input.LT(1).getText().equals(",") || 
input.LT(1).getText().equals(")")}?
   | argPassingType? argSpec2
   ;

As I said, all these cause ANTLR to produce OutOfMemoryError, presumably
in an infinite loop matching this rule, but consuming no token.
Any suggestions?


More information about the antlr-interest mailing list