Reputation: 456
I want to specify a production in ANTLR where the order of its sub-productions does not matter. Note that I want each of the sub-productions to occur once. The brute force way to accomplish this would look something like this:
grammar Foo;
r : (A B) | (B A) ;
A : 'a' ;
B : 'b' ;
As more sub-productions are added, the number of permutations increases... n!/(n-k)!
grammar Foo2;
r : (A B C) | (A C B) | (B A C) | (B C A) | (C A B) | (C B A) ;
A : 'a' ;
B : 'b' ;
C : 'c' ;
Obviously the above solution is not practical. Is there a construct in ANTLR which describes permutations like this in a more concise way?
Upvotes: 3
Views: 184
Reputation: 170278
No, there is no way to describe a permutation in a production in ANTLR. You will need to match A
, B
or C
three times and then use a listener to check if all three tokens have been matched exactly one time.
You could do it inside your grammar using custom code and a predicate, but that would make your grammar very hard to read: best do this with a listener.
Upvotes: 6