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

 


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"il-antlr-interest" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/il-antlr-interest?hl=en
-~----------~----~----~----~------~----~------~--~---

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

Reply via email to