Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Yet Another Math Parser (YAMP)

, 30 Sep 2012 CPOL
Constructing a fast math parser using Reflection to do numerics like Matlab.
YAMP_Demo.zip
YAMP
YAMPCompare
LLMathParser
MathFormula
MathParser
MathParserNet
Exceptions
MathParserTK
YAMPCompare.pidb
YAMPConsole
YAMPConsole.pidb
Exceptions
Expressions
Functions
ArgumentFunctions
LinearAlgebra
LogicFunctions
Spectroscopy
StandardFunctions
Trigonometric
Interfaces
Numerics
Decompositions
Integration
Interpolations
ODE
Optimization
Others
Solvers
Operators
AssigmentOperators
BinaryOperators
DotOperators
LogicOperators
UnaryOperators
Values
YAMP.csproj.user
YAMP.pidb
YAMP_Source.zip
YAMP.csproj.user
YAMP.pidb
using System;
using System.Text;
using System.Collections;
using System.Collections.Generic;

namespace YAMP
{
	public class ParseTree
	{
		#region Constants

		const string SPACING = "  ";

		#endregion

		#region Members
		
		Operator _operator;
		Expression[] _expressions;
		string _input;
		int _offset;
		bool _isList;

		#endregion

		#region Properties

		public bool IsList
		{
			get { return _isList; }
		}

		public Operator Operator
		{
			get
			{
				return _operator;
			}
			set
			{
				_operator = value;
			}
		}
		
		public Expression[] Expressions
		{
			get
			{
				return _expressions;
			}
			set
			{
				_expressions = value;
			}
		}

		#endregion

		#region ctor
		
		public ParseTree(Operator op, Expression[] expressions)
		{
			_operator = op;
			_expressions = expressions;
		}
		
		public ParseTree(Operator op, Expression expression)
		{
			_operator = op;
			_expressions = new Expression[] { expression };
		}
		
		public ParseTree(Operator op, Expression left, Expression right)
		{
			_operator = op;
			_expressions = new Expression[] { left, right };
		}

		public ParseTree (string input, int offset) : this(input, offset, false)
		{
		}

		public ParseTree (string input) : this(input, 0, false)
		{
		}

		public ParseTree (string input, bool isList) : this(input, 0, isList)
		{
		}

		public ParseTree(string input, int offset, bool isList)
		{
			_offset = offset;
			_input = input;
			_isList = isList;

			if(_input.Length > 0 && _input[0] == '-')
				_input = "0" + _input;

			Parse();
		}
		
		#endregion
		
		void Parse()
		{			
			BracketExpression bracket = null;
			var ops = new List<Operator>();
			var exps = new List<Expression>();
			var shadow = _input;
			var takeop = false;
			var maxLevel = -100;
			var maxIndex = 0;
			var expIndex = 0;
			var tmpExp = 0;
            var offset = _offset;

            if (_input.Length == 0)
            {
                _expressions = new Expression[] { new EmptyExpression() };
                return;
            }

			while(shadow.Length > 0)
			{
				if(shadow[0] == ' ')
				{
					offset++;
					shadow = shadow.Substring(1);
					continue;
				}

				if(shadow.Length > 1)
				{
					var key = new string(new char[] { shadow[0], shadow[1] });

					if(Tokens.Instance.Sanatizers.ContainsKey(key))
					{
						offset++;
						shadow = Tokens.Instance.Sanatizers[key] + shadow.Substring(2);
						continue;
					}
				}

				if(takeop)
				{
					var op = Tokens.Instance.FindOperator(shadow);

					if(op.Level >= maxLevel)
					{
						maxLevel = op.Level;
						maxIndex
							= ops.Count;
						expIndex = exps.Count;
					}

					op.IsList = _isList;
					ops.Add(op);
					shadow = op.Set(shadow);
					offset += op.Input.Length;
					takeop = !op.ExpectExpression;
				}
				else
				{
					var exp = Tokens.Instance.FindExpression(shadow);
					exp.Offset = offset;
					exps.Add(exp);
					shadow = exp.Set(shadow);
					offset += exp.Input.Length;
					takeop = true;
				}
            }

            expIndex--;

			while(ops.Count > 1)
            {
				if(ops[maxIndex].ExpectExpression)
				{
					tmpExp = expIndex + 1;
					bracket = new BracketExpression(new ParseTree(ops[maxIndex], exps[expIndex], exps[tmpExp]));
					exps.RemoveAt(tmpExp);
				}
				else
				{
					bracket = new BracketExpression(new ParseTree(ops[maxIndex], exps[expIndex]));
				}

				exps.RemoveAt(expIndex);
				exps.Insert(expIndex, bracket);
				ops.RemoveAt(maxIndex);
				tmpExp = 1;
				expIndex = 0;
				maxIndex = 0;
				maxLevel = ops[0].Level;

				for(var i = 1; i < ops.Count; i++)
				{	
					if(maxLevel <= ops[i].Level)
					{
						expIndex = tmpExp;
						maxIndex = i;
						maxLevel = ops[i].Level;
                    }

                    if (ops[i].ExpectExpression)
                        tmpExp++;
				}
			}

			_expressions = exps.ToArray();
			
			if(ops.Count == 1)
				_operator = ops[0];
		}
		
		public override string ToString ()
		{
			var sb = new StringBuilder();
			sb.Append(PrintExpression(Expressions[0]));
			
			if(Operator != null)
			{
				sb.Append(SPACING).AppendLine(Operator.ToString());
				
				if(Expressions.Length == 2)
					sb.Append(PrintExpression(Expressions[1]));
			}
			
			return sb.ToString();
		}
		
		string PrintExpression(Expression exp)
		{
			var lines = exp.ToString().Split(new string [] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
			var sb = new StringBuilder();
			
			foreach(var line in lines)
				sb.Append(SPACING).AppendLine(line);
			
			return sb.ToString();
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Florian Rappl
Chief Technology Officer
Germany Germany
Florian is from Regensburg, Germany. He started his programming career with Perl. After programming C/C++ for some years he discovered his favorite programming language C#. He did work at Siemens as a programmer until he decided to study Physics. During his studies he worked as an IT consultant for various companies.
 
Florian is also giving lectures in C#, HTML5 with CSS3 and JavaScript, and other topics. Having graduated from University with a Master's degree in theoretical physics he is currently busy doing his PhD in the field of High Performance Computing.
Follow on   Google+

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150326.1 | Last Updated 30 Sep 2012
Article Copyright 2012 by Florian Rappl
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid