如何获取 ANTLR3 TreeParser 中的行号

发布于 2024-10-11 03:02:33 字数 216 浏览 0 评论 0 原文

我正在尝试获取 ANTLR3 树语法中的行号(ANTLR3 生成的代码属于 TreeParser 类)。
谷歌只找到了 ANTLR2 的解决方案,遗憾的是在 ANTLR3 中不起作用。
为了澄清我正在尝试访问 .g 文件本身中的行号。

我想我必须覆盖一个方法或扩展一个类,我只是不知道是哪一个。

提前 tnx

编辑:我应该指出我正在使用 java api

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

遇见了你 2024-10-18 03:02:34

antlr3.Token 类及其子类 antlr3.ClassicTokenantlr3.CommonToken
似乎提供已弃用的函数 def getLine ( self )< /code> 和成员 line

我不知道你如何使用 antlr3.TreeParser,但我想你有访问令牌。

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.

自演自醉 2024-10-18 03:02:33

看来我正在寻找很远的地方。要在 .g 文件中访问树语法中规则的行号,只需询问 token.getLine(); (它内部是一个 CommonTree)

因此例如

分配: ID '=' 表达式 {int line = $ID.getLine()}; // $ID 的类型为 CommonTree

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文