Search code examples
antlrantlr4tlv

ANTLR4 and parsing a type-length-value format


I am trying create a grammar for a format that follows a type-length-value convention. Can ANTLR4 read in a length value and then parse that many characters?


Solution

  • NO ...

    From your question (which is very short so I could miss something ...) I gather you are mixing grammar and encoding rules.

    When you say type-length-value, it sounds like an encoding rule to me (how to serialize a data). In my experience, you write this code yourself.

    A grammar is at a higher level: it's a piece of text that describes something. Antlr will help you breaking this text into tokens and then into a tree that you can navigate. This step only handles text: if you were going that way to solve your problem, you would still have to handle type, length and value yourself.

    EDIT: with a bit of googling I found this https://github.com/NickstaDB/SerializationDumper