Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix left-recursion with packrat #93

Merged
merged 5 commits into from
Apr 10, 2014

Conversation

pesco
Copy link

@pesco pesco commented Mar 21, 2014

This makes the following test case (leftrec-ne) work (parse "aaa"):

S -> S 'a' | 'a'

It does not work with (leftrec):

S -> S 'a' | epsilon

To be specific, it always succeeds with an empty parse (the second choice).
I currently think this is actually a case that Warth doesn't work for. It might be a degenerate form of the ones discussed in

http://tratt.net/laurie/research/pubs/html/tratt__direct_left_recursive_parsing_expression_grammars/ .

In fact, I cross-checked against the Scala implementation and it appears to exhibit the same behaviour with the following test program (compare the results or lr vs. lr1):

import scala.util.parsing.combinator._
import scala.util.parsing.input._

object P extends RegexParsers with PackratParsers {
  def a : PackratParser[Any] = elem('a')
  def eps : PackratParser[Any] = success("eps")
  lazy val lr : PackratParser[Any] = (lr~a) | eps
  lazy val lr1 : PackratParser[Any] = (lr1~a) | a

  def input = "aaa"

  def test() = parse(lr, new PackratReader(new CharArrayReader(input.toArray)))
}

P.test()

@pesco
Copy link
Author

pesco commented Mar 21, 2014

@fbz
Copy link

fbz commented Apr 10, 2014

The docs should be updated to clarify the cases that left recursion can and can't handle, but yeah this is ready to merge.

fbz added a commit that referenced this pull request Apr 10, 2014
Fix left-recursion with packrat
@fbz fbz merged commit f95dc6c into UpstandingHackers:master Apr 10, 2014
@pesco pesco deleted the fix-left-recursion branch December 11, 2015 11:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants