[antlr-interest] Binary files and different int types

Gavin Lambert antlr at mirality.co.nz
Sun Jul 22 14:19:40 PDT 2007


At 04:59 23/07/2007, Xavier Pegenaute wrote:
 >I am trying to parse a binary file with a set of values but I 
have
 >some problems.  I have been reading the user manual and seems 
that
 >it is ok for a basic datatype but I don't know how I can support 

 >these other different data types:
 >- Int 32
 >- Int 64
 >- Double
 >
 >any one know how I can differentiate every rule?
 >
 >The binary file "follow" this rule:
 >(Double Int32 Int32 Int64 Int32)*

ANTLR isn't really designed for parsing binary files, since 
normally those don't actually require any parsing at all -- 
they're trivial to read in.  Especially if as you said the whole 
file is a fixed pattern of records -- that's like 3 lines of C 
code to read in.

However, if you really want to do it, then what you should do 
depends on how you want to deal with the output.

You could make the above rule (without the *) a lexer rule to 
treat the entire set as one token.  That would work, but I suspect 
wouldn't be all that useful.

Another option would be to make only one token, a Byte, and change 
everything else to be parser rules in terms of Bytes.  This will 
let you be a bit more context-sensitive, but you'll have to 
manipulate the tokens a bit if you want to combine the resulting 
value.

Really, I think just reading it directly instead of using ANTLR is 
your best bet.



More information about the antlr-interest mailing list