开发者

Can I use ANTLR for both two-way parsing/generating?

开发者 https://www.devze.com 2022-12-25 09:47 出处:网络
I need to both parse incoming messages and generate outgoing messages in EDIFACT format (basically a structured delimited format).

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


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消