如何解析 Java 中的 REXX 代码?
我想解析 REXX 源代码,以便我可以从 Java 分析程序的结构。
我需要做一些事情,比如标准化源代码中语法不同的等效逻辑结构、查找重复的变量声明等,而且我已经有 Java 背景。
有比编写大量代码更简单的方法吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
REXX 不是一种容易用常用工具解析的语言,尤其是那些需要 BNF 语法的语言。 与大多数接触 C 的人设计的语言不同,REXX 没有任何保留字,这使得任务有些复杂。 每个看起来像保留字的术语实际上仅在其特定上下文中解析(例如,“PULL”仅保留为
PULL
指令的第一个字或第二个字)PARSE PULL
指令的单词 - 您还可以有一个名为 PULL 的变量(“PULL = 1 + 2
”))。 另外,评论还有一些非常令人惊讶的效果。 但是 ANSI REXX 标准 具有完整的语法和所有规则。REXX is not an easy language to parse with common tools, especially those that expect a BNF grammar. Unlike most languages designed by people exposed to C, REXX doesn't have any reserved words, making the task somewhat complicated. Every term that looks like a reserved word is actually only resolved in its specific context (e.g., "PULL" is only reserved as the first word of a
PULL
instruction or the second word of aPARSE PULL
instruction - you can also have a variable called PULL ("PULL = 1 + 2
")). Plus there are some very surprising effects of comments. But the ANSI REXX standard has the full syntax and all the rules.如果您有 BNF Rexx 语法,则javacc 可以帮助你构建一个 AST(抽象语法树)表示。
更准确地说,javacc 将构建 Java 类,它将:
仍然会有“代码负载”,但您不会是为该 Rexx 代码解析器编写类的人。 只有它的一代。
If you have BNF Rexx grammar, then javacc can help you build an AST (Abstract Syntax Tree) representation of that Rexx code.
More accurately, javacc will build the Java classes which will :
There would still be "load of code", but you would not to be the one doing the writing of the classes for that Rexx code parser. Only its generation.
看看 ANTLR,它确实在构建 AST、转换它等方面做得很好......
它有一个很好的编辑器(ANTLRWorks),基于 Java 构建,并且可以在解析器/树遍历器在您的应用程序中运行时对其进行调试。 对于任何类型的解析工作都确实值得研究。
Have a look at ANTLR, it really does a nice work of building an AST, transforming it etc...
It has a nice editor (ANTLRWorks), is built on Java, and can debug your parser / tree walkers while they run in your application. Really worth investigating for any kind of parsing job.