Skip to content

Instantly share code, notes, and snippets.

@kmizu
Created March 7, 2010 05:52
Show Gist options
  • Save kmizu/324179 to your computer and use it in GitHub Desktop.
Save kmizu/324179 to your computer and use it in GitHub Desktop.
/**
* This code doen't work in Scala 2.7.X.
*/
import scala.util.parsing.combinator._
import scala.util.parsing.input._
object LeftRecursion extends PackratParsers {
type Elem = Char
lazy val any = elem("", _ != CharSequenceReader.EofCh)
lazy val P: PackratParser[Any] = A ~ not(any)
lazy val A: PackratParser[Any] = A ~ 'a' | success(null)
def main(args: Array[String]) {
implicit def in(str: String) =
new PackratReader(new CharSequenceReader(str))
println(P("")) // success
println(P("aaa")) // success
println(P("b")) // failure
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment