[antlr-interest] How do I accept input ending with a newline *or* EOF?

Kirby Bohling kirby.bohling at gmail.com
Mon Jan 31 16:03:52 PST 2011

No idea if it is related to the problem, but you likely really want to
have ID use a '+' not a '*' after ('a'..'z'), otherwise ID to match
nothing and be cause an infinite loop while lexing at points
(generally speaking, any time rules like

bar: (foo)*;

foo: (baz)*;

You are just asking for problems.  Whether foo and baz are lexers or
parser rules.  Every time I do that it is a mistake (or a failure of
imagination).  Generally speaking, low level items you want to force
the consumption of something, and make them optional at a higher level
(at least that has been true in my limited experience).

I believe the EOF is precisely because of the lack of a + vs. a *
there.  As rather then consume the EOF, you can spin consuming nothing
forever.  But I didn't actually crack out ANTLR and check.

Also, unless you really know what you are doing, you might want to
skip using constants in your parser rules.  While many the examples do
so, from what I've read, it can have complex interaction (it generates
a token for it internally that can't be seen).  I'd try making a
NEWLINE token and seeing if that helps make the error message any


On Mon, Jan 31, 2011 at 5:49 PM, chris king <kingces95 at gmail.com> wrote:
> Hello! I'm trying to write a grammar that will accept lines of zero or more
> IDs and I'd like to allow the last line to end in a new line *or *EOF. I
> came up with this grammar:
> grammar test;
> start
>  : input*
>  ;
> input
>  : ID* ('\n' | EOF)
>  ;
> ID
>  : ('a'..'z')*
>  ;
>  : ' '+ {skip();}
>  ;
> But got this error from ANTLRWorks saying start has un-reachable
> alternatives:
> [15:38:33] error(201): test2.g:9:5: The following alternatives can never be
> matched: 2
> If I remove the reference to EOF than everything works but I have to end the
> last line in a new line and I don't want to have to do that. Any
> suggestions?
> Thanks,
> Chris
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address

More information about the antlr-interest mailing list