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"?>
|
||||
<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" />
|
||||
</component>
|
||||
</project>
|
@ -9,25 +9,26 @@ fun main(args: Array<String>){
|
||||
|
||||
// Declare states of the NFA
|
||||
val stateS = nfa.makeState("s")
|
||||
val stateQ1 = nfa.makeState("q1")
|
||||
val stateQ2 = nfa.makeState("q2", true)
|
||||
val stateP = nfa.makeState("p")
|
||||
val stateQ = nfa.makeState("q")
|
||||
val stateR = nfa.makeState("r", true)
|
||||
val stateA = nfa.makeState("a")
|
||||
val stateB = nfa.makeState("b", true)
|
||||
val stateC = nfa.makeState("c")
|
||||
val stateD = nfa.makeState("d")
|
||||
val stateE = nfa.makeState("e", true)
|
||||
|
||||
// Add epsilon transition from S to Q1 and P
|
||||
stateS.addEpsilon(stateQ1, stateP)
|
||||
|
||||
// Add regular state-transition connectives
|
||||
stateQ1.addConnective(0, stateQ1)
|
||||
stateQ1.addConnective(1, stateQ2)
|
||||
stateS.addEpsilon(stateA, stateC)
|
||||
|
||||
stateQ2.addConnective(0, stateQ1)
|
||||
stateA.addConnective(1, stateB)
|
||||
|
||||
stateP.addConnective(arrayOf(0, 1), stateP)
|
||||
stateP.addConnective(1, stateQ)
|
||||
stateB.addConnective(0, stateB)
|
||||
|
||||
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
|
||||
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
|
||||
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("Accepts: ${dfa.accepts(1, 0, 0, 1, 0)}\n")
|
||||
|
||||
// Do the same as above but for the NFA
|
||||
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("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
|
||||
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
|
||||
for(oldState in oldToNew.keys)
|
||||
@ -351,9 +351,9 @@ class Automaton<T>(val language: Language<T>, val deterministic: Boolean){
|
||||
if(!contains(state)) add(state)
|
||||
|
||||
// Recursively traverse epsilon connectives for new connectives (i.e. for states not already traversed)
|
||||
for(epsilonState in state.getEpsilon())
|
||||
if(!contains(epsilonState))
|
||||
traverseEpsilon(epsilonState)
|
||||
state.getEpsilon()
|
||||
.filterNot { contains(it) }
|
||||
.forEach { traverseEpsilon(it) }
|
||||
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user