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

Anand.Vidyasagar at sungard.com Anand.Vidyasagar at sungard.com
Mon Sep 1 23:24:57 PDT 2008


Thanks for the help!!

I tried this new definition of LINE_COMMENT, I also changed the
"operator" grammar definition to 
operator
options { k=2;}
      :
              STAR
        |     '/'
        |     '+'
        |     '-'
        |     DBLSTAR
        |     '<<'
        |     '>>'
      ;

And now I get a different kind of exception. 

line 1:1 rule LINE_COMMENT failed predicate: { getCharPositionInLine()
== 0 }?
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space

Regards,

Anand Vidyasagar

-----Original Message-----
From: antlr-interest-bounces at antlr.org
[mailto:antlr-interest-bounces at antlr.org] On Behalf Of Gavin Lambert
Sent: Tuesday, September 02, 2008 1:32 AM
To: Vidyasagar, Anand; antlr-interest at antlr.org
Subject: Re: [antlr-interest] Lexer Help, line-comment begins with a
character that is also an operator

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; }
     )
   ;


List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe:
http://www.antlr.org/mailman/options/antlr-interest/your-email-addr
ess





More information about the antlr-interest mailing list