[antlr-interest] grammar for single-line comments

Austin Hastings Austin_Hastings at Yahoo.com
Wed Oct 10 20:58:51 PDT 2007


Try ignoring the end-of-line/end-of-file issue:

COMMENT_LINE: '#' ~(NEWLINE)* ;

This says that the #, and any characters other than a newline that come 
after it, are the comment. The newline, if there is one (could be eof) 
is left to be treated as whitespace.

=Austin


Mark Volkmann wrote:
> I'm having trouble adding support for single-line comments to my grammar.
> It's probably a simple mistake, but I don't see it.
> Here's the part of my grammar that deals with comments.
>
> COMMENT_LINE:
>   '#'
>   (options { greedy = false; } : .)*
>   (NEWLINE | EOF)
>   { $channel = HIDDEN; };
>
> NEWLINE: '\r'? '\n' { $channel = HIDDEN; };
>
> My sample input is just a single comment line with no NEWLINE at the end.
>
> # This is a comment.
>
> I get these error messages from the generated parser.
>
> line 1:1 mismatched character ' ' expecting '<EOF>'
> line 1:19 no viable alternative at character '.'
>
>   



More information about the antlr-interest mailing list