我可以使用 Antlr 创建的词法分析器/解析器来解析 PDDL 文件并将数据返回到 Java 程序吗?
我是 Antlr 的新手,但以前使用过 Flex/Bison。我想知道我想使用 Antlr 做的事情是否可行。
我想使用 Antlr 解析 PDDL 文件,并在解析 PDDL 文件时编写的 Java 类中构建我自己的 PDDL 文件内容表示(在规则的操作中?)。文件解析完成后,我想将文件内容的对象表示返回给 Java 程序以运行其他操作。
因此,本质上,我想从 Java 程序内部对 PDDL 文件调用 Antler 生成的 PDDL 解析器,并让它返回一个描述 PDDL 文件的对象给主 Java 程序。
这可能吗?我尝试查看文档,但没有找到好的答案。
非常感谢。
I am new to Antlr, but have used Flex/Bison before. I want to know if what I want to do using Antlr is possible.
I want to parse an PDDL file using Antlr and build up my own representation of the PDDL file's contents in a Java Class that I wrote as the PDDL file is parsed (in the actions for the rules?). After the file is finished parsing I want to return the object representation of the file's contents to the Java program to run other operations on.
So essentially, I want to invoke an Antler produced PDDL parser on a PDDL file from inside a Java program and have it return an object that describes the PDDL file to the main Java program.
Is this possible? I have tried looking at the documentation, but haven't found a good answer.
Thanks very much.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当然。
首先,您需要在(ANTLR)语法文件中描述您的语言。最简单的方法是使用组合语法来做到这一点。组合语法将为您的语言创建词法分析器和解析器。当语言变得更加复杂时,最好将这两个文件分开,但一开始,只使用一个(组合的)语法文件会更容易。
假设 PDDL 语言只是一种简单的语言:它是一系列由空格分隔的十六进制 (0x12FD)、八进制 (0745) 或十进制 (12345) 表示法的数字。这种语言可以在以下名为
PDDL.g
的 ANTLR 语法文件中进行描述:在该语法中,以大写字母开头的规则(解析、数字、十六进制……是规则)是词法分析器:规则。其他的是解析器规则。
根据此语法,您可以创建如下所示的词法分析器和解析器:
它(至少)生成文件
PDDLParser.java
和PDDLLexer.java
。现在创建一个小测试类,您可以在其中使用这些词法分析器和解析器类:
其中
source.txt
文件的内容可能如下所示:现在编译所有
.java
files:并运行主类:
如果一切顺利,控制台上不会打印任何内容。
现在您说您想让解析器根据源文件的内容返回某些对象。假设我们希望语法返回一个
List
。这可以通过在语法规则中嵌入“动作”来完成,如下所示:如您所见,您可以让规则返回对象(
返回 [Type t]
),并且如果包装它,则可以嵌入纯 Java 代码在{
和}
中。parse
规则中的@init
部分放置在PDDLParser.java
中parse
方法的开头文件。使用此类测试新的解析器:
您将看到以下内容打印到控制台:
Sure.
First you need to describe your language in a (ANTLR) grammar file. The easiest is to do this in a combined grammar. A combined grammar will create a lexer and parser for your language. When the language gets more complex, it is better to separate these two, but to start out, it will be easier to use just one (combined) grammar file.
Let's say the PDDL language is just an easy language: it is a succession of one or more numbers either in hexadecimal (0x12FD), octal (0745) or decimal (12345) notation separated by white spaces. This language can be described in the following ANTLR grammar file called
PDDL.g
:In this grammar, the rules (parse, number, Hex, ... are rules) that start with a capital are lexer-rules. The other ones are parser-rules.
From this grammar, you can create a lexer and parser like this:
which produces (at least) the files
PDDLParser.java
andPDDLLexer.java
.Now create a little test class in which you can use these lexer and parser classes:
where the contents of the
source.txt
file might look like this:Now compile all
.java
files:and run the main class:
If all goes well, nothing is being printed to the console.
Now you say you wanted to let the parser return certain objects based on the contents of your source file. Let's say we want our grammar to return a
List<Integer>
. This can be done by embedding "actions" in your grammar rules like this:As you can see, you can let rules return objects (
returns [Type t]
) and can embed plain Java code if wrapping it in{
and}
. The@init
part in theparse
rule is placed at the start of theparse
method in thePDDLParser.java
file.Test the new parser with this class:
and you'll see the following being printed to the console:
这当然是可能的,因为 Antlr 被设计为生成解析器,然后将其作为更大系统(例如编译器或静态代码分析器)的一部分进行调用。
从 Terence Parr 的权威 Antlr 参考:构建特定领域语言开始。他是 Antlr 的作者,也是一位异常清晰、没有行话的语言处理老师。
Martin Fowler 的特定领域语言在许多示例中使用了 Antlr。例如,在第 200 页,他展示了一个简单的“Hello World”示例,其中 Java 程序调用 Antlr 来解析要问候的人员文件,并在执行此操作时发出问候语。这里是完成工作的地方(第 206 页):
第三本好书是 Terence 的关于 DSL 的新书 语言实现模式。他描述了使用 Antlr 的各种方法,例如编写一个抽象语法树生成器以放入编译器中。
This is certainly possible, since Antlr is designed to generate parsers that then get invoked as part of a larger system (eg, a compiler or a static code analyzer).
Start with Terence Parr's The Definitive Antlr Reference: Building Domain-Specific Languages. He's the Antlr author, and also an unusually clear and jargon-free teacher on language processing.
Martin Fowler's Domain-Specific Languages uses Antlr in a lot of its examples. For instance on page 200 he shows a simple "Hello World" example where a Java program calls Antlr to parse a file of people to greet, and while doing it emits the greetings. Here's where the work gets done (page 206):
A third good book is Terence's new one on DSLs Language Implementation Patterns. He describes various ways to use Antlr, as for instance to write an abstract syntax tree generator to put into a compiler.