Esempio n. 1
0
 private void ProcessDFA(List <State> curr_states, List <State> newStates, List <Transition> transitions, State Sink, State current_State)
 {
     foreach (Alphabet a in ListAlphabets)
     {
         List <State> reachableStateByA = new List <State>();
         foreach (State s in curr_states)
         {
             List <Transition> processedtransitions = new List <Transition>();
             List <State>      value;
             if (s.ReachableState().TryGetValue(a, out value))
             {
                 reachableStateByA.AddRange(value);
             }
             // for epsilon move
             foreach (State s1 in value)
             {
                 reachableStateByA.AddRange(s1.GetEpsilonClosure(ListTransitions, processedtransitions).Except(reachableStateByA));
             }
         }
         reachableStateByA = reachableStateByA.Distinct().ToList();
         string state_name = $"{String.Join("", reachableStateByA)}";
         State  next_state;
         if (state_name != "")
         {
             next_state = CheckExistState(state_name, newStates);
             if (next_state == null)
             {
                 next_state = new State(state_name);
                 if (reachableStateByA.Exists(x => x.IsFinal))
                 {
                     next_state.IsFinal = true;
                 }
                 newStates.Add(next_state);
             }
         }
         else
         {
             next_state = CheckExistState("Sink", newStates);
             if (next_state == null)
             {
                 next_state = Sink;
                 newStates.Add(next_state);
             }
         }
         Transition t = new Transition(a.Character, null);
         t.SetLeftState(current_State);
         t.SetRightState(next_state);
         if (!transitions.Exists(x => x.ToString() == t.ToString()))
         {
             transitions.Add(t);
         }
         else
         {
             return;
         }
         ProcessDFA(reachableStateByA, newStates, transitions, Sink, next_state);
     }
 }
Esempio n. 2
0
        public override List <Transition> GetAutomaton(ref int i, ref List <Transition> ListTransitions, ref List <State> ListStates, ref List <Alphabet> ListAlphabets)
        {
            List <Transition> transitions_got_by_parsed_repetition = new List <Transition>();
            State             initial = new State($"S{i}");

            initial.IsInitial = true;
            i++;
            ListStates.Add(initial);
            List <Transition> transitions_got_by_parsing_leftexpression = Left_Expression.GetAutomaton(ref i, ref ListTransitions, ref ListStates, ref ListAlphabets);
            State             final = new State($"S{i}");

            final.IsFinal = true;
            i++;
            ListStates.Add(final);
            State left_left_state  = transitions_got_by_parsing_leftexpression.Find(x => x.GetLeftState().IsInitial).GetLeftState();
            State right_left_state = transitions_got_by_parsing_leftexpression.Find(x => x.GetRightState().IsFinal).GetRightState();

            left_left_state.IsInitial = false;
            right_left_state.IsFinal  = false;

            Transition transition1 = new Transition("_", null);

            transition1.SetLeftState(initial);
            transition1.SetRightState(left_left_state);
            transitions_got_by_parsed_repetition.Add(transition1);
            ListTransitions.Add(transition1);

            Transition transition2 = new Transition("_", null);

            transition2.SetLeftState(initial);
            transition2.SetRightState(final);
            transitions_got_by_parsed_repetition.Add(transition2);
            ListTransitions.Add(transition2);

            Transition transition3 = new Transition("_", null);

            transition3.SetLeftState(right_left_state);
            transition3.SetRightState(left_left_state);
            transitions_got_by_parsed_repetition.Add(transition3);
            ListTransitions.Add(transition3);

            transitions_got_by_parsed_repetition.AddRange(transitions_got_by_parsing_leftexpression);

            Transition transition4 = new Transition("_", null);

            transition4.SetLeftState(right_left_state);
            transition4.SetRightState(final);
            transitions_got_by_parsed_repetition.Add(transition4);
            ListTransitions.Add(transition4);

            return(transitions_got_by_parsed_repetition);
        }
Esempio n. 3
0
        public override List <Transition> GetAutomaton(ref int i, ref List <Transition> ListTransitions, ref List <State> ListStates, ref List <Alphabet> ListAlphabets)
        {
            List <Transition> transitions_got_by_parse_alphabet = new List <Transition>();
            State             initial = new State($"S{i}");
            State             final   = new State($"S{i+1}");

            i += 2;
            initial.IsInitial = true;
            final.IsFinal     = true;
            Transition transition = new Transition($"_", null);

            transition.SetLeftState(initial);
            transition.SetRightState(final);
            transitions_got_by_parse_alphabet.Add(transition);
            ListTransitions.Add(transition);
            ListStates.Add(initial);
            ListStates.Add(final);
            return(transitions_got_by_parse_alphabet);
        }
Esempio n. 4
0
        public void ParsingFile(string fileName)
        {
            listStates      = new List <State>();
            listAlphabets   = new List <Alphabet>();
            listTransitions = new List <Transition>();
            listWords       = new List <Word>();
            listStacks      = new List <Stack>();
            FileStream   fs = null;
            StreamReader sr = null;

            try
            {
                fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
                sr = new StreamReader(fs);
                string line       = sr.ReadLine();
                int    count_line = 1;
                while (line != null)
                {
                    string s = line.Replace(" ", "");
                    if (s != "" && s[0] != '#')
                    {
                        string first_word = s.Substring(0, s.IndexOf(':'));
                        if (first_word == "alphabet")
                        {
                            string the_rest = s.Substring(s.IndexOf(':') + 1);
                            if (the_rest == "")
                            {
                                throw new InvalidValueInFileException($"Invalid value at line {count_line}");
                            }
                            foreach (char character in the_rest)
                            {
                                Alphabet alphabet = CheckExistAlphabet(character.ToString(), listAlphabets);
                                if (alphabet == null)
                                {
                                    alphabet = new Alphabet(character.ToString());
                                    listAlphabets.Add(alphabet);
                                }
                            }
                        }
                        if (first_word == "stack")
                        {
                            string the_rest = s.Substring(s.IndexOf(':') + 1);
                            if (the_rest == "")
                            {
                                throw new InvalidValueInFileException($"Invalid value at line {count_line}");
                            }
                            foreach (char character in the_rest)
                            {
                                Stack stack = CheckExistStack(character.ToString(), listStacks);
                                if (stack == null)
                                {
                                    stack = new Stack(character.ToString());
                                    listStacks.Add(stack);
                                }
                            }
                        }
                        if (first_word == "states")
                        {
                            string the_rest = s.Substring(s.LastIndexOf(':') + 1);
                            if (the_rest == "")
                            {
                                throw new InvalidValueInFileException($"Invalid value at line {count_line}");
                            }
                            string[] state_names = the_rest.Split(',');
                            foreach (string name in state_names)
                            {
                                State state = CheckExistState(name, listStates);
                                if (state == null)
                                {
                                    state = new State(name);
                                    listStates.Add(state);
                                }
                            }
                            listStates[0].IsInitial = true;
                        }
                        if (first_word == "final")
                        {
                            string the_rest = s.Substring(s.LastIndexOf(':') + 1);
                            if (the_rest == "")
                            {
                                throw new InvalidValueInFileException($"Invalid value at line {count_line}");
                            }
                            string[] state_names = the_rest.Split(',');
                            foreach (string name in state_names)
                            {
                                foreach (State state in listStates)
                                {
                                    if (state.State_Name == name)
                                    {
                                        state.IsFinal = true;
                                    }
                                }
                            }
                        }
                        if (first_word == "transitions")
                        {
                            line = sr.ReadLine();
                            count_line++;
                            while (line != "end.")
                            {
                                s = line.Replace(" ", "");
                                string state_and_symbol = s.Substring(0, s.IndexOf('-'));
                                string left_state       = state_and_symbol.Substring(0, state_and_symbol.IndexOf(','));
                                string symbol           = state_and_symbol.Substring(state_and_symbol.IndexOf(',') + 1);
                                string right_state      = s.Substring(s.IndexOf('>') + 1);
                                if (left_state == "" || symbol == "" || right_state == "")
                                {
                                    throw new InvalidValueInFileException($"Invalid transition at line {count_line}");
                                }
                                if (IsIncorrectCharacterInLabeledTransition(symbol, listAlphabets, listStacks))
                                {
                                    throw new InvalidValueInFileException($"Invalid transition at line {count_line}");
                                }
                                Transition transition = new Transition(symbol, listStacks);
                                State      Left       = CheckExistState(left_state, listStates);
                                State      Right      = CheckExistState(right_state, listStates);
                                if (Left != null && Right != null)
                                {
                                    transition.SetLeftState(Left);
                                    transition.SetRightState(Right);
                                }
                                listTransitions.Add(transition);
                                line = sr.ReadLine();
                                count_line++;
                            }
                        }
                        if (first_word == "dfa")
                        {
                            string the_rest = s.Substring(s.LastIndexOf(':') + 1);
                            if (the_rest != "n" && the_rest != "y")
                            {
                                throw new InvalidValueInFileException($"Invalid value at line {count_line}");
                            }
                        }
                        if (first_word == "finite")
                        {
                            string the_rest = s.Substring(s.LastIndexOf(':') + 1);
                            if (the_rest != "n" && the_rest != "y")
                            {
                                throw new InvalidValueInFileException($"Invalid value at line {count_line}");
                            }
                        }
                        if (first_word == "words")
                        {
                            line = sr.ReadLine();
                            count_line++;
                            while (line != "end.")
                            {
                                s = line.Replace(" ", "");
                                string[] data       = s.Split(',');
                                string   words      = data[0];
                                string   indication = data[1];
                                if (DoesWordContainIncorrectCharacter(words, listAlphabets))
                                {
                                    throw new InvalidValueInFileException($"Invalid value at line {count_line}");
                                }
                                Word word = new Word(words);
                                if (indication != "y" && indication != "n")
                                {
                                    throw new InvalidValueInFileException($"Invalid value at line {count_line}");
                                }
                                listWords.Add(word);
                                line = sr.ReadLine();
                                count_line++;
                            }
                        }
                    }
                    else if (s != "" && s[0] == '#')
                    {
                        comment = s.Substring(1);
                    }
                    line = sr.ReadLine();
                    count_line++;
                }
                Automaton         = new Automaton(listAlphabets, listStates, listTransitions, listWords, listStacks);
                Automaton.Comment = comment;
            }
            catch (IOException)
            {
                throw new IOException();
            }
            finally
            {
                if (sr != null)
                {
                    sr.Close();
                }
                if (fs != null)
                {
                    fs.Close();
                }
            }
        }