Ejemplo n.º 1
0
        private static void EnterRules()
        {
            if (nonterminals == null || terminals == null)
            {
                Console.WriteLine("Please enter nonterminal and terminal symbols first.");
                return;
            }
            if (alphabet == null)
            {
                alphabet = nonterminals.Concat(terminals).ToArray();
            }

            string message    = "Enter number of production rules: ";
            int    rulesCount = AskForNumberInput(message, 1);

            rules = new ProductionInfo[rulesCount];
            for (int i = 0; i < rulesCount; i++)
            {
                message = string.Format("Enter original symbol of production rule number {0}: ", i + 1);
                string original = AskForSymbolInput(message, nonterminals);

                message = "Enter number of symbol in the sentence: ";
                int symbolsCount = AskForNumberInput(message, 0);

                string[] sentence = new string[symbolsCount];
                for (int j = 0; j < symbolsCount; j++)
                {
                    message     = string.Format("Enter the symbol number {0}: ", j + 1);
                    sentence[j] = AskForSymbolInput(message, alphabet);
                }

                rules[i] = new ProductionInfo(original, sentence);
            }

            grammar    = new ContextFreeGrammar(nonterminals, terminals, rules, starting);
            normalForm = grammar.GetChomskyNormalForm();
        }
Ejemplo n.º 2
0
        private static void EnterRules()
        {
            if (nonterminals == null || terminals == null)
            {
                Console.WriteLine("Please enter nonterminal and terminal symbols first.");
                return;
            }
            if (alphabet == null)
                alphabet = nonterminals.Concat(terminals).ToArray();

            string message = "Enter number of production rules: ";
            int rulesCount = AskForNumberInput(message, 1);

            rules = new ProductionInfo[rulesCount];
            for (int i = 0; i < rulesCount; i++)
            {
                message = string.Format("Enter original symbol of production rule number {0}: ", i + 1);
                string original = AskForSymbolInput(message, nonterminals);

                message = "Enter number of symbol in the sentence: ";
                int symbolsCount = AskForNumberInput(message, 0);

                string[] sentence = new string[symbolsCount];
                for (int j = 0; j < symbolsCount; j++)
                {
                    message = string.Format("Enter the symbol number {0}: ", j + 1);
                    sentence[j] = AskForSymbolInput(message, alphabet);
                }

                rules[i] = new ProductionInfo(original, sentence);
            }

            grammar = new ContextFreeGrammar(nonterminals, terminals, rules, starting);
            normalForm = grammar.GetChomskyNormalForm();
        }