[antlr-interest] ANTLR 2: single-line comment lexing with no newline

Gavin Lambert antlr at mirality.co.nz
Thu Mar 8 12:24:45 PST 2007


At 08:03 9/03/2007, Matt Benson wrote:
 >I have the following lexer rules (can't remember how
 >much of them are copied vs. original).  My intent is,
 >as implied by my subject line, to recognize a
 >single-line comment at the end of the file--an old
 >problem.  I am seeing ANTLR spin forever when it hits
 >the comment with these rules.  Can anyone see what
 >might be wrong?  Rules follow.
[...]
 >// Single-line comments
 >SL_COMMENT
 >	:	"//"
 >		(~('\n'|'\r'))* (NL)?
 >		{$setType(Token.SKIP);}
 >	;

My first thought is that since you've made NL optional and 
permitted just about anything before it, it's doing the greediest 
possible match first (the entire file) and working its way back, 
spinning for ages.

Try either making NL not optional, or using NL|EOF (also not 
optional).



More information about the antlr-interest mailing list