Exemple #1
0
        private void tsbToPng_Click(object sender, EventArgs e)
        {
            NDFA <string, char> ndfa = getNDFA();

            if (ndfa == null)
            {
                return;
            }

            SaveFileDialog dialog = new SaveFileDialog();

            dialog.AddExtension    = true;
            dialog.DefaultExt      = "png";
            dialog.Filter          = ".png|*.png";
            dialog.OverwritePrompt = true;
            dialog.ValidateNames   = true;
            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                ndfa.MakePngFile(dialog.FileName);
                FrmImage image = new FrmImage();
                image.SetPicture(dialog.FileName);
                image.MdiParent = this.MdiParent;
                image.Show();
            }
        }
        public static NDFA <string> ReverseDFA(DFA <string> dfa)
        {
            NDFA <string> ndfa = new NDFA <string>();

            ndfa.symbols = dfa.symbols;
            ndfa.states  = dfa.states;

            foreach (var t in dfa.finalStates)
            {
                ndfa.addStartState(t);
            }

            ndfa.addFinalState(dfa.startState);
            ndfa.transitions = dfa.transitions;

            foreach (Transition <string> t in ndfa.transitions)
            {
                string fromState = t.fromState;
                string toState   = t.toState;

                t.fromState = toState;
                t.toState   = fromState;
            }

            return(ndfa);
        }
Exemple #3
0
        private void tsbVerifyMachine_Click(object sender, EventArgs e)
        {
            NDFA <string, char> ndfa = getNDFA();

            if (ndfa == null)
            {
                return;
            }
        }
