author | Christian Urban <urbanc@in.tum.de> |
Mon, 01 Oct 2018 14:04:48 +0100 | |
changeset 567 | 4573d36d0b2f |
parent 93 | 4794759139ea |
permissions | -rw-r--r-- |
64
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
1 |
//:load parser3.scala |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
2 |
|
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
3 |
case class StringParser(s: String) extends Parser[String, String] { |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
4 |
def parse(ts: String) = { |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
5 |
if (s.length <= ts.length && ts.startsWith(s)) Set((s, ts.drop(s.length))) |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
6 |
else Set() |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
7 |
} |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
8 |
} |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
9 |
|
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
10 |
implicit def string2parser(s: String) = StringParser(s) |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
11 |
|
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
12 |
def time_needed[T](i: Int, code: => T) = { |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
13 |
val start = System.nanoTime() |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
14 |
for (j <- 1 to i) code |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
15 |
val end = System.nanoTime() |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
16 |
(end - start)/(i * 1.0e9) |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
17 |
} |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
18 |
|
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
19 |
// unambiguous grammar |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
20 |
|
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
21 |
lazy val U: Parser[String, String] = |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
22 |
("1" ~ U) ==> { case (x, y) => "1" + y} || "" |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
23 |
|
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
24 |
def test1(i: Int) = { |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
25 |
val result = U.parse_all("1" * i) |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
26 |
//print(result.size + " ") |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
27 |
} |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
28 |
|
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
29 |
for (i <- 1 to 1000 by 50) { |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
30 |
print(i + " ") |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
31 |
print("%.5f".format(time_needed(1, test1(i)))) |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
32 |
print("\n") |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
33 |
} |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
34 |
|
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
35 |
|
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
36 |
|
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
37 |
// ambiguous grammar |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
38 |
// n = 16 -> over 35 million parse trees |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
39 |
|
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
40 |
lazy val S: Parser[String, String] = |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
41 |
("1" ~ S ~ S) ==> { case ((x, y), z) => "1" + y + z} || "" |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
42 |
|
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
43 |
def test2(i: Int) = { |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
44 |
val result = S.parse_all("1" * i) |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
45 |
print(result.size + " ") |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
46 |
} |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
47 |
|
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
48 |
for (i <- 1 to 30) { |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
49 |
print(i + " ") |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
50 |
print("%.5f".format(time_needed(1, test2(i)))) |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
51 |
print("\n") |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
52 |
} |