1 // Simple matcher for basic regular expressions |
1 // A simple matcher for basic regular expressions |
2 |
2 |
3 abstract class Rexp |
3 abstract class Rexp |
4 case object ZERO extends Rexp // matches nothing |
4 case object ZERO extends Rexp // matches nothing |
5 case object ONE extends Rexp // matches the empty string |
5 case object ONE extends Rexp // matches an empty string |
6 case class CHAR(c: Char) extends Rexp // matches a character c |
6 case class CHAR(c: Char) extends Rexp // matches a character c |
7 case class ALT(r1: Rexp, r2: Rexp) extends Rexp // alternative |
7 case class ALT(r1: Rexp, r2: Rexp) extends Rexp // alternative |
8 case class SEQ(r1: Rexp, r2: Rexp) extends Rexp // sequence |
8 case class SEQ(r1: Rexp, r2: Rexp) extends Rexp // sequence |
9 case class STAR(r: Rexp) extends Rexp // star |
9 case class STAR(r: Rexp) extends Rexp // star |
10 |
10 |
17 case ALT(r1, r2) => nullable(r1) || nullable(r2) |
17 case ALT(r1, r2) => nullable(r1) || nullable(r2) |
18 case SEQ(r1, r2) => nullable(r1) && nullable(r2) |
18 case SEQ(r1, r2) => nullable(r1) && nullable(r2) |
19 case STAR(_) => true |
19 case STAR(_) => true |
20 } |
20 } |
21 |
21 |
22 |
22 // the derivative of a regular expression w.r.t. a character |
23 |
|
24 // derivative of a regular expression w.r.t. a character |
|
25 def der (c: Char, r: Rexp) : Rexp = r match { |
23 def der (c: Char, r: Rexp) : Rexp = r match { |
26 case ZERO => ZERO |
24 case ZERO => ZERO |
27 case ONE => ZERO |
25 case ONE => ZERO |
28 case CHAR(d) => if (c == d) ONE else ZERO |
26 case CHAR(d) => if (c == d) ONE else ZERO |
29 case ALT(r1, r2) => ALT(der(c, r1), der(c, r2)) |
27 case ALT(r1, r2) => ALT(der(c, r1), der(c, r2)) |
31 if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2)) |
29 if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2)) |
32 else SEQ(der(c, r1), r2) |
30 else SEQ(der(c, r1), r2) |
33 case STAR(r1) => SEQ(der(c, r1), STAR(r1)) |
31 case STAR(r1) => SEQ(der(c, r1), STAR(r1)) |
34 } |
32 } |
35 |
33 |
36 // derivative w.r.t. a string (iterates der) |
34 // the derivative w.r.t. a string (iterates der) |
37 def ders (s: List[Char], r: Rexp) : Rexp = s match { |
35 def ders (s: List[Char], r: Rexp) : Rexp = s match { |
38 case Nil => r |
36 case Nil => r |
39 case c::s => ders(s, der(c, r)) |
37 case c::s => ders(s, der(c, r)) |
40 } |
38 } |
41 |
39 |
42 // main matcher function |
40 // the main matcher function |
43 def matches(r: Rexp, s: String) : Boolean = |
41 def matches(r: Rexp, s: String) : Boolean = |
44 nullable(ders(s.toList, r)) |
42 nullable(ders(s.toList, r)) |
45 |
43 |
46 |
44 |
47 //examples from the homework |
45 // examples from the homework |
48 |
46 |
49 val r = STAR(ALT(SEQ(CHAR('a'), CHAR('b')), CHAR('b'))) |
47 val r = STAR(ALT(SEQ(CHAR('a'), CHAR('b')), CHAR('b'))) |
50 der('a', r) |
48 der('a', r) |
51 der('b', r) |
49 der('b', r) |
52 der('c', r) |
50 der('c', r) |
55 der('x', r2) |
53 der('x', r2) |
56 der('y', der('x', r2)) |
54 der('y', der('x', r2)) |
57 der('z', der('y', der('x', r2))) |
55 der('z', der('y', der('x', r2))) |
58 |
56 |
59 |
57 |
60 //optional regular expression (one or zero times) |
58 // the optional regular expression (one or zero times) |
61 def OPT(r: Rexp) = ALT(r, ONE) |
59 def OPT(r: Rexp) = ALT(r, ONE) |
62 |
60 |
63 //n-times regular expression (explicitly expanded) |
61 // the n-times regular expression (explicitly expanded) |
64 def NTIMES(r: Rexp, n: Int) : Rexp = n match { |
62 def NTIMES(r: Rexp, n: Int) : Rexp = n match { |
65 case 0 => ONE |
63 case 0 => ONE |
66 case 1 => r |
64 case 1 => r |
67 case n => SEQ(r, NTIMES(r, n - 1)) |
65 case n => SEQ(r, NTIMES(r, n - 1)) |
68 } |
66 } |
74 def EVIL1(n: Int) = SEQ(NTIMES(OPT(CHAR('a')), n), NTIMES(CHAR('a'), n)) |
72 def EVIL1(n: Int) = SEQ(NTIMES(OPT(CHAR('a')), n), NTIMES(CHAR('a'), n)) |
75 |
73 |
76 // the evil regular expression (a*)*b |
74 // the evil regular expression (a*)*b |
77 val EVIL2 = SEQ(STAR(STAR(CHAR('a'))), CHAR('b')) |
75 val EVIL2 = SEQ(STAR(STAR(CHAR('a'))), CHAR('b')) |
78 |
76 |
79 //for measuring time |
77 // for measuring time |
80 def time_needed[T](i: Int, code: => T) = { |
78 def time_needed[T](i: Int, code: => T) = { |
81 val start = System.nanoTime() |
79 val start = System.nanoTime() |
82 for (j <- 1 to i) code |
80 for (j <- 1 to i) code |
83 val end = System.nanoTime() |
81 val end = System.nanoTime() |
84 (end - start)/(i * 1.0e9) |
82 "%.5f".format((end - start) / (i * 1.0e9)) |
85 } |
83 } |
86 |
84 |
87 |
85 |
88 //test: (a?{n}) (a{n}) |
86 // test: (a?{n}) (a{n}) |
89 println("Test (a?{n}) (a{n})") |
87 println("Test (a?{n}) (a{n})") |
90 for (i <- 1 to 20) { |
88 |
91 println(i + ": " + "%.5f".format(time_needed(2, matches(EVIL1(i), "a" * i)))) |
89 for (i <- 0 to 20 by 2) { |
|
90 println(s"$i: ${time_needed(2, matches(EVIL1(i), "a" * i))}") |
92 } |
91 } |
93 |
92 |
94 for (i <- 1 to 20) { |
93 // test: (a*)* b |
95 println(i + ": " + "%.5f".format(time_needed(2, matches(EVIL1(i), "a" * i)))) |
|
96 } |
|
97 |
|
98 //test: (a*)* b |
|
99 println("Test (a*)* b") |
94 println("Test (a*)* b") |
100 |
95 |
101 for (i <- 1 to 20) { |
96 for (i <- 0 to 20 by 2) { |
102 println(i + " " + "%.5f".format(time_needed(2, matches(EVIL2, "a" * i)))) |
97 println(s"$i: ${time_needed(2, matches(EVIL2, "a" * i))}") |
103 } |
|
104 |
|
105 for (i <- 1 to 20) { |
|
106 println(i + " " + "%.5f".format(time_needed(2, matches(EVIL2, "a" * i)))) |
|
107 } |
98 } |
108 |
99 |
109 |
100 |
110 |
101 // the size of a regular expressions - for testing purposes |
111 |
|
112 // size of a regular expressions - for testing purposes |
|
113 def size(r: Rexp) : Int = r match { |
102 def size(r: Rexp) : Int = r match { |
114 case ZERO => 1 |
103 case ZERO => 1 |
115 case ONE => 1 |
104 case ONE => 1 |
116 case CHAR(_) => 1 |
105 case CHAR(_) => 1 |
117 case ALT(r1, r2) => 1 + size(r1) + size(r2) |
106 case ALT(r1, r2) => 1 + size(r1) + size(r2) |
145 size(ders("abababababab".toList, BIG)) // 536 |
134 size(ders("abababababab".toList, BIG)) // 536 |
146 |
135 |
147 |
136 |
148 size(ders(("ab" * 200).toList, BIG)) // 366808 |
137 size(ders(("ab" * 200).toList, BIG)) // 366808 |
149 |
138 |
150 for (i <- 1 to 21) { |
139 for (i <- 0 to 200 by 10) { |
151 println(i + " " + "%.5f".format(time_needed(2, matches(BIG, "ab" * i)))) |
140 println(s"$i: ${time_needed(2, matches(BIG, "ab" * i))}") |
152 } |
141 } |