[antlr-interest] How to get the line no. of a token at lexer

Gavin Lambert antlr at mirality.co.nz
Fri Dec 21 01:21:01 PST 2007


At 21:47 21/12/2007, Nalin Chandrawansha wrote:
>Is there any method of retrieving the line no. of a token at 
>lexer?
>
>For example, if we want to catch the line numbers of comments,
>
>LINE_COMMENT : label='//'  ~('\n'|'\r')* '\r'? 
>'\n'  {System.out.println("Found a comment at line : 
>"  +$label.line );}  {$channel=HIDDEN;}    ;
>
>This doesn't work at lexer though we can use the .line at the 
>paresr rules.

This is because in the parser a label represents a token (which 
contains line information) whereas in the lexer a label is just a 
character.

However you should be able to access the members of the lexer 
class which are tracking the information it will end up putting in 
the token it's about to create; this is target-specific, 
though.  For the Java target I think you can just use 
"tokenStartLine".
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20071221/27237963/attachment.html 


More information about the antlr-interest mailing list