[antlr-interest] Surprising behaviour with syntactic predicates (X+ != X X*)

Steve Bennett stevagewp at gmail.com
Wed Nov 21 15:54:27 PST 2007


I've been experimenting with using syntactic predicates to suppress
the warnings that arise when you have rules that boil down to
something like: y: x* x*;

Here are some findings. In the following, X: x'; and the input string
is 'xxxxx';


start: X* X+;
EarlyExitException

start: X* ((X)=>X)+;
EarlyExitException

(ie, syntactic predicate makes no difference)

start: X* X;
Success - a single non-repeated token gets matched, but a "one or
more" token fails?

start: ((X)=>X)* X;
Success - even when explicitly saying you want the first X to match
wherever possible, the single token takes priority.

Here it makes a difference:
start: ((X=>)X* x2?;
x2: X;

With the predicate, x2 matches nothing. Without it, x2 matches one token.

All of which leads to this surprising result: the following two
grammars should (I think) be equivalent, but the first succeeds and
the second fails.

start: ((X)=>X)* X X*;
start: ((X)=>X)* X+;

Something to beware of, I guess?

Steve


More information about the antlr-interest mailing list