progs/scala/nfas.scala
author Chengsong
Mon, 09 May 2022 17:17:52 +0100
changeset 511 47618d607bbf
parent 243 09ab631ce7fa
permissions -rw-r--r--
isarfied
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
243
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
     1
// NFAs based on Scala's partial functions (returning
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
     2
// sets of states)
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
     3
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
     4
239
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
import scala.util.Try
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
// type abbreviation for partial functions
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
type :=>[A, B] = PartialFunction[A, B]
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
// some states for test cases 
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
abstract class State
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
case object Q0 extends State
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
case object Q1 extends State
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
case object Q2 extends State
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
case object Q3 extends State
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
case object Q4 extends State
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
case object Q5 extends State
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
case object Q6 extends State
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
243
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
    23
// return empty set when not defined
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
    24
def applyOrElse[A, B](f: A :=> Set[B], x: A) : Set[B] =
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
    25
  Try(f(x)) getOrElse Set[B]()
239
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
// class for NFAs
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
case class NFA[A, C](starts: Set[A],            // starting states
243
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
    31
                     delta: (A, C) :=> Set[A],  // transitions
239
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
                     fins:  A => Boolean) {     // final states 
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
  // given a state and a character, what is the set of next states?
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
  // if there is none => empty set
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
  def next(q: A, c: C) : Set[A] = 
243
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
    37
    applyOrElse(delta, (q, c))
239
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
  def nexts(qs: Set[A], c: C) : Set[A] =
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
    qs.flatMap(next(_, c))
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
  // given some states and a string, what is the set of next states?
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
  def deltas(qs: Set[A], s: List[C]) : Set[A] = s match {
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
    case Nil => qs
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
    case c::cs => deltas(nexts(qs, c), cs)
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
  }
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
  // is a string accepted by an NFA?
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
  def accepts(s: List[C]) : Boolean = 
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
    deltas(starts, s).exists(fins)
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
243
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
    52
  // depth-first search version of accept
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
    53
  def search(q: A, s: List[C]) : Boolean = s match {
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
    54
    case Nil => fins(q)
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
    55
    case c::cs => next(q, c).exists(search(_, cs)) 
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
    56
  }
239
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    57
243
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
    58
  def accepts2(s: List[C]) : Boolean = 
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
    59
    starts.exists(search(_, s))
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
    60
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
    61
}
239
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    62
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    63
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    64
// NFA test cases
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
243
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
    66
val trans2 : (State, Char) :=> Set[State] = 
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
    67
 { case (Q0, 'a') => Set(Q0, Q1)
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
    68
   case (Q0, 'b') => Set(Q2)
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
    69
   case (Q1, 'a') => Set(Q1)
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
    70
   case (Q2, 'b') => Set(Q2)
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
    71
 }
239
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    72
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    73
val nfa2 = NFA(Set[State](Q0), trans2, Set[State](Q2))
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    74
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    75
nfa2.accepts("aa".toList)             // false
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    76
nfa2.accepts("aaaaa".toList)          // false
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    77
nfa2.accepts("aaaaab".toList)         // true
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    78
nfa2.accepts("aaaaabbb".toList)       // true
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    79
nfa2.accepts("aaaaabbbaaa".toList)    // false
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    80
nfa2.accepts("ac".toList)             // false
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    81
243
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
    82
nfa2.accepts2("aa".toList)             // false
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
    83
nfa2.accepts2("aaaaa".toList)          // false
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
    84
nfa2.accepts2("aaaaab".toList)         // true
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
    85
nfa2.accepts2("aaaaabbb".toList)       // true
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
    86
nfa2.accepts2("aaaaabbbaaa".toList)    // false
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
    87
nfa2.accepts2("ac".toList)             // false
239
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    88
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    89
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    90
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    91
243
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
    92
// epsilon NFAs
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
    93
