简体   繁体   English

Antlr解析树节点坐标?

[英]Antlr parse Tree node coordinates?

I use Antlr4 4.9.2我使用 Antlr4 4.9.2

I have a requirement to perform multiple passes of the same parse tree at different stages of my analysis.我需要在分析的不同阶段对同一解析树执行多次传递。 Some of the files my application handles are very large, therefore I'd like to be able to avoid keeping the parse tree in memory, and be able to regenerate a different parse tree instance each time.我的应用程序处理的一些文件非常大,因此我希望能够避免将解析树保留在 memory 中,并且每次都能够重新生成不同的解析树实例。 So far so good.到目前为止,一切都很好。

My challenge is that I need a way to (a) compare nodes and (b) quickly access nodes that works with different instances of equivalent parse trees.我的挑战是我需要一种方法来 (a) 比较节点和 (b) 快速访问与等效解析树的不同实例一起工作的节点。

For example the following pseudo-code generates two separate instances of a parse tree that represent the same file (therefore the parse trees and their nodes are equivalent)例如,下面的伪代码生成表示同一文件的解析树的两个单独实例(因此解析树及其节点是等价的)

ParseTree parseTree1 = parse(myFile, myGrammar)
ParseTree parseTree2 = parse(myFile, myGrammar) 

Since myFile and myGrammar are the same, both parseTree1 and parseTree2 are equivalent, however are different instances and don't satisfy Objects.equals()由于myFilemyGrammar相同,因此parseTree1parseTree2是等价的,但是是不同的实例并且不满足Objects.equals()

In ANTLR, how do I represent the coordinates C of a node in such a way that:在 ANTLR 中,我如何以如下方式表示节点的坐标 C:

  • C(node1) = C(node2) if the nodes are equivalent C(node1) = C(node2) 如果节点是等价的
  • I can access C(parseTree1) or C(parseTree2) without having to visit the parse trees - so I can quickly position myself on the same node, for any instance of the parsetree我可以访问 C(parseTree1) 或 C(parseTree2) 而无需访问解析树 - 所以我可以在同一个节点上快速 position 自己,对于解析树的任何实例

You can use ANTLR4's XPath implementation to directly access nodes in a given parse tree path.您可以使用ANTLR4 的 XPath实现直接访问给定解析树路径中的节点。 Here's how I get all query expressions in MySQL code, after parsing:以下是我在解析后如何在 MySQL 代码中获取所有查询表达式:

const expressions = XPath.findAll(tree, "/query/simpleStatement//queryExpression", this.parser);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM