[antlr-interest] two ways to match nothing

Daniel Killebrew killebrew.daniel at gmail.com
Mon Jan 25 17:52:16 PST 2010

Antlr doesn't like it when there are multiple ways to match nothing. It 
says there's an error in my grammar because the second "alternative" 
(which is another way to match nothing) will never match.
Antlr can enter the optional (...)? element and match nothing, or skip 
the optional element, thus matching nothing.


     :    Start (A? List*)? End
Start    :    'start';
A    :    'aaa';
End    :    'end';
List    :    'list';

Rewritten so Antlr is happy
     :    Start End
     |    Start A List* End
     |    Start List+ End

While I can rewrite my grammar easily enough, it seems odd that Antlr 
doesn't recognize that it's trying to match nothing in two different 
ways, so who cares if it can't match the second alternative. That 
shouldn't be an error. If it's a warning, I could understand that. To 
make it the user rewrite their code into something less legible seems to 
be opposite of the usual 'Antlr way'. Although I guess this would 
require making the code a little more complicated to detect this special 
case, so perhaps this was already considered.


More information about the antlr-interest mailing list