author | Christian Urban <christian dot urban at kcl dot ac dot uk> |
Fri, 23 Nov 2012 19:31:37 +0000 | |
changeset 74 | 8f85d1f61663 |
parent 64 | 2d625418c011 |
child 75 | 898c25a4e399 |
permissions | -rw-r--r-- |
62 | 1 |
|
2 |
// regular expressions including NOT |
|
3 |
abstract class Rexp |
|
4 |
||
5 |
case object NULL extends Rexp |
|
6 |
case object EMPTY extends Rexp |
|
7 |
case class CHAR(c: Char) extends Rexp |
|
8 |
case class ALT(r1: Rexp, r2: Rexp) extends Rexp |
|
9 |
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp |
|
10 |
case class STAR(r: Rexp) extends Rexp |
|
11 |
case class NOT(r: Rexp) extends Rexp |
|
12 |
||
13 |
||
14 |
// nullable function: tests whether the regular |
|
15 |
// expression can recognise the empty string |
|
16 |
def nullable (r: Rexp) : Boolean = r match { |
|
17 |
case NULL => false |
|
18 |
case EMPTY => true |
|
19 |
case CHAR(_) => false |
|
20 |
case ALT(r1, r2) => nullable(r1) || nullable(r2) |
|
21 |
case SEQ(r1, r2) => nullable(r1) && nullable(r2) |
|
22 |
case STAR(_) => true |
|
23 |
case NOT(r) => !(nullable(r)) |
|
24 |
} |
|
25 |
||
26 |
// tests whether a regular expression |
|
27 |
// cannot recognise more |
|
28 |
def no_more (r: Rexp) : Boolean = r match { |
|
29 |
case NULL => true |
|
30 |
case EMPTY => false |
|
31 |
case CHAR(_) => false |
|
32 |
case ALT(r1, r2) => no_more(r1) && no_more(r2) |
|
33 |
case SEQ(r1, r2) => if (nullable(r1)) (no_more(r1) && no_more(r2)) else no_more(r1) |
|
34 |
case STAR(_) => false |
|
35 |
case NOT(r) => !(no_more(r)) |
|
36 |
} |
|
37 |
||
38 |
||
39 |
// derivative of a regular expression w.r.t. a character |
|
40 |
def der (c: Char, r: Rexp) : Rexp = r match { |
|
41 |
case NULL => NULL |
|
42 |
case EMPTY => NULL case CHAR(d) => if (c == d) EMPTY else NULL |
|
43 |
case ALT(r1, r2) => ALT(der(c, r1), der(c, r2)) |
|
44 |
case SEQ(r1, r2) => |
|
45 |
if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2)) |
|
46 |
else SEQ(der(c, r1), r2) |
|
47 |
case STAR(r) => SEQ(der(c, r), STAR(r)) |
|
48 |
case NOT(r) => NOT(der (c, r)) |
|
49 |
} |
|
50 |
||
51 |
// regular expression for specifying |
|
52 |
// ranges of characters |
|
64
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
62
diff
changeset
|
53 |
def Range(s : List[Char]) : Rexp = s match { |
62 | 54 |
case Nil => NULL |
55 |
case c::Nil => CHAR(c) |
|
64
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
62
diff
changeset
|
56 |
case c::s => ALT(CHAR(c), Range(s)) |
62 | 57 |
} |
64
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
62
diff
changeset
|
58 |
def RANGE(s: String) = Range(s.toList) |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
62
diff
changeset
|
59 |
|
62 | 60 |
|
61 |
// one or more |
|
62 |
def PLUS(r: Rexp) = SEQ(r, STAR(r)) |
|
63 |
||
64
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
62
diff
changeset
|
64 |
// many alternatives |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
62
diff
changeset
|
65 |
def Alts(rs: List[Rexp]) : Rexp = rs match { |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
62
diff
changeset
|
66 |
case Nil => NULL |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
62
diff
changeset
|
67 |
case r::Nil => r |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
62
diff
changeset
|
68 |
case r::rs => ALT(r, Alts(rs)) |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
62
diff
changeset
|
69 |
} |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
62
diff
changeset
|
70 |
def ALTS(rs: Rexp*) = Alts(rs.toList) |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
62
diff
changeset
|
71 |
|
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
62
diff
changeset
|
72 |
// repetitions |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
62
diff
changeset
|
73 |
def Seqs(rs: List[Rexp]) : Rexp = rs match { |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
62
diff
changeset
|
74 |
case Nil => NULL |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
62
diff
changeset
|
75 |
case r::Nil => r |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
62
diff
changeset
|
76 |
case r::rs => SEQ(r, Seqs(rs)) |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
62
diff
changeset
|
77 |
} |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
62
diff
changeset
|
78 |
def SEQS(rs: Rexp*) = Seqs(rs.toList) |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
62
diff
changeset
|
79 |
|
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
62
diff
changeset
|
80 |
// some convenience for typing in regular expressions |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
62
diff
changeset
|
81 |
def charlist2rexp(s : List[Char]) : Rexp = s match { |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
62
diff
changeset
|
82 |
case Nil => EMPTY |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
62
diff
changeset
|
83 |
case c::Nil => CHAR(c) |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
62
diff
changeset
|
84 |
case c::s => SEQ(CHAR(c), charlist2rexp(s)) |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
62
diff
changeset
|
85 |
} |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
62
diff
changeset
|
86 |
implicit def string2rexp(s : String) : Rexp = charlist2rexp(s.toList) |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
62
diff
changeset
|
87 |
|
62 | 88 |
|
89 |
type Rule[T] = (Rexp, List[Char] => T) |
|
90 |
||
64
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
62
diff
changeset
|
91 |
case class Tokenizer[T](rules: List[Rule[T]], excl: List[T] = Nil) { |
62 | 92 |
|
64
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
62
diff
changeset
|
93 |
def munch(r: Rexp, action: List[Char] => T, s: List[Char], t: List[Char]) : Option[(List[Char], T)] = |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
62
diff
changeset
|
94 |
s match { |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
62
diff
changeset
|
95 |
case Nil if (nullable(r)) => Some(Nil, action(t)) |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
62
diff
changeset
|
96 |
case Nil => None |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
62
diff
changeset
|
97 |
case c::s if (no_more(der (c, r)) && nullable(r)) => Some(c::s, action(t)) |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
62
diff
changeset
|
98 |
case c::s if (no_more(der (c, r))) => None |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
62
diff
changeset
|
99 |
case c::s => munch(der (c, r), action, s, t ::: List(c)) |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
62
diff
changeset
|
100 |
} |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
62
diff
changeset
|
101 |
|
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
62
diff
changeset
|
102 |
def one_token(s: List[Char]) : Either[(List[Char], T), String] = { |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
62
diff
changeset
|
103 |
val somes = rules.map { (r) => munch(r._1, r._2, s, Nil) }.flatten |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
62
diff
changeset
|
104 |
if (somes == Nil) Right(s.mkString) |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
62
diff
changeset
|
105 |
else Left(somes sortBy (_._1.length) head) |
62 | 106 |
} |
107 |
||
64
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
62
diff
changeset
|
108 |
def tokenize(cs: List[Char]) : List[T] = cs match { |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
62
diff
changeset
|
109 |
case Nil => Nil |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
62
diff
changeset
|
110 |
case _ => one_token(cs) match { |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
62
diff
changeset
|
111 |
case Left((rest, token)) => token :: tokenize(rest) |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
62
diff
changeset
|
112 |
case Right(s) => { println("Cannot tokenize: \"" + s + "\""); Nil } |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
62
diff
changeset
|
113 |
} |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
62
diff
changeset
|
114 |
} |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
62
diff
changeset
|
115 |
|
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
62
diff
changeset
|
116 |
def fromString(s: String) : List[T] = |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
62
diff
changeset
|
117 |
tokenize(s.toList).filterNot(excl.contains(_)) |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
62
diff
changeset
|
118 |
|
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
62
diff
changeset
|
119 |
def fromFile(name: String) : List[T] = |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
62
diff
changeset
|
120 |
fromString(io.Source.fromFile(name).mkString) |
2d625418c011
added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
62
diff
changeset
|
121 |
|
62 | 122 |
} |
123 |