[antlr-interest] need help with predicates

Andy Tripp antlr at jazillian.com
Thu Aug 9 08:33:56 PDT 2007


The language I'm parsing, visual basic, lets an identifier have a '!' 
suffix:

Identifier:
    '['? LETTER (LETTER| DECIMAL_LITERAL)* ('%'|'#'|'$'|'&'|'!')? ']'?
    ;

But it also lets you use '!' as a "separator" the way C/C++/Java/etc. 
use '.'
In the midst of a hierarchy of rules dealing with expressions, I have 
this rule:

dotOpExpression:
    unaryOps (
          DOT^ dotOperand?
        | BANG^ anyName?
        )*
    ;

Here, the unaryOps, dotOperand, and anyName rules all eventually refer 
to Identifier.
So the problem is that during the dotOpExpression processing, the 
unaryOps consumes
the Identifier, including the '!'. So in trying to match "a!b", it 
fails, because it took "a!"
as the Identifier and couldn't match the rest.

So one solution is to take the '!' out of the Identifier rule, perhaps 
now calling it IdentifierNoBang,
and then have alternative versions of other rules (unaryOpsNoBang, 
dotOperandNoBang, anyNameNoBang, etc).
But that would be a huge mess.

It seems like a syntactic predicate with "backtrack=true" should work 
here, but I can't quite see how.
I want to say, in dotOpExpression, "try to match this pattern, but if 
that doesn't work, try again, but this
time don't allow a '!' at the end of unaryOps". I can't see how to do 
that without all that rework to
remove the '!' from Identifier.

Any ideas?
Thanks,
Andy






More information about the antlr-interest mailing list