[antlr-interest] Lexing question

Gavin Lambert antlr at mirality.co.nz
Sat Apr 5 20:34:54 PDT 2008


At 13:04 6/04/2008, John Ridgway wrote:
 >A "JavaLetter" is a character for which the method
 >Character.isJavaIdentifierStart(int) returns true.  A
 >"JavaLetterOrDigit" is a character for which the method
 >Character.isJavaIdentifierPart(int) returns true.  How
 >do I specify that in ANTLR (v3)?

Well, you *could* do it via a gated semantic predicate:

fragment JavaLetter
   : { Character.isJavaIdentifierStart(input.LA(1)) }? => .
   ;
fragment JavaLetterOrDigit
   : { Character.isJavaIdentifierPart(input.LA(1)) }? => .
   ;

However doing this will mean that the interpreter will no longer 
work (since that can't execute target language blocks), and can 
limit the things ANTLR can do with the generated code.  It's 
usually better to explicitly spell out the characters you mean 
within ANTLR itself rather than calling a target language function 
to make the decisions, wherever possible.



More information about the antlr-interest mailing list