[antlr-interest] Ambiguity between identifier and operator

David Chipping david.chipping at anubex.com
Thu Jun 11 02:22:56 PDT 2009


I'm having some trouble trying to work out the best approach for some 
ambiguity.

If I have an identifier token defined as:

IDENTIFIER: ('A'..'Z' | 'a'..'z' | '_') ('A'..'Z' | 'a'..'z' | '_' | 
'0'..'9')* ('$' | '!' | '&')?

Were the last part of the rule indicates an optional implicit type 
character.

Unfortunately, "!" is also a binary operator that works with 
identifiers. For example, the following is valid: 

foo!bar

and indicates a ! operator with a left side of foo and right of bar. 
This is only possible when the left side identifier doesn't end with a 
implicit type character.

I was initially thinking of doing some token re-writing to determine if 
an identifier (without an implicit type char)  followed by a "!" is 
followed by another identifier and then emit a separate "!" so that can 
be picked up by the parser. But I'm not keen on doing this, as (as far 
as I can see, please correct me if i'm wrong) this takes some of my 
lexing rules out of my grammar and into another place, complicating any 
maintenance on the grammar itself.

Is there a cleaner/different way to achieve this?

Cheers

-- 
David Chipping
Anubex

Direct Tel.: +32 3 4504263
General Fax: +32 3 4504251
Web Address: http://www.anubex.com
Reply To: david.chipping at anubex.com 



More information about the antlr-interest mailing list