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

chris king kingces95 at gmail.com
Mon Jan 31 15:49:53 PST 2011


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')*
  ;

WHITESPACE
  : ' '+ {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


More information about the antlr-interest mailing list