[antlr-interest] assigning preference to one of two intersectingrules
cgorski at cgorski.org
cgorski at cgorski.org
Fri Aug 17 12:49:29 PDT 2007
Thanks. I suppose my real question is, "is it considered
acceptable practice to have ambiguities similar to those
that I specified?"
I think I read the following in some documentation and got
it in my head that every ambiguity warning has some
elegant resolution drastically different from what what I
was doing:
"What happens when the same character predicts more than a
single lexical rule? ANTLR generates an nondeterminism
warning between the offending rules, indicating you need
to make sure your rules do not have common left-prefixes.
ANTLR does not follow the common lexer rule of "first
definition wins" (the alternatives within a rule, however,
still follow this rule). Instead, sufficient power is
given to handle the two most common cases of ambiguity,
namely "keywords vs. identifiers", and "common prefixes";
and for especially nasty cases you can use syntactic or
semantic predicates."
-Chris
On Fri, 17 Aug 2007 10:26:01 -0700
"Diehl, Matthew J" <matthew.j.diehl at intel.com> wrote:
>>
>> 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