Exemple #1
0
        public string ReadFile(string line)
        {
            bool empty = false;

            if (line.Contains("alphabet"))
            {
                //Get the alphabet
                Alpha = GetWord(line);
            }
            else if (line.Contains("stack"))
            {
                Stack = GetWord(line);
            }
            else if (line.Contains("states"))
            {
                string        s   = GetWord(line);
                List <string> sta = new List <string>();
                sta = s.Split(',').ToList <string>();
                foreach (string sa in sta)
                {
                    int i = 0;
                    if (i == 0)
                    {
                        state.Add(new State(sa, false, true));
                    }
                    else
                    {
                        state.Add(new State(sa, false, false));
                    }
                    i++;
                }
            }
            else if (line.Contains("final"))
            {
                string        f     = GetWord(line);
                List <string> final = new List <string>();
                final = f.Split(',').ToList <string>();

                foreach (string fi in final)
                {
                    foreach (State i in state)
                    {
                        if (fi == i.Stat)
                        {
                            i.Final = true;
                        }
                    }
                }
            }
            else if (line.Contains("transitions"))
            {
                trans = true;
                if (Alpha == "")
                {
                    empty = true;
                }
            }
            else if (line.Contains("dfa"))
            {
                bool   b = false;
                string f = GetWord(line);
                if (f == "y")
                {
                    b = true;
                }
                if (b == CheckDFA())
                {
                    return("\tRight");
                }
                else
                {
                    return("\tWrong");
                }
            }
            else if (line.Contains("words"))
            {
                word = true;
            }
            else if (line.Contains("end."))
            {
                trans = false;
                word  = false;
                Alpha = new String(Alpha.Distinct().ToArray());
                //foreach (Transmission t in transmissions)
                //{
                //    Console.WriteLine(t.In.Stat + "," + t.Value + " --> " + t.Out.Stat);
                //}
            }
            else if (trans)
            {
                string stackremove = "";
                string stackadd    = "";
                if (line.Contains("["))
                {
                    string stackItems;
                    int    Start = line.IndexOf('[');
                    int    End   = line.IndexOf(']');
                    stackItems  = line.Substring(Start + 1, End - Start - 1);
                    stackremove = stackItems.Substring(0, 1);
                    stackadd    = stackItems.Substring(2);
                    line        = line.Replace("[" + stackItems + "]", "");
                }
                line = line.Replace("-->", ",");
                if (line != "")
                {
                    List <string> s = line.Split(',').ToList();
                    Regex         r = new Regex("^[a-zA-Z&]*$");
                    if (r.IsMatch(s[1]))
                    {
                        State sin  = null;
                        State sout = null;
                        foreach (State ss in state)
                        {
                            if (ss.Stat == s[0])
                            {
                                sin = ss;
                            }
                            if (ss.Stat == s[2])
                            {
                                sout = ss;
                            }
                        }
                        Transmission t = new Transmission(sin, sout, s[1]);
                        if (stackadd != "" && stackremove != "")
                        {
                            t.Sremove = stackremove;
                            t.Sadd    = stackadd;
                        }
                        transmissions.Add(t);
                        foreach (State ss in state)
                        {
                            if (ss.Stat == s[0])
                            {
                                ss.outgoing.Add(t);
                            }
                            if (ss.Stat == s[2])
                            {
                                ss.incoming.Add(t);
                            }
                        }
                        if (empty && s[1] != "&")
                        {
                            Alpha += s[1];
                        }
                    }
                }
            }
            else if (word)
            {
                List <string> s = line.Split(',').ToList();
                if (CheckWord(s[0], s[1]))
                {
                    return("\tRight");
                }
                else
                {
                    return("\tWrong");
                }
            }
            return("");
        }
Exemple #2
0
        private void CreateTransition(int startIndex, State endState, char letter = '_')
        {
            var transition = new Transmission(States[startIndex - 1], endState, letter.ToString());

            States[startIndex - 1].outgoing.Add(transition);
        }