If you have a very good reason to put a comment in the parser (which I would like to hear), you should put it in the lexer:
Comment : '#' ~( '\r' | '\n' )* ;
And since you already consider line breaks in your Space rule, there is no problem with input like # comment eof without <CR><LF>
In addition, if parser rules use literal tokens, ANTLR automatically creates lexer rules from behind the scenes. So in your case:
Comment : '#' ~( '\r' | '\n' )* ;
will match '#' followed by zero or more tokens other than '\r' and '\n' and not zero or more characters other than '\r' and '\n' .
For future reference:
Inside the parser rules
~ cancel tokens. matches any token
Internal lexer rules
~ cancels characters. matches any character in the range 0x0000 ... 0xFFFF
Bart kiers
source share