// (not explicitly defined, but immediately translated into NFAs)
239
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    94
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    95
// fixpoint construction
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    96
import scala.annotation.tailrec
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    97
@tailrec
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    98
def fixpT[A](f: A => A, x: A): A = {
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    99
  val fx = f(x)
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   100
  if (fx == x) x else fixpT(f, fx) 
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   101
}
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   102
243
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   103
// translates eNFAs directly into NFAs 
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   104
def eNFA[A, C](starts: Set[A], 
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   105
	       delta: (A, Option[C]) :=> Set[A], 
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   106
	       fins: A => Boolean) : NFA[A, C] = { 
239
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   107
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   108
  // epsilon transitions
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   109
  def enext(q: A) : Set[A] = 
243
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   110
    applyOrElse(delta, (q, None))
239
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   111
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   112
  def enexts(qs: Set[A]) : Set[A] = 
243
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   113
    qs | qs.flatMap(enext(_))
239
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   114
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   115
  // epsilon closure
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   116
  def ecl(qs: Set[A]) : Set[A] = 
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   117
    fixpT(enexts, qs)
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   118
243
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   119
  // "normal" transitions
239
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   120
  def next(q: A, c: C) : Set[A] = 
243
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   121
    applyOrElse(delta, (q, Some(c)))
239
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   122
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   123
  def nexts(qs: Set[A], c: C) : Set[A] = 
243
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   124
    ecl(ecl(qs).flatMap(next(_, c)))
239
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   125
243
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   126
  NFA(ecl(starts), 
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   127
      { case (q, c) => nexts(Set(q), c) }, 
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   128
      q => ecl(Set(q)) exists fins)
239
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   129
}
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   130
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   131
243
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   132
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   133
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   134
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   135
// test cases for eNFAs
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   136
val etrans1 : (State, Option[Char]) :=> Set[State] =
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   137
  { case (Q0, Some('a')) => Set(Q1)
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   138
    case (Q1, None) => Set(Q0)
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   139
  }
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   140
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   141
val enfa1 = eNFA(Set[State](Q0), etrans1, Set[State](Q1))
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   142
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   143
enfa1.accepts("a".toList)              // true
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   144
enfa1.accepts("".toList)               // false
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   145
enfa1.accepts("aaaaa".toList)          // true
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   146
enfa1.accepts("aaaaab".toList)         // false
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   147
enfa1.accepts("aaaaabbb".toList)       // false
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   148
enfa1.accepts("aaaaabbbaaa".toList)    // false
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   149
enfa1.accepts("ac".toList)             // false
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   150
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   151
// example from handouts 
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   152
val etrans2 : (State, Option[Char]) :=> Set[State] = 
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   153
  { case (Q0, Some('a')) => Set(Q0)
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   154
    case (Q0, None) => Set(Q1, Q2)
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   155
    case (Q1, Some('a')) => Set(Q1)
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   156
    case (Q2, Some('b')) => Set(Q2)
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   157
    case (Q1, None) => Set(Q0)
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   158
  }
239
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   159
243
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   160
val enfa2 = eNFA(Set[State](Q0), etrans2, Set[State](Q2))
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   161
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   162
enfa2.accepts("a".toList)              // true
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   163
enfa2.accepts("".toList)               // true
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   164
enfa2.accepts("aaaaa".toList)          // true
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   165
enfa2.accepts("aaaaab".toList)         // true
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   166
enfa2.accepts("aaaaabbb".toList)       // true
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   167
enfa2.accepts("aaaaabbbaaa".toList)    // false
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   168
enfa2.accepts("ac".toList)             // false
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   169
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   170
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   171
// states for Thompson construction
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   172
case class TState(i: Int) extends State
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   173
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   174
object TState {
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   175
  var counter = 0
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   176
  
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   177
  def apply() : TState = {
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   178
    counter += 1;
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   179
    new TState(counter - 1)
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   180
  }
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   181
}
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   182
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   183
// some types abbreviations
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   184
type NFAt = NFA[TState, Char]
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   185
type NFAtrans = (TState, Char) :=> Set[TState]
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   186
type eNFAtrans = (TState, Option[Char]) :=> Set[TState]
239
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   187
243
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   188
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   189
// for composing an eNFA transition with a NFA transition
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   190
implicit class RichPF(val f: eNFAtrans) extends AnyVal {
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   191
  def +++(g: NFAtrans) : eNFAtrans = 
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   192
  { case (q, None) =>  applyOrElse(f, (q, None)) 
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   193
    case (q, Some(c)) => applyOrElse(f, (q, Some(c))) | applyOrElse(g, (q, c))  }
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   194
}
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   195
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   196
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   197
// NFA that does not accept any string
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   198
def NFA_ZERO(): NFAt = {
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   199
  val Q = TState()
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   200
  NFA(Set(Q), { case _ => Set() }, Set())
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   201
}
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   202
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   203
// NFA that accepts the empty string
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   204
def NFA_ONE() : NFAt = {
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   205
  val Q = TState()
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   206
  NFA(Set(Q), { case _ => Set() }, Set(Q))
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   207
}
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   208
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   209
// NFA that accepts the string "c"
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   210
def NFA_CHAR(c: Char) : NFAt = {
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   211
  val Q1 = TState()
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   212
  val Q2 = TState()
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   213
  NFA(Set(Q1), { case (Q1, d) if (c == d) => Set(Q2) }, Set(Q2))
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   214
}
239
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   215
243
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   216
// sequence of two NFAs
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   217
def NFA_SEQ(enfa1: NFAt, enfa2: NFAt) : NFAt = {
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   218
  val new_delta : eNFAtrans = 
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   219
    { case (q, None) if enfa1.fins(q) => enfa2.starts }
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   220
  
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   221
  eNFA(enfa1.starts, new_delta +++ enfa1.delta +++ enfa2.delta, 
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   222
       enfa2.fins)
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   223
}
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   224
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   225
// alternative of two NFAs
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   226
def NFA_ALT(enfa1: NFAt, enfa2: NFAt) : NFAt = {
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   227
  val new_delta : NFAtrans = { 
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   228
    case (q, c) =>  applyOrElse(enfa1.delta, (q, c)) | 
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   229
                    applyOrElse(enfa2.delta, (q, c)) }
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   230
  val new_fins = (q: TState) => enfa1.fins(q) || enfa2.fins(q)
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   231
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   232
  NFA(enfa1.starts | enfa2.starts, new_delta, new_fins)
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   233
}
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   234
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   235
// star of a NFA
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   236
def NFA_STAR(enfa: NFAt) : NFAt = {
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   237
  val Q = TState()
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   238
  val new_delta : eNFAtrans = 
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   239
    { case (Q, None) => enfa.starts
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   240
      case (q, None) if enfa.fins(q) => Set(Q) }
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   241
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   242
  eNFA(Set(Q), new_delta +++ enfa.delta, Set(Q))
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   243
}
239
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   244
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   245
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   246
// Regular expressions fro derivative automata
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   247
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   248
abstract class Rexp
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   249
case object ZERO extends Rexp
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   250
case object ONE extends Rexp
240
820228ac1d0f updated for extended partial derivatives
Christian Urban <urbanc@in.tum.de>
parents: 239
diff changeset
   251
case class CHAR(c: Char) extends Rexp 
239
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   252
case class ALT(r1: Rexp, r2: Rexp) extends Rexp 
240
820228ac1d0f updated for extended partial derivatives
Christian Urban <urbanc@in.tum.de>
parents: 239
diff changeset
   253
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp 
820228ac1d0f updated for extended partial derivatives
Christian Urban <urbanc@in.tum.de>
parents: 239
diff changeset
   254
case class STAR(r: Rexp) extends Rexp 
239
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   255
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   256
import scala.language.implicitConversions    
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   257
import scala.language.reflectiveCalls 
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   258
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   259
def charlist2rexp(s: List[Char]): Rexp = s match {
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   260
  case Nil => ONE
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   261
  case c::Nil => CHAR(c)
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   262
  case c::s => SEQ(CHAR(c), charlist2rexp(s))
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   263
}
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   264
implicit def string2rexp(s: String): Rexp = charlist2rexp(s.toList)
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   265
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   266
implicit def RexpOps (r: Rexp) = new {
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   267
  def | (s: Rexp) = ALT(r, s)
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   268
  def % = STAR(r)
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   269
  def ~ (s: Rexp) = SEQ(r, s)
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   270
}
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   271
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   272
implicit def stringOps (s: String) = new {
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   273
  def | (r: Rexp) = ALT(s, r)
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   274
  def | (r: String) = ALT(s, r)
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   275
  def % = STAR(s)
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   276
  def ~ (r: Rexp) = SEQ(s, r)
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   277
  def ~ (r: String) = SEQ(s, r)
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   278
}
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   279
243
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   280
//optional
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   281
def OPT(r: Rexp) = ALT(r, ONE)
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   282
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   283
//n-times
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   284
def NTIMES(r: Rexp, n: Int) : Rexp = n match {
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   285
  case 0 => ONE
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   286
  case 1 => r
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   287
  case n => SEQ(r, NTIMES(r, n - 1))
239
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   288
}
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   289
243
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   290
// evil regular exproession
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   291
def EVIL(n: Int) = SEQ(NTIMES(OPT("a"), n), NTIMES("a", n))
239
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   292
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   293
243
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   294
val EVIL2 = STAR(STAR("a")) ~ "b"
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   295
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   296
// thompson construction 
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   297
def thompson (r: Rexp) : NFAt = r match {
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   298
  case ZERO => NFA_ZERO()
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   299
  case ONE => NFA_ONE()
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   300
  case CHAR(c) => NFA_CHAR(c)  
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   301
  case ALT(r1, r2) => NFA_ALT(thompson(r1), thompson(r2))
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   302
  case SEQ(r1, r2) => NFA_SEQ(thompson(r1), thompson(r2))
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   303
  case STAR(r1) => NFA_STAR(thompson(r1))
239
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   304
}
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   305
243
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   306
// regular expression matcher using Thompson's
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   307
def tmatcher(r: Rexp, s: String) : Boolean = 
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   308
  thompson(r).accepts(s.toList)
239
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   309
243
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   310
def tmatcher2(r: Rexp, s: String) : Boolean = 
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   311
  thompson(r).accepts2(s.toList)
