[antlr-interest] Lexer Help, line-comment begins with a character that is also an operator

Gavin Lambert antlr at mirality.co.nz
Mon Sep 1 13:01:42 PDT 2008


At 21:14 1/09/2008, Anand.Vidyasagar at sungard.com wrote:
>In our small language we have a notion that if a line begins with 
>a '*' character on Column 0 we treat it is a line comment.
>However the '*' happens to be the multiplication operator in the 
>language at the same time.
[...]
>LINE_COMMENT
>     : '*' ~('\n'|'\r')* '\r'? ('\n')
>     {
>       if(getCharPositionInLine() == 0){
>             System.out.println("Found Line Comment :" + 
> getLine());
>             $channel=HIDDEN;
>         }
>     }
>     ;

This will match the '*' as a LINE_COMMENT always, but only hide it 
part of the time.  You need to change your recognition around, and 
also provide named tokens for the other variations on the asterisk 
(instead of using parser literals):

fragment STAR: '*';
fragment DBLSTAR: '**';

LINE_COMMENT
   : '*'
     ( { getCharPositionInLine() == 0 }? ~('\n'|'\r')* '\r'? '\n' 
{ $channel=HIDDEN; }
     | '*' { $type = DBLSTAR; }
     | { $type = STAR; }
     )
   ;



More information about the antlr-interest mailing list