|

Introduction
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.
Basic concepts
Before we go into the details, let's understand a few terms first:
- Infix notation is a common arithmetic and logical formula notation, in which the operators are written in infix-style, i.e., between the operands they act on. The major problem with this notation is that it is not that simple to parse, by a computer, as prefix or postfix notations. In infix notation, unlike the prefix or postfix notations, the parentheses surrounding the groups of operands and operators are necessary to indicate the intended order in which the operations are to be performed. In the absence of parentheses, certain precedence rules determine the order of operations.
- Postfix notation, also known as Reverse Polish notation (RPN), is an arithmetic formula notation, derived from the Polish notation introduced in 1920 by the Polish mathematician Jan Lukasiewicz. RPN was invented by an Australian philosopher and computer scientist Charles Hamblin in the mid-1950s, to enable zero-address memory stores. As a user interface for calculation, the notation was first used in Hewlett-Packard's desktop calculators in the late 1960s, and then in the HP-35 handheld scientific calculator launched in 1972. In RPN, the operands precede the operator, thus dispensing the need for parentheses. Why is this notation more suitable to computers? The implementations of RPN are stack-based; that is, operands are popped from a stack, and calculation results are pushed back into it. Although this concept may seem obscure at first, RPN has the advantage of being extremely easy, and therefore fast, for a computer to analyze. Let's look at the practical implications of RPN:
- Calculations proceed from left to right.
- There are no brackets or parentheses, as they are unnecessary.
- Operands precede operator, they are removed as the operation is evaluated.
- When an operation is done, the result becomes an operand itself (for later operators).
- There is no hidden state, and no need to worry about whether you have hit an operator or not.
Examples
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.
Converting from infix notation
Edsger Dijkstra invented an algorithm named "shunting yard", which converts from infix notation to RPN. The shunting yard algorithm is also stack-based.
- While there are tokens to be read:
- Read a token.
- If the token is a number, then add it to the output queue.
- If the token is a function token, then push it onto the stack.
- If the token is a function argument separator (e.g., a comma):
- Until the topmost element of the stack is a left parenthesis, pop the element onto the output queue. If no left parentheses are encountered, then either the separator was misplaced or the parentheses were mismatched.
- If the token is an operator, O1, then:
- while there is an operator O2 at the top of the stack, and either
- O1 is left-associative and its precedence is less than or equal to that of O2, or
- O1 is right-associative and its precedence is less than that of O2,
- pop O2 off the stack, onto the output queue;
- push O1 onto the operator stack.
- If the token is a left parenthesis, then push it onto the stack.
- If the token is a right parenthesis, then pop the operators off the stack, onto the output queue, until the token at the top of the stack is a left parenthesis, at which point it is popped off the stack but not added to the output queue. At this point, if the token at the top of the stack is a function token, pop it too onto the output queue. If the stack runs out without finding a left parenthesis, then there are mismatched parentheses.
- When there are no more tokens to be read, pop all the tokens, if any, off the stack, add each to the output as it is popped out, and exit. (These must be operators; if a left parenthesis is popped, then there are mismatched parentheses.)
Tokenizer
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.
Stack
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.
Evaluator
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 |
License
This code is released under the GPL variant. Please see License.txt for more details.
History
- October 31st, 2005 - Initial release.
- November 04th, 2005 - Added licensing details.
- May 26th, 2006 - Fixed a small bug pointed out by Mr. Raphael Wils.
- June 21st, 2006 - Updated the JavaScript and sample page to demonstrate variable support.
| You must Sign In to use this message board. |
|
| | Msgs 1 to 13 of 13 (Total in Forum: 13) (Refresh) | FirstPrevNext |
|
|
 |
|
|
 |
|
|
Hello,
This support is already available. You will have to call the AddVariable method of Expression. This method takes two parameters. The variable name as used in the expression and it's value. Refer to ExpressionTester.html page for more details.
Regards,
Prasad P. Khandekar Knowledge exists, man only discovers it.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Thanks a lot for the nice article. Although I am not a javascript guy but a nice explaination of infix notation in your article helped my solving a bug in my own app that I wrote for a unit converter application
Grewal. http://www.mycoolaids.com
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
I have found another bug:
in Evaluator.js Line 766
'varTmp' should be 'varTerm'
if (varTmp == "LEFT") pStack.Push(varTerm.substring(0, objTmp)); //changed else pStack.Push(varTerm.substr((varTerm.length - objTmp), objTmp)); //changed break;
Jay
|
| Sign In·View Thread·PermaLink | 2.00/5 (1 vote) |
|
|
|
 |
|
|
If have found a Bug:
if you try to eval (1+1)/2 there is an exception in line 403 in Tokanizer.js.
if you change it to
function IsFunction(strArg) { var idx = 0; if(strArg == null) //Added return false; //Added
strArg = strArg.toUpperCase(); for (idx = 0; idx < lstFuncOps.length; idx++) { if (strArg == lstFuncOps[idx]) return true; } return false; }
the code seams to work. please verify the code
Jay
-- modified at 1:54 Tuesday 26th September, 2006
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Raising error, when the first char is '(' will call IsFunction to check. In order not to modify the author's thinking. Insert a try & catch section to prevent the first '(' appears at the beginning string.
function IsFunction(strArg) { var idx = 0; try{ strArg = strArg.toUpperCase(); for (idx = 0; idx { if (strArg == lstFuncOps[idx]) return true; } return false; }catch(E){ return false; } }
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Dear Prasad Khandekar,
I tried the following Infix expression using JS Expression Evaluator, but in vain, I am only getting an error.
Infix - a & b | c | d | e | f | g
I am able to get posfix equivalent for the above.
Postfix - {a}{b}{&}{c}{|}{d}{|}{e}{|}{f}{|}{g}{|}
However, I unable to get the results for evaluating the postfix expression.
Please help.
With thanks, Gopalakrishnan,B.R.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Hello,
I have updated the sample and the javascript to demonstrate the use of variables. Since this is an edited article the update should be available in next 2/3 days. Keep watching.
Regards,
Prasad P. Khandekar Knowledge exists, man only discovers it.
|
| Sign In·View Thread·PermaLink | 2.00/5 (1 vote) |
|
|
|
 |
|
|
SQRT function does not work because there is a small bug in the file evaluator.js. letter T shall be add to 'SQR' at line 460.
It don't mean a thing if it ain't got that swing !
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Hello Raphael,
Thank's a lot for pointing out the bug. I have updated the article's content, the associated souce code and submitted to the editors. The new contents should be available very soon.
Regards,
Prasad P. Khandekar Knowledge exists, man only discovers it.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Javascript supports an eval() function for evaluating expressions at runtime.
eval("1+2") == 3
you can even access local and global variables. Why did you choose to implement your own mechanism for that?
Jaroslaw Kowalski http://blog.jkowalski.net/
|
| Sign In·View Thread·PermaLink | 3.00/5 (2 votes) |
|
|
|
 |
|
|
For the fun of it ?
According to some security rules, the eval() function is sometimes disabled to prevent malware to perform on your computer. Thus, this 'mechanism' offer the liberty to do some math evaluation without 'eval()' 
Kochise
In Code we trust !
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Hi,
I know that javascript does have have a Eval function. The main reason to write this was to understand the expression evaluator mechanism in detail and the reason I chose javascript is because it is very easy to implement this mechanism and test it.
Regards,
Prasad P. Khandekar Knowledge exists, man only discovers it.
|
| Sign In·View Thread·PermaLink | 5.00/5 (2 votes) |
|
|
|
 |
|
|
General News Question Answer Joke Rant Admin
|