240
820228ac1d0f updated for extended partial derivatives
Christian Urban <urbanc@in.tum.de>
parents: 239
diff changeset
   312
243
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   313
// test cases for thompson construction
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   314
tmatcher(ZERO, "")   // false
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   315
tmatcher(ZERO, "a")  // false
240
820228ac1d0f updated for extended partial derivatives
Christian Urban <urbanc@in.tum.de>
parents: 239
diff changeset
   316
243
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   317
tmatcher(ONE, "")    // true
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   318
tmatcher(ONE, "a")   // false
241
1075bba7b8b1 updated
Christian Urban <urbanc@in.tum.de>
parents: 240
diff changeset
   319
243
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   320
tmatcher(CHAR('a'), "")    // false
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   321
tmatcher(CHAR('a'), "a")   // true
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   322
tmatcher(CHAR('a'), "b")   // false
241
1075bba7b8b1 updated
Christian Urban <urbanc@in.tum.de>
parents: 240
diff changeset
   323
243
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   324
tmatcher("a" | "b", "")    // false
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   325
tmatcher("a" | "b", "a")   // true
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   326
tmatcher("a" | "b", "b")   // true
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   327
tmatcher("a" | "b", "c")   // false
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   328
tmatcher("a" | "b", "ab")  // false
241
1075bba7b8b1 updated
Christian Urban <urbanc@in.tum.de>
parents: 240
diff changeset
   329
243
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   330
tmatcher("a" ~ "b", "")    // false
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   331
tmatcher("a" ~ "b", "a")   // false
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   332
tmatcher("a" ~ "b", "b")   // false
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   333
tmatcher("a" ~ "b", "c")   // false
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   334
tmatcher("a" ~ "b", "ab")  // true
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   335
tmatcher("a" ~ "b", "aba") // false
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   336
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   337
tmatcher(STAR("a"), "")      // true
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   338
tmatcher(STAR("a"), "a")     // true
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   339
tmatcher(STAR("a"), "aaaaa") // true
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   340
tmatcher(STAR("a"), "b")     // false
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   341
tmatcher(STAR("a"), "aaab")  // false
241
1075bba7b8b1 updated
Christian Urban <urbanc@in.tum.de>
parents: 240
diff changeset
   342
243
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   343
tmatcher(STAR(STAR("a")), "")      // true
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   344
tmatcher(STAR(STAR("a")), "a")     // true
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   345
tmatcher(STAR(STAR("a")), "aaaaa") // true
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   346
tmatcher(STAR(STAR("a")), "b")     // false
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   347
tmatcher(STAR(STAR("a")), "aaab")  // false
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   348
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   349
tmatcher(EVIL2, "aaaaaab")   // true
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   350
tmatcher(EVIL2, "aaaaaa")    // false
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   351
tmatcher(EVIL2, "a" * 100)   // false
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   352
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   353
// helper function for recording time
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   354
def time_needed[T](i: Int, code: => T) = {
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   355
  val start = System.nanoTime()
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   356
  for (j <- 1 to i) code
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   357
  val end = System.nanoTime()
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   358
  (end - start)/(i * 1.0e9)
242
dcfc9b23b263 added more literature about extended partial derivative automata
Christian Urban <urbanc@in.tum.de>
parents: 241
diff changeset
   359
}
dcfc9b23b263 added more literature about extended partial derivative automata
Christian Urban <urbanc@in.tum.de>
parents: 241
diff changeset
   360
dcfc9b23b263 added more literature about extended partial derivative automata
Christian Urban <urbanc@in.tum.de>
parents: 241
diff changeset
   361
dcfc9b23b263 added more literature about extended partial derivative automata
Christian Urban <urbanc@in.tum.de>
parents: 241
diff changeset
   362
243
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   363
// test harness for the matcher
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   364
for (i <- 0 to 9) {
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   365
  println(i + ": " + "%.5f".format(time_needed(1, tmatcher(EVIL(i), "a" * i))))
239
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   366
}
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   367
243
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   368
for (i <- 0 to 7) {
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   369
  println(i + ": " + "%.5f".format(time_needed(1, tmatcher2(EVIL(i), "a" * i))))
239
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   370
}
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   371
243
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   372
for (i <- 0 to 100 by 5) {
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   373
  println(i + ": " + "%.5f".format(time_needed(1, tmatcher(EVIL2, "a" * i))))
239
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   374
}
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   375
e59cec211f4f added automata implementation
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   376
243
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   377
for (i <- 0 to 8) {
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   378
  println(i + ": " + "%.5f".format(time_needed(1, tmatcher2(EVIL2, "a" * i))))
09ab631ce7fa updated literature
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   379
}