Search code examples
antlrantlr4

Is there a shorter way in ANTLR to specify optional repeating elements?


When defining grammars I find myself writing this pattern quite often:

expr4: expr1 expr2 expr3 (',' expr1 expr2 expr3)*

Is there a more concise way to express it, without redundancy? Of course I could define something like:

expr5: expr1 expr2 expr3

but I don't think it's worth it.


Solution

  • No, besides the expr5 : expr1 expr2 expr3; (and then doing expr4 : expr5 (‘,’ expr5)*;, there is no other way.