[antlr-interest] How to build a hex to string parser?

Sebastiaan Blom sebastiaan.blom at gmail.com
Fri Jun 15 12:52:13 PDT 2007


Hello,

I'm trying to build a parser (using ANTLRWorks 1.02) which can convert
some hex strings into some readable text strings (e.g. command X,
lenght Y, option Z, etc).

The input can be for example:

sE4w3737
sE5r370136
sE4w1A001A

Using the grammar below I getting problems with the following input:

sE4w2301020B052E
This one works fine in the interpreter (cmdD=020B052E) but not in the
debugger. There it thinks cmdD=E5 which is already the next line, I
don't understand why.

sE5rFF3701C9
For this line it is expecting a 3 for the C, I guess it wants to match
to the 370136 of data but why?
It should separate it in id, length and crc.

Any suggestions are welcome, thanks in advance.

Sebastiaan.


grammar test;

tokens {
	START = 's';
	READ = 'r';
	WRITE = 'w';
}

line	: (START address (READ | WRITE) data NEWLINE)*
	;

address	: HEX_DIGITS	{System.out.println("Addr:" + $address.text);}
	;

data	:'2401' cmdA
	|'2301' cmdD
	|'3737'		{System.out.println("3333 - ...");}
	|'29656E6745'	{System.out.println("29656E6745 - ...");}
	|'2A0000002A	{System.out.println("2A0000002A - ...");}
	|'36410077'		{System.out.println("36410077 - ...");}
	|'370136'		{System.out.println("370136 - ...");}
	|'370433'		{System.out.println("370433 - ...");}
	|'370532'		{System.out.println("370532 - ...");}
	|'61FF9E'		{System.out.println("61FF9E - ...");}
	|'FF' id length crc
	|'FFFF'		{System.out.println("Master enquires the interrupt function");}	
	// plus many more
	| HEX_DIGITS*	{System.out.println("Unknown");}
	;

cmdA	:'020B012D'	{System.out.println("02...");}
	|'040E13006458'	{System.out.println("04...");}
	|'051B010203043F'	{System.out.println("05...");}
	// plus many more
	| HEX_DIGITS*	{System.out.println("unknown");}
	;
	
cmdD	:'021A003A'	{System.out.println("xx");}
	|'0527DC0AE60131'	{System.out.println("xx");}
	// plus many more
	| HEX_DIGITS*	{System.out.println("unknown");}
	;	

id	: '37'		{System.out.println("37");}
	| '24'		{System.out.println("24");}
	// plus many more
	| HEX_DIGITS 	{System.out.println("id=" + $id.text);}
	;

crc	: HEX_DIGITS
	;

length	: HEX_DIGITS 	{System.out.println("length=" + $length.text);}
	;

HEX_DIGITS
	: HEX_DIGIT HEX_DIGIT
	;

fragment
HEX_DIGIT
	: ('a'..'f'|'A'..'F'|'0'..'9')
	;

NEWLINE
	: '\r'? '\n'
	;


More information about the antlr-interest mailing list