[antlr-interest] fragment: simple (or naive) usage does not work

Martin d'Anjou martin.danjou at neterion.com
Tue Feb 27 11:26:42 PST 2007


Hi,

This is my input:
int id;
int int_id;
int _int_id;
45b32
6h87z

I have to parse those pesky numbers at the botom. So I wrote the following 
lexer:

lexer grammar DUMMY_Lexer;
INT          : 'int' ;
SEMI         : ';' ;
WS           :  (  ' '| '\t'| '\r' | '\n' )+ {$channel=HIDDEN;} ;

IDENTIFIER   :
    ('a'..'z'|'A'..'Z'|'_')+ ;

NUMBER : DIGIT+ (BASE ZNUM+)? ;
fragment ZNUM : DIGIT|'z'|'Z' ;
fragment BASE : 'b' | 'h';
fragment DIGIT : '0'..'9';

And of course the parser:

parser grammar DUMMY_Parser;
options {
   tokenVocab=DUMMY_Lexer;
}

source_text :
   { System.out.println("Weird lexer"); }
   int_defs+
   numbers+
   ;

int_defs :
   INT            { System.out.print("int "); }
   id=IDENTIFIER  { System.out.print($id.text); }
   SEMI           { System.out.println(";"); }
   ;

numbers :
   n=NUMBER         { System.out.println($n.text); }
   ;


Alas, I get:
line 4:0 required (...)+ loop did not match anything at input '45b32'

If I move ZNUM inside NUMBER, like this:

NUMBER : DIGIT+ (BASE (DIGIT|'z'|'Z')+)? ;

then it works. What's up with fragment lexer rules?

Thanks,
Martin


More information about the antlr-interest mailing list