author | Christian Urban <christian dot urban at kcl dot ac dot uk> |
Wed, 21 Nov 2012 02:20:16 +0000 | |
changeset 66 | 9215b9fb8852 |
parent 64 | 2d625418c011 |
child 70 | e6868bd2942b |
permissions | -rw-r--r-- |
64
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
1 |
|
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
2 |
// parser combinators with input type I and return type T |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
3 |
// and memoisation |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
4 |
|
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
5 |
case class SubString(s: String, l: Int, h: Int) { |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
6 |
def low = l |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
7 |
def high = h |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
8 |
def length = h - l |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
9 |
def substring(l: Int = l, h: Int = h) = s.slice(l, h) |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
10 |
def set(low: Int = l, high: Int = h) = SubString(s, low, high) |
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 |
} |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
13 |
|
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
14 |
type Ctxt = List[(String, SubString)] |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
15 |
|
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
16 |
abstract class Parser[T] { |
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 |
def parse(ts: SubString, ctxt: Ctxt): Set[(T, SubString)] |
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 |
def parse_all(s: String) : Set[T] = |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
21 |
for ((head, tail) <- parse(SubString(s, 0, s.length), Nil); if (tail.substring() == "")) yield head |
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 |
def || (right : => Parser[T]) : Parser[T] = new AltParser(this, right) |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
24 |
def ==>[S] (f: => T => S) : Parser [S] = new FunParser(this, f) |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
25 |
def ~[S] (right : => Parser[S]) : Parser[(T, S)] = new SeqParser(this, right) |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
26 |
def ~>[S] (right : => Parser[S]) : Parser[S] = this ~ right ==> (_._2) |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
27 |
def <~[S] (right : => Parser[S]) : Parser[T] = this ~ right ==> (_._1) |
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 |
|
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
30 |
class SeqParser[T, S](p: => Parser[T], q: => Parser[S]) extends Parser[(T, S)] { |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
31 |
def parse(sb: SubString, ctxt: Ctxt) = |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
32 |
for ((head1, tail1) <- p.parse(sb, ctxt); |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
33 |
(head2, tail2) <- q.parse(tail1, ctxt)) yield ((head1, head2), tail2) |
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 |
class AltParser[T](p: => Parser[T], q: => Parser[T]) extends Parser[T] { |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
37 |
def parse(sb: SubString, ctxt: Ctxt) = p.parse(sb, ctxt) ++ q.parse(sb, ctxt) |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
38 |
} |
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 |
class FunParser[T, S](p: => Parser[T], f: T => S) extends Parser[S] { |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
41 |
def parse(sb: SubString, ctxt: Ctxt) = |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
42 |
for ((head, tail) <- p.parse(sb, ctxt)) yield (f(head), tail) |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
43 |
} |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
44 |
|
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
45 |
case class SubStringParser(s: String) extends Parser[SubString] { |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
46 |
val n = s.length |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
47 |
def parse(sb: SubString, ctxt: Ctxt) = { |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
48 |
if (n <= sb.length && sb.substring(sb.low, sb.low + n) == s) |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
49 |
Set((sb.set(high = sb.low + n), sb.set(low = sb.low + n))) |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
50 |
else Set() |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
51 |
} |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
52 |
} |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
53 |
|
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
54 |
implicit def string2parser(s: String) = SubStringParser(s) ==> (_.substring()) |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
55 |
|
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
56 |
class IgnLst[T](p: => Parser[T]) extends Parser[T] { |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
57 |
def parse(sb: SubString, ctxt: Ctxt) = { |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
58 |
if (sb.length == 0) Set() |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
59 |
else for ((head, tail) <- p.parse(sb.set(high = sb.high - 1), ctxt)) |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
60 |
yield (head, tail.set(high = tail.high + 1)) |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
61 |
} |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
62 |
} |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
63 |
|
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
64 |
class CHECK[T](nt: String, p: => Parser[T]) extends Parser[T] { |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
65 |
def parse(sb: SubString, ctxt: Ctxt) = { |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
66 |
val should_trim = ctxt.contains (nt, sb) |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
67 |
if (should_trim && sb.length == 0) Set() |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
68 |
else if (should_trim) new IgnLst(p).parse(sb, (nt, sb)::ctxt) |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
69 |
else p.parse(sb, (nt, sb)::ctxt) |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
70 |
} |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
71 |
} |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
72 |
|
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
73 |
lazy val E: Parser[Int] = |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
74 |
new CHECK("E", (E ~ "+" ~ E) ==> { case ((x, y), z) => x + z} || |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
75 |
(E ~ "*" ~ E) ==> { case ((x, y), z) => x * z} || |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
76 |
("(" ~ E ~ ")") ==> { case ((x, y), z) => y} || |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
77 |
"0" ==> { (s) => 0 } || |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
78 |
"1" ==> { (s) => 1 } || |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
79 |
"2" ==> { (s) => 2 } || |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
80 |
"3" ==> { (s) => 3 }) |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
81 |
|
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
82 |
println("foo " + E.parse_all("1+2*3")) |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
83 |
|
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
84 |
|
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
85 |
// ambiguous grammar |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
86 |
|
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
87 |
lazy val S: Parser[String] = |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
88 |
new CHECK("S", ("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
|
89 |
|
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
90 |
lazy val S2: Parser[String] = |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
91 |
new CHECK("S2", (S2 ~ S2 ~ S2) ==> { case ((x, y), z) => x + y + z} || "1" || "") |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
92 |
|
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
93 |
def test2(i: Int) = { |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
94 |
val result = E.parse_all("1" * i) |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
95 |
print(result.size + " " + result + " ") |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
96 |
} |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
97 |
|
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
98 |
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
|
99 |
val start = System.nanoTime() |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
100 |
for (j <- 1 to i) code |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
101 |
val end = System.nanoTime() |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
102 |
(end - start)/(i * 1.0e9) |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
103 |
} |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
104 |
|
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
105 |
|
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
106 |
for (i <- 1 to 10) { |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
107 |
print(i + " ") |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
108 |
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
|
109 |
print("\n") |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
110 |
} |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
111 |