Created
August 4, 2011 14:30
-
-
Save dcsobral/1125284 to your computer and use it in GitHub Desktop.
Generating strings from a regex
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
import scala.util.regexp._ | |
import scala.util.automata._ | |
object MyLang extends WordExp { | |
type _regexpT = RegExp | |
type _labelT = MyChar | |
case class MyChar(c:Char) extends Label | |
} | |
import MyLang._ | |
object MyBerriSethi extends WordBerrySethi { | |
override val lang = MyLang | |
} | |
import scala.util.Random | |
def generate(dfa: DetWordAutom[MyChar]) = { | |
var result = "" | |
var state = 0 | |
while (!((dfa isFinal state) && ((Random nextInt 5) == 0 || (dfa delta state isEmpty)))) { | |
val nextState = Random nextInt (dfa delta state size) | |
val transition = (dfa delta state).keys.toSeq(nextState) | |
result += transition.c | |
state = dfa next (state, transition) | |
} | |
result | |
} | |
// (a* | b)* | |
val rex = Star(Alt(Star(Letter(MyChar('a'))),Letter(MyChar('b')))) | |
val nfa = MyBerriSethi.automatonFrom(Sequ(rex), 1) | |
val dfa = new SubsetConstruction(nfa).determinize | |
generate(dfa) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment