|
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 // some convenience for typing in regular expressions |
|
15 def charlist2rexp(s : List[Char]) : Rexp = s match { |
|
16 case Nil => EMPTY |
|
17 case c::Nil => CHAR(c) |
|
18 case c::s => SEQ(CHAR(c), charlist2rexp(s)) |
|
19 } |
|
20 implicit def string2rexp(s : String) : Rexp = charlist2rexp(s.toList) |
|
21 |
|
22 |
|
23 // nullable function: tests whether the regular |
|
24 // expression can recognise the empty string |
|
25 def nullable (r: Rexp) : Boolean = r match { |
|
26 case NULL => false |
|
27 case EMPTY => true |
|
28 case CHAR(_) => false |
|
29 case ALT(r1, r2) => nullable(r1) || nullable(r2) |
|
30 case SEQ(r1, r2) => nullable(r1) && nullable(r2) |
|
31 case STAR(_) => true |
|
32 case NOT(r) => !(nullable(r)) |
|
33 } |
|
34 |
|
35 // tests whether a regular expression |
|
36 // cannot recognise more |
|
37 def no_more (r: Rexp) : Boolean = r match { |
|
38 case NULL => true |
|
39 case EMPTY => false |
|
40 case CHAR(_) => false |
|
41 case ALT(r1, r2) => no_more(r1) && no_more(r2) |
|
42 case SEQ(r1, r2) => if (nullable(r1)) (no_more(r1) && no_more(r2)) else no_more(r1) |
|
43 case STAR(_) => false |
|
44 case NOT(r) => !(no_more(r)) |
|
45 } |
|
46 |
|
47 |
|
48 // derivative of a regular expression w.r.t. a character |
|
49 def der (c: Char, r: Rexp) : Rexp = r match { |
|
50 case NULL => NULL |
|
51 case EMPTY => NULL case CHAR(d) => if (c == d) EMPTY else NULL |
|
52 case ALT(r1, r2) => ALT(der(c, r1), der(c, r2)) |
|
53 case SEQ(r1, r2) => |
|
54 if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2)) |
|
55 else SEQ(der(c, r1), r2) |
|
56 case STAR(r) => SEQ(der(c, r), STAR(r)) |
|
57 case NOT(r) => NOT(der (c, r)) |
|
58 } |
|
59 |
|
60 // regular expression for specifying |
|
61 // ranges of characters |
|
62 def RANGE(s : List[Char]) : Rexp = s match { |
|
63 case Nil => NULL |
|
64 case c::Nil => CHAR(c) |
|
65 case c::s => ALT(CHAR(c), RANGE(s)) |
|
66 } |
|
67 |
|
68 // one or more |
|
69 def PLUS(r: Rexp) = SEQ(r, STAR(r)) |
|
70 |
|
71 |
|
72 type Rule[T] = (Rexp, List[Char] => T) |
|
73 |
|
74 def error (s: String) = throw new IllegalArgumentException ("Cannot tokenize: " + s) |
|
75 |
|
76 def munch[T](r: Rexp, action: List[Char] => T, s: List[Char], t: List[Char]) : Option[(List[Char], T)] = |
|
77 s match { |
|
78 case Nil if (nullable(r)) => Some(Nil, action(t)) |
|
79 case Nil => None |
|
80 case c::s if (no_more(der (c, r)) && nullable(r)) => Some(c::s, action(t)) |
|
81 case c::s if (no_more(der (c, r))) => None |
|
82 case c::s => munch(der (c, r), action, s, t ::: List(c)) |
|
83 } |
|
84 |
|
85 def one_token[T](rs: List[Rule[T]], s: List[Char]) : (List[Char], T) = { |
|
86 val somes = rs.map { (r) => munch(r._1, r._2, s, Nil) } .flatten |
|
87 if (somes == Nil) error(s.mkString) else (somes sortBy (_._1.length) head) |
|
88 } |
|
89 |
|
90 def tokenize[T](rs: List[Rule[T]], s: List[Char]) : List[T] = s match { |
|
91 case Nil => Nil |
|
92 case _ => one_token(rs, s) match { |
|
93 case (rest, token) => token :: tokenize(rs, rest) |
|
94 } |
|
95 } |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |