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

Loring Craymer lgcraymer at yahoo.com
Mon Sep 1 11:02:50 PDT 2008


"starts on column zero" means "is preceded by '\n'":  you want to recognize '\n' '*' as the start of a line comment, not just '*'.

--Loring


r 1, 2008 2:14:52 AM
Subject: [antlr-interest] Lexer Help, line-comment begins with a character that is also an operator

 
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.
 
Here is my Grammar so far,
 
script
     
:
           
(expression SEMICOLON)+ 
     
;
 
expression
     
:
           
lvalue operator (lvalue | expression)
           
{
                 
System.out.println("Expression : "+ $expression.text);
           
}  
     
;
 
lvalue
     
:
           
FLOAT | NUMERIC
     
;
 
FLOAT
     
:
           
 DIGIT+ ('.' DIGIT+)
     
;
NUMERIC
      
:       
      
|       DIGIT+
      
;
 
fragment
DIGIT
     
: '0'..'9'
     
;
 
operator
options { k=2;}
     
:
                 
'*'
           
|     '/'
           
|     '+'
           
|     '-'
           
|     '**'
           
|     '<<'
           
|    
'>>'                                
     
;
 
WS  :  (' '|'\r'|'\t'|'\u000C'|'\n')
{$channel=HIDDEN;}
    ;
 
LINE_COMMENT
    : '*'
~('\n'|'\r')* '\r'? ('\n') 
    { 
     
if(getCharPositionInLine() == 0){
   
        System.out.println("Found Line
Comment :" + getLine());
       
    $channel=HIDDEN;       
      
       
}          
    
    }
    ;
 
Now for an Input as below I get an error because the lexer
treats ‘*’ as a comment, what do I have to do to make sure the
‘*’ is recognized as a comment if it starts on Column 0 and as an
operator elsewhere
 
*THIS IS A LINE COMMENT
1+2+3;
 
*ANOTHER LINE COMMENT, The
Following gives me an Exception
1+4*3;
 
 
 
And here is the output when I run the parser
Found Line Comment :2
Found Line Comment :5
Found Line Comment :6
Expression : 2+3
Expression : 1+2+3
line 5:2 no viable alternative at input '4'
BR.recoverFromMismatchedToken
 
Regards,

Anand Vidyasagar


      
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20080901/bc1b1c45/attachment.html 


More information about the antlr-interest mailing list