Exemple #4
0
        private NDFA <string, char> getNDFA()
        {
            NDFA <string, char> ndfa = new NDFA <string, char>(this.alphabetSource.GetTableArray(), 'e');

            foreach (DataGridViewRow dataRow in this.dgvTransitions.Rows)
            {
                if (dataRow.Cells[2].Value == null || dataRow.Cells[2].Value.ToString() == "")
                {
                    continue;
                }

                string from   = dataRow.Cells[0].Value.ToString();
                string to     = dataRow.Cells[1].Value.ToString();
                char   symbol = dataRow.Cells[2].Value.ToString().First();
                ndfa.AddTransition(from, to, symbol);
            }

            foreach (DataGridViewRow dataRow in this.dgvStates.Rows)
            {
                if (dataRow.Cells[0].Value == null || dataRow.Cells[0].Value.ToString() == "")
                {
                    continue;
                }

                string name       = dataRow.Cells[0].Value.ToString();
                bool   startState = (((dataRow.Cells[1]).Value == null) ? false : (bool)(dataRow.Cells[1]).Value);
                bool   endState   = (((dataRow.Cells[2]).Value == null) ? false : (bool)(dataRow.Cells[2]).Value);

                if (startState)
                {
                    ndfa.StartState = name;
                }
                if (endState)
                {
                    ndfa.EndStates.Add(name);
                }
            }

            if (!ndfa.IsMachineValid())
            {
                MessageBox.Show("Machine is not valid", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return(null);
            }

            tsbToDFA.Enabled          = true;
            tsbToPng.Enabled          = true;
            tsbToRegram.Enabled       = true;
            tsbVerifyLanguage.Enabled = true;
            return(ndfa);
        }
        public NDFA <T> TransformToNDFA()
        {
            NDFA <T> ndfa = new NDFA <T>();

            ndfa.invoerSymbolen = alfabet;
            foreach (KeyValuePair <string, HashSet <ProductieRegel <T> > > toestandEnOvergangen in productieRegels)
            {
                foreach (ProductieRegel <T> t in toestandEnOvergangen.Value)
                {
                    Toestand <T> toestand = new Toestand <T>(toestandEnOvergangen.Key, new Tuple <string, T>(t.rechterkant, t.x));
                    ndfa.toestanden.Add(toestand);
                }
            }
            return(ndfa);
        }
Exemple #6
0
        private void tsbToNDFA_Click(object sender, EventArgs e)
        {
            Regram <string, char> regram = getRegram();

            if (regram == null)
            {
                return;
            }

            NDFA <string, char> ndfa = SearchAlgorithmParser.Converter <string, char> .ConvertToNDFA(regram, 'e');

            FrmNDFA frmNdfa = new FrmNDFA(ndfa);

            frmNdfa.MdiParent = this.MdiParent;
            frmNdfa.Show();
        }
Exemple #7
0
        private void tsbToRegram_Click(object sender, EventArgs e)
        {
            NDFA <string, char> ndfa = getNDFA();

            if (ndfa == null)
            {
                return;
            }

            Regram <MultiState <string>, char> regram = SearchAlgorithmParser.Converter <string, char> .ConvertToRegram(ndfa, new SearchAlgorithmParser.MultiStateViewConcat <string>("", "O"));

            FrmRegularGrammar frmRegram = new FrmRegularGrammar(regram);

            frmRegram.MdiParent = this.MdiParent;
            frmRegram.Show();
        }
Exemple #8
0
        private void tsbToDFA_Click(object sender, EventArgs e)
        {
            NDFA <string, char> ndfa = getNDFA();

            if (ndfa == null)
            {
                return;
            }

            DFA <MultiState <string>, char> dfa = SearchAlgorithmParser.Converter <string, char> .ConvertToDFA(ndfa, new SearchAlgorithmParser.MultiStateViewConcat <string>("", "O"));

            FrmDFA frmDfa = new FrmDFA(dfa);

            frmDfa.MdiParent = this.MdiParent;
            frmDfa.Show();
        }
        public static DFA <string> Minimize(DFA <string> dfa)
        {
            NDFA <string> step1 = ReverseDFA(dfa);
            DFA <string>  step2 = NDFA <string> .toDFA(step1);

            step1 = ReverseDFA(step2);

            //Renaming states:
            var renameStates = new Dictionary <string, string>();
            var newNDFA      = new NDFA <string>();
            int cnt          = 0;

            newNDFA.symbols = step1.symbols;

            foreach (var state in new HashSet <string>(step1.states.Where(x => x != "")))
            {
                renameStates.Add(state, cnt.ToString());
                cnt++;
            }

            foreach (var transition in step1.transitions)
            {
                newNDFA.addTransition(new Transition <string>(renameStates[transition.fromState], transition.symbol, transition.toState != "" ? renameStates[transition.toState] : ""));
            }

            foreach (var state in step1.startStates)
            {
                if (!state.Equals(""))
                {
                    newNDFA.addStartState(renameStates[state]);
                }
            }

            foreach (var state in step1.finalStates)
            {
                if (!state.Equals(""))
                {
                    newNDFA.addFinalState(renameStates[state]);
                }
            }

            step2 = NDFA <string> .toDFA(newNDFA);

            return(step2);
        }
Exemple #10
0
        private void tsbVerifyLanguage_Click(object sender, EventArgs e)
        {
            NDFA <string, char> ndfa = getNDFA();

            if (ndfa == null)
            {
                return;
            }
            string value = Prompt.ShowDialog("Fill in your text", "Validate language");

            if (ndfa.Validate(value.ToArray()))
            {
                MessageBox.Show("This string is valid", "Success", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
            else
            {
                MessageBox.Show("This string is invalid", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
 public abstract void Convert(iStateCreater <T> creater, T beginState, ref T endState, NDFA <T, char> grammar);
Exemple #12
0
        public override void Convert(iStateCreater <T> creater, T beginState, ref T endState, NDFA <T, char> grammar)
        {
            T leftEndState = default(T);

            left.Convert(creater, beginState, ref leftEndState, grammar);
            right.Convert(creater, leftEndState, ref endState, grammar);
        }
        public override void Convert(iStateCreater <T> creater, T beginState, ref T endState, NDFA <T, char> ndfa)
        {
            T choiceOneBeginState = creater.Next();
            T choiceTwoBeginState = creater.Next();
            T choiceOneEndState   = default(T);
            T choiceTwoEndState   = default(T);

            //Create empty epsilon from begin to begin of the choices
            ndfa.AddTransition(beginState, choiceOneBeginState, ndfa.Epsilon);
            ndfa.AddTransition(beginState, choiceTwoBeginState, ndfa.Epsilon);

            choiceOne.Convert(creater, choiceOneBeginState, ref choiceOneEndState, ndfa);
            choiceTwo.Convert(creater, choiceTwoBeginState, ref choiceTwoEndState, ndfa);

            //Create empty epsilon from end of the coices to end state
            endState = creater.Next();
            ndfa.AddTransition(choiceOneEndState, endState, ndfa.Epsilon);
            ndfa.AddTransition(choiceTwoEndState, endState, ndfa.Epsilon);
        }
        public NDFA <char> ToNDFA()
        {
            NDFA <char> ndfa        = new NDFA <char>();
            bool        orOperation = false;
            Stack <Tuple <int, Toestand <char> > > bracketLocations = new Stack <Tuple <int, Toestand <char> > >();
            Stack <Toestand <char> > stuffInBrackets = new Stack <Toestand <char> >();
            int index = 0;

            foreach (char c in terminals)
            {
                Toestand <char> t = ndfa.toestanden.LastOrDefault(y => y.volgendeToestand.Item2 != EPSILON);
                if (t == null)
                {
                    t = new Toestand <char>("0", new Tuple <string, char>("0", EPSILON));
                }
                switch (c)
                {
                case '(':
                    bracketLocations.Push(new Tuple <int, Toestand <char> >(index, t));
                    break;

                case ')':
                    var             indexLastBracket = bracketLocations.Pop();
                    Toestand <char> p = new Toestand <char>(t.volgendeToestand.Item1, new Tuple <string, char>(indexLastBracket.Item2.naam, EPSILON));
                    stuffInBrackets.Push(p);
                    break;

                case '*':
                    if (stuffInBrackets.Count == 0)
                    {
                        //epsilon van vorige naar nieuwste
                        Toestand <char> ts = new Toestand <char>(t.naam, new Tuple <string, char>(t.volgendeToestand.Item1, EPSILON));
                        //epsilon van nieuwste naar vorige
                        Toestand <char> t3 = new Toestand <char>(t.volgendeToestand.Item1, new Tuple <string, char>(t.naam, EPSILON));
                        ndfa.toestanden.Add(ts);
                        ndfa.toestanden.Add(t3);
                    }
                    else
                    {
                        var t5 = stuffInBrackets.Pop();
                        ndfa.toestanden.Add(t5);
                        ndfa.toestanden.Add(new Toestand <char>(t5.volgendeToestand.Item1, new Tuple <string, char>(t5.naam, EPSILON)));
                    }
                    break;

                case '+':
                    if (stuffInBrackets.Count == 0)
                    {
                        //epsilon van nieuwste naar vorige
                        Toestand <char> t4 = new Toestand <char>(t.volgendeToestand.Item1, new Tuple <string, char>(t.naam, EPSILON));
                        ndfa.toestanden.Add(t4);
                    }
                    else
                    {
                        var t6 = stuffInBrackets.Pop();
                        ndfa.toestanden.Add(t6);
                    }
                    break;

                case '|':
                    orOperation = true;
                    break;

                default:
                    if (orOperation)
                    {
                        ndfa.toestanden.Add(new Toestand <char>(t.naam, new Tuple <string, char>(t.volgendeToestand.Item1, c)));
                    }
                    else
                    {
                        if (ndfa.toestanden.Count == 0)
                        {
                            ndfa.toestanden.Add(new Toestand <char>("0", new Tuple <string, char>((ndfa.toestanden.Count + 1).ToString(), c)));
                        }
                        else
                        {
                            ndfa.toestanden.Add(new Toestand <char>(t.volgendeToestand.Item1, new Tuple <string, char>((ndfa.toestanden.Count + 1).ToString(), c)));
                        }
                    }

                    orOperation = false;
                    break;
                }
                index++;
            }
            ndfa.startSymbolen.Add(ndfa.toestanden.First().naam);
            int eindtoestand = 0;

            foreach (var r in ndfa.toestanden)
            {
                if (eindtoestand < int.Parse(r.volgendeToestand.Item1))
                {
                    eindtoestand = int.Parse(r.volgendeToestand.Item1);
                }
                if (!r.volgendeToestand.Item2.Equals(EPSILON))
                {
                    ndfa.invoerSymbolen.Add(r.volgendeToestand.Item2);
                }
            }
            ndfa.eindToestanden.Add(eindtoestand.ToString());
            return(ndfa);
        }
Exemple #15
0
        public override void Convert(iStateCreater <T> creater, T beginState, ref T endState, NDFA <T, char> ndfa)
        {
            endState = creater.Next();

            if (loopMin == 0)
            {
                ndfa.AddTransition(beginState, endState, ndfa.Epsilon);
            }

            T beginLoopState = creater.Next();
            T endLoopState   = default(T);

            this.block.Convert(creater, beginLoopState, ref endLoopState, ndfa);

            //Epsilon from start to start loop
            ndfa.AddTransition(beginState, beginLoopState, ndfa.Epsilon);
            //Epsilon from end loop to start loop
            ndfa.AddTransition(endLoopState, beginLoopState, ndfa.Epsilon);
            //Epsilon from end loop to end state
            ndfa.AddTransition(endLoopState, endState, ndfa.Epsilon);
        }
Exemple #16
0
        public FrmNDFA(NDFA <string, char> ndfa) : this()
        {
            foreach (char c in ndfa.Alphabet)
            {
                DataGridViewRow row = new DataGridViewRow();
                row.Cells.Add(new DataGridViewTextBoxCell());
                row.Cells[0].Value = c;
                this.dgvAlphabet.Rows.Add(row);
                this.alphabetSource.PosibleChange(ListChangedType.ItemAdded, this.dgvAlphabet.Rows.Count);
            }

            foreach (string state in ndfa.GetStates())
            {
                DataGridViewRow row = new DataGridViewRow();
                row.Cells.Add(new DataGridViewTextBoxCell());
                row.Cells.Add(new DataGridViewCheckBoxCell());
                row.Cells.Add(new DataGridViewCheckBoxCell());
                row.Cells[0].Value = state.ToString();
                row.Cells[1].Value = (ndfa.StartState.Equals(state));
                row.Cells[2].Value = ndfa.EndStates.Contains(state);
                this.dgvStates.Rows.Add(row);
                this.stateSource.PosibleChange(ListChangedType.ItemAdded, this.dgvStates.Rows.Count);
            }

            foreach (string state in ndfa.GetStates())
            {
                Dictionary <char, HashSet <string> > toState = ndfa.GetStates(state);

                foreach (char key in toState.Keys)
                {
                    foreach (string toString in toState[key])
                    {
                        DataGridViewRow row = new DataGridViewRow();
                        row.Cells.Add(new DataGridViewComboBoxCell());
                        row.Cells.Add(new DataGridViewComboBoxCell());
                        row.Cells.Add(new DataGridViewComboBoxCell());
                        this.dgvTransitions.Rows.Add(row);

                        DataGridViewRow tRow = this.dgvTransitions.Rows[this.dgvTransitions.Rows.Count - 2];

                        BindingSource clm1BS = new BindingSource();
                        clm1BS.DataSource = stateSource;

                        ((DataGridViewComboBoxCell)tRow.Cells[0]).DataSource = clm1BS;
                        ((DataGridViewComboBoxCell)tRow.Cells[0]).ValueType  = typeof(char);
                        ((DataGridViewComboBoxCell)tRow.Cells[0]).Value      = state.ToString();

                        BindingSource clm2BS = new BindingSource();
                        clm2BS.DataSource = stateSource;
                        ((DataGridViewComboBoxCell)tRow.Cells[1]).DataSource = clm2BS;
                        ((DataGridViewComboBoxCell)tRow.Cells[1]).ValueType  = typeof(string);
                        ((DataGridViewComboBoxCell)tRow.Cells[1]).Value      = toString;

                        BindingSource clm3BS = new BindingSource();
                        clm3BS.DataSource = alphabetSource;
                        ((DataGridViewComboBoxCell)tRow.Cells[2]).DataSource = clm3BS;
                        ((DataGridViewComboBoxCell)tRow.Cells[2]).ValueType  = typeof(char);
                        ((DataGridViewComboBoxCell)tRow.Cells[2]).Value      = key;
                    }
                }
            }
        }