Documentation
This commit is contained in:
parent
887e2d63c7
commit
0f5d632388
2 changed files with 31 additions and 3 deletions
11
DFA.fs
11
DFA.fs
|
|
@ -8,10 +8,16 @@ type State = { name: String }
|
|||
/// A deterministic finite automaton
|
||||
///
|
||||
type DFA = {
|
||||
/// All possible characters
|
||||
sigma: Char list
|
||||
/// All possible states
|
||||
states: State list
|
||||
/// All possible transitions
|
||||
delta: State -> Char -> State
|
||||
/// The state this DFA begins in
|
||||
beginState: State
|
||||
/// The states in which this DFA accepts
|
||||
/// the word
|
||||
acceptingStates: State list
|
||||
}
|
||||
|
||||
|
|
@ -43,14 +49,19 @@ let validateDFA (dfa:DFA) =
|
|||
allAcceptingStatesAreStates ||
|
||||
deltaIsComplete
|
||||
|
||||
/// Returns the state the DFA is in after
|
||||
/// reading this character
|
||||
let processChar (dfa: DFA) (state: State) (char: Char) =
|
||||
dfa.delta state char
|
||||
|
||||
/// Returns the state the DFA is in after
|
||||
/// reading this character array
|
||||
let rec processCharArray (dfa: DFA) (state: State) (charArr: Char list) =
|
||||
match charArr with
|
||||
| [] -> state
|
||||
| head :: tail -> processCharArray dfa (processChar dfa state head) tail
|
||||
|
||||
/// Returns whether the DFA accepts this word
|
||||
let acceptsWord (dfa: DFA) (word: String) =
|
||||
dfa.acceptingStates
|
||||
|> List.contains (processCharArray dfa dfa.beginState (Seq.toList word))
|
||||
17
NFA.fs
17
NFA.fs
|
|
@ -6,16 +6,26 @@ type State = { name: String }
|
|||
|
||||
/// A non-deterministic finite automaton
|
||||
type NFA = {
|
||||
/// All possible characters
|
||||
sigma: Char list
|
||||
/// All possible states
|
||||
states: State list
|
||||
/// All possible transitions
|
||||
delta: State -> Char -> State list
|
||||
/// The state this NFA begins in
|
||||
beginState: State
|
||||
/// The states in which this NFA accepts
|
||||
/// the word
|
||||
acceptingStates: State list
|
||||
}
|
||||
|
||||
/// Returns a list of possible states after this charater
|
||||
/// was read
|
||||
let processChar (nfa: NFA) (state: State) (char: Char) =
|
||||
nfa.delta state char
|
||||
|
||||
/// Returns a list of possible states after this character
|
||||
/// list was read
|
||||
let rec processCharArray (nfa: NFA) (states: State list) (charArr: Char list) : State list =
|
||||
match charArr with
|
||||
| [] -> states
|
||||
|
|
@ -23,9 +33,15 @@ let rec processCharArray (nfa: NFA) (states: State list) (charArr: Char list) :
|
|||
processCharArray
|
||||
nfa
|
||||
(
|
||||
// Look at all states
|
||||
states
|
||||
// Where are we going to from each state
|
||||
// if we read this character
|
||||
|> List.map (fun x -> processChar nfa x head)
|
||||
// Which entries are not empty
|
||||
|> List.filter (List.isEmpty >> not)
|
||||
// If there are no states left,
|
||||
// return an empty list
|
||||
|> (fun x ->
|
||||
if not (List.isEmpty x) then
|
||||
List.reduce List.append x
|
||||
|
|
@ -33,6 +49,7 @@ let rec processCharArray (nfa: NFA) (states: State list) (charArr: Char list) :
|
|||
)
|
||||
tail
|
||||
|
||||
/// Returns whether this NFA accepts this word
|
||||
let acceptsWord nfa word =
|
||||
match processCharArray nfa [nfa.beginState] (Seq.toList word) with
|
||||
| [] -> false
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue