Esempio n. 1
0
        public NFA(RegGram gram)
        {
            description = gram.Description;
            alph = gram.Terminals;

            int length = gram.Nonterminals.Length;
            string [] nterminals = new string [length + 2];
            gram.Nonterminals.CopyTo (nterminals, 0);
            nterminals [length] = "ZFinal";
            nterminals [length + 1] = "ERROR";
            states = nterminals;

            string [] nistate = new string [1];
            nistate [0] = gram.ISym;
            i_state = nistate;

            GetDeltaFinal (gram);

            i_string = gram.IString;
        }
Esempio n. 2
0
        private void GetDeltaFinal(RegGram gram)
        {
            Hashtable hash = new Hashtable ();
            bool acepta = false;
            string [][] propositions = gram.Propositions;

            foreach (string [] prop in propositions) {
            string left = prop [0];
            string right = prop [1];

            if (right == "epsilon")
                acepta = true;

            if (right.IndexOf (":") == -1) {
                Context con = new Context (left, right);
                if (hash [con] == null) {
                    hash.Add (con, "ZFinal");
                } else {
                    string temp = (string) hash [con];
                    hash.Remove (con);
                    hash.Add (con, temp + ":ZFinal");
                }
            } else {
                string [] sub = right.Split (':');
                string subleft = sub [0];
                string subright = sub [1];
                Context con = new Context (left, subleft);

                if (hash [con] == null) {
                    hash.Add (con, subright);
                } else {
                    string temp = (string) hash [con];
                    hash.Remove (con);
                    hash.Add (con, temp + ":" + subright);
                }
            }
            }

            foreach (string sym in alph) {
            hash.Add (new Context ("ZFinal", sym), "ERROR");
            hash.Add (new Context ("ERROR", sym), "ERROR");
            }

            delta = hash;

            string [] nfstates;
            if (acepta) {
            nfstates = new string [2];
            nfstates [0] = "ZFinal";
            nfstates [1] = gram.ISym;
            } else {
            nfstates = new string [1];
            nfstates [0] = "ZFinal";
            }

            f_states = nfstates;
        }
Esempio n. 3
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 ();
        }