[antlr-interest] need help with predicates

Johannes Luber jaluber at gmx.de
Thu Aug 9 09:40:46 PDT 2007


Andy Tripp wrote:
> 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
> 

Why don't you test, when you encounter at the identifier the bang, if
input.LT(1) is a space? If not then it can't be a suffixed bang. (What
does "a!" actually mean in VB?) Regarding your thought to do
backtracking: It won't work. Please look for my email "Lexing troubles",
which details my encountering of the problem and how my workaround is -
which I have only solved to the biggest part. I'm not sure how to do
error messages but I believe you know this. :)

Best regards,
Johannes Luber


More information about the antlr-interest mailing list