Packrat parser conflict

247 views Asked by At

Suppose I try to parse a string abc with a Packrat Parser:

  lazy val abc: PackratParser[AnyRef] = ab ~ "c" 

  lazy val ab: PackratParser[AnyRef] = (ab | abc) ~ "b" | "a" 

  def parse(in: String) = parseAll(abc, in)

Here I use left recursion supported by Packrat parser, but I do not understand why it fails. According to Parser documentation P | Q equals P if P succeeds, so in this case ab should be replaced with "ab" instead of "a" as it does if I replace ab with:

  lazy val ab: PackratParser[AnyRef] = ab ~ "b" | "a"
1

There are 1 answers

0
David Pierre On

A Packrat parser supports left recursion, but does it support cycles between rules (without progress).

That's what you have here: abc calls ab which can call abc.

Maybe you should try putting the | in the abc rule to avoid the cycle.