Created
August 19, 2014 17:47
-
-
Save Derekokich/79989550a2efa8afd5ea to your computer and use it in GitHub Desktop.
regexblog10
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 NFA { | |
def regexToNFA(regex: RegexExpr): State = | |
regexToNFA(regex, Match()) | |
private def regexToNFA(regex: RegexExpr, | |
andThen: State): State = { | |
regex match { | |
case Literal(c) => new Consume(c, andThen) | |
case Concat(first, second) => { | |
// Convert first to an NFA. The "out" of that is | |
// the result of converting second to an NFA. | |
regexToNFA(first, regexToNFA(second, andThen)) | |
} | |
case Or(l, r) => new Split( | |
regexToNFA(l, andThen), | |
regexToNFA(r, andThen) | |
) | |
case Repeat(r) => | |
val placeholder = new Placeholder(null) | |
val split = new Split( | |
// One path goes to the placeholder, | |
// the other path goes back to r | |
regexToNFA(r, placeholder), | |
andThen | |
) | |
placeholder.pointingTo = split | |
placeholder | |
case Plus(r) => | |
regexToNFA(Concat(r, Repeat(r)), andThen) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment