progs/display/thompson1.scala
author Christian Urban <urbanc@in.tum.de>
Sun, 07 May 2017 00:20:58 +0100
changeset 488 598741d39d21
parent 487 a697421eaa04
child 491 d5776c6018f0
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:
diff changeset
     1
// Thompson Construction (Part 1)
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
// (needs  :load nfa.scala
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
//         :load enfa.scala)
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
// states for Thompson construction
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
case class TState(i: Int) extends State
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
object TState {
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
  var counter = 0
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
  
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
  def apply() : TState = {
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
    counter += 1;
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
    new TState(counter - 1)
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
  }
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
}
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
488
598741d39d21 updated
Christian Urban <urbanc@in.tum.de>
parents: 487
diff changeset
    19
// a type abbreviation
487
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
type NFAt = NFA[TState, Char]
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
488
598741d39d21 updated
Christian Urban <urbanc@in.tum.de>
parents: 487
diff changeset
    23
// a NFA that does not accept any string
487
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
def NFA_ZERO(): NFAt = {
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
  val Q = TState()
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
  NFA(Set(Q), { case _ => Set() }, Set())
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
}
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
488
598741d39d21 updated
Christian Urban <urbanc@in.tum.de>
parents: 487
diff changeset
    29
// a NFA that accepts the empty string
487
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
def NFA_ONE() : NFAt = {
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
  val Q = TState()
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
  NFA(Set(Q), { case _ => Set() }, Set(Q))
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
}
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
488
598741d39d21 updated
Christian Urban <urbanc@in.tum.de>
parents: 487
diff changeset
    35
// a NFA that accepts the string "c"
487
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
def NFA_CHAR(c: Char) : NFAt = {
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
  val Q1 = TState()
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
  val Q2 = TState()
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
  NFA(Set(Q1), { case (Q1, d) if (c == d) => Set(Q2) }, Set(Q2))
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
}