[antlr-interest] assigning preference to one of two intersectingrules

Diehl, Matthew J matthew.j.diehl at intel.com
Fri Aug 17 10:26:01 PDT 2007


> 
> In the grammar that follows, I am attempting to match all 
> strings such as:
> 
> "54"
> "a54"
> "#75"
> "n.ob89"
> 
> I'd like to be able to extract the Prefix and Number 
> values for later use in a parser.  What's the best way to 
> go about this?  How do I tell
> ANTLR to attempt to match first the first rule in 
> BoxNumberComplete?

I don't think you can tell it to match the first...but since your lexer
already works, don't change it and in the parser just analyze the string
and get the location of the split manually:

identifier :
  BoxNumberComplete
{
  String tmp = BoxNumberComplete.text;
  int i;
  for(i = tmp.length(); i >= 0; i--){
    if (!Character.isAlphaNumeric(tmp.charAt(i))) //or something like
that
      break;
  }
  //i is the location of the end of your first rule in
BoxNumberComplete.
}
  ;

I like modifying the parser a lot better than the lexer.
Good luck,
Matt


More information about the antlr-interest mailing list