Exemplo n.º 1
0
        /// <summary>
        /// Creates new instance of <strong>ExpressionTree</strong> with specified
        /// expression.
        /// </summary>
        /// <exception cref="System.ArgumentNullException" caption="">expression is null</exception>
        /// <exception cref="System.ArgumentException" caption="">expression has unknown lexemes</exception>
        /// <exception cref="ExpressionSyntaxException" caption="ExpressionSyntaxException Class">expression has syntax errors</exception>
        /// <example>
        ///     <code lang="CS">
        /// ExpressionTree tree = new ExpressionTree("sin(x^2) + 10/x");
        /// </code>
        /// </example>
        /// <remarks></remarks>
        /// <param name="expression">An symbpolic mathematical expression.</param>
        public ExpressionTree(string expression)
        {
            if (expression == null)
            {
                throw new ArgumentNullException("expression");
            }
            if (expression == "")
            {
                throw new ArgumentException("Blank expression not allowed");
            }

            _expression = expression;

            ExpressionLexeme[] lexemes = ExpressionLexeme.GetLexemes(expression);

            foreach (ExpressionLexeme lex in lexemes)
            {
                if (lex.Lexeme == Lexeme.Unknown)
                {
                    throw new ArgumentException(
                              string.Format("Expression {0} has unknown lexeme \"{1}\"", _expression, lex.LexemeName),
                              "expression");
                }
            }

            _parsedExpression = "";
            CreateTree(lexemes);

            _variables = (string[])new ArrayList(FindVariables(_root).Keys).ToArray(typeof(string));
        }
Exemplo n.º 2
0
        // unary + - operators
        private ExpressionTreeNode ExprLevel4()
        {
            ExpressionTreeNode temp = null;
            ExpressionLexeme   lex;

            switch (CurrentLexeme.Lexeme)
            {
            case Lexeme.Plus:
                NextLexeme();
                temp = ExprLevel5();
                break;

            case Lexeme.Minus:
                lex = new ExpressionLexeme("", Lexeme.UMinus);
                NextLexeme();
                ExpressionTreeNode right = ExprLevel5();
                if (right == null)
                {
                    throw new ExpressionSyntaxException("expression expected", _expression, _parsedExpression);
                }
                temp = new ExpressionTreeNode(lex, right);
                break;

            default:
                temp = ExprLevel5();
                break;
            }

            return(temp);
        }
Exemplo n.º 3
0
		private void CreateTree(ExpressionLexeme[] lexemes)
		{
			_root = null;
			_lexemesQueue = new Queue (lexemes);
			NextLexeme();

			_root = ExprLevel1();
			if (CurrentLexeme.Lexeme != ExpressionLexeme.EndLexeme.Lexeme)
				throw new ExpressionSyntaxException("operator expected", _expression, _parsedExpression);
		}
Exemplo n.º 4
0
 private void NextLexeme()
 {
     if (_lexemesQueue.Count > 0)
     {
         _parsedExpression += CurrentLexeme.LexemeName;
         _curLexeme         = (ExpressionLexeme)_lexemesQueue.Dequeue();
     }
     else
     {
         _curLexeme = ExpressionLexeme.EndLexeme;
     }
 }
Exemplo n.º 5
0
		public ExpressionTreeNode(ExpressionLexeme lexeme,ExpressionTreeNode leftNode,ExpressionTreeNode rightNode )
		{
			ExpressionLexeme = lexeme;
			Left = leftNode;
			Right = rightNode;
		}
Exemplo n.º 6
0
		public ExpressionTreeNode(ExpressionLexeme lexeme,ExpressionTreeNode leftNode) : 
			this (lexeme, leftNode,null)
		{
		}
Exemplo n.º 7
0
		public ExpressionTreeNode(ExpressionLexeme lexeme) : 
			this (lexeme, null,null)
		{
		}
Exemplo n.º 8
0
 public ExpressionTreeNode(ExpressionLexeme lexeme, ExpressionTreeNode leftNode, ExpressionTreeNode rightNode)
 {
     ExpressionLexeme = lexeme;
     Left             = leftNode;
     Right            = rightNode;
 }
Exemplo n.º 9
0
 public ExpressionTreeNode(ExpressionLexeme lexeme, ExpressionTreeNode leftNode) :
     this(lexeme, leftNode, null)
 {
 }
Exemplo n.º 10
0
 public ExpressionTreeNode(ExpressionLexeme lexeme) :
     this(lexeme, null, null)
 {
 }
Exemplo n.º 11
0
		private void NextLexeme()
		{
			if (_lexemesQueue.Count > 0)
			{
				_parsedExpression += CurrentLexeme.LexemeName;
				_curLexeme = (ExpressionLexeme)_lexemesQueue.Dequeue();
			}
			else
				_curLexeme = ExpressionLexeme.EndLexeme;
		}
Exemplo n.º 12
0
		// unary + - operators
		private ExpressionTreeNode ExprLevel4()
		{
			ExpressionTreeNode temp = null;
			ExpressionLexeme lex;

			switch (CurrentLexeme.Lexeme)
			{
				case Lexeme.Plus:
					NextLexeme();
					temp = ExprLevel5();
					break;
				case Lexeme.Minus:
					lex = new ExpressionLexeme("",Lexeme.UMinus);
					NextLexeme();
					ExpressionTreeNode right = ExprLevel5();						
					if (right == null) throw new ExpressionSyntaxException("expression expected", _expression, _parsedExpression);																		
					temp = new ExpressionTreeNode(lex,right);
					break;

				default:
					temp = ExprLevel5();
					break;
			}

			return temp;
		}