[antlr-interest] parsing empty function parameters

Gavin Lambert antlr at mirality.co.nz
Fri Oct 5 16:01:30 PDT 2007


At 09:39 6/10/2007, Andy Tripp wrote:
>It should be that simple, but (at least with my grammar) when arg
>can be empty, ANTLR bombs out.

How is arg/argSpec defined?  Does it refer back to argList?  Does 
it contain loops?

There are three things that tend to result in 
OutOfMemoryExceptions (which are actually all variations on one 
underlying problem):

1. infinite recursion (typically caused by left recursion); ANTLR 
is usually pretty good about detecting these, but sometimes it can 
miss particularly involuted cases.

2. empty loops.  If you've got a (...)* or (...)+ loop that can 
result in matching nothing (within the parentheses), this will 
create an infinite loop.  Be especially careful when putting 
optional terms or loops inside other loops.  ANTLR will not detect 
these cases for you at all -- you have to make sure it's correct 
all by yourself.

3. empty lexer rules.  (This doesn't sound like your problem, but 
I'm including it for completeness.)  If a top-level 
(non-'fragment') lexer rule can end up matching no characters, 
then ANTLR will internally construct an infinite loop generating 
that token repeatedly.  Again, ANTLR won't help you find these, 
you just need to be careful.

Incidentally, to get the behaviour you want, you probably want 
this (making sure that 'argSpec' always matches something and 
can't be empty, and doesn't have any of the above):

argList : argSpec? (COMMA argSpec?)*;

(Which you'll note can end up matching nothing at all, so make 
sure you don't use argList itself inside a list unless there's 
something non-optional there too!)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20071006/7c610dc7/attachment.html 


More information about the antlr-interest mailing list