[antlr-interest] A little trouble with single line comments and EOFs

Alexey Demakov demakov at ispras.ru
Tue Mar 15 23:11:22 PST 2005


Quote from my TreeDL parser:

protected
NL : (   {LA(2) == '\n'}? '\r' '\n'
       | '\r'
       | '\n'
     )
     { newline(); }
   ;

protected
SL_COMMENT : "//"
             ( ~( '\n' | '\r' ) )*
             /* 
              * single line comment may not
              * be followed by new line
              * at the end of file
              */
             ( NL )? 
           ;

Regards,
Alexey

-----
Alexey Demakov
TreeDL: Tree Description Language: http://treedl.sourceforge.net
RedVerst Group: http://www.unitesk.com


----- Original Message ----- 
From: "Eric Merritt" <cyberlync at gmail.com>
To: <antlr-interest at antlr.org>
Sent: Wednesday, March 16, 2005 2:22 AM
Subject: [antlr-interest] A little trouble with single line comments and EOFs


> I am having a bit of trouble with a single line comment rule that
> looks like the rule below. The problem comes in when a single line
> comment is at the very end of a file and isn't followed by a newline.
> The problem occurs becuase the lexer expects that new line.
> 
> COMMENT 
>   : ( '#' ) ( ~'\n' )* '\n'  
>   { 
>       newline(); 
>       _ttype = Token.SKIP; 
>   }
>   ;
> 
> 
>  The obvious fix to this is something like
> 
> 
> COMMENT 
>   : ( '#' ) ( ~'\n' )* ( '\n' | EOF_CHAR)   
>   { 
>       newline(); 
>       _ttype = Token.SKIP; 
>   }
>   ;
> 
> However, there isn't any EOF or EOF_CHAR defined in the lexer. Can
> anyone offer a solution to this problem?
> 
> Thanks,
> Eric



More information about the antlr-interest mailing list