Is ANTLR an appropriate tool to serialize/deserial

2019-04-21 07:43发布

I need to read and write octet streams to send over various networks to communicate with smart electric meters. There is an ANSI standard, ANSI C12.19, that describes the binary data format. While the data format is not overly complex the standard is very large (500+ pages) in that it describes many distinct types. The standard is fully described by an EBNF grammar. I am considering utilizing ANTLR to read the EBNF grammar or a modified version of it and create C# classes that can read and write the octet stream.

Is this a good use of ANTLR?

If so, what do I need to do to be able to utilize ANTLR 3.1? From searching the newsgroup archives it seems like I need to implement a new stream that can read bytes instead of characters. Is that all or would I have to implement a Lexer derivative as well?

If ANTLR can help me read/parse the stream can it also help me write the stream?

Thanks.

dan finucane

3条回答
虎瘦雄心在
2楼-- · 2019-04-21 08:05

You might take a look at Ragel. It is a state machine compiler/lexer that is useful for implementing on-the-wire protocols. I have read reports that it generates very fast code. If you don't need a parser and template engine, ragel has less overhead than ANTLR. If you need a full-blown parser, AST, and nice template engine support, ANTLR might be a better choice.

查看更多
闹够了就滚
3楼-- · 2019-04-21 08:08

It seems to me that having a grammar gives you a tremendous leg up.

ANTLR 3.1 has StringTemplate and code generation features that are separate from the parsing/lexing, so you can decompose the problem that way.

Seems like a winner to me, worth trying.

查看更多
Juvenile、少年°
4楼-- · 2019-04-21 08:12

This subject comes up from time to time on the ANTLR mailing list. The answer is usually no, because binary file formats are very regular and it's just not worth the overhead.

查看更多
登录 后发表回答