[antlr-interest] Re: SL_COMMENT in java.g

rodrigo reyes rodrigor at in-fusio.com
Fri Oct 17 05:15:19 PDT 2003


Hi Matt,

> I accidentally deleted the original message, but at
> one time I had modified the lexer grammar to make the
> line terminators optional on this token, seemingly
> without ill effect; something like:
> SL_COMMENT
> 	:	"//"
> 		(~('\n'|'\r'))* ('\n'|'\r'('\n')?)?
> 		{$setType(Token.SKIP); newline();}
> 	;
> Does anyone see a problem with this approach?

In my first attempt at solving the problem, I just removed the match for
('\n'|'\r'('\n')?), so that the rule would match anything but (\n|\r) after
the "//". I assumed the new lines would just be handled by the WS rule,
which already matches well the newlines. 

Unfortunately, neither my first solution or the one you present (which are
quite similar) work (at least for me). The parser still loops forever on the
source file I use to test this, which is a source from a real project, and
which compiles fine with javac.

However, now I really don't see the point of keeping the newline matching at
the end of the rule, so I propose this, which is even simpler (as stated
above, the newline thing is matched by the WS rule):

SL_COMMENT
	:	"//"
		(~('\n'|'\r')
                {
                     if (LA(1) == EOF_CHAR) 
                            break; 
                }
           )* 
	;


Bests,
Rodrigo

 

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




More information about the antlr-interest mailing list