Ohm.js: Nullable expression section is not allowed inside '*'

182 Views Asked by At

I'm having difficulty writing an ohm grammar.

Here it is:

MyGrammar {
  Whole = section*
  section = partA? partB?
  partA = "foo" | "bar"
  partB = "baz"
}

And what its giving me is an error:

Nullable expression section is not allowed inside '*' (possible infinite loop)

Which is understandable but the problem is that what Im trying to do is say that there will always be either a partA in a section or a partB or both.

1

There are 1 best solutions below

0
N. McA. On
MyGrammar {
  Whole = something+
  something = partA | partB
  partA = "foo" | "bar"
  partB = "baz"
}

So as the error message suggests, you can't kleene-star a nullable expression. 'nullable' means 'will accept the empty string'; the reason is fairly intuitive - if your grammar will accept an infinite repetition of the empty string, then parsing it will not terminate. I provided an example of something that will work, but I'm not sure if it's equivalent to the grammar you want - if you provide more details in the question then I'll try and be more specific.