Ejemplo n.º 1
0
        /**
         * Generate the next word of the language based on the specified alphabet, axiom and rewrite rules.
         * Rewrite the CurrentWord
         *
         *
         * public override String Rewrite()
         * {
         *  StringBuilder NextGeneration = new StringBuilder();
         *  for (int i = 0; i < CurrentString.Length; i++)
         *  {
         *      Char NextSymbol = CurrentString[i];
         *      ProductionRule RewriteRule = GetProductionRule(NextSymbol, i, CurrentString);
         *      if(RewriteRule!=null)
         *          NextGeneration.Append(RewriteRule.Rewrite(NextSymbol, i, CurrentString));
         *      else//if a rewrite rule is missing, rewrite the symbol with itself:
         *          NextGeneration.Append(NextSymbol);
         *  }
         *  AddString(NextGeneration.ToString());
         *
         *  return NextGeneration.ToString();
         * }*/

        public override Word Rewrite()
        {
            Word nextWord = new Word();

            for (int i = 0; i < currentWord.GetSize(); i++)
            {
                Symbol predecessor = currentWord.SymbolAt(i);
                Word   successor   = null;
                foreach (ProductionRule productionRule in ProductionRules)
                {
                    successor = productionRule.Rewrite(predecessor, i, currentWord);
                    if (successor != null)//A Rule for that symbol is found!
                    {
                        nextWord.Append(successor);
                        break;
                    }
                }
                //there was no Rule which rewrote the predecessor symbol
                //As a matter of convention, if there is no rule
                // for a character, that character is left unchanged.
                if (successor == null)
                {
                    nextWord.Append(predecessor);
                }
            }

            AddWord(nextWord);
            return(nextWord);
        }
Ejemplo n.º 2
0
        public Word ParseWord(String wordSource)
        {
            Word word = new Word();

            //using the lexer to parse the character sequence into symbols
            ITokenStream tokenStream = lexer.Analyze(wordSource);
            Token        nextToken   = null;

            while ((nextToken = tokenStream.NextToken()) != null)
            {
                word.Append(Symbol.valueOf(nextToken.ToString()));//in this case a token = a symbol
            }

            return(word);
        }