[antlr-interest] end-of-line/end-of-file issue: *was* grammar for single-line comments

Edwards, Waverly Waverly.Edwards at genesys.com
Thu Oct 11 04:20:38 PDT 2007


I haven't done this using ANTLR but in my hand written lexers I
eliminate the issue of having an EOF on the same line as any of my
source code by immediately introducing to the handle/stream upon opening
a file a carriage return/newline followed by an EOF or an EOF sentinel.
I speed up my code and simplify my logic because I know the EOF is going
to be the start of a new line ALWAYS.

I saw a somewhat complicated way of dealing with an EOF in the wiki and
didn't understand why it was an issue. The previous email awakened the
solution that I have used in the past.


W.


-----Original Message-----
From: antlr-interest-bounces at antlr.org
[mailto:antlr-interest-bounces at antlr.org] On Behalf Of Austin Hastings
Sent: Wednesday, October 10, 2007 11:59 PM
To: Mark Volkmann
Cc: antlr-interest at antlr.org
Subject: Re: [antlr-interest] grammar for single-line comments

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