dcsimg
need help can't describe with a antlr grammar
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Hugo_Greneche
Posted On:   Thursday, January 14, 2010 03:20 PM

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 hexadecimal caracter with space or not A0 A4 B5 77 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 t   More>>

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 hexadecimal caracter with space or not

A0 A4 B5 77



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 : (' '|' '|'\
')+ {$channel = HIDDEN;} ;



// a new line
NEWLINE : '
'? '
'
;



   <<Less
About | Sitemap | Contact