Created
August 19, 2014 17:52
-
-
Save Derekokich/18484e9d940fcaf7bb7b to your computer and use it in GitHub Desktop.
regexblog11
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object NFAEvaluator { | |
def evaluate(nfa: State, input: String): Boolean = | |
evaluate(Set(nfa), input) | |
def evaluate(nfas: Set[State], input: String): Boolean = { | |
input match { | |
case "" => | |
evaluateStates(nfas, None).exists(_ == Match()) | |
case string => | |
evaluate( | |
evaluateStates(nfas, input.headOption), | |
string.tail | |
) | |
} | |
} | |
def evaluateStates(nfas: Set[State], | |
input: Option[Char]): Set[State] = { | |
val visitedStates = mutable.Set[State]() | |
nfas.flatMap { state => | |
evaluateState(state, input, visitedStates) | |
} | |
} | |
def evaluateState(currentState: State, input: Option[Char], | |
visitedStates: mutable.Set[State]): Set[State] = { | |
if (visitedStates contains currentState) { | |
Set() | |
} else { | |
visitedStates.add(currentState) | |
currentState match { | |
case placeholder: Placeholder => | |
evaluateState( | |
placeholder.pointingTo, | |
input, | |
visitedStates | |
) | |
case consume: Consume => | |
if (Some(consume.c) == input | |
|| consume.c == '.') { | |
Set(consume.out) | |
} else { | |
Set() | |
} | |
case s: Split => | |
evaluateState(s.out1, input, visitedStates) ++ | |
evaluateState(s.out2, input, visitedStates) | |
case m: Match => | |
if (input.isDefined) Set() else Set(Match()) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment