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

Eric Merritt cyberlync at gmail.com
Tue Mar 15 15:22:40 PST 2005


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