EBNF: one or more and zero or more, in no particular order

179 Views Asked by At

In an EBNF grammar I have two tokens shape and attr. How would I express that shape should occur one or more times and attr zero or more times, but in no particular order, i.e.:

shape1 shape2 shape3
attr1 shape1
shape1 attr1
shape1 shape2 attr1 shape3

Would all be parsed, but

attr1 attr2 ..

would not?

1

There are 1 best solutions below

0
judep On

Answering my own question:

seq = shape as_seq | attr seq
as_seq = (shape | attr)*

does the trick

(where shape and attr are defined elsewhere in the grammar. e.g. shape = 'a' and attr = 'b'. Then 'a' 'ab' 'aaab' 'bbbab' will all match but 'b' 'bb' 'bbb..' etc will not)