progs/automata/thompson.sc
author Christian Urban <christian.urban@kcl.ac.uk>
Wed, 21 Oct 2020 09:24:32 +0100
changeset 785 faa4489267d5
parent 784 7dac4492b0e6
child 932 5678414a3898
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
487
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
     1
// Thompson Construction
733
022e2cb1668d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 586
diff changeset
     2
//=======================
022e2cb1668d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 586
diff changeset
     3
022e2cb1668d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 586
diff changeset
     4
import $file.dfa, dfa._ 
022e2cb1668d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 586
diff changeset
     5
import $file.nfa, nfa._
022e2cb1668d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 586
diff changeset
     6
import $file.enfa, enfa._
487
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
     7
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
     8
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
     9
// states for Thompson construction
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
    10
case class TState(i: Int) extends State
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
    11
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
    12
object TState {
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
    13
  var counter = 0
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
    14
  
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
    15
  def apply() : TState = {
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
    16
    counter += 1;
733
022e2cb1668d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 586
diff changeset
    17
    new TState(counter)
487
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
    18
  }
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
    19
}
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
    20
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
    21
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
    22
// some types abbreviations
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
    23
type NFAt = NFA[TState, Char]
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
    24
type NFAtrans = (TState, Char) :=> Set[TState]
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
    25
type eNFAtrans = (TState, Option[Char]) :=> Set[TState]
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
    26
733
022e2cb1668d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 586
diff changeset
    27
 
487
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
    28
// NFA that does not accept any string
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
    29
def NFA_ZERO(): NFAt = {
486
8178fcf377dc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
  val Q = TState()
8178fcf377dc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
  NFA(Set(Q), { case _ => Set() }, Set())
8178fcf377dc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
}
8178fcf377dc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
487
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
    34
// NFA that accepts the empty string
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
    35
def NFA_ONE() : NFAt = {
486
8178fcf377dc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
  val Q = TState()
8178fcf377dc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
  NFA(Set(Q), { case _ => Set() }, Set(Q))
8178fcf377dc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
}
8178fcf377dc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
487
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
    40
// NFA that accepts the string "c"
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
    41
def NFA_CHAR(c: Char) : NFAt = {
486
8178fcf377dc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
  val Q1 = TState()
8178fcf377dc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
  val Q2 = TState()
8178fcf377dc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
  NFA(Set(Q1), { case (Q1, d) if (c == d) => Set(Q2) }, Set(Q2))
8178fcf377dc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
}
8178fcf377dc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
753
d94fdbef1a4f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 742
diff changeset
    47
d94fdbef1a4f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 742
diff changeset
    48
// for composing an eNFA transition with an NFA transition
d94fdbef1a4f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 742
diff changeset
    49
// | is for set union
d94fdbef1a4f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 742
diff changeset
    50
implicit def nfaOps(f: eNFAtrans) = new {
d94fdbef1a4f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 742
diff changeset
    51
  def +++(g: NFAtrans) : eNFAtrans = 
d94fdbef1a4f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 742
diff changeset
    52
  { case (q, None) =>  applyOrElse(f, (q, None)) 
d94fdbef1a4f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 742
diff changeset
    53
    case (q, Some(c)) => applyOrElse(f, (q, Some(c))) | applyOrElse(g, (q, c))  }
d94fdbef1a4f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 742
diff changeset
    54
}
d94fdbef1a4f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 742
diff changeset
    55
d94fdbef1a4f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 742
diff changeset
    56
487
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
    57
// sequence of two NFAs
779
5385c8342f02 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 753
diff changeset
    58
def NFA_SEQ(nfa1: NFAt, nfa2: NFAt) : NFAt = {
487
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
    59
  val new_delta : eNFAtrans = 
779
5385c8342f02 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 753
diff changeset
    60
    { case (q, None) if nfa1.fins(q) => nfa2.starts }
487
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
    61
  
779
5385c8342f02 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 753
diff changeset
    62
  eNFA(nfa1.starts, 
5385c8342f02 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 753
diff changeset
    63
       new_delta +++ nfa1.delta +++ nfa2.delta, 
5385c8342f02 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 753
diff changeset
    64
       nfa2.fins)
487
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
    65
}
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
    66
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
    67
// alternative of two NFAs
779
5385c8342f02 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 753
diff changeset
    68
def NFA_ALT(nfa1: NFAt, nfa2: NFAt) : NFAt = {
489
e28d7a327870 updated
Christian Urban <urbanc@in.tum.de>
parents: 488
diff changeset
    69
  val new_delta : NFAtrans = { 
779
5385c8342f02 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 753
diff changeset
    70
    case (q, c) =>  applyOrElse(nfa1.delta, (q, c)) | 
5385c8342f02 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 753
diff changeset
    71
                    applyOrElse(nfa2.delta, (q, c)) }
5385c8342f02 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 753
diff changeset
    72
  val new_fins = (q: TState) => nfa1.fins(q) || nfa2.fins(q)
486
8178fcf377dc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    73
779
5385c8342f02 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 753
diff changeset
    74
  NFA(nfa1.starts | nfa2.starts, new_delta, new_fins)
486
8178fcf377dc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    75
}
8178fcf377dc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    76
487
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
    77
// star of a NFA
779
5385c8342f02 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 753
diff changeset
    78
def NFA_STAR(nfa: NFAt) : NFAt = {
486
8178fcf377dc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    79
  val Q = TState()
8178fcf377dc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    80
  val new_delta : eNFAtrans = 
779
5385c8342f02 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 753
diff changeset
    81
    { case (Q, None) => nfa.starts
5385c8342f02 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 753
diff changeset
    82
      case (q, None) if nfa.fins(q) => Set(Q) }
486
8178fcf377dc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    83
779
5385c8342f02 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 753
diff changeset
    84
  eNFA(Set(Q), new_delta +++ nfa.delta, Set(Q))
487
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
    85
}
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
    86
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
    87
733
022e2cb1668d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 586
diff changeset
    88
// We are now ready to translate regular expressions
022e2cb1668d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 586
diff changeset
    89
// into DFAs (via eNFAs and NFAs, and the subset construction)
487
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
    90
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
    91
// regular expressions
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
    92
abstract class Rexp
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
    93
case object ZERO extends Rexp                    // matches nothing
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
    94
case object ONE extends Rexp                     // matches the empty string
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
    95
case class CHAR(c: Char) extends Rexp            // matches a character c
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
    96
case class ALT(r1: Rexp, r2: Rexp) extends Rexp  // alternative
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
    97
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp  // sequence
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
    98
case class STAR(r: Rexp) extends Rexp            // star
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
    99
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
   100
// thompson construction 
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
   101
def thompson (r: Rexp) : NFAt = r match {
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
   102
  case ZERO => NFA_ZERO()
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
   103
  case ONE => NFA_ONE()
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
   104
  case CHAR(c) => NFA_CHAR(c)  
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
   105
  case ALT(r1, r2) => NFA_ALT(thompson(r1), thompson(r2))
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
   106
  case SEQ(r1, r2) => NFA_SEQ(thompson(r1), thompson(r2))
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
   107
  case STAR(r1) => NFA_STAR(thompson(r1))
486
8178fcf377dc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   108
}
487
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
   109
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
   110
//optional regular expression (one or zero times)
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
   111
def OPT(r: Rexp) = ALT(r, ONE)
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
   112
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
   113
//n-times regular expression (explicitly expanded)
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
   114
def NTIMES(r: Rexp, n: Int) : Rexp = n match {
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
   115
  case 0 => ONE
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
   116
  case 1 => r
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
   117
  case n => SEQ(r, NTIMES(r, n - 1))
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
   118
}
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
   119
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
   120
733
022e2cb1668d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 586
diff changeset
   121
def tmatches_nfa(r: Rexp, s: String) : Boolean =
488
598741d39d21 updated
Christian Urban <urbanc@in.tum.de>
parents: 487
diff changeset
   122
  thompson(r).accepts(s.toList)
598741d39d21 updated
Christian Urban <urbanc@in.tum.de>
parents: 487
diff changeset
   123
733
022e2cb1668d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 586
diff changeset
   124
def tmatches_nfa2(r: Rexp, s: String) : Boolean =
488
598741d39d21 updated
Christian Urban <urbanc@in.tum.de>
parents: 487
diff changeset
   125
  thompson(r).accepts2(s.toList)
598741d39d21 updated
Christian Urban <urbanc@in.tum.de>
parents: 487
diff changeset
   126
733
022e2cb1668d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 586
diff changeset
   127
// dfas via subset construction
491
d5776c6018f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 489
diff changeset
   128
def tmatches_dfa(r: Rexp, s: String) : Boolean =
d5776c6018f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 489
diff changeset
   129
  subset(thompson(r)).accepts(s.toList)
488
598741d39d21 updated
Christian Urban <urbanc@in.tum.de>
parents: 487
diff changeset
   130
487
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
   131
// Test Cases
733
022e2cb1668d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 586
diff changeset
   132
//============
488
598741d39d21 updated
Christian Urban <urbanc@in.tum.de>
parents: 487
diff changeset
   133
487
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
   134
// the evil regular expression  a?{n} a{n}
489
e28d7a327870 updated
Christian Urban <urbanc@in.tum.de>
parents: 488
diff changeset
   135
def EVIL1(n: Int) : Rexp = SEQ(NTIMES(OPT(CHAR('a')), n), NTIMES(CHAR('a'), n))
487
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
   136
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
   137
// the evil regular expression (a*)*b
489
e28d7a327870 updated
Christian Urban <urbanc@in.tum.de>
parents: 488
diff changeset
   138
val EVIL2 : Rexp = SEQ(STAR(STAR(CHAR('a'))), CHAR('b'))
487
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
   139
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
   140
//for measuring time
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
   141
def time_needed[T](i: Int, code: => T) = {
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
   142
  val start = System.nanoTime()
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
   143
  for (j <- 1 to i) code
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
   144
  val end = System.nanoTime()
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
   145
  (end - start)/(i * 1.0e9)
488
598741d39d21 updated
Christian Urban <urbanc@in.tum.de>
parents: 487
diff changeset
   146
}
487
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
   147
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
   148
// the size of the NFA can be large, 
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
   149
// thus slowing down the breadth-first search
779
5385c8342f02 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 753
diff changeset
   150
println("Breadth-first search EVIL1 / EVIL2")
487
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
   151
489
e28d7a327870 updated
Christian Urban <urbanc@in.tum.de>
parents: 488
diff changeset
   152
for (i <- 1 to 13) {
733
022e2cb1668d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 586
diff changeset
   153
  println(i + ": " + "%.5f".format(time_needed(2, tmatches_nfa(EVIL1(i), "a" * i))))
487
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
   154
}
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
   155
489
e28d7a327870 updated
Christian Urban <urbanc@in.tum.de>
parents: 488
diff changeset
   156
for (i <- 1 to 100 by 5) {
733
022e2cb1668d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 586
diff changeset
   157
  println(i + " " + "%.5f".format(time_needed(2, tmatches_nfa(EVIL2, "a" * i))))
487
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
   158
}
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
   159
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
   160
733
022e2cb1668d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 586
diff changeset
   161
// the backtracking that is needed in depth-first 
022e2cb1668d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 586
diff changeset
   162
// search can be painfully slow
779
5385c8342f02 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 753
diff changeset
   163
println("Depth-first search EVIL1 / EVIL2")
487
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
   164
779
5385c8342f02 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 753
diff changeset
   165
for (i <- 1 to 9) {
5385c8342f02 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 753
diff changeset
   166
  println(i + " " + "%.5f".format(time_needed(2, tmatches_nfa2(EVIL1(i), "a" * i))))
5385c8342f02 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 753
diff changeset
   167
}
5385c8342f02 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 753
diff changeset
   168
5385c8342f02 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 753
diff changeset
   169
for (i <- 1 to 7) {
733
022e2cb1668d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 586
diff changeset
   170
  println(i + " " + "%.5f".format(time_needed(2, tmatches_nfa2(EVIL2, "a" * i))))
487
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
   171
}
491
d5776c6018f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 489
diff changeset
   172
d5776c6018f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 489
diff changeset
   173
d5776c6018f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 489
diff changeset
   174
586
Christian Urban <urbanc@in.tum.de>
parents: 521
diff changeset
   175
// while my thompson->enfa->subset->partial-function-chain
491
d5776c6018f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 489
diff changeset
   176
// is probably not the most effcient way to obtain a fast DFA 
521
95af9beb4b7f updated
cu
parents: 491
diff changeset
   177
// (the test below should be much faster with a more direct 
95af9beb4b7f updated
cu
parents: 491
diff changeset
   178
// construction), in general the DFAs can be slow because of 
95af9beb4b7f updated
cu
parents: 491
diff changeset
   179
// the state explosion in the subset construction
491
d5776c6018f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 489
diff changeset
   180
742
b5b5583a3a08 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 733
diff changeset
   181
for (i <- 1 to 7) {
491
d5776c6018f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 489
diff changeset
   182
  println(i + ": " + "%.5f".format(time_needed(2, tmatches_dfa(EVIL1(i), "a" * i))))
d5776c6018f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 489
diff changeset
   183
}
d5776c6018f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 489
diff changeset
   184
d5776c6018f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 489
diff changeset
   185
for (i <- 1 to 100 by 5) {
d5776c6018f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 489
diff changeset
   186
  println(i + " " + "%.5f".format(time_needed(2, tmatches_dfa(EVIL2, "a" * i))))
d5776c6018f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 489
diff changeset
   187
}
784
7dac4492b0e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 779
diff changeset
   188
7dac4492b0e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 779
diff changeset
   189
7dac4492b0e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 779
diff changeset
   190
7dac4492b0e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 779
diff changeset
   191
7dac4492b0e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 779
diff changeset
   192