[antlr-interest] getting the correct line # from an exception

Vladimir Sutskever vs667 at nyu.edu
Fri Mar 3 19:53:40 PST 2006


Dear Mark,

Actually my last suggestion had an error. It would incremented line 
count on detecting a SPACE in addition to the newline, (which is not 
what you want I think).

I think this should work:

*SPACE*
    :    ' '
        {
           
            $setType(Token.SKIP);
        }
    ;

*NEWLINE*
    :    '\t'
    |    '\n'
    |    '\r'
        {
            $setType(Token.SKIP);
            newline();
        }
    ;   
  
Hope This Helps


Vladimir Sutskever wrote:
> Dear Mark,
>
> You can add the additional newline() function into your lexer rule, 
> that fixed it for me. Below is my grammar for white space.
>
> //========================================================[WHITE SPACE]
> WS      :    (SPACE
>    |    '\t'
>    |    '\n'
>    |    '\r')
>        {
>            $setType(Token.SKIP);
>            newline();
>        }
>    ;
>
> Hope This Helps
> Vladimir Sutskever
>
> Mark Felzer wrote:
>> Dear Friends,
>>
>> When my grammar contains an illegal token an exception is properly 
>> thrown
>> The exceptions message seems to always indicate the line # as being 
>> "1" which is sorta useless when it comes to debugging,
>>
>> My grammar indicates that "\n\r" should be skipped by the lexer, 
>> should I increment something each time "\n\r" is encountered so that 
>> the exception translates to the correct line?
>>
>>
>> Thank You Very Much,
>> Mark F.
>>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20060303/30950769/attachment.html


More information about the antlr-interest mailing list