[antlr-interest] newbie needs help

Hugo greneche.hugo at gmail.com
Thu Jan 21 11:20:46 PST 2010


I started using antlr to parse a specific file format.
The problem is that i don't know how to write correctly my grammar.

The file have the following format.
It contains multiple lines and each can have the following format:

Only one or multilple hexadecimal caracter with space or not
ex: A0 A4 B5 77
or: A0

Only variable identifier with the format VAR_XXX
ex: VAR_MY_VARIABLE

Or the combinaison of the two previous format
ex:
A0 A4B5 VAR_MY_VARIABLE 77 98 VAR_MY_VARIABLE2
or
VAR_MY_VARIABLE AA BB
or
AA BB VAR_MY_VARIABLE


what i want to do is to build a AST tree

And the problem is that i don't know how to do this with antlr. the tool
always tell me that multiple rule can be applies with my grammar.

please help me to solve my problem. 

Here is my grammar:

stmts               : bytes+ ;


bytes : multiple_byte bytes? -> ^(EXPR_DEF multiple_byte  bytes? )

| define_expression bytes? -> ^(EXPR_DEF define_expression bytes? )

| NEWLINE ;

define_expression : define_var -> ^(DEFINE_VAR_DEF define_var) ;

define_var : DEFINE_VARIABLE ;
multiple_byte : single_byte (single_byte)+ -> ^(MULTIPLE_BYTES_DEF
single_byte single_byte+) ;


single_byte : byte_digit -> ^(BYTES_DEF byte_digit) ;

byte_digit : BYTE_DIGIT ;

DEFINE_VARIABLE :
'VAR_'('a'..'z'|'A'..'Z'|'_')('a'..'z'|'A'..'Z'|'0'..'9'|'_')*;

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

// Ignore whitespace, tab and escape sequence WS : (' '|'\t'|'\\\r\n')+
{$channel = HIDDEN;} ;

// a new line NEWLINE : '\r'? '\n' ;

thanks a lot



More information about the antlr-interest mailing list