author | Christian Urban <christian dot urban at kcl dot ac dot uk> |
Sun, 01 Dec 2013 10:08:53 +0000 | |
changeset 216 | f5ec7c597c5b |
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 matcher.scala |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
2 |
//:load parser3.scala |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
3 |
|
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
4 |
abstract class Token |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
5 |
case object T_ONE extends Token |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
6 |
|
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
7 |
val lexing_rules : List[Rule[Token]] = |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
8 |
List(("1", (s: List[Char]) => T_ONE)) |
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 |
val T = Tokenizer(lexing_rules) |
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 |
case class TokParser(tok: Token) extends Parser[List[Token], Token] { |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
13 |
def parse(ts: List[Token]) = ts match { |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
14 |
case t::ts if (t == tok) => Set((t, ts)) |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
15 |
case _ => Set () |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
16 |
} |
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 |
implicit def token2tokparser(t: Token) = TokParser(t) |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
19 |
|
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
20 |
case object EmpParser extends Parser[List[Token], String] { |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
21 |
def parse(ts: List[Token]) = Set(("", ts)) |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
22 |
} |
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 |
|
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
25 |
lazy val Su: Parser[List[Token], String] = |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
26 |
(T_ONE ~ Su) ==> { case (x, y) => "1" + y} || EmpParser |
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 |
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
|
30 |
val start = System.nanoTime() |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
31 |
for (j <- 1 to i) code |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
32 |
val end = System.nanoTime() |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
33 |
(end - start)/(i * 1.0e9) |
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 |
def test(i: Int) = { |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
37 |
val result = Su.parse_all(T.fromString("1" * i)) |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
38 |
//print(result.size + " ") |
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 |
|
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
41 |
|
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
42 |
for (i <- 1 to 1000 by 50) { |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
43 |
print(i + " ") |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
44 |
print("%.5f".format(time_needed(1, test(i)))) |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
45 |
print("\n") |
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 |