我可以使用 ANTLR 进行双向解析/生成吗?

发布于 2024-08-27 13:57:20 字数 503 浏览 3 评论 0原文

我需要解析传入消息并生成 EDIFACT 格式(基本上是结构化分隔格式)的传出消息。

我想要一个通过解析消息生成的 Java 模型。然后我想使用相同的模型来创建实例并生成消息。

前半部分很好,我之前使用过 ANTLR 从原始 -> 开始。 Java 对象。但我从来没有做过相反的事,或者即使有,也是习惯。

ANTLR 是否支持使用语法生成,或者它实际上只是一个仅解析的工具?

编辑:

扩展 - 我想理想地定义两件事。描述原始消息的语法(在本例中为 EDIFACT,但如果您愿意,可以假装它是 CSV)。以及 Java 对象模型。

我知道我可以编写一个 ANTLR 语法来从原始数据中获取 -> Java 模型。例如解析 SQL 字符串 ->我以前做过的Java模型。但理想情况下我也需要走另一条路,而不改变语法。

如果您将其比作 JAXB(XML 世界),那么我真的希望将 JAXB 用于 EDIFACT(而不是 XML)。

I need to both parse incoming messages and generate outgoing messages in EDIFACT format (basically a structured delimited format).

I would like to have a Java model that will be generated by parsing a message. Then I would like to use the same model to create an instance and generate a message.

The first half is fine, I've used ANTLR before to go from raw -> Java objects. But I've never done the reverse, or if I have it's been custom.

Does ANTLR support generating using a grammar or is it really just a parse-only tool?

EDIT:

Expansion - I want to define two things ideally. A grammar that describes the raw message (EDIFACT in this case but pretend it's CSV if you like). And a Java object model.

I know I can write an ANTLR grammar to get from the raw -> Java model. e.g. Parsing a SQL string -> Java model which I've done before. But I need to go the other way as well ideally without changing the grammar.

If you liken it to JAXB (XML world), I really want JAXB for EDIFACT (rather than XML).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

離殇 2024-09-03 13:57:20

ANTLR 可以做你所要求的吗,是的。尽管它可能需要多个语法人员。

对我来说,这听起来像是您想从解析器创建 AST。让一个树遍历器完成所需的所有 java 对象创建(可能是第二个语法)。然后第二个树遍历器创建输出消息(第三个语法),如果需要,您甚至可以使用 StringTemplate。也许你可以摆脱两个语法。

但此时必须提供实际详细信息才能获得更多帮助,例如特定输入的 AST 是什么样子以及输出消息应该是什么。

Can ANTLR do what you are asking, YES. Although it might require multiple grammers.

To me, this sounds like you want to create a AST from your parser. Have one tree walker doing all the java object creation required (second grammer possibly). And then a second tree walker to create the output messages (third grammer), and you can even use StringTemplate if you want. Maybe you can get away with two grammers.

But at this point actual details would have to be given for any more help, what the AST will look like for a specific input and what the output message should be.

沫尐诺 2024-09-03 13:57:20

我自己从来没有这样做过(我也使用 ANTLR 仅用于解析),但我确信 ANRLR 也可以用作生成器。

事实上,它使用一个名为 stringtemplates 的库来生成自己的代码(由同一作者编写)。

I have never done it myself (I also used ANTLR for parsing only) but I know for sure that ANRLR can be used as a generator as well.

in fact, it's using a library called stringtemplates for it's own code generation (by the same author).

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