Ejemplo n.º 1
0
        public DFA(NFA nfa)
        {
            description = nfa.Description;
            alph = nfa.Alph;
            i_string = nfa.IString;

            int count = 1;
            string istr = "";
            foreach (string sub in nfa.IStates) {
            if (count++ != nfa.IStates.Length)
                istr += sub + ":";
            else
                istr +=  sub;
            }

            string [] istate = new string [1];
            istate[0] = istr;

            i_state = istate;
            current_state = istr;
            GetDeltaFinal (nfa);
        }
Ejemplo n.º 2
0
 public bool RunMachine()
 {
     NFA nfa = new NFA (this);
     return nfa.RunMachine ();
 }
Ejemplo n.º 3
0
        private void GetDeltaFinal(NFA nfa)
        {
            Queue newstates = new Queue ();
            Queue queue = new Queue ();
            Hashtable odelta = nfa.Delta;
            Hashtable ndelta = new Hashtable ();
            newstates.Enqueue (current_state);
            queue.Enqueue (current_state);

            while (queue.Count != 0) {
            string substate = (string) queue.Dequeue ();
            string [] states = substate.Split (':');

            foreach (string sym in alph) {

                int icount = 0;
                string [] nstates = new string [states.Length];
                foreach (string sub in states) {
                    nstates [icount++] = (string) odelta [new Context (sub, sym)];
                }

                string union = Union (nstates);
                if (union == null)
                    union = "ERROR";

                if (!newstates.Contains (union)) {
                    newstates.Enqueue (union);
                    queue.Enqueue (union);
                }

                ndelta.Add (new Context (substate, sym), union);
            }
            }

            this.delta = ndelta;

            int qcount = 0;
            string [] qstates = new string [newstates.Count];
            foreach (object o in newstates)
            qstates [qcount++] = (string) o;

            this.states = qstates;
            string [] ofstates = nfa.FState;
            ArrayList list = new ArrayList ();

            foreach (string sub in states)
            foreach (string asub in ofstates)
                if (sub.IndexOf (asub) != -1) {
                    list.Add (sub);
                    break;
                }

            string [] farray = new string [list.Count];
            list.CopyTo (farray);
            f_states = farray;
        }
Ejemplo n.º 4
0
        public static void Main(string[] args)
        {
            bool help = false;
            bool adfa = false;
            bool anfa = false;
            bool gram = false;
            string filename = null;
            string input = null;

            Header ();

            try {
            for (int i = 0; i < args.Length - 2; i++ ) {
                switch (args [i]) {
                case "--dfa":
                    adfa = true;
                    filename = args [i+1];
                    input = args [i+2];
                    break;
                case "--nfa":
                    anfa = true;
                    filename = args [i+1];
                    input = args [i+2];
                    break;
                case "--gra":
                    gram = true;
                    filename = args [i+1];
                    input = args [i+2];
                    break;
                case "-h":
                case "--help":
                    help = true;
                    break;
                }
            }
            } catch (Exception e) {
            Console.WriteLine ("Error: " + e.ToString ());
            }

            if (help)
            Help ();

            if (adfa) {
            IFA fa = new DFA (filename, input);
            Console.WriteLine ("Input: {0} was {1} by the DFA",
                input == ""? "epsilon" : input,
                fa.RunMachine ()? "accepted" : "not accepted");
            } else if (anfa) {

            IFA fa = new NFA (filename, input);
            Console.WriteLine ("Input: {0} was {1} by the NFA",
                input == ""? "epsilon" : input,
                fa.RunMachine ()? "accepted" : "not accepted");
            } else if (gram) {
            RegGram rg = new RegGram (filename, input);
            Console.WriteLine ("Input: {0} was {1} by the grammar",
                input == ""? "epsilon" : input,
                rg.RunMachine ()? "accepted" : "not accepted");
            } else
            Help ();
        }