Implementation of Grammar

Hello,

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(Instruction.java:279)
at org.sonar.sslr.internal.vm.Machine.execute(Machine.java:162)
at org.sonar.sslr.internal.vm.Machine.execute(Machine.java:105)
at org.sonar.sslr.internal.vm.Machine.parse(Machine.java:83)
at org.sonar.sslr.parser.ParseRunner.parse(ParseRunner.java:46)
at org.sonar.sslr.tests.RuleAssert.matches(RuleAssert.java:81)

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.rule(TOKEN_STRUCTURE).is(b.firstOf(
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})
Thanks
JPK

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

b.rule(TOKEN).is(b.optional("toto"))

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
JPK

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