Skip to content

Instantly share code, notes, and snippets.

@Derekokich
Created August 19, 2014 17:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Derekokich/79989550a2efa8afd5ea to your computer and use it in GitHub Desktop.
Save Derekokich/79989550a2efa8afd5ea to your computer and use it in GitHub Desktop.
regexblog10
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