3

i'm trying to get line number in an ANTLR3 tree grammar (the code generated by ANTLR3 is of the TreeParser class).
google only found solutions for ANTLR2 which sadly don't work in ANTLR3.
to clarify i'm trying to access the line number in the .g file itself.

i think i have to overwrite a method or extend a class i just don't know which one.

tnx in advance

Edit: i should point out i'm using the java api

danny
  • 71
  • 6
  • [How do I make a TreeParser in ANTLR3](http://stackoverflow.com/questions/2061166/how-do-i-make-a-treeparser-in-antlr3) ant [ANTLR3 Tutorials](http://jnb.ociweb.com/jnb/jnbJun2008.html) might help you. – Tamara Wijsman Jan 07 '11 at 23:17
  • danny, could you post the link to the v2 code that does this? – Bart Kiers Jan 08 '11 at 07:49
  • @bart Kiers that would be: http://antlraux.sourceforge.net/ for the library and http://tech.puredanger.com/2007/02/01/recovering-line-and-column-numbers-in-your-antlr-ast/ for the code hope this helps – danny Jan 08 '11 at 13:18

2 Answers2

3

it would appear that i was searching way to far. to access the line number of a rule in Tree Grammar while in the .g file simply ask for token.getLine(); (it is a CommonTree internally)

so for example

assign: ID '=' expression {int line = $ID.getLine()}; // $ID is of type CommonTree

danny
  • 71
  • 6
  • I had the same trouble in an @init, but without the benefit of a token. Solution - moved the code into the rule actions, and added a marker token to the AST. – Andy Thomas Nov 30 '11 at 21:18
1

The antlr3.Token class and the subclasses antlr3.ClassicToken and antlr3.CommonToken
seem to provide a deprecated function def getLine ( self ) and a member line.

I have no idea how you are using antlr3.TreeParser, but I suppose you have access to the tokens.

Tamara Wijsman
  • 12,198
  • 8
  • 53
  • 82
  • 1
    yes i know you can access the tokens (using the TreeAdaptor fromt he tree parser) but how do i get the current one ? especially in the .g file ? (tnx for trying to answer btw) – danny Jan 07 '11 at 23:52