예제 #1
0
파일: Program.cs 프로젝트: RabbitRu/cc_lab1
        static void Main(string[] args)
        {
            var x = "(a|b)*.a.b.b#";

            x = Console.ReadLine();
            var  l = new TreeBuilder();
            Node n = l.StartParse(x);

            Console.Write("Total pos:" + l.pos + "\n");
            var aaa               = TreeOps.Lastpos(n.Left);
            var bbb               = TreeOps.Firstpos(n.Left);
            var ccc               = TreeOps.Nullable(n);
            var followpos         = TreeOps.Followpos(n, l.pos);
            var inputDictionary01 = new[] { '0', '1' };
            var DFSA              = DFSABuilder.Build(n, l.pos);
            {
                //((string poses, bool ending)[] DStates, Dictionary<string, Dictionary<char, string>> DTrans) testDFSA;
                //(string poses, bool ending)[] states = new[]
                //{
                //    ("A", false),//a
                //    ("B", false),//b
                //    ("C", false),//c
                //    ("D", false),//d
                //    ("E", false),//e
                //    ("F", true),//f
                //    ("G", true),//g
                //    ("H", false)//h
                //};
                //Dictionary<string, Dictionary<char, string>> trans = new Dictionary<string, Dictionary<char, string>>
                //{
                //    {"A", new Dictionary<char, string>
                //        {
                //            {'0', "H"},
                //            {'1', "B"}
                //        }
                //    },
                //    {"B", new Dictionary<char, string>
                //        {
                //            {'0', "H"},
                //            {'1', "A"}
                //        }
                //    },
                //    {"C", new Dictionary<char, string>
                //        {
                //            {'0', "E"},
                //            {'1', "F"}
                //        }
                //    },
                //    {"D", new Dictionary<char, string>
                //        {
                //            {'0', "E"},
                //            {'1', "F"}
                //        }
                //    },
                //    {"E", new Dictionary<char, string>
                //        {
                //            {'0', "F"},
                //            {'1', "G"}
                //        }
                //    },
                //    {"F", new Dictionary<char, string>
                //        {
                //            {'0', "F"},
                //            {'1', "F"}
                //        }
                //    },
                //    {"G", new Dictionary<char, string>
                //        {
                //            {'0', "G"},
                //            {'1', "F"}
                //        }
                //    },
                //    {"H", new Dictionary<char, string>
                //        {
                //            {'0', "C"},
                //            {'1', "C"}
                //        }
                //    }
                //};
                //testDFSA.DStates = states;
                //testDFSA.DTrans = trans;
            }
            var    minDfsa      = DFSAMinimizer.Minimize(DFSA.States, DFSA.Trans, DFSA.InputDict);
            var    model        = new DFSAModel(minDfsa.DStates, minDfsa.DTrans);
            string consoleInput = "";

            while (consoleInput != "end")
            {
                consoleInput = Console.ReadLine();
                var answer = model.Model(consoleInput);
                Console.WriteLine(answer + "\n");
            }
            Console.Write("That's All Folks");
        }
예제 #2
0
        Build(Node root, int maxPos)
        {
            List <(List <int> poses, bool marked)> DStates = new List <(List <int>, bool marked)>();
            Dictionary <List <int>, Dictionary <char, List <int> > > DTrans =
                new Dictionary <List <int>, Dictionary <char, List <int> > >();
            int endingPos = -1;

            DStates.Add((TreeOps.Firstpos(root), false));
            DTrans.Add(DStates.First().poses, new Dictionary <char, List <int> >());
            List <Node> poses           = new List <Node>();
            List <char> inputDictionary = new List <char>();

            for (int i = 1; i <= maxPos; i++)
            {
                poses.Add(TreeOps.TreePosSearch(root, i));
                if (poses.Last().Value == '#')
                {
                    endingPos = i;
                }
                else
                {
                    inputDictionary.Add(poses.Last().Value);
                }
            }

            inputDictionary = inputDictionary.Distinct().ToList();
            var fp = TreeOps.Followpos(root, maxPos);

            while (DStates.Any(x => x.marked == false))
            {
                var S = DStates.First(x => x.marked == false);
                DStates.Remove(S);
                S.marked = true;
                DStates.Add(S);
                foreach (var letter in inputDictionary)
                {
                    var        matchingPoses = poses.Where(x => x.Value == letter && S.poses.Any(y => y == x.Pos));
                    List <int> U             = new List <int>();
                    foreach (var pos in matchingPoses)
                    {
                        U.AddRange(fp[pos.Pos]);
                    }

                    U = U.Distinct().OrderBy(i => i).ToList();
                    if (!DStates.Any(x => x.poses.SequenceEqual(U)))
                    {
                        DStates.Add((U, false));
                        DTrans.Add(U, new Dictionary <char, List <int> >());
                    }

                    DTrans[S.poses].Add(letter, U);
                }
            }

            List <(string poses, bool ending)> DStatesWOMarks = new List <(string poses, bool ending)>();

            foreach (var state in DStates)
            {
                DStatesWOMarks.Add((Helpers.ArrayToString(state.poses), state.poses.Contains(endingPos)));
            }

            Dictionary <string, Dictionary <char, string> > DTransArray =
                new Dictionary <string, Dictionary <char, string> >();

            foreach (var trans in DTrans)
            {
                Dictionary <char, string> insides = new Dictionary <char, string>();
                foreach (var letter in trans.Value)
                {
                    insides.Add(letter.Key, Helpers.ArrayToString(letter.Value));
                }

                DTransArray.Add(Helpers.ArrayToString(trans.Key), insides);
            }

            return(DStatesWOMarks.ToArray(), DTransArray, inputDictionary.ToArray());
        }