From a8886bec336fd940ab20919929457fd976c892c8 Mon Sep 17 00:00:00 2001 From: jackfiled Date: Tue, 23 Jul 2024 22:46:34 +0800 Subject: [PATCH] =?UTF-8?q?add:=20NFA=E5=AE=9A=E4=B9=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CanonSharp.Parser/CanonSharp.Parser.fsproj | 5 +++ .../NondeterministicFiniteAutomation.fs | 38 +++++++++++++++++++ .../LexicalAnalyzer/RegularExpression.fs | 12 ++++++ 3 files changed, 55 insertions(+) create mode 100644 CanonSharp.Parser/LexicalAnalyzer/NondeterministicFiniteAutomation.fs create mode 100644 CanonSharp.Parser/LexicalAnalyzer/RegularExpression.fs diff --git a/CanonSharp.Parser/CanonSharp.Parser.fsproj b/CanonSharp.Parser/CanonSharp.Parser.fsproj index b53f1b2..defded5 100644 --- a/CanonSharp.Parser/CanonSharp.Parser.fsproj +++ b/CanonSharp.Parser/CanonSharp.Parser.fsproj @@ -5,4 +5,9 @@ true + + + + + diff --git a/CanonSharp.Parser/LexicalAnalyzer/NondeterministicFiniteAutomation.fs b/CanonSharp.Parser/LexicalAnalyzer/NondeterministicFiniteAutomation.fs new file mode 100644 index 0000000..4e8150f --- /dev/null +++ b/CanonSharp.Parser/LexicalAnalyzer/NondeterministicFiniteAutomation.fs @@ -0,0 +1,38 @@ +module CanonSharp.Parser.LexicalAnalyzer.NondeterministicFiniteAutomation + +open System +open System.Collections.Generic +open CanonSharp.Parser.LexicalAnalyzer.RegularExpression + +type NondeterministicState(id: Guid, transaction: Option -> list) = + member val id = id + member val transaction = transaction + + override this.GetHashCode() = this.id.GetHashCode() + + new() = NondeterministicState(Guid.NewGuid(), fun a -> list.Empty) + + +type NondeterministicFiniteAutomation(states: HashSet, entryTransaction: Option -> list) = + member val states = states + member val entryTransaction = entryTransaction + +let convertEmptyToNonDeterministicFiniteAutomation (expression: EmptyExpression) = + let final = NondeterministicState() + let transaction (a: Option) = + match a with + | Some _ -> list.Empty + | None -> [final] + + let states = HashSet() + let _ = states.Add(final) + + NondeterministicFiniteAutomation(states, transaction) + + + + + +let convertToNondeterministicFiniteAutomation expression: RegularExpression = + match expression with + | EmptyExpression -> diff --git a/CanonSharp.Parser/LexicalAnalyzer/RegularExpression.fs b/CanonSharp.Parser/LexicalAnalyzer/RegularExpression.fs new file mode 100644 index 0000000..2bc366f --- /dev/null +++ b/CanonSharp.Parser/LexicalAnalyzer/RegularExpression.fs @@ -0,0 +1,12 @@ +module CanonSharp.Parser.LexicalAnalyzer.RegularExpression + +type RegularExpression = + | EmptyExpression + | SymbolExpression of symbol: char + | AlternationExpression of left: RegularExpression * right : RegularExpression + | ConcatenationExpression of first: RegularExpression * second : RegularExpression + | KleeneExpression of expression: RegularExpression + +let convertToNondeterministicFiniteAutomation expression = + match expression with + | EmptyExpression emptyExpression ->