485
|
1 |
// epsilon NFAs...immediately translated into NFAs
|
733
|
2 |
|
|
3 |
import $file.dfa, dfa._
|
|
4 |
import $file.nfa, nfa._
|
|
5 |
|
623
|
6 |
// a fixpoint construction
|
485
|
7 |
import scala.annotation.tailrec
|
|
8 |
@tailrec
|
779
|
9 |
def fixpT[S](f: S => S, x: S): S = {
|
485
|
10 |
val fx = f(x)
|
|
11 |
if (fx == x) x else fixpT(f, fx)
|
|
12 |
}
|
|
13 |
|
|
14 |
// translates eNFAs directly into NFAs
|
623
|
15 |
def eNFA[A, C](starts: Set[A], // the starting states
|
|
16 |
delta: (A, Option[C]) :=> Set[A], // the epsilon-transitions
|
|
17 |
fins: A => Boolean) : NFA[A, C] = { // the final states
|
485
|
18 |
|
|
19 |
// epsilon transitions
|
|
20 |
def enext(q: A) : Set[A] =
|
|
21 |
applyOrElse(delta, (q, None))
|
|
22 |
|
|
23 |
def enexts(qs: Set[A]) : Set[A] =
|
488
|
24 |
qs | qs.flatMap(enext(_)) // | is the set-union in Scala
|
485
|
25 |
|
|
26 |
// epsilon closure
|
|
27 |
def ecl(qs: Set[A]) : Set[A] =
|
|
28 |
fixpT(enexts, qs)
|
|
29 |
|
|
30 |
// "normal" transitions
|
|
31 |
def next(q: A, c: C) : Set[A] =
|
|
32 |
applyOrElse(delta, (q, Some(c)))
|
|
33 |
|
|
34 |
def nexts(qs: Set[A], c: C) : Set[A] =
|
|
35 |
ecl(ecl(qs).flatMap(next(_, c)))
|
|
36 |
|
|
37 |
// result NFA
|
|
38 |
NFA(ecl(starts),
|
|
39 |
{ case (q, c) => nexts(Set(q), c) },
|
|
40 |
q => ecl(Set(q)) exists fins)
|
|
41 |
}
|
487
|
42 |
|
|
43 |
|
|
44 |
// eNFA examples
|
|
45 |
val enfa_trans1 : (State, Option[Char]) :=> Set[State] =
|
|
46 |
{ case (Q0, Some('a')) => Set(Q0)
|
|
47 |
case (Q0, None) => Set(Q1, Q2)
|
|
48 |
case (Q1, Some('a')) => Set(Q1)
|
|
49 |
case (Q2, Some('b')) => Set(Q2)
|
|
50 |
}
|
|
51 |
|
779
|
52 |
val nfa1 =
|
|
53 |
eNFA(Set[State](Q0), enfa_trans1, Set[State](Q2))
|
487
|
54 |
|
|
55 |
|
779
|
56 |
// more examples
|
487
|
57 |
case object R1 extends State
|
|
58 |
case object R2 extends State
|
|
59 |
case object R3 extends State
|
|
60 |
|
|
61 |
val enfa_trans2 : (State, Option[Char]) :=> Set[State] =
|
|
62 |
{ case (R1, Some('b')) => Set(R3)
|
|
63 |
case (R1, None) => Set(R2)
|
|
64 |
case (R2, Some('a')) => Set(R1, R3)
|
|
65 |
}
|
|
66 |
|
|
67 |
|
779
|
68 |
val nfa2 =
|
|
69 |
eNFA(Set[State](R1), enfa_trans1, Set[State](R3))
|