Implementation of Grammar


We are trying to implement a grammar with sslr and our unit tests are failing when trying to match some tokens.
Stack trace is ;

org.sonar.sslr.grammar.GrammarException: The inner part of ZeroOrMore and OneOrMore must not allow empty matches
at org.sonar.sslr.internal.vm.Instruction$CommitVerifyInstruction.execute(
at org.sonar.sslr.internal.vm.Machine.execute(
at org.sonar.sslr.internal.vm.Machine.execute(
at org.sonar.sslr.internal.vm.Machine.parse(
at org.sonar.sslr.parser.ParseRunner.parse(
at org.sonar.sslr.tests.RuleAssert.matches(

We assume we are not doing correct usage for method zeroOrMore of org.sonar.sslr.grammar.GrammarBuilder
But what does the message mean exactly ?
If it helps we have grammar rule key defined like

b.sequence("(", b.zeroOrMore(TOKEN), “)”),
b.sequence("[", b.zeroOrMore(TOKEN), “]”),
b.sequence("{", b.zeroOrMore(TOKEN), “}”)));

where TOKEN is another (non empty) rule key
the message The inner part of ZeroOrMore and OneOrMore must not allow empty matches is obscure, can you help us understand why is that ?
(We want the rule to match strings like (toto) [toto] or {toto})

You didn’t specify how the TOKEN rule is defined. Let’s suppose it’s defined as:


In that case, how should we parse [] against TOKEN_STRUCTURE? How many TOKEN nodes should it contain? 0, 1, 2,…? That could end with an infinite loop.

Instead of an infinite loop, you got the error message: “The inner part of ZeroOrMore and OneOrMore must not allow empty matches”.

You should either:

  • Change TOKEN so that it cannot match an empty string.
  • Change the content of zeroOrMore in TOKEN_STRUCTURE, for example: b.zeroOrMore(TOKEN, ",")

Thanks you Pierre-Yves, very helpful !
Actually the error message is meaningful once you understand what “inner part” means. (English is not my native language)
I quickly applied your first suggestion and it works ! Thanks again

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.