使用 Java 为特定领域语言编写解析器
我们想要设计一种简单的领域特定语言来编写测试脚本,以自动测试我们的应用程序之一的基于 XML 的界面。示例测试如下:
- 从网络共享文件夹或 subversion 存储库获取输入 XML 文件
- 使用界面导入 XML 文件
- 检查导入结果消息是否成功
- 导出与刚刚使用界面导入的对象相对应的 XML,并检查是否成功它是正确的。
如果领域特定语言可以是声明性的,并且它的语句看起来与上面示例中的句子尽可能接近,那就太棒了,因为人们不一定必须是程序员才能理解/编写/维护测试。类似这样的:
newObject = GET FILE "http://svn/repos/template1.xml"
reponseMessage = IMPORT newObject
newObjectID = GET PROPERTY '/object/id/' FROM responseMessage
(..)
但是我不确定如何在 Java 中为该语言实现一个简单的解析器。 10 年前,回到学校,我使用 Lex 和 Yacc 用于 C 语言。也许一种方法是使用 Java 的等效方法?
或者,我可以放弃使用声明性语言的想法,而选择基于 XML 的语言,这可能更容易为其创建解析器?您会推荐什么方法?
We want to design a simple domain specific language for writing test scripts to automatically test a XML-based interface of one of our applications. A sample test would be:
- Get an input XML file from network shared folder or subversion repository
- Import the XML file using the interface
- Check if the import result message was successfull
- Export the XML corresponding to the object that was just imported using the interface and check if it correct.
If the domain specific language can be declarative and its statements look as close as my sentences in the sample above as possible, it will be awesome because people won't necessarily have to be programmers to understand/write/maintain the tests. Something like:
newObject = GET FILE "http://svn/repos/template1.xml"
reponseMessage = IMPORT newObject
newObjectID = GET PROPERTY '/object/id/' FROM responseMessage
(..)
But then I'm not sure how to implement a simple parser for that languange in Java. Back in school, 10 years ago, I coded a language parser using Lex and Yacc for the C language. Maybe an approach would be to use some equivalent for Java?
Or, I could give up the idea of having a declarative language and choose an XML-based language instead, which would possibly be easier to create a parser for? What approach would you recommend?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您可以尝试 JavaCC 或 Antlr 用于为您的域特定语言创建解析器。如果该文件的编辑者不是程序员,我会更喜欢这种方法而不是 XML。
You could try JavaCC or Antlr for creating a parser for your domain specific language. If the editors of that file are not programmers, I would prefer this approach over XML.
看一下 Xtext - 它将采用语法定义并生成解析器以及完整的-具有语法突出显示和检查功能的特色 Eclipse 编辑器插件。
Take a look at Xtext - it will take a grammar definition and generate a parser as well as a fully-featured eclipse editor pluging with syntax highlighting and -checking.
ANTLR 就足够了
ANTLR should suffice
看看 Antlr 库。您必须使用 EBNF 语法来描述您的语言,然后使用 Antlr 来创建 java 类从你的语法来看。
Look at Antlr library. You'll have to use EBNF grammatic to describe your language and then use Antlr to make java classes from your grammatic.
看看 Cucumber 如何定义其测试用例:
(来源:cukes.info)
http://cukes.info/ - 可以在 JRuby 中运行。
Have a look at how Cucumber defines its test cases:
(source: cukes.info)
http://cukes.info/ - can run in JRuby.
使用 XML 描述您的测试场景可以轻松完成此操作。
< GETFILE object="newObject" file="http://svn/repos/template1.xml"/ >
由于您的语法示例非常简单,因此也应该可以简单地使用 StringTokenizer 来标记和解析此类脚本。
由于您
如果你想引入更复杂的表达式或控制结构,你可能最好选择 ANTLR
This could be easily done using XML to describe your test scenarios.
< GETFILE object="newObject" file="http://svn/repos/template1.xml"/ >
Since your example of syntax is quite simple, it should also be possible to simply use StringTokenizer to tokenize and parse these kind of scripts.
If you want to introduce more complex expressions or control structures you probably better choose ANTLR
我意识到这个帖子已经有 3 年历史了,但仍然觉得有必要提出我的看法。提问者询问 Java 是否可以用于 DSL,以尽可能仔细地观察,例如,
答案是可以,并且已经针对类似的需求完成了。许多年前,我构建了一个 Java DSL 框架,通过简单的定制,它可以允许将以下语法用于可编译、可运行的代码:
在上面的关键字
file
、message、
get
、import
、validate
和export
都是自定义关键字,每个关键字都需要两个简单的类不到一页代码即可实现其编译器和运行时功能。当每一项功能完成后,它就会被放入框架中,在那里它可以立即完成其工作。请注意,这只是一种可能的形式;确切的语法可以由实现者自由选择。该系统实际上是一种 DIY 高级汇编语言,使用预先编写的 Java 类来执行所有功能块,包括编译和运行时。该框架定义了这些功能的放置位置,并提供了要实现的必要抽象类和接口。
该系统满足了清晰度的主要需求,非程序员也可以轻松了解正在发生的情况。由于编译几乎是瞬时的,因此可以快速进行更改并立即运行。
可根据要求提供完整(开放)源代码。有一个通用的 Java 版本,还有一个适用于 Android 的版本。
I realize this thread is 3 years old but still feel prompted to offer my take on it. The questioner asked if Java could be used for a DSL to look as closely as possible like
The answer is yes it can be done, and has been done for similar needs. Many years ago I built a Java DSL framework that - with simple customization - could allow the following syntax to be used for compilable, runnable code:
In the above, the keywords
file
,message
,get
,import
,validate
andexport
are all custom keywords, each one requiring two simple classes of less than a page of code to implement their compiler and runtime functions. As each piece of functionality is completed it is dropped into the framework, where it is immediately available to do its job.Note that this is just one possible form; the exact syntax can be freely chosen by the implementor. The system is effectively a DIY high-level assembly language, using pre-written Java classes to perform all the functional blocks, both for compiling and for the runtime. The framework defines where these bits of functionality have to be placed, and provides the necessary abstract classes and interfaces to be implemented.
The system meets the primary need of clarity, where non-programmers can easily see what's happening. Changes can be made quickly and run immediately as compilation is almost instantaneous.
Complete (open) source code is available on request. There's a generic Java version and also one for Android.