Question

The following simple pegjs grammar works fine:

start
  = sentence

sentence
  = word ws sentence
  / word

word
  = [a-z]*

ws
  = " "

It is available at http://jsfiddle.net/4V3Zt/ . The gramar can be also pasted into http://pegjs.majda.cz/online .

If I change the ws rule to allow whitespaces arbitrary spaces:

ws
  = " "*    // add an asterisk to allow " ", "  ", "   ", ...

parsing fails with a maximum callstack exception thrown. (If using jsfiddle, you see the exception in browser tools.The exception comes up in a node.js environment as well, so it is certainly related to pegjs).

What is wrong with the * in this ws rule?

Était-ce utile?

La solution

Use + instead of * (for word as well).

* can match the empty string; + requires at least one instance.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top