antlr函数可以从解析树中的单个节点检索文本

发布于 2025-01-26 20:54:06 字数 139 浏览 3 评论 0原文

我想从解析树节点中检索文本,但是当我使用getText()时,它会从该节点的孩子中检索所有文本。 我已经搜索过,但是我找不到只能检索节点的文本或函数来删除节点的孩子的任何功能(这样一旦我删除了他的孩子,就可以在该节点上使用getText())。任何帮助都受到欢迎。

I want to retrieve text from a parse tree's node but when i use getText(), it retrieves all the text from that node's children.
I have searched but i don't find any ANTLR function that can retrieve only the node's text or a function to delete a node's children (so that i can use getText() on that node once i have deleted his children). Any help is welcomed.

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

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

发布评论

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

评论(1

梦行七里 2025-02-02 20:54:06

那是因为“节点”(尤其是文本)包括它的孩子。如果将其定义为不属于孩子的文本,那么您只留下该解析器规则中的令牌(叶节点),这可能远不及有用(某些带有其他规则启动的孔的文本) 。

也许您有这样的情况,这样的情况将很有用。如果是这样,您可能需要详细说明。我们可能,然后提出更具体的东西。


要解决您的评论,以下是getText()的实现/v4/runtime/rulecontext.java“ rel =“ nofollow noreferrer”> rulecontext

    public String getText() {
        if (getChildCount() == 0) {
            return "";
        }

        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < getChildCount(); i++) {
            builder.append(getChild(i).getText());
        }

        return builder.toString();
    }

因此,从技术上讲,节点的文本不过是孩子的文本的串联。按照这个定义,没有父母的文字不是孩子的文字。

也许您的意思是孩子节点的定义是“ rulenode s”的“儿童节点”(与Errornode s或terminalnade s相反)。您可以实现getText()忽略parserrulecontext,但这将返回可能发生在之前,之后,之后,之后,之后,之后,之后,或者之间,您的rulenode s。

我认为您的要求有任何通用的解决方案。您需要通过上下文来删除父节点上下文所需的特定文本。

That would be because a “node” (and particularly, it’s text) includes it’s children. If you define it as the text that’s not part of a child, you’re left with just to tokens (leaf nodes) in that parser rule, and that’s probably a bit less than useful (some text with holes where other rules kick in).

Maybe you have a situation where something like this would be useful. If so, you might want to elaborate. We might, then propose something more specific.


To address your comment, here's the implementation of getText() for RuleContext

    public String getText() {
        if (getChildCount() == 0) {
            return "";
        }

        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < getChildCount(); i++) {
            builder.append(getChild(i).getText());
        }

        return builder.toString();
    }

So, technically, the text of a node is nothing but the concatenation of the texts of it's children. By that definition, there'd be no text of a parent that's not child text.

Perhaps the definition of children nodes you mean is "children nodes that are RuleNodes" (as opposed to ErrorNodes or TerminalNodes). You could implement a getText() that ignored ParserRuleContext, but that would return a rather odd concatenation of tokens that could have occurred before, after, or between, your RuleNodes.

I don't think there's any generalized solution to what you're asking for. You'll need to pull out the specific text you want for parent nodes context by context.

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