[antlr-interest] How to distinguish between an assembler comment and a literal integer

Antonio Martínez Álvarez amartinez at atc.ugr.es
Tue Sep 28 11:13:05 PDT 2010


Hi All ,
I'm working on an MSP430 assembly parser and I have this problem:

Fist of all this is a possible input for my grammar:

###################
#  Theese are comments     #
#                                            #
#    bla bla bla                      #
###################
labelA:
MOV.W  #0x1234, R5



As you an see '#' is used either for an introductory log and also to 
express a literal hex integer.

I'm trying something like (without success):


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

COMMENT
     :   '//' ~('\n'|'\r')* '\r'? '\n' {$channel=HIDDEN;}
     |    '#' '0x' => HEX_LITERAL
     |   '/*' ( options {greedy=false;} : . )* '*/' {$channel=HIDDEN;}
     ;


Result: [20:06:35] error(100): msp430.g:111:11: syntax error: antlr: 
msp430.g:111:11: unexpected token: '0x'


Could you please help me? How can I capture literal integer within this 
grammar?


More information about the antlr-interest mailing list