Exemple #1
0
        public string TestWordAccaptence(char[] word, bool testOutcome, bool automataIsDfa)
        {
            bool accepted = false;

            if (this.isPDA())
            {
                accepted = wordAcceptancePDA(word);
            }
            else if (automataIsDfa)
            {
                Node currentNode = automata.nodes[0];

                for (int i = 0; i < word.Length; i++)
                {
                    Connection thisConn = new Connection('q', null);

                    foreach (Connection conn in currentNode.Connections)
                    {
                        if (conn.Symbol == word[i])
                        {
                            thisConn = conn;
                        }
                    }

                    currentNode = thisConn.ToNode;
                }
                if (currentNode.Final)
                {
                    accepted = true;
                }
            }
            else
            {
                DFAnode currentNode = dFAautomata.dfaNodes[0];

                for (int i = 0; i < word.Length; i++)
                {
                    DFAconnection thisConn = new DFAconnection('q');

                    foreach (DFAconnection conn in currentNode.Connections)
                    {
                        if (conn.Symbol == word[i])
                        {
                            thisConn = conn;
                        }
                    }

                    currentNode = thisConn.ToNode;
                }
                if (currentNode.Final)
                {
                    accepted = true;
                }
            }

            return($"{new string(word)},{(testOutcome ? 'y' : 'n')} == {accepted == testOutcome}");
        }
Exemple #2
0
        public void ConvertNFAtoDFA()
        {
            List <SymbolConnection> symbolConnections = new List <SymbolConnection>();
            Node sinkNode = new Node(false, "SINK");

            GetAllTransitionsNFA(symbolConnections, sinkNode);

            dFAautomata          = new DFAautomata();
            dFAautomata.alphabet = new List <char>(automata.alphabet);

            //starting node
            List <Node> startingNode = new List <Node>()
            {
                automata.nodes[0]
            };

            for (int i = 0; i < startingNode.Count; i++)
            {
                foreach (SymbolConnection symbConn in symbolConnections)
                {
                    if (symbConn.symbol == '_' && symbConn.startingNode == automata.nodes[0])
                    {
                        foreach (Node node in symbConn.toPossibleNodes)
                        {
                            if (!startingNode.Contains(node))
                            {
                                startingNode.Add(node);
                            }
                        }
                    }
                }
            }

            dFAautomata.dfaNodes.Add(new DFAnode(startingNode.OrderBy(a => a.Name).ToList()));
            dFAautomata.dfaNodes[0].Starting = true;
            dFAautomata.dfaNodes[0].CreateName();


            for (int i = 0; i < dFAautomata.dfaNodes.Count; i++)
            {
                foreach (char symbol in dFAautomata.alphabet)
                {
                    List <Node>   newNode       = new List <Node>();
                    DFAconnection newConnection = new DFAconnection(symbol);

                    foreach (Node node in dFAautomata.dfaNodes[i].nodesTogether)
                    {
                        foreach (SymbolConnection symbConn in symbolConnections)
                        {
                            if (node == symbConn.startingNode && symbConn.symbol == symbol && !newNode.Contains(node))
                            {
                                foreach (Node toNode in symbConn.toPossibleNodes)
                                {
                                    if (!newNode.Contains(toNode))
                                    {
                                        newNode.Add(toNode);
                                    }
                                }
                            }
                        }
                    }

                    if (newNode.Contains(sinkNode) && newNode.Count > 1)
                    {
                        newNode.Remove(sinkNode);
                    }

                    foreach (Node node in newNode)
                    {
                        foreach (SymbolConnection symbConn in symbolConnections)
                        {
                            if (node == symbConn.startingNode && symbConn.symbol == '_' && !newNode.Contains(node))
                            {
                                foreach (Node toNode in symbConn.toPossibleNodes)
                                {
                                    if (!newNode.Contains(toNode))
                                    {
                                        newNode.Add(toNode);
                                    }
                                }
                            }
                        }
                    }

                    DFAnode tempNode   = new DFAnode(newNode.OrderBy(a => a.Name).ToList());
                    bool    nodeExists = false;
                    foreach (DFAnode node in dFAautomata.dfaNodes)
                    {
                        if (node.Name.Equals(tempNode.Name))
                        {
                            newConnection.ToNode = node;
                            dFAautomata.dfaNodes[i].Connections.Add(newConnection);
                            nodeExists = true;
                        }
                    }
                    if (!nodeExists)
                    {
                        newConnection.ToNode = tempNode;
                        dFAautomata.dfaNodes[i].Connections.Add(newConnection);
                        dFAautomata.dfaNodes.Add(tempNode);
                    }
                }
            }

            List <Node> acceptingNodes = new List <Node>();

            foreach (Node node in automata.nodes)
            {
                if (node.Final)
                {
                    acceptingNodes.Add(node);
                }
            }

            foreach (DFAnode node in dFAautomata.dfaNodes)
            {
                foreach (Node finalNode in acceptingNodes)
                {
                    if (node.nodesTogether.Contains(finalNode))
                    {
                        node.Final = true;
                    }
                }
            }
        }