使用 Java 创建元语言
伙计们!我需要创建某种元语言,可以将其嵌入 XML 中,然后用 Java 进行解析。例如:
<code>
[if value1>value2 then "Hello, Bob!" else "Hello, Jack"]
</code>
或者
<code>
[if value1+2>value2 return true]
</code>
我需要实现条件语句、算术。
有什么建议我应该从哪里开始寻找?
guys! I need to create some sort of meta language which I could embed in XML and then parse with Java. For example:
<code>
[if value1>value2 then "Hello, Bob!" else "Hello, Jack"]
</code>
or
<code>
[if value1+2>value2 return true]
</code>
I need to implement conditional statements,arithmetics.
Any suggestions where should I start looking?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Java 有一个内置的 JavaScript 解释器:
当然,您可以自由地从任何需要的地方加载脚本并为其提供任何上下文(在此示例中的
value
和value2
例如)你想要的。另请参阅Java 平台脚本编写一文。
Java has a built-in JavaScript interpreter:
Of course you are free to both load the script from anywhere you need and to provide it with any context (
value
andvalue2
in this example) you want.See also Scripting for the Java Platform article.
这里有一位用户,Bart Kiers。编写了关于创建简单语言的教程在 Java 中使用 ANTLR。
A user here, Bart Kiers. Wrote a tutorial about creating a simple language in Java with ANTLR.
Java 有一个脚本 API,您可以使用它来实现此目的。查找
javax.script
包的API文档。您可以在
code
元素中包含 JavaScript 等代码,并使用脚本 API 执行该代码。Java has a scripting API that you could use for this. Lookup the API documentation of the package
javax.script
.You could include code in for example JavaScript in the
code
element, and execute that using the scripting API.如果您确实想开发自己的语言,请从解释器模式开始。如果您只想在 Java 代码中利用其他人的语言,请考虑集成 JSP 风格的嵌入式语言。
If you really want to develop your own language, start off with the interpreter pattern. If you just want to leverage somebody else's language in your Java code, look to integration ala JSP style embedded languages.
几乎可以肯定,自制语言会很糟糕,尤其是从长远来看,所以不要自己开发一些东西。
有几个类似 jsp 的框架可用,也许其中一个可以解决问题:
非 JSP(独立)上下文中的 JSTL/JSP EL(表达式语言)
It is almost certain that a homemade language would suck, especially in the long run, so don't roll something on your own.
There are several jsp-like frameworks available, maybe one of those would do the trick:
JSTL/JSP EL (Expression Language) in a non JSP (standalone) context