Maybe finished NFAs
This commit is contained in:
parent
bb641e26f1
commit
887e2d63c7
3 changed files with 45 additions and 2 deletions
2
DFA.fs
2
DFA.fs
|
|
@ -49,7 +49,7 @@ let processChar (dfa: DFA) (state: State) (char: Char) =
|
|||
let rec processCharArray (dfa: DFA) (state: State) (charArr: Char list) =
|
||||
match charArr with
|
||||
| [] -> state
|
||||
| _ -> processCharArray dfa (processChar dfa state charArr.Head) charArr.Tail
|
||||
| head :: tail -> processCharArray dfa (processChar dfa state head) tail
|
||||
|
||||
let acceptsWord (dfa: DFA) (word: String) =
|
||||
dfa.acceptingStates
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ module Tests
|
|||
|
||||
open System
|
||||
open DFA
|
||||
open NFA
|
||||
|
||||
let numericDFA:DFA = {
|
||||
sigma = Seq.toList "01ab";
|
||||
|
|
@ -19,9 +20,27 @@ let numericDFA:DFA = {
|
|||
acceptingStates = [{name = "yes"}]
|
||||
}
|
||||
|
||||
let numericNFA:NFA = {
|
||||
sigma = Seq.toList "01ab";
|
||||
states = [
|
||||
{name = "yes"}
|
||||
];
|
||||
delta = (fun x y ->
|
||||
match (x, y) with
|
||||
| ({name = "yes"}, '0') -> [{name = "yes"}]
|
||||
| ({name = "yes"}, '1') -> [{name = "yes"}]
|
||||
| _ -> []
|
||||
);
|
||||
beginState = {name = "yes"};
|
||||
acceptingStates = [{name = "yes"}]
|
||||
}
|
||||
|
||||
let test =
|
||||
printfn "Testing numeric checker DFA"
|
||||
printfn "Is valid DFA: %b" (DFA.validateDFA numericDFA)
|
||||
printfn "0101: %b" (DFA.acceptsWord numericDFA "0101")
|
||||
printfn "01a1: %b" (DFA.acceptsWord numericDFA "01a1")
|
||||
printfn "Testing numeric checker NFA"
|
||||
printfn "0101: %A" (NFA.acceptsWord numericNFA "0101")
|
||||
printfn "01a1: %b" (NFA.acceptsWord numericNFA "01a1")
|
||||
0
|
||||
24
NFA.fs
24
NFA.fs
|
|
@ -15,3 +15,27 @@ type NFA = {
|
|||
|
||||
let processChar (nfa: NFA) (state: State) (char: Char) =
|
||||
nfa.delta state char
|
||||
|
||||
let rec processCharArray (nfa: NFA) (states: State list) (charArr: Char list) : State list =
|
||||
match charArr with
|
||||
| [] -> states
|
||||
| head :: tail ->
|
||||
processCharArray
|
||||
nfa
|
||||
(
|
||||
states
|
||||
|> List.map (fun x -> processChar nfa x head)
|
||||
|> List.filter (List.isEmpty >> not)
|
||||
|> (fun x ->
|
||||
if not (List.isEmpty x) then
|
||||
List.reduce List.append x
|
||||
else [])
|
||||
)
|
||||
tail
|
||||
|
||||
let acceptsWord nfa word =
|
||||
match processCharArray nfa [nfa.beginState] (Seq.toList word) with
|
||||
| [] -> false
|
||||
| x -> x
|
||||
|> List.map (fun x -> List.contains x nfa.acceptingStates)
|
||||
|> List.exists id
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue