1

Given a set S of n rules, I need an antlr4 rule to match any of S subset, in any order :

  • each rule of S can appear zero or one time
  • any permutation of the subset is ok

Example :

Given S = {a,b}, (n = 2) the rule must match

  • a
  • b
  • a b
  • b a

while "a b b", for instance must not match.

It is possible to parse such expression with an antlr4 grammar ? My real set has n = 6, so listing all combinations in the grammar seems not to be a possible choice !

Alice Oualouest
  • 836
  • 12
  • 20

1 Answers1

2

No, you can't define combinations and/or permutations of rules in ANTLR (or any other parser generator that I know).

You could use predicates to accomplish your goal, but that means adding target specific code to your grammar: I'd just parse any a or b and validate the structure after parsing (in a custom visitor/listener).

Bart Kiers
  • 166,582
  • 36
  • 299
  • 288