[antlr-interest] Parsing whole-line comments?

Loring Craymer lgcraymer at yahoo.com
Sun Jun 6 17:16:28 PDT 2010


Then you probably would want to handle this special case with a custom subclass of ANTLRFileStream that checks to see if the first line begins with 'C' and consumes the line (but not the newline following) if it does.  (You would also need to subclass CommonTokenStream if you need to keep that first comment).  This is not the most elegant solution, perhaps, but it provides optimal performance at about the same implementation cost as any of the alternatives.

Lexer modes would be another way of handling this problem, but they are not currently supported in ANTLR 3.  For that matter, this would also not be a problem if ANTLR lexer grammars were true context free grammars that supported start symbols, but then you would have to deal with writing the top level Tokens rule yourself--probably not a good trade.

--Loring




----- Original Message ----
> From: Christian Convey <christian.convey at gmail.com>
> To: Loring Craymer <lgcraymer at yahoo.com>
> Cc: John B. Brodie <jbb at acm.org>; antlr-interest at antlr.org
> Sent: Sun, June 6, 2010 3:56:10 PM
> Subject: Re: [antlr-interest] Parsing whole-line comments?
> 
> On Sun, Jun 6, 2010 at 6:49 PM, Loring Craymer <
> ymailto="mailto:lgcraymer at yahoo.com" 
> href="mailto:lgcraymer at yahoo.com">lgcraymer at yahoo.com> wrote:
> You 
> can, of course, do
>
> COMMENT : '\n' 'C' (~'\n')+ ;
>
> 
> NEWLINE: '\n' ;
>
> (the ordering matters for ANTLR 3's DFA 
> construction), but the approach Brodie suggested is the common idiom since it 
> costs less in terms of performance and does not depend on the quirks of ANTLR 
> DFA construction.  "Start of line" is a semantic notion, whereas '\n' 'C' 
> specifies syntax.


Thanks.  How would you handle the case where 
> the very first line in
the file is a comment line?  That is, there's no 
> preceding newline
sequence.


      


More information about the antlr-interest mailing list