In ANTLR I want to define a rule like this:
rule : ( a b c | a c b | b a c | b c a | c a b | c b a );
But in my case I have 10 rules instead of three, that I want to permute so it gets very impractical.
Is there any way of expressing this in ANTLR without having to write all the permutations?
I would just match any
a,borconce or more:and then, after parsing, traversing the parse tree and checking if
a,bandcall matched exactly once.But Yes, it is possible in the grammar itself by using predicates where needed.
A demo:
The demo grammar above uses 2 different types of predicates: 1) a gated semantic predicate i to make sure that the
permutationrule matches no more than the parameterfinal int ntokens, and 2) a validating semantic predicate i to ensure that thesetholds exactly thefinal int nelements to ensure that it’s a proper permutation of the 5 tokens.More info about semantic predicates can be found here: What is a 'semantic predicate' in ANTLR?
You can test the grammar with the following class: