Prettified debug output (sorted initial state and accept-states)
Properly tested epsilon transitions
This commit is contained in:
parent
382d2e9501
commit
51dd52aa86
6
.idea/vcs.xml
generated
Normal file
6
.idea/vcs.xml
generated
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="VcsDirectoryMappings">
|
||||||
|
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||||
|
</component>
|
||||||
|
</project>
|
@ -1,24 +1,37 @@
|
|||||||
import dev.w1zzrd.automata.*
|
import dev.w1zzrd.automata.*
|
||||||
|
|
||||||
fun main(args: Array<String>){
|
fun main(args: Array<String>){
|
||||||
val language = Language.makeLanguage(0, 1, 2)
|
val language = Language.makeLanguage(0, 1)
|
||||||
val automaton = Automaton(language, false)
|
val nfa = Automaton(language, false)
|
||||||
|
|
||||||
val stateA = automaton.makeState("a")
|
val stateS = nfa.makeState("s")
|
||||||
val stateB = automaton.makeState("b")
|
val stateQ1 = nfa.makeState("q1")
|
||||||
val stateC = automaton.makeState("c")
|
val stateQ2 = nfa.makeState("q2", true)
|
||||||
val stateD = automaton.makeState("d", true)
|
val stateP = nfa.makeState("p")
|
||||||
|
val stateQ = nfa.makeState("q")
|
||||||
|
val stateR = nfa.makeState("r", true)
|
||||||
|
|
||||||
stateA.addConnective(arrayOf(0, 1), stateB)
|
stateS.addEpsilon(stateQ1, stateP)
|
||||||
stateA.addConnective(arrayOf(1, 2), stateC)
|
|
||||||
|
|
||||||
stateB.addConnective(arrayOf(0, 2), stateD)
|
stateQ1.addConnective(0, stateQ1)
|
||||||
|
stateQ1.addConnective(1, stateQ2)
|
||||||
|
|
||||||
stateC.addConnective(arrayOf(0, 1), stateD)
|
stateQ2.addConnective(0, stateQ1)
|
||||||
|
|
||||||
stateD.addConnective(0, stateA)
|
stateP.addConnective(arrayOf(0, 1), stateP)
|
||||||
|
stateP.addConnective(1, stateQ)
|
||||||
|
|
||||||
automaton.entryPoint = stateA
|
stateQ.addConnective(arrayOf(0, 1), stateR)
|
||||||
|
|
||||||
val dfa = automaton.toDeterministicAutomaton(true)
|
nfa.entryPoint = stateS
|
||||||
|
|
||||||
|
val dfa = nfa.toDeterministicAutomaton(true)
|
||||||
|
|
||||||
|
val dtraverser = dfa.makeTraverser()
|
||||||
|
dtraverser.traverse(1, 1, 0, 0)
|
||||||
|
println(dtraverser.currentState.toString())
|
||||||
|
|
||||||
|
val ntraverser = nfa.makeTraverser()
|
||||||
|
ntraverser.traverse(1, 1, 0, 0)
|
||||||
|
println(ntraverser.currentState.toString())
|
||||||
}
|
}
|
@ -97,7 +97,7 @@ class Automaton<T>(val language: Language<T>, val deterministic: Boolean){
|
|||||||
}
|
}
|
||||||
|
|
||||||
if(printTable)
|
if(printTable)
|
||||||
for(key in tableEntries.keys){
|
for(key in tableEntries.keys.sortedBy { if(it.contentsEquals(startingState)) 0 else if (it.isAcceptState()) 2 else 1 }){
|
||||||
print(
|
print(
|
||||||
(if(key.contentsEquals(startingState)) "→ " else " ") +
|
(if(key.contentsEquals(startingState)) "→ " else " ") +
|
||||||
(if(key.isAcceptState()) "F" else " ") +
|
(if(key.isAcceptState()) "F" else " ") +
|
||||||
@ -131,7 +131,13 @@ class Automaton<T>(val language: Language<T>, val deterministic: Boolean){
|
|||||||
|
|
||||||
private fun MutableList<State<T>>.isAcceptState() = firstOrNull { it.acceptState } != null
|
private fun MutableList<State<T>>.isAcceptState() = firstOrNull { it.acceptState } != null
|
||||||
|
|
||||||
private inner class StateTraverser(entryPoint: State<T>) {
|
fun makeTraverser(): StateTraverser{
|
||||||
|
if(entryPoint == null)
|
||||||
|
throw IllegalStateException("Entry point state must be defined!")
|
||||||
|
return StateTraverser(entryPoint!!)
|
||||||
|
}
|
||||||
|
|
||||||
|
inner class StateTraverser(entryPoint: State<T>) {
|
||||||
var currentState: MutableList<State<T>> = ArrayList()
|
var currentState: MutableList<State<T>> = ArrayList()
|
||||||
|
|
||||||
val accepted: Boolean
|
val accepted: Boolean
|
||||||
@ -150,8 +156,9 @@ class Automaton<T>(val language: Language<T>, val deterministic: Boolean){
|
|||||||
val nextState = ArrayList<State<T>>()
|
val nextState = ArrayList<State<T>>()
|
||||||
|
|
||||||
for(state in currentState)
|
for(state in currentState)
|
||||||
for(traverseState in state.getConnective(verb))
|
for(traverseState in state.getConnective(verb)) {
|
||||||
nextState.traverseEpsilon(traverseState)
|
nextState.traverseEpsilon(traverseState)
|
||||||
|
}
|
||||||
|
|
||||||
currentState = nextState
|
currentState = nextState
|
||||||
}
|
}
|
||||||
@ -161,6 +168,7 @@ class Automaton<T>(val language: Language<T>, val deterministic: Boolean){
|
|||||||
for(epsilonState in state.getEpsilon())
|
for(epsilonState in state.getEpsilon())
|
||||||
if(!contains(epsilonState))
|
if(!contains(epsilonState))
|
||||||
traverseEpsilon(epsilonState)
|
traverseEpsilon(epsilonState)
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -44,6 +44,10 @@ class State<T>(
|
|||||||
override fun hashCode(): Int {
|
override fun hashCode(): Int {
|
||||||
return name.hashCode()
|
return name.hashCode()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun toString(): String {
|
||||||
|
return name
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class StateFactory<T>(val language: Language<T>, val deterministic: Boolean){
|
class StateFactory<T>(val language: Language<T>, val deterministic: Boolean){
|
||||||
|
Loading…
x
Reference in New Issue
Block a user