author | Christian Urban <christian.urban@kcl.ac.uk> |
Fri, 18 Sep 2020 00:33:03 +0100 | |
changeset 757 | ea0be0662be0 |
parent 412 | 1cef3924f7a2 |
child 764 | 6718ef6143b8 |
permissions | -rw-r--r-- |
412 | 1 |
def nullable(r: Rexp) : Boolean = r match { |
399
5c1fbb39c93e
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
261
diff
changeset
|
2 |
case ZERO => false |
5c1fbb39c93e
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
261
diff
changeset
|
3 |
case ONE => true |
7 | 4 |
case CHAR(_) => false |
5 |
case ALT(r1, r2) => nullable(r1) || nullable(r2) |
|
6 |
case SEQ(r1, r2) => nullable(r1) && nullable(r2) |
|
7 |
case STAR(_) => true |
|
8 |
} |
|
261
24531cfaa36a
updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
9 |
|
412 | 10 |
def der(c: Char, r: Rexp) : Rexp = r match { |
399
5c1fbb39c93e
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
261
diff
changeset
|
11 |
case ZERO => ZERO |
5c1fbb39c93e
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
261
diff
changeset
|
12 |
case ONE => ZERO |
5c1fbb39c93e
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
261
diff
changeset
|
13 |
case CHAR(d) => if (c == d) ONE else ZERO |
261
24531cfaa36a
updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
14 |
case ALT(r1, r2) => ALT(der(c, r1), der(c, r2)) |
24531cfaa36a
updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
15 |
case SEQ(r1, r2) => |
24531cfaa36a
updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
16 |
if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2)) |
24531cfaa36a
updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
17 |
else SEQ(der(c, r1), r2) |
24531cfaa36a
updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
18 |
case STAR(r) => SEQ(der(c, r), STAR(r)) |
24531cfaa36a
updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
19 |
} |
24531cfaa36a
updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
20 |
|
412 | 21 |
def ders(s: List[Char], r: Rexp) : Rexp = s match { |
261
24531cfaa36a
updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
22 |
case Nil => r |
24531cfaa36a
updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
23 |
case c::s => ders(s, der(c, r)) |
24531cfaa36a
updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
24 |
} |
24531cfaa36a
updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
25 |
|
24531cfaa36a
updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
26 |
def matches(r: Rexp, s: String) : Boolean = |
24531cfaa36a
updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
27 |
nullable(ders(s.toList, r)) |