[antlr-interest] SL_COMMENT in java.g

rodrigo reyes rodrigor at in-fusio.com
Thu Oct 16 08:04:38 PDT 2003


Dear All,

I had an issue with the following rule of the java grammar (java.g) bundled
with antlr:

SL_COMMENT
	:	"//"
		(~('\n'|'\r'))* ('\n'|'\r'('\n')?)
		{$setType(Token.SKIP); newline();}
	;

This is the rule for single-line comments, but it fails on parsing java
source code which ends with a single-line comment but no carriage-return.
This case is part of the java language, but the parser seems to loop
forever.

I fixed the rule like this:

SL_COMMENT
	:	"//"
		(~('\n'|'\r')
                {
                     if (LA(1) == EOF_CHAR) 
                            break; 
                }
           )* 
           ('\n'|'\r'('\n')?)?  
           { $setType(Token.SKIP); newline(); }
	;

It works, but I am not really satisfied with this solution. 

Is there a reason why the EOF is not a useable character for the lexer ? The
solution would have been straightforward if only this EOF char had been
available... 

Alternatively, if someone has a solution which does not involve adding java
code in the lexer, I'd be happy to use it ;-)

Bests,
Rodrigo

 

Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/ 




More information about the antlr-interest mailing list