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