Home > Enterprise >  EBNF with comma repetition
EBNF with comma repetition

Time:11-19

I find myself doing the following quite frequently as allowing one multiple entries separated by a comma:

( function | expression ) ( ',' ( function | expression ))*

Is there a more compact way to do this? Ideally I'd just like to be able to do something along the lines of:

( function | expression ) [,...]

Or:

( function | expression ',')*

By the way, I am using this as a validator: enter image description here

CodePudding user response:

( function | expression ) ( ',' ( function | expression ))*

Is there a more compact way to do this?

Other than introducing "helper rules" like this:

rule
 : atom_list
 ;

atom_list
 : atom (',' atom)*
 ;

atom
 : function
 | expression
 ;

the answer is: no, there is no shorter way to write a (',' a)* into something like (a ',')* with ANTLR.

If you're repeating function | expression a lot, at the very least make a separate rule of those alternatives.

  • Related