解析不完整的Java源代码 [英] parsing incomplete Java source code

查看:114
本文介绍了解析不完整的Java源代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在某些问题中,我需要解析可能不完整的Java源代码片段。例如,代码可以引用未在此类片段中定义的变量。

In certain problem I need to parse a Java source code fragment that is potentially incomplete. For example, the code can refer to variables that are not defined in such fragment.

在这种情况下,我仍然希望解析这些不完整的Java代码,将其转换为方便的可检查表示,并能够从这种抽象表示生成源代码。

In that case, I would still like to parse such incomplete Java code, transform it to a convenient inspectable representation, and being able to generate source code from such abstract representation.

这是什么工具?在这篇文章中,我找到了使用Antlr,JavaCC或Eclipse JDT的建议。
但是,我没有找到关于处理不完整的Java源代码片段的任何参考,因此这个问题(此外,链接的问题还有两年多了,所以我想知道地图上是否有新内容) 。

What is the right tool for this ? In this post I found suggestions to use Antlr, JavaCC or the Eclipse JDT. However, I did not find any reference regarding dealing with incomplete Java source code fragments, hence this question (and in addition the linked question is more than two years old, so I am wondering if something new is on the map).

例如,代码可能类似于以下表达式:

As an example, the code could be something like the following expression:

"myMethod(aVarName)"

在这种情况下,我希望能够以某种方式检测到代码中引用了变量 aVarName

In that case, I would like to be able to somehow detect that the variable aVarName is referenced in the code.

推荐答案

嗯...这个问题甚至没有任何含糊的喜欢一个简单的答案。任何上述解析器技术都允许你做你想做的事情,如果你编写正确的语法并操纵解析器做回退解析未知令牌逾越节的事情。

Uhm... This question does not have anything even vaguely like a simple answer. Any of the above parser technologies will allow you to do what you wish to do, if you write the correct grammar and manipulate the parser to do fallback parsing unknown token passover sort of things.

让你到达目的地的最少工作是要么使用具有可恢复解析的ANTLR并且附带一个相当完整的java 7语法,要么看看你可以从日食JDT中取出什么(使用的是在eclipse IDE中执行错误和意图符号以及语法高亮。)

The least amount of work to get you where you're going is either to use ANTLR which has resumable parsing and comes with a reasonably complete java 7 grammar, or see what you can pull out of the eclipse JDT ( which is used for doing the error and intention notations and syntax highlighting in the eclipse IDE. )

请注意,这些东西都不容易 - 你正在编写klocs,而不仅仅是导入一个班级,告诉它去。

Note that none of this stuff is easy -- you're writing klocs, not just importing a class and telling it to go.

在某个不正确/不完整的地方,所有这些策略都会失败,因为没有计算机(甚至是那个人)能够辨别出你的意思,除非你至少模糊地说出来。

At a certain point of incorrect/incompleteness all of these strategies will fail just because no computer ( or even person for that matter ) is able to discern what you mean unless you at least vaguely say it correctly.

这篇关于解析不完整的Java源代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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