A
Parser that runs
this and then runs
op for 0 or more times greedily.
The
Function objects returned from
op are applied from left to right to the return
value of p.
This is the preferred API to avoid
StackOverflowError in left-recursive parsers.
For example, to parse array types in the form of "T[]" or "T[][]", the following
left recursive grammar will fail:
Terminals terms = Terminals.operators("[", "]");Parsers.sequence(ref.lazy(), terms.phrase("[", "]"), new Unary() {...})));
return ref.get();
}
A correct implementation is:
Terminals terms = Terminals.operators("[", "]");return leafTypeParer.postfix(terms.phrase("[", "]").retn(new Unary() {...}));
}
A not-so-obvious example, is to parse the
expr ? a : b ternary operator. It too is a
left recursive grammar. And un-intuitively it can also be thought as a postfix operator.
Basically, we can parse "? a : b" as a whole into a unary operator that accepts the condition
expression as input and outputs the full ternary expression:
Parser ternary(Parser expr) }
OperatorTable also handles left recursion transparently.
p.postfix(op) is equivalent to
p op* in EBNF.