author | Chengsong |
Thu, 17 Nov 2022 23:13:57 +0000 | |
changeset 625 | b797c9a709d9 |
parent 359 | fedc16924b76 |
permissions | -rw-r--r-- |
157
1fe44fb6d0a4
cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
156
diff
changeset
|
1 |
/* lexer without simplification */ |
1fe44fb6d0a4
cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
156
diff
changeset
|
2 |
|
34
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
3 |
import scala.language.implicitConversions |
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
4 |
import scala.language.reflectiveCalls |
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
5 |
import scala.annotation.tailrec |
166
cab1ae6f339a
added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
6 |
import scala.io.Source |
238
2dc1647eab9e
added AND-regular expression (intersection/conjunction)
Christian Urban <urbanc@in.tum.de>
parents:
178
diff
changeset
|
7 |
import scala.util._ |
34
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
8 |
|
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
9 |
abstract class Rexp |
156
6a43ea9305ba
updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
34
diff
changeset
|
10 |
case object ZERO extends Rexp |
6a43ea9305ba
updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
34
diff
changeset
|
11 |
case object ONE extends Rexp |
34
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
12 |
case class CHAR(c: Char) extends Rexp |
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
13 |
case class ALT(r1: Rexp, r2: Rexp) extends Rexp |
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
14 |
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp |
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
15 |
case class STAR(r: Rexp) extends Rexp |
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
16 |
case class RECD(x: String, r: Rexp) extends Rexp |
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
17 |
|
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
18 |
abstract class Val |
156
6a43ea9305ba
updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
34
diff
changeset
|
19 |
case object Empty extends Val |
34
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
20 |
case class Chr(c: Char) extends Val |
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
21 |
case class Sequ(v1: Val, v2: Val) extends Val |
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
22 |
case class Left(v: Val) extends Val |
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
23 |
case class Right(v: Val) extends Val |
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
24 |
case class Stars(vs: List[Val]) extends Val |
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
25 |
case class Rec(x: String, v: Val) extends Val |
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
26 |
|
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
27 |
// some convenience for typing in regular expressions |
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
28 |
def charlist2rexp(s : List[Char]): Rexp = s match { |
156
6a43ea9305ba
updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
34
diff
changeset
|
29 |
case Nil => ONE |
34
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
30 |
case c::Nil => CHAR(c) |
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
31 |
case c::s => SEQ(CHAR(c), charlist2rexp(s)) |
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
32 |
} |
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
33 |
implicit def string2rexp(s : String) : Rexp = charlist2rexp(s.toList) |
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
34 |
|
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
35 |
implicit def RexpOps(r: Rexp) = new { |
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
36 |
def | (s: Rexp) = ALT(r, s) |
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
37 |
def % = STAR(r) |
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
38 |
def ~ (s: Rexp) = SEQ(r, s) |
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
39 |
} |
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
40 |
|
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
41 |
implicit def stringOps(s: String) = new { |
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
42 |
def | (r: Rexp) = ALT(s, r) |
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
43 |
def | (r: String) = ALT(s, r) |
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
44 |
def % = STAR(s) |
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
45 |
def ~ (r: Rexp) = SEQ(s, r) |
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
46 |
def ~ (r: String) = SEQ(s, r) |
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
47 |
def $ (r: Rexp) = RECD(s, r) |
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
48 |
} |
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
49 |
|
166
cab1ae6f339a
added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
50 |
def Alts(rs: List[Rexp]) : Rexp = rs match { |
cab1ae6f339a
added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
51 |
case Nil => ZERO |
cab1ae6f339a
added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
52 |
case r::Nil => r |
cab1ae6f339a
added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
53 |
case r::rs => ALT(r, Alts(rs)) |
cab1ae6f339a
added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
54 |
} |
cab1ae6f339a
added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
55 |
def ALTS(rs: Rexp*) = Alts(rs.toList) |
cab1ae6f339a
added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
56 |
|
cab1ae6f339a
added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
57 |
def Seqs(rs: List[Rexp]) : Rexp = rs match { |
cab1ae6f339a
added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
58 |
case Nil => ONE |
cab1ae6f339a
added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
59 |
case r::Nil => r |
cab1ae6f339a
added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
60 |
case r::rs => SEQ(r, Seqs(rs)) |
cab1ae6f339a
added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
61 |
} |
cab1ae6f339a
added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
62 |
def SEQS(rs: Rexp*) = Seqs(rs.toList) |
cab1ae6f339a
added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
63 |
|
34
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
64 |
// nullable function: tests whether the regular |
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
65 |
// expression can recognise the empty string |
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
66 |
def nullable (r: Rexp) : Boolean = r match { |
156
6a43ea9305ba
updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
34
diff
changeset
|
67 |
case ZERO => false |
6a43ea9305ba
updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
34
diff
changeset
|
68 |
case ONE => true |
34
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
69 |
case CHAR(_) => false |
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
70 |
case ALT(r1, r2) => nullable(r1) || nullable(r2) |
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
71 |
case SEQ(r1, r2) => nullable(r1) && nullable(r2) |
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
72 |
case STAR(_) => true |
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
73 |
case RECD(_, r1) => nullable(r1) |
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
74 |
} |
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
75 |
|
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
76 |
// derivative of a regular expression w.r.t. a character |
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
77 |
def der (c: Char, r: Rexp) : Rexp = r match { |
156
6a43ea9305ba
updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
34
diff
changeset
|
78 |
case ZERO => ZERO |
6a43ea9305ba
updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
34
diff
changeset
|
79 |
case ONE => ZERO |
6a43ea9305ba
updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
34
diff
changeset
|
80 |
case CHAR(d) => if (c == d) ONE else ZERO |
34
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
81 |
case ALT(r1, r2) => ALT(der(c, r1), der(c, r2)) |
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
82 |
case SEQ(r1, r2) => |
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
83 |
if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2)) |
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
84 |
else SEQ(der(c, r1), r2) |
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
85 |
case STAR(r) => SEQ(der(c, r), STAR(r)) |
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
86 |
case RECD(_, r1) => der(c, r1) |
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
87 |
} |
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
88 |
|
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
89 |
// derivative w.r.t. a string (iterates der) |
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
90 |
def ders (s: List[Char], r: Rexp) : Rexp = s match { |
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
91 |
case Nil => r |
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
92 |
case c::s => ders(s, der(c, r)) |
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
93 |
} |
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
94 |
|
359 | 95 |
|
96 |
def closed_seq_ders() |
|
97 |
||
98 |
def ders2(s: List[Char], r: Rexp) : Rexp = (s, r) match { |
|
99 |
case (Nil, r) => r |
|
100 |
case (s, ZERO) => ZERO |
|
101 |
case (s, ONE) => if (s == Nil) ONE else ZERO |
|
102 |
case (s, CHAR(c)) => if (s == List(c)) ONE else |
|
103 |
if (s == Nil) CHAR(c) else ZERO |
|
104 |
case (s, ALT(r1, r2)) => ALT(ders2(s, r1), ders2(s, r2)) |
|
105 |
case (c::s, r) => ders2(s, simp(der(c, r))) |
|
106 |
} |
|
107 |
||
108 |
// derivative w.r.t. a string (iterates der) |
|
109 |
def cders (s: List[Char], r: Rexp) : Rexp = s match { |
|
110 |
case ZERO => ZERO |
|
111 |
case ONE => ZERO |
|
112 |
case CHAR(d) => if (s == List(d)) ONE else ZERO |
|
113 |
case ALT(r1, r2) => ALT(cders(s, r1), cders(s, r2)) |
|
114 |
case SEQ(r1, r2) => closed_seq_ders(r1, r2, s) ??? |
|
115 |
case STAR(r) => SEQ(cders(s, r), STAR(r)) ??? |
|
116 |
case RECD(_, r1) => cders(s, r1) |
|
117 |
} |
|
118 |
||
119 |
||
34
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
120 |
// extracts a string from value |
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
121 |
def flatten(v: Val) : String = v match { |
156
6a43ea9305ba
updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
34
diff
changeset
|
122 |
case Empty => "" |
34
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
123 |
case Chr(c) => c.toString |
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
124 |
case Left(v) => flatten(v) |
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
125 |
case Right(v) => flatten(v) |
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
126 |
case Sequ(v1, v2) => flatten(v1) + flatten(v2) |
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
127 |
case Stars(vs) => vs.map(flatten).mkString |
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
128 |
case Rec(_, v) => flatten(v) |
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
129 |
} |
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
130 |
|
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
131 |
// extracts an environment from a value |
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
132 |
def env(v: Val) : List[(String, String)] = v match { |
156
6a43ea9305ba
updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
34
diff
changeset
|
133 |
case Empty => Nil |
34
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
134 |
case Chr(c) => Nil |
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
135 |
case Left(v) => env(v) |
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
136 |
case Right(v) => env(v) |
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
137 |
case Sequ(v1, v2) => env(v1) ::: env(v2) |
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
138 |
case Stars(vs) => vs.flatMap(env) |
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
139 |
case Rec(x, v) => (x, flatten(v))::env(v) |
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
140 |
} |
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
141 |
|
157
1fe44fb6d0a4
cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
156
diff
changeset
|
142 |
// injection part |
1fe44fb6d0a4
cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
156
diff
changeset
|
143 |
def mkeps(r: Rexp) : Val = r match { |
1fe44fb6d0a4
cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
156
diff
changeset
|
144 |
case ONE => Empty |
1fe44fb6d0a4
cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
156
diff
changeset
|
145 |
case ALT(r1, r2) => |
1fe44fb6d0a4
cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
156
diff
changeset
|
146 |
if (nullable(r1)) Left(mkeps(r1)) else Right(mkeps(r2)) |
1fe44fb6d0a4
cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
156
diff
changeset
|
147 |
case SEQ(r1, r2) => Sequ(mkeps(r1), mkeps(r2)) |
1fe44fb6d0a4
cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
156
diff
changeset
|
148 |
case STAR(r) => Stars(Nil) |
1fe44fb6d0a4
cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
156
diff
changeset
|
149 |
case RECD(x, r) => Rec(x, mkeps(r)) |
1fe44fb6d0a4
cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
156
diff
changeset
|
150 |
} |
1fe44fb6d0a4
cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
156
diff
changeset
|
151 |
|
34
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
152 |
def inj(r: Rexp, c: Char, v: Val) : Val = (r, v) match { |
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
153 |
case (STAR(r), Sequ(v1, Stars(vs))) => Stars(inj(r, c, v1)::vs) |
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
154 |
case (SEQ(r1, r2), Sequ(v1, v2)) => Sequ(inj(r1, c, v1), v2) |
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
155 |
case (SEQ(r1, r2), Left(Sequ(v1, v2))) => Sequ(inj(r1, c, v1), v2) |
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
156 |
case (SEQ(r1, r2), Right(v2)) => Sequ(mkeps(r1), inj(r2, c, v2)) |
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
157 |
case (ALT(r1, r2), Left(v1)) => Left(inj(r1, c, v1)) |
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
158 |
case (ALT(r1, r2), Right(v2)) => Right(inj(r2, c, v2)) |
156
6a43ea9305ba
updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
34
diff
changeset
|
159 |
case (CHAR(d), Empty) => Chr(c) |
34
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
160 |
case (RECD(x, r1), _) => Rec(x, inj(r1, c, v)) |
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
161 |
} |
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
162 |
|
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
163 |
|
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
164 |
// main lexing function (produces a value) |
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
165 |
def lex(r: Rexp, s: List[Char]) : Val = s match { |
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
166 |
case Nil => if (nullable(r)) mkeps(r) else throw new Exception("Not matched") |
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
167 |
case c::cs => inj(r, c, lex(der(c, r), cs)) |
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
168 |
} |
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
169 |
|
238
2dc1647eab9e
added AND-regular expression (intersection/conjunction)
Christian Urban <urbanc@in.tum.de>
parents:
178
diff
changeset
|
170 |
def lexing(r: Rexp, s: String) : Try[Val] = Try(lex(r, s.toList)) |
34
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
171 |
|
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
172 |
// Examples |
157
1fe44fb6d0a4
cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
156
diff
changeset
|
173 |
|
34
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
174 |
val K: Rexp = "a" | "b" |
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
175 |
val I: Rexp = "ab" | "ba" |
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
176 |
|
157
1fe44fb6d0a4
cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
156
diff
changeset
|
177 |
println(lexing((K | I).%, "abab")) |
34
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
178 |
|
157
1fe44fb6d0a4
cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
156
diff
changeset
|
179 |
val K2: Rexp = ("key" $ "a" | "b") |
1fe44fb6d0a4
cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
156
diff
changeset
|
180 |
val I2: Rexp = ("id" $ ("ab" | "ba")) |
34
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
181 |
|
157
1fe44fb6d0a4
cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
156
diff
changeset
|
182 |
println(lexing((K2 | I2).%, "abaa")) |
1fe44fb6d0a4
cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
156
diff
changeset
|
183 |
println(env(lexing((K2 | I2).%, "abaa"))) |
34
33065bde3bbd
added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
184 |
|
178
2835d13be702
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
166
diff
changeset
|
185 |
val r1: Rexp = "abc" |
2835d13be702
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
166
diff
changeset
|
186 |
val r2: Rexp = der('a', r1) |
2835d13be702
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
166
diff
changeset
|
187 |
val r3: Rexp = der('b', r2) |
2835d13be702
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
166
diff
changeset
|
188 |
val r4: Rexp = der('c', r3) |
2835d13be702
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
166
diff
changeset
|
189 |
println(r1) |
2835d13be702
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
166
diff
changeset
|
190 |
println(r2) |
2835d13be702
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
166
diff
changeset
|
191 |
println(r3) |
2835d13be702
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
166
diff
changeset
|
192 |
println(r4) |
2835d13be702
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
166
diff
changeset
|
193 |
|
2835d13be702
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
166
diff
changeset
|
194 |
|
166
cab1ae6f339a
added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
195 |
// time keeping |
cab1ae6f339a
added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
196 |
def time_needed[T](i: Int, code: => T) = { |
cab1ae6f339a
added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
197 |
val start = System.nanoTime() |
cab1ae6f339a
added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
198 |
for (j <- 1 to i) code |
cab1ae6f339a
added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
199 |
val end = System.nanoTime() |
cab1ae6f339a
added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
200 |
(end - start)/(i * 1.0e9) |
cab1ae6f339a
added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
201 |
} |
157
1fe44fb6d0a4
cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
156
diff
changeset
|
202 |
|
166
cab1ae6f339a
added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
203 |
|
cab1ae6f339a
added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
204 |
// first benchmark regex |
cab1ae6f339a
added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
205 |
|
cab1ae6f339a
added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
206 |
val reWord = ALTS("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v", |
cab1ae6f339a
added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
207 |
"w","x","y","z","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R", |
cab1ae6f339a
added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
208 |
"S","T","U","V","W","X","Y","Z","0","1","2","3","4","5","6","7","8","9") |
cab1ae6f339a
added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
209 |
|
cab1ae6f339a
added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
210 |
|
cab1ae6f339a
added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
211 |
val reWordStar = STAR(reWord) |
cab1ae6f339a
added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
212 |
val reWordPlus = reWord ~ reWordStar |
cab1ae6f339a
added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
213 |
val optionSet1 = "-" | "+" | "." |
cab1ae6f339a
added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
214 |
val optionSet2 = "-" | "." |
cab1ae6f339a
added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
215 |
val atTheRate = "@" |
cab1ae6f339a
added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
216 |
val period = "." |
cab1ae6f339a
added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
217 |
val optionSet3 = "," | ";" |
cab1ae6f339a
added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
218 |
val whitespace = " " |
cab1ae6f339a
added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
219 |
|
cab1ae6f339a
added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
220 |
val re01 = reWordPlus |
cab1ae6f339a
added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
221 |
val re02 = STAR(optionSet1 ~ reWordPlus) |
cab1ae6f339a
added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
222 |
val re03 = atTheRate |
cab1ae6f339a
added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
223 |
val re04 = reWordPlus |
cab1ae6f339a
added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
224 |
val re05 = STAR(optionSet2 ~ reWordPlus) |
cab1ae6f339a
added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
225 |
val re06 = period |
cab1ae6f339a
added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
226 |
val re07 = reWordPlus |
cab1ae6f339a
added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
227 |
val re08 = re05 |
cab1ae6f339a
added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
228 |
|
cab1ae6f339a
added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
229 |
val re09 = optionSet3 |
cab1ae6f339a
added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
230 |
val re10 = STAR(whitespace) |
cab1ae6f339a
added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
231 |
val re11 = reWordPlus |
cab1ae6f339a
added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
232 |
val re12 = re02 |
cab1ae6f339a
added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
233 |
val re13 = atTheRate |
cab1ae6f339a
added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
234 |
val re14 = reWordPlus |
cab1ae6f339a
added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
235 |
val re15 = re05 |
cab1ae6f339a
added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
236 |
val re16 = period |
cab1ae6f339a
added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
237 |
val re17 = reWordPlus |
cab1ae6f339a
added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
238 |
val re18 = re05 |
cab1ae6f339a
added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
239 |
|
cab1ae6f339a
added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
240 |
|
cab1ae6f339a
added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
241 |
val re01_08 = SEQS(re01, re02, re03, re04, re05, re06, re07, re08) |
cab1ae6f339a
added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
242 |
val re09_10 = re09 ~ re10 |
cab1ae6f339a
added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
243 |
val re11_18 = re01_08 |
cab1ae6f339a
added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
244 |
|
cab1ae6f339a
added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
245 |
val re = re01_08 ~ STAR(re09_10 ~ re11_18) |
cab1ae6f339a
added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
246 |
|
cab1ae6f339a
added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
247 |
|
cab1ae6f339a
added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
248 |
def process(s: String, i: Int) : Unit = { |
cab1ae6f339a
added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
249 |
println(i + " " + "%.5f".format(time_needed(1, lexing(re, s)))) |
cab1ae6f339a
added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
250 |
} |
cab1ae6f339a
added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
251 |
|
cab1ae6f339a
added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
252 |
val filename = "../tests/emails.txt" |
cab1ae6f339a
added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
253 |
val filelines = Source.fromFile(filename).getLines.take(22).zipWithIndex |
cab1ae6f339a
added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
254 |
|
cab1ae6f339a
added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
255 |
|
cab1ae6f339a
added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
256 |
filelines.foreach({ case (s: String, i: Int) => process(s, i) }) |
cab1ae6f339a
added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
257 |
|
293 | 258 |
|
259 |
||
260 |
// test: ("a" | "aa")* |
|
261 |
val EVIL3 = STAR(ALT(CHAR('a'), SEQ(CHAR('a'), CHAR('a')))) |
|
262 |
||
263 |
for (i <- 1 to 29 by 1) { |
|
264 |
println(i + " " + "%.5f".format(time_needed(2, matcher(EVIL3, "a" * i))) + |
|
265 |
" size: " + size(ders(("a" * i).toList, EVIL3))) |
|
266 |
} |
|
267 |