|
1 import scala.annotation.tailrec |
|
2 |
|
3 abstract class Rexp |
|
4 |
|
5 case object NULL extends Rexp |
|
6 case object EMPTY extends Rexp |
|
7 case object ALLCHAR extends Rexp |
|
8 case class CHAR(c: Char) extends Rexp |
|
9 case class STR(s: String) extends Rexp |
|
10 case class ALT(r1: Rexp, r2: Rexp) extends Rexp |
|
11 case class SEQ(r1: Rexp, r2: Rexp) extends Rexp |
|
12 case class STAR(r: Rexp) extends Rexp |
|
13 case class NOT(r: Rexp) extends Rexp |
|
14 case class REP(r: Rexp, n: Int) extends Rexp |
|
15 |
|
16 // some convenience for typing in regular expressions |
|
17 implicit def string2rexp(s : String) : Rexp = STR(s) |
|
18 |
|
19 implicit def RexpOps(r: Rexp) = new { |
|
20 def | (s: Rexp) = ALT(r, s) |
|
21 def % = STAR(r) |
|
22 def %(n: Int) = REP(r, n) |
|
23 def %%(n: Int) = SEQ(REP(r, n), STAR(r)) |
|
24 def ? = ALT(EMPTY, r) |
|
25 def unary_! = NOT(r) |
|
26 def ~ (s: Rexp) = SEQ(r, s) |
|
27 } |
|
28 |
|
29 implicit def stringOps(s: String) = new { |
|
30 def | (r: Rexp) = ALT(s, r) |
|
31 def | (r: String) = ALT(s, r) |
|
32 def % = STAR(s) |
|
33 def %(n: Int) = REP(s, n) |
|
34 def %%(n: Int) = SEQ(REP(s, n), STAR(s)) |
|
35 def ? = ALT(EMPTY, s) |
|
36 def unary_! = NOT(s) |
|
37 def ~ (r: Rexp) = SEQ(s, r) |
|
38 def ~ (r: String) = SEQ(s, r) |
|
39 } |
|
40 |
|
41 |
|
42 // nullable function: tests whether the regular |
|
43 // expression can recognise the empty string |
|
44 |
|
45 def nullable (r: Rexp) : Boolean = r match { |
|
46 case NULL => false |
|
47 case EMPTY => true |
|
48 case ALLCHAR => false |
|
49 case CHAR(_) => false |
|
50 case STR(s) => s.isEmpty |
|
51 case ALT(r1, r2) => nullable(r1) || nullable(r2) |
|
52 case SEQ(r1, r2) => nullable(r1) && nullable(r2) |
|
53 case STAR(_) => true |
|
54 case NOT(r) => !(nullable(r)) |
|
55 case REP(r, i) => if (i == 0) true else nullable(r) |
|
56 } |
|
57 |
|
58 // derivative of a regular expression w.r.t. a character |
|
59 def der (c: Char, r: Rexp) : Rexp = r match { |
|
60 case NULL => NULL |
|
61 case EMPTY => NULL |
|
62 case ALLCHAR => EMPTY |
|
63 case CHAR(d) => if (c == d) EMPTY else NULL |
|
64 case STR(s) => if (s.isEmpty || s.head != c) NULL else STR(s.tail) |
|
65 case ALT(r1, r2) => ALT(der(c, r1), der(c, r2)) |
|
66 case SEQ(r1, r2) => |
|
67 if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2)) |
|
68 else SEQ(der(c, r1), r2) |
|
69 case STAR(r) => SEQ(der(c, r), STAR(r)) |
|
70 case NOT(r) => NOT(der (c, r)) |
|
71 case REP(r, i) => |
|
72 if (i == 0) NULL else SEQ(der(c, r), REP(r, i - 1)) |
|
73 } |
|
74 |
|
75 // derivative w.r.t. a string (iterates der) |
|
76 @tailrec |
|
77 def ders (s: List[Char], r: Rexp) : Rexp = s match { |
|
78 case Nil => r |
|
79 case c::s => ders(s, der(c, r)) |
|
80 } |
|
81 |
|
82 // main matcher function |
|
83 def matcher(r: Rexp, s: String) : Boolean = nullable(ders(s.toList, r)) |
|
84 |
|
85 //examples |
|
86 val digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" |
|
87 val int = ("+" | "-").? ~ digit.%%(1) |
|
88 val real = ("+" | "-").? ~ digit.%%(1) ~ ("." ~ digit.%%(1)).? ~ (("e" | "E") ~ ("+" | "-").? ~ digit.%%(1)).? |
|
89 |
|
90 val ints = List("0", "-4534", "+049", "99") |
|
91 val reals = List("0.9", "-12.8", "+91.0", "9e12", "+9.21E-12", "-512E+01") |
|
92 val errs = List("", "-", "+", "+-1", "-+2", "2-") |
|
93 |
|
94 ints.map(s => matcher(int, s)) |
|
95 reals.map(s => matcher(int, s)) |
|
96 errs.map(s => matcher(int, s)) |
|
97 |
|
98 ints.map(s => matcher(real, s)) |
|
99 reals.map(s => matcher(real, s)) |
|
100 errs.map(s => matcher(real, s)) |
|
101 |
|
102 |
|
103 |
|
104 def RTEST(n: Int) = ("a".? %(n)) ~ ("a" %(n)) |
|
105 |
|
106 def time_needed[T](i: Int, code: => T) = { |
|
107 val start = System.nanoTime() |
|
108 for (j <- 1 to i) code |
|
109 val end = System.nanoTime() |
|
110 (end - start)/(i * 1.0e9) |
|
111 } |
|
112 |
|
113 for (i <- 1 to 12000 by 500) { |
|
114 println(i + ": " + "%.5f".format(time_needed(1, matcher(RTEST(i), "a" * i)))) |
|
115 } |
|
116 |
|
117 |