API来比较AST? [英] API to compare AST?

查看:38
本文介绍了API来比较AST?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有开源的java api可以比较java源代码的两个抽象语法树?

我想看看两种语法树之间的区别,类似于在 diff 工具中的做法.

解决方案

大多数差异工具比较行,而不是语法树(请参阅 供讨论的维基百科文章).

有一些技术论文讨论了如何进行语法树比较,例如,Diff/TS:细粒度结构变化分析工具>

据我所知,在任何地方都没有用于计算树差异的 API.如果您想获得最小的差异,问题会比听起来更复杂.但基本技术是使用 Levenstein 距离度量的一些变体.

我们不得不为我们的 SmartDifferencers 系列推出自己的产品;幸运的是,我们为许多语言提供了非常好的前端来生成准确的 AST.

您最终会得到更多惊喜,例如尽管您拥有的是 AST,但仍然想要比较评论、想要比较损坏的文件、比较您的语法不匹配的语言方言或代码包含其他语言的插入等等.按行做差异没有这些问题,这也是 line-diff 广泛使用而 tree-diff 没有的原因之一.

Is there a open source java api that allows to compare two Abstract Syntax Trees of java source code?

I would like to see the differences between the two syntax trees, similar to how it is done in diff tools.

解决方案

Most diff tools compare lines, not syntax trees (see Wikipedia article for discussion).

There are some techical papers that talk about how to do syntax tree compares, e.g., Diff/TS: A Tool for Fine-Grained Structural Change Analysis

There are no APIs for computing tree differences available anywhere as far as I know. The problem is more complex than it first sounds, if you want to get a minimal diff. But the basic technique is to use some variation of Levenstein distance metrics.

We had to roll our own for our line of SmartDifferencers; fortunately, we have really good front ends for many langauges to produce accurate ASTs.

You end up with additional surprises, such as people that want to compare comments in spite of the fact that what you have are ASTs, wanting to compare broken files, to compare language dialects your grammar doesn't match, or codes that contain insertions of other languages, etc.. Do diff by lines doesn't have these issues, which is one reason line-diff is widespread and tree-diff is not.

这篇关于API来比较AST?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