Failed Predicate Exception

Can anyone give me some examples of Failed Predicate exception in ANTLR.

and some examples that will clearly explain :- input mismatch VS failed predicate VS no viable alt exceptions in ANTLR. thanks in advance.

Upvotes: -1

Views: 257

Answers (1)

Mike Lischke
Mike Lischke

Reputation: 53502

The names of the exceptions pretty much explain when they can appear. Only the failed predicate exception is a bit special.

NoViableAlt:

Thrown when you try to match block with several alternatives and none matches. Example:

r: 'a' | 'b';

Input: 'c'.

InputMismatch:

Thrown when you have input that partially matches. Example:

r: 'a' 'b' 'c' EOF;

Input: 'a' 'b' or 'a' 'c' EOF.

FailedPredicateException:

Thrown in certain situations where a path is guarded by a predicate and this path is the only possible match (or a required match), but the predicate prevents matching it. For example:

... | a ({condition}? b)

However if the block with the predicate is optional (so it's not required) then no predicate exception is thrown, like with:

... | a ({condition}? b)?

For more advanced use of these exceptions for generating better error messages see this error listener.

Upvotes: 0

Related Questions