Fix accept states for NFA-DFA translation
Implement relevant loops as stdlib functions
This commit is contained in:
parent
752f7f2a1c
commit
53402051aa
2
.idea/misc.xml
generated
2
.idea/misc.xml
generated
@ -1,6 +1,6 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<project version="4">
|
<project version="4">
|
||||||
<component name="ProjectRootManager" version="2" project-jdk-name="11" project-jdk-type="JavaSDK">
|
<component name="ProjectRootManager" version="2" project-jdk-name="9.0" project-jdk-type="JavaSDK">
|
||||||
<output url="file://$PROJECT_DIR$/out" />
|
<output url="file://$PROJECT_DIR$/out" />
|
||||||
</component>
|
</component>
|
||||||
</project>
|
</project>
|
@ -9,25 +9,26 @@ fun main(args: Array<String>){
|
|||||||
|
|
||||||
// Declare states of the NFA
|
// Declare states of the NFA
|
||||||
val stateS = nfa.makeState("s")
|
val stateS = nfa.makeState("s")
|
||||||
val stateQ1 = nfa.makeState("q1")
|
val stateA = nfa.makeState("a")
|
||||||
val stateQ2 = nfa.makeState("q2", true)
|
val stateB = nfa.makeState("b", true)
|
||||||
val stateP = nfa.makeState("p")
|
val stateC = nfa.makeState("c")
|
||||||
val stateQ = nfa.makeState("q")
|
val stateD = nfa.makeState("d")
|
||||||
val stateR = nfa.makeState("r", true)
|
val stateE = nfa.makeState("e", true)
|
||||||
|
|
||||||
// Add epsilon transition from S to Q1 and P
|
|
||||||
stateS.addEpsilon(stateQ1, stateP)
|
|
||||||
|
|
||||||
// Add regular state-transition connectives
|
stateS.addEpsilon(stateA, stateC)
|
||||||
stateQ1.addConnective(0, stateQ1)
|
|
||||||
stateQ1.addConnective(1, stateQ2)
|
|
||||||
|
|
||||||
stateQ2.addConnective(0, stateQ1)
|
stateA.addConnective(1, stateB)
|
||||||
|
|
||||||
stateP.addConnective(arrayOf(0, 1), stateP)
|
stateB.addConnective(0, stateB)
|
||||||
stateP.addConnective(1, stateQ)
|
|
||||||
|
stateC.addConnective(1, stateB, stateD)
|
||||||
|
stateC.addConnective(0, stateB)
|
||||||
|
|
||||||
|
stateD.addConnective(0, stateE)
|
||||||
|
|
||||||
|
stateE.addConnective(arrayOf(0, 1), stateC)
|
||||||
|
|
||||||
stateQ.addConnective(arrayOf(0, 1), stateR)
|
|
||||||
|
|
||||||
// Declare S as the initial state
|
// Declare S as the initial state
|
||||||
nfa.entryPoint = stateS
|
nfa.entryPoint = stateS
|
||||||
@ -37,11 +38,15 @@ fun main(args: Array<String>){
|
|||||||
|
|
||||||
// Get a traverser for the DFA and manually traverse the string "1100", then print the resulting state
|
// Get a traverser for the DFA and manually traverse the string "1100", then print the resulting state
|
||||||
val dtraverser = dfa.makeTraverser()
|
val dtraverser = dfa.makeTraverser()
|
||||||
dtraverser.traverse(1, 1, 0, 0)
|
dtraverser.traverse(1, 0, 0, 1, 0)
|
||||||
|
println("DFA simulation:")
|
||||||
println(dtraverser.currentState.toString())
|
println(dtraverser.currentState.toString())
|
||||||
|
println("Accepts: ${dfa.accepts(1, 0, 0, 1, 0)}\n")
|
||||||
|
|
||||||
// Do the same as above but for the NFA
|
// Do the same as above but for the NFA
|
||||||
val ntraverser = nfa.makeTraverser()
|
val ntraverser = nfa.makeTraverser()
|
||||||
ntraverser.traverse(1, 1, 0, 0)
|
ntraverser.traverse(1, 0, 0, 1, 0)
|
||||||
|
println("NFA simulation:")
|
||||||
println(ntraverser.currentState.toString())
|
println(ntraverser.currentState.toString())
|
||||||
|
println("Accepts: ${nfa.accepts(1, 0, 0, 1, 0)}")
|
||||||
}
|
}
|
@ -243,7 +243,7 @@ class Automaton<T>(val language: Language<T>, val deterministic: Boolean){
|
|||||||
|
|
||||||
// Generate the NFA-DFA state-set-to-state mappings, as well as populating the DFA with the necessary states
|
// Generate the NFA-DFA state-set-to-state mappings, as well as populating the DFA with the necessary states
|
||||||
for(tableState in tableEntries.keys)
|
for(tableState in tableEntries.keys)
|
||||||
oldToNew[tableState] = dfa.makeState(tableState.toReadableString(), tableState.contentsEquals(startingState))
|
oldToNew[tableState] = dfa.makeState(tableState.toReadableString(), tableState.isAcceptState())
|
||||||
|
|
||||||
// Apply the mapping schema determined by the generated state-table to the DFA
|
// Apply the mapping schema determined by the generated state-table to the DFA
|
||||||
for(oldState in oldToNew.keys)
|
for(oldState in oldToNew.keys)
|
||||||
@ -351,9 +351,9 @@ class Automaton<T>(val language: Language<T>, val deterministic: Boolean){
|
|||||||
if(!contains(state)) add(state)
|
if(!contains(state)) add(state)
|
||||||
|
|
||||||
// Recursively traverse epsilon connectives for new connectives (i.e. for states not already traversed)
|
// Recursively traverse epsilon connectives for new connectives (i.e. for states not already traversed)
|
||||||
for(epsilonState in state.getEpsilon())
|
state.getEpsilon()
|
||||||
if(!contains(epsilonState))
|
.filterNot { contains(it) }
|
||||||
traverseEpsilon(epsilonState)
|
.forEach { traverseEpsilon(it) }
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user