antlr函数可以从解析树中的单个节点检索文本
我想从解析树节点中检索文本,但是当我使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
那是因为“节点”(尤其是文本)包括它的孩子。如果将其定义为不属于孩子的文本,那么您只留下该解析器规则中的令牌(叶节点),这可能远不及有用(某些带有其他规则启动的孔的文本) 。
也许您有这样的情况,这样的情况将很有用。如果是这样,您可能需要详细说明。我们可能,然后提出更具体的东西。
要解决您的评论,以下是
getText()
的实现/v4/runtime/rulecontext.java“ rel =“ nofollow noreferrer”> rulecontext因此,从技术上讲,节点的文本不过是孩子的文本的串联。按照这个定义,没有父母的文字不是孩子的文字。
也许您的意思是孩子节点的定义是“
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 RuleContextSo, 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
RuleNode
s" (as opposed toErrorNode
s orTerminalNode
s). You could implement agetText()
that ignoredParserRuleContext
, but that would return a rather odd concatenation of tokens that could have occurred before, after, or between, yourRuleNode
s.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.