progs/automata.scala
author Christian Urban <urbanc@in.tum.de>
Wed, 30 Oct 2019 12:37:18 +0000
changeset 286 5c57c407e27b
parent 121 4fc05d4f0e01
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
121
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
// NFAs and DFAs based on Scala's partial functions
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
// (1) Write a polymorphic function that tests whether the 
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
// intersection of two sets is non-empty
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
def share[A](a: Set[A], b: Set[A]) : Boolean = ...
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
share(Set(1,2,3), Set(2, 3, 4))   // true
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
share(Set(1,2,3), Set(4, 5, 6))   // false
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
// State nodes of the DFAs and NFAs
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
abstract class State
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
type States = Set[State]
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
// Some states for test cases
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
case object Q0 extends State
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
case object Q1 extends State
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
case object Q2 extends State
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
case object Q3 extends State
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
case object Q4 extends State
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
case object Q5 extends State
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
case object Q6 extends State
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
// Transitions for DFAs and NFAs
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
type Trans = PartialFunction[(State, Char), State]
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
type NTrans = Set[Trans]
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
// example transition of an DFA
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
val dtrans : Trans = 
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
  { case (Q0, 'a') => Q1 
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
    case (Q0, 'b') => Q0
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
    case (Q1, 'a') => Q2 
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
    case (Q1, 'b') => Q0
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
    case (Q2, 'a') => Q2 
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
    case (Q2, 'b') => Q0 
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
  }
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
// (2) Write a function that takes a transition and a 
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
// (state, character)-pair as arguments and produces an 
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
// optional state (the state specified by the partial transition
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
// function whenever it is defined; if the transition function 
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
// is undefined, return None.
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
def fire(e: Trans, qc: (State, Char)) : Option[State] = ...
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    52
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    53
// (3) Write a function that takes a transition, a state 
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
// and a list of characters as arguments and produces 
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    55
// the state generated by following the transitions for 
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    56
// each character in the list.
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    57
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    58
def nexts(trans: Trans, q: State, s: List[Char]) : Option[State] = ...
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    59
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    60
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    61
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    62
// class for DFAs
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    63
case class DFA(start: State,      // starting state
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    64
               trans: Trans,      // transition
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
               fins:  States)     // final states
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    66
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    67
// (4) Write a function that tests whether a string is accepted 
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    68
// by an DFA or not.
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    69
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    70
def accepts(dfa: DFA, s: String) : Boolean = ...
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    71
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    72
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    73
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    74
// DFA examples
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    75
 
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    76
val dtrans1 : Trans = 
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    77
  { case (Q0, 'a') => Q0 
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    78
    case (Q0, 'b') => Q1 
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    79
  }
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    80
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    81
val dfa1 = DFA(Q0, dtrans1, Set[State](Q1))
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    82
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    83
accepts(dfa1, "aaab")     // true
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    84
accepts(dfa1, "aacb")     // false
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    85
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    86
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    87
// NFAs
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    88
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    89
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    90
// (5) Write a function that takes a transition set, a state
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    91
// and a character as arguments, and calculates all possible 
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    92
// next states (returned as set).
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    93
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    94
def nnext(trans: NTrans, q: State, c: Char) : States = ...
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    95
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    96
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    97
// (6) Write a function that takes a transition set, a set of states 
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    98
// and a character as arguments, and calculates all possible 
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    99
// next states that can be reached from any state in the set.
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   100
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   101
def nnexts(trans: NTrans, qs: States, c: Char) : States = ...
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   102
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   103
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   104
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   105
// (7) Write a function that lifts nnexts from from single 
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   106
// characters to lists of characters.
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   107
def nnextss(trans: NTrans, qs: States, s: List[Char]) : States = ...
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   108
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   109
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   110
// class for NFAs
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   111
case class NFA(start: States,       // starting state
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   112
               trans: NTrans,       // transition edges
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   113
               fins:  States)       // final states
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   114
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   115
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   116
// (8) Write a function that tests whether a string is 
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   117
// accepted by an NFA or not.
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   118
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   119
def naccepts(nfa: NFA, s: String) : Boolean = ...
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   120
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   121
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   122
// (9) Write similar functions as in (7) and (8), but instead of 
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   123
// returning states or a boolean, calculate the number of states 
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   124
// that need to be followed in each step.
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   125
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   126
def max_nextss(trans: NTrans, qs: States, s: List[Char], max: Int) : Int = ...
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   127
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   128
def max_accepts(nfa: NFA, s: String) : Int = ...
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   129
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   130
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   131
// NFA examples
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   132
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   133
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   134
// 1 
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   135
val trans1 : NTrans = Set(
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   136
  { case (Q0, 'a') => Q1 },
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   137
  { case (Q0, _)   => Q0 },
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   138
  { case (Q1, _)   => Q2 },
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   139
  { case (Q2, _)   => Q3 },
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   140
  { case (Q3, _)   => Q4 },
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   141
  { case (Q4, 'b') => Q5 },
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   142
  { case (Q5, 'c') => Q6 }
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   143
)
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   144
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   145
val nfa1 = NFA(Set[State](Q0), trans1, Set[State](Q6))
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   146
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   147
naccepts(nfa1, "axaybzbc")     // true
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   148
naccepts(nfa1, "aaaaxaybzbc")  // true
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   149
naccepts(nfa1, "axaybzbd")     // false
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   150
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   151
// the nfa has five states, which might be all 
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   152
// active
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   153
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   154
max_accepts(nfa1, "axaybzbc")     // 3 
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   155
max_accepts(nfa1, "aaaaxaybzbc")  // 5
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   156
max_accepts(nfa1, "axaybzbd")     // 3
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   157
max_accepts(nfa1, "aaaaaaaaaaaaaxaybzbd")   // 5
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   158
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   159
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   160
// 2
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   161
val trans2 : NTrans = Set(
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   162
  { case (Q0, 'a') => Q0 },
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   163
  { case (Q0, 'a') => Q1 },
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   164
  { case (Q0, 'b') => Q2 },
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   165
  { case (Q1, 'a') => Q1 },
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   166
  { case (Q2, 'b') => Q2 }
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   167
)
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   168
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   169
val nfa2 = NFA(Set[State](Q0), trans2, Set[State](Q2))
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   170
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   171
naccepts(nfa2, "aa")             // false
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   172
naccepts(nfa2, "aaaaa")          // false
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   173
naccepts(nfa2, "aaaaab")         // true
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   174
naccepts(nfa2, "aaaaabbb")       // true
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   175
naccepts(nfa2, "aaaaabbbaaa")    // false
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   176
naccepts(nfa2, "ac")             // false
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   177
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   178
// 3
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   179
val trans3 : NTrans = Set(
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   180
  { case (Q0, _)   => Q0 },
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   181
  { case (Q0, 'a') => Q1 },
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   182
  { case (Q0, 'b') => Q3 },
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   183
  { case (Q1, 'b') => Q2 },
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   184
  { case (Q2, 'c') => Q5 },
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   185
  { case (Q3, 'c') => Q4 },
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   186
  { case (Q4, 'd') => Q5 }
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   187
)
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   188
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   189
val nfa3 = NFA(Set[State](Q0), trans3, Set[State](Q5))
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   190
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   191
naccepts(nfa3, "aaaaabc")      // true
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   192
naccepts(nfa3, "aaaabcd")      // true
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   193
naccepts(nfa3, "aaaaab")       // false
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   194
naccepts(nfa3, "aaaabc")       // true
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   195
naccepts(nfa3, "aaaaabbbaaa")  // false
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   196
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   197