progs/dfa.scala
changeset 487 a697421eaa04
parent 485 bd6d999ab7b6
child 576 550186034b6e
equal deleted inserted replaced
486:8178fcf377dc 487:a697421eaa04
    15 
    15 
    16   def accepts(s: List[C]) : Boolean = 
    16   def accepts(s: List[C]) : Boolean = 
    17     Try(fins(deltas(start, s))) getOrElse false
    17     Try(fins(deltas(start, s))) getOrElse false
    18 }
    18 }
    19 
    19 
    20 // the example shown earlier in the handout 
    20 // the example shown in the handout 
    21 abstract class State
    21 abstract class State
    22 case object Q0 extends State
    22 case object Q0 extends State
    23 case object Q1 extends State
    23 case object Q1 extends State
    24 case object Q2 extends State
    24 case object Q2 extends State
    25 case object Q3 extends State
    25 case object Q3 extends State
    39 
    39 
    40 val dfa = DFA(Q0, delta, Set[State](Q4))
    40 val dfa = DFA(Q0, delta, Set[State](Q4))
    41 
    41 
    42 dfa.accepts("bbabaab".toList)   // true
    42 dfa.accepts("bbabaab".toList)   // true
    43 dfa.accepts("baba".toList)      // false
    43 dfa.accepts("baba".toList)      // false
       
    44 
       
    45 
       
    46 // another DFA test with a Sink state
       
    47 abstract class S
       
    48 case object S0 extends S
       
    49 case object S1 extends S
       
    50 case object S2 extends S
       
    51 case object Sink extends S
       
    52 
       
    53 // transition function with a sink state
       
    54 val sigma : (S, Char) :=> S = 
       
    55   { case (S0, 'a') => S1
       
    56     case (S1, 'a') => S2
       
    57     case _ => Sink
       
    58   }
       
    59 
       
    60 val dfa1a = DFA(S0, sigma, Set[S](S2))
       
    61 
       
    62 dfa1a.accepts("aa".toList)        // true
       
    63 dfa1a.accepts("".toList)          // false
       
    64 dfa1a.accepts("ab".toList)        // false
       
    65