This article explain the techniques to read and evaluate the result of an arithmetic expression string. The tokenizer, expression validator (both syntax validation and data-type validation) and expression evaluator are implemented in C# and Silverlight and source code is available for download below.

Expression Evaluation Demo in Silverlight

The techniques of expression-evaluation are illustrated in the following demo. The source code of the demo can be downloaded below. The input expression accepts variable names, numbers and operators.

All these steps are illustrated with a live demo (below) in Silverlight. The user can enter a free text expression with numbers, variables and operators and see the evaluation process step by step.

Data Structures for this Article

An expression is ordered collection of tokens. A token is a string of characters that represents a symbol of the programming language in context. The symbol can be a number constant, text constant, arithmetic operators, variable names, function names, etc.

For illustration purposes, let us consider three types of tokens, namely number constants, variables and arithmetic operators. First, we create an interface ITokenObject and then create classes for each type of token, namely Constant, Variable and Operator.

The above classes act as data-structures for each token type. The OperatorSymbol enum indicates the operator symbol; we support these arithmetic operators: add (+), subtract (-), multiply (*), divide (/) and modulus (%). The open and close parenthesis are also considered as operators; the parenthesis are used to control the precedence of operators. The OperatorAssociativity enum indicates the associativity of the corresponding operator. The associativity controls how operands are chosen for operators in the absence of parenthesis for controlling precedence. For more information, see http://en.wikipedia.org/wiki/Operator_associativity.

The following Token class is the data-structure for an instance of a token in expressions. The Expression class contains a single property, the list of tokens.

The process of building token objects from input expression string is called tokenization or lexical analysis. For more information on tokens and tokenization, see http://en.wikipedia.org/wiki/Tokenizing.

The following is the tokenizer class for data-structures defined above.

The AllOperators class contains instances of Operator class for all supported operators. The Resolve() method of Token class creates instances of corresponding token class. For example, it creates a Variable class for "a", Constant class for "52" and Operator class for "+", "-", etc.

Infix, Postfix and Prefix Notations

Infix, postfix and prefix notations are three different ways of writing an expression.

In infix notation, the operators are written in-between their operands. Infix notation needs extra information (operator precedence, associativity and parenthesis) to control the order of evaluation. Example: (3 + 5 ) * 7

In postfix notation, the operators are written after their operands. The operators act on operands immediately to the left of them. Example: 3 5 + 7 *

In prefix notation, the operators are written before their operands. The operators act on operands to their right. Example: * 7 + 3 5

Postfix and prefix notations do not need any extra information like parenthesis or rules of operator precedence and associativity. Evaluating an infix expression (with its complex rules) will be a daunting task, unless it is converted to postfix. Evaluating an expression in postfix notation is trivially easy while using stacks.

Converting Infix to Postfix

The shunting yard algorithm, invented by Edsger Dijkstra is a stack-based algorithm used to convert an expression from infix notation to postfix notation. It maintains a stack to hold operators that are not yet added to postfix notation. The following is a summary of this algorithm:

Read a token (left to right).

If token is a variable or constant, push it onto the stack.

If token is an operator,

If open parenthesis, push it onto stack.

If close parenthesis, pop all tokens (until but not including a left parenthesis) from stack and add them to output.

If current token and top token in stack are arithmetic operators, then push the later onto output if its precedence is greater than the former.

Comment by Abdul Qadir on Dec-23-2013Sir, the explanation is FANTASTIC!!!!... Saved lots of hours...thanks a lot....

Comment by rama on Mar-23-2013very nice

Comment by hichem147 on Apr-01-2012Hi, this is an excellent article
I was looking for an Expression evaluator to evaluate logical expressions with use of variables
a = true; b = false; c = true;
expression = !a || b && c
for this I need to use unary operators like NOT (!)
could you please help me to deal with unary aperators
thanks for your help and for sharing
best regards

Comment by mukund.thonge on Jun-09-2011Hi All,
This is excellent article that I found .
Particularly It does expression evaluation perfectly.
If we want to add some other operator like Log,Exp,Square etc
we can add.
Many many Thanks for such article!!!