开发者

how to get the line number in an ANTLR3 TreeParser

开发者 https://www.devze.com 2023-02-03 16:15 出处:网络
i\'m trying to get line number in an ANTLR3 tree grammar (the code generated by ANTLR3 is of the TreeParser class).

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 clari开发者_运维百科fy 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


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消