--- a/parser4.scala Wed Nov 21 07:28:28 2012 +0000
+++ b/parser4.scala Wed Nov 21 09:04:11 2012 +0000
@@ -1,6 +1,5 @@
// parser combinators with input type I and return type T
-// and memoisation
case class SubString(s: String, l: Int, h: Int) {
def low = l
@@ -23,8 +22,6 @@
def || (right : => Parser[T]) : Parser[T] = new AltParser(this, right)
def ==>[S] (f: => T => S) : Parser [S] = new FunParser(this, f)
def ~[S] (right : => Parser[S]) : Parser[(T, S)] = new SeqParser(this, right)
- def ~>[S] (right : => Parser[S]) : Parser[S] = this ~ right ==> (_._2)
- def <~[S] (right : => Parser[S]) : Parser[T] = this ~ right ==> (_._1)
}
class SeqParser[T, S](p: => Parser[T], q: => Parser[S]) extends Parser[(T, S)] {
@@ -70,6 +67,7 @@
}
}
+// ambigous grammar
lazy val E: Parser[Int] =
new CHECK("E", (E ~ "+" ~ E) ==> { case ((x, y), z) => x + z} ||
(E ~ "*" ~ E) ==> { case ((x, y), z) => x * z} ||
@@ -79,33 +77,6 @@
"2" ==> { (s) => 2 } ||
"3" ==> { (s) => 3 })
-println("foo " + E.parse_all("1+2*3"))
+println(E.parse_all("1+2*3"))
-// ambiguous grammar
-
-lazy val S: Parser[String] =
- new CHECK("S", ("1" ~ S ~ S) ==> { case ((x, y), z) => "1" + y + z} || "")
-
-lazy val S2: Parser[String] =
- new CHECK("S2", (S2 ~ S2 ~ S2) ==> { case ((x, y), z) => x + y + z} || "1" || "")
-
-def test2(i: Int) = {
- val result = E.parse_all("1" * i)
- print(result.size + " " + result + " ")
-}
-
-def time_needed[T](i: Int, code: => T) = {
- val start = System.nanoTime()
- for (j <- 1 to i) code
- val end = System.nanoTime()
- (end - start)/(i * 1.0e9)
-}
-
-
-for (i <- 1 to 10) {
- print(i + " ")
- print("%.5f".format(time_needed(1, test2(i))))
- print("\n")
-}
-