![]() |
Web Development »
Client side scripting »
General
Intermediate
License: A Public Domain dedication
JavaScript Mathematical Expression EvaluatorBy Prasad KhandekarA mathematical expression evaluator in pure JavaScript, with support for user defined variables. |
Javascript, Windows, Visual Studio, Dev
|
|
Advanced Search |
|
|
|
||||||||||||||||

There are a number of good articles in CodeProject on this subject. This one, however, presents the same thing in pure JavaScript. Although this expression evaluator does not come close to some of those which have been written in C#, C++, or VB, I am sure it can be useful to those who want to learn the concepts behind expression parsing and subsequent evaluation.
Before we go into the details, let's understand a few terms first:
The calculation, ((5 - 2) * 4) / 3, can be written down like this in RPN:
5 2 - 4 * 3 /
The expression is evaluated in the following way (the stack is displayed after the operation has taken place):
| Input | Stack | Operation |
| 5 | 5 | Push operand |
| 2 | 5, 2 | Push operand |
| - | 3 | Subtract top two operands |
| 4 | 3, 4 | Push operand |
| * | 12 | Multiply top two operands |
| 3 | 12, 3 | Push operand |
| / | 4 | Divide the top two operands |
The final result, 4, lies on the top of the stack at the end of the calculation.
Edsger Dijkstra invented an algorithm named "shunting yard", which converts from infix notation to RPN. The shunting yard algorithm is also stack-based.
The tokanizer.js file contains the code to break the expression into various tokens. This is very much important as we get the expression in string form. Breaking this string into tokens helps in subsequent parsing and conversion. Once the tokenizer completes, we are left with an array of tokens, which is then fed to the infix2postfix converter. The tokenizer makes use of the switch clause to distinguish the common operators and the string constants.
The stack data structure is implemented in the Stack.js file. This data structure is implemented using an array. For the purpose of debugging, the stack also provides a toString method which basically converts the stack into a comma separated string.
The entire expression conversion and evaluation logic is written in the Evaluator.js file. The root object is called Expression, which exposes two methods, namely:
ParseExpression: To convert an infix expression into an equivalent postfix expression. EVal: Which actually evaluates the postfix expression. This method internally makes a call to ParseExpression, if the expression is not already parsed. Like the tokenizer, the evaluator also makes use of the switch clause to distinguish between the various operators. The most important part of this code is the default case handler in the EVal() method and the Parse() function. The evaluator also makes use of an excellent piece of code written by Mr. Matt Kruse for handling the date data type. The rest of the code in this file simply contains some helper functions. The user defined variables can be added using the AddVar(varName, varValue) method on the Expression object. Similarly, the user defined variables can be cleared using the ClearVars() method. The following table summarizes the various operators and functions supported by the expression evaluator:
| Arithmetic | Logical | Comparison | Functions |
| + | ! | = | AVG |
| - | & | > | ABS |
| * | | | < | ACOS |
| / | <= | ASC |
|
| % | >= | ASIN |
|
| ^ | <> | ATAN |
|
CDATE |
|||
CHR |
|||
COS |
|||
DATE |
|||
FIX |
|||
HEX |
|||
IIF |
|||
LCASE |
|||
LEFT |
|||
LOG |
|||
MAX |
|||
MID |
|||
MIN |
|||
RIGHT |
|||
ROUND |
|||
SIN |
|||
SQRT |
|||
TAN |
|||
UCASE |
This code is released under the GPL variant. Please see License.txt for more details.
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 18 Nov 2008 Editor: Sean Ewington |
Copyright 2005 by Prasad Khandekar Everything else Copyright © CodeProject, 1999-2009 Web16 | Advertise on the Code Project |