使用 Java 为特定领域语言编写解析器

发布于 2024-08-24 02:06:44 字数 799 浏览 6 评论 0原文

我们想要设计一种简单的领域特定语言来编写测试脚本,以自动测试我们的应用程序之一的基于 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 年前,回到学校,我使用 LexYacc 用于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

鹊巢 2024-08-31 02:06:44

您可以尝试 JavaCCAntlr 用于为您的域特定语言创建解析器。如果该文件的编辑者不是程序员,我会更喜欢这种方法而不是 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.

魄砕の薆 2024-08-31 02:06:44

看一下 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.

偏爱自由 2024-08-31 02:06:44

ANTLR 就足够了

ANTLR 是另一种语言识别工具,它提供了一个框架,用于根据包含各种目标语言的动作的语法描述构建识别器、解释器、编译器和翻译器。 ANTLR 为树构建、树遍历、翻译、错误恢复和错误报告提供了出色的支持。

ANTLR should suffice

ANTLR, ANother Tool for Language Recognition, is a language tool that provides a framework for constructing recognizers, interpreters, compilers, and translators from grammatical descriptions containing actions in a variety of target languages. ANTLR provides excellent support for tree construction, tree walking, translation, error recovery, and error reporting.

猛虎独行 2024-08-31 02:06:44

看看 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.

你的心境我的脸 2024-08-31 02:06:44

看看 Cucumber 如何定义其测试用例:

替代文字
(来源:cukes.info

http://cukes.info/ - 可以在 JRuby 中运行。

Have a look at how Cucumber defines its test cases:

alt text
(source: cukes.info)

http://cukes.info/ - can run in JRuby.

痴梦一场 2024-08-31 02:06:44

或者,我可以放弃使用声明性语言的想法,
选择基于 XML 的语言,
这可能会更容易
创建一个解析器?什么办法
你会推荐吗?

  1. 使用 XML 描述您的测试场景可以轻松完成此操作。

    < GETFILE object="newObject" file="http://svn/repos/template1.xml"/ >

  2. 由于您的语法示例非常简单,因此也应该可以简单地使用 StringTokenizer 来标记和解析此类脚本。

    由于您

如果你想引入更复杂的表达式或控制结构,你可能最好选择 ANTLR

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?

  1. This could be easily done using XML to describe your test scenarios.

    < GETFILE object="newObject" file="http://svn/repos/template1.xml"/ >

  2. 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

渡你暖光 2024-08-31 02:06:44

我意识到这个帖子已经有 3 年历史了,但仍然觉得有必要提出我的看法。提问者询问 Java 是否可以用于 DSL,以尽可能仔细地观察,例如,

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.

答案是可以,并且已经针对类似的需求完成了。许多年前,我构建了一个 Java DSL 框架,通过简单的定制,它可以允许将以下语法用于可编译、可运行的代码:

file InputFile
message Message

get InputFile from http://<....>
import Message from InputFile
if validate Message export Message
else
begin
   ! Signal an error
end

在上面的关键字 filemessage、getimportvalidateexport 都是自定义关键字,每个关键字都需要两个简单的类不到一页代码即可实现其编译器和运行时功能。当每一项功能完成后,它就会被放入框架中,在那里它可以立即完成其工作。

请注意,这只是一种可能的形式;确切的语法可以由实现者自由选择。该系统实际上是一种 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

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.

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:

file InputFile
message Message

get InputFile from http://<....>
import Message from InputFile
if validate Message export Message
else
begin
   ! Signal an error
end

In the above, the keywords file, message, get, import, validate and export 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文