java中的自定义解释器
嗨
我想编写一个程序能够解析和执行一些文件
该文件由一些自定义命令组成(我希望能够定义命令和适当的方法,以便当程序看到该命令时它应该执行适当的方法)
它的语法对我来说并不重要;我只想能够定义命令
类似于用 java 编写的简单小型解释器。
有没有专门用于此目的的库?
还是需要我自己写?
谢谢
Hi
I want to write a program to be able to parse & execute some files
The file consists of some custom commands (I want to be able to define commands and appropriate methods so when program see that command it should execute the appropriate method)
It's grammar is not important for me; I just want to be able to define commands
Something like a simple and small interpreter written in java.
Is there any library for this purpose?
Or I need to write by myself?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Java 6(及更新版本)的标准库中包含此内容:查看包
javax.script
的 API 文档。您可以使用任何具有可用于javax.script
API 的插件引擎的脚本语言,使用它来运行 Java 程序中的脚本(例如,自动化您的程序)。默认情况下,提供对 JavaScript 的支持。请参阅 Java 脚本程序员指南。
该指南中的简单示例:
Java 6 (and newer) have this in the standard library: have a look at the API documentation of the package
javax.script
. You can use this to run scripts from your Java program (for example to automate your program) using any scripting language for which there is a plug-in engine available for thejavax.script
API. By default, support for JavaScript is supplied.See the Java Scripting Programmer's Guide.
Simple example from that guide:
你考虑过看看 BeanShell 吗?
提供在运行时解释类似 Java 的代码片段。
如果您需要更多,请考虑嵌入 JPython 或其他小型解释器。只需选择一个符合 JSR-233 的即可获得通用调试支持。
Have you considered looking at BeanShell?
Provides for having Java-like snippets interpreted at runtime.
If you need a bit more than that, then consider embedding e.g. JPython or another small interpreter. Just choose one that is JSR-233 compliant to get generic debugging support.
你需要的是所谓的“脚本引擎”。快速搜索即可发现 Rhino。这是一个不错的选择,因为它是 JavaScript,而且很多人都了解 JavaScript,并且存在一定数量的第三方扩展(库和代码片段)。
What you need is called "scripting engine". Quick search reveals Rhino. This is a good option cause it's JavaScript, and many people know JavaScript and there exists a certain amount of third-party extensions (libraries and code snippets) for it.
您可以使用 ANTLR,但您必须定义语法(将为您生成解析器)。
检查这个 表达式电梯示例,它与您的问题类似,但是相反读取它从标准输入读取的文件。
如果您选择 ANTLR,请查看 ANTLRWorks,它是一个 GUI,可以帮助您ANTLR 开发(我认为它也可以作为 Eclipse 插件使用)。
You could use ANTLR, but you will have to define a grammar (the parser will be generated for you).
Check this expression elevator example, it is similar to your problem, but instead of reading files it reads from the standard input.
If you choose ANTLR, take I look at ANTLRWorks, it is a GUI that will help with ANTLR development (I think it is also available as an Eclipse plugin).
我知道这是一个旧线程,但前段时间我已经实现了一个类似于 JavaScript 的小型解释器(有更多限制),代码发布在 Github 上 https://github.com/guilhermelabigalini/interpreter
但它确实支持 IF/CASE/LOOPS/FUNCTIONs,见下文:
I know this an old thread, but some time ago I've implemented a small interpreter for language similar with JavaScript (with more restrictions), the code is published in Github at https://github.com/guilhermelabigalini/interpreter
But it does support IF/CASE/LOOPS/FUNCTIONs, see below: