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