Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // Scala implementation to test strings that match the expression a*bb* | aa*bc* | ef
- // by Miguel Marques
- //
- object Main {
- val characters = Map('a' -> 0, 'b' -> 1, 'c' -> 2, 'e' -> 3, 'f' -> 4).withDefaultValue(5)
- val transitions = Array(
- Array(0, 0, 0, 0, 0, 0),
- Array(2, 3, 0, 6, 0, 0),
- Array(2, 4, 0, 0, 0, 0),
- Array(0, 3, 0, 0, 0, 0),
- Array(0, 3, 5, 0, 0, 0),
- Array(0, 0, 5, 0, 0, 0),
- Array(0, 0, 0, 0, 7, 0),
- Array(0, 0, 0, 0, 0, 0))
- val initialState = 1
- val finalStates = Set(3, 4, 5, 7)
- def eval(str: String): Boolean =
- finalStates contains (
- str.foldLeft(initialState) { (state, character) =>
- val charInt = characters(character)
- transitions(state)(charInt)
- })
- def main(args: Array[String]): Unit = {
- Stream continually {
- print("Enter a string to test (ctrl+z to exit): ")
- readLine
- } takeWhile (_ != null) foreach { line =>
- println(if (eval(line)) "accept" else "reject")
- }
- println("\nBye!!!")
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement