public NFA RemoveEpsilonPasses() { var nodes = GetAllNodes().Select(n => n as NodeNFA); var noEpsilonNodes = new List <NodeNFA>(); foreach (var node in nodes) { noEpsilonNodes.Add(node.RemoveEpsilonPasses(Alphabet)); } foreach (var node in noEpsilonNodes) { foreach (var symbol in Alphabet) { var newAssociationReferences = new List <NodeNFA>(); foreach (var oldAssociatedNode in node.Associations[symbol]) { newAssociationReferences.Add(noEpsilonNodes.Where(x => x.Name == oldAssociatedNode.Name).Single()); } node.Associations[symbol] = newAssociationReferences.OrderBy(n => n.Name); } } var startingNode = noEpsilonNodes.Where(n => n.Name == StartingNode.Name).Single(); var newNfa = new NFA(Alphabet, startingNode); return(newNfa); }
public static void Main(string[] args) { NodeNFA q1 = new NodeNFA(nameof(q1), false); NodeNFA q2 = new NodeNFA(nameof(q2), true); NodeNFA q3 = new NodeNFA(nameof(q3), false); string[] alphabet = { "a", "b" }; q1.Build(alphabet, new[] { q2, q3 }, null); q2.Build(alphabet, null, new[] { q2, q3 }, new[] { q1 }); q3.Build(alphabet, new[] { q2 }, null); var automata = new NFA(alphabet, q1); automata.Print(); Console.WriteLine(); automata.RemoveEpsilonPasses().Print(); Console.WriteLine(); automata.ToDFA().Print(); //automata.PrintIsAccepted("ababb"); }