Question

I would like to find a javascript parser that can handle and evaluate simple expressions. The parser should be able to evaluate the regular mathematical expressions, and support custom functions with parameters. It also has to support strings handling. String concatenation with || operator support is preferred, but it is okay if + will do the trick.

Examples of an expression that should be handled by the parser:

3 * (2 + 1) - 1

2 * func(2, 2)

func('hello world', 0, 5) || ' you'

Has anyone implemented such a thing or where can I find something similar?

Was it helpful?

Solution

I have a modified version of an ActionScript parser (written in AS, not parses AS) that supports custom functions, but not strings. It would probably be easy to add string support though. I'll upload it somewhere so you can get it at http://silentmatt.com/parser2.js http://silentmatt.com/parser3.js.

Edit: I added basic support for strings pretty easily. It doesn't support escape sequences and toJSFunction doesn't work, but it only took a few minutes to get it working. Changing the concatenation operator to "||" should be pretty easy too.

Here's how you would evaluate your example expressions:

js> var parser = new Parser();
js> parser.parse("3 * (2 + 1) - 1").evaluate();
8
js> parser.parse("2 * func(2; 2)").evaluate({ func:Math.pow });
8
js> function substr(s, start, end) { return s.substring(start, end); }
js> parser.parse("func('hello world'; 0; 5) + ' you'").evaluate({ func:substr });
hello you

I don't remember why I used semicolons as argument separators; I think it has something to do with differentiating between functions and built-in "operator" functions.

Another edit:

I've been playing with this a little, and now there's a version with better string support at http://silentmatt.com/parser3.js (toJSFunction works, and you can use standard JavaScript escape sequences). It also uses commas to separate arguments for all functions and || as the string concatenation operator instead of +, which only does addition.

OTHER TIPS

Assuming you mean a javascript parser in javascript, you probably want eval()

see: https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Global_Functions/Eval

Just note that eval, if used improperly, can represent a security risk.

haven't used it, but a quick google reveals http://jsfromhell.com/classes/math-parser

edit:

What you want to do may be out of reach of the first link, you could also have a look at Douglas Crockford's "parser for Simplified JavaScript"

It's just a parser, so you would have to do all the evaluation yourself. It would, however, make it somewhat easier and it doesn't use eval.

Try math.js:

http://mathjs.org

Comes with an extensive and easy to use parser, which also supports assignment and usage of variables and functions like in your example expression. Integrates seamlessly with "native" JavaScript: you can get and set variables and functions from the Parsers scope.

Your example code would be evaluated as:

var parser = math.parser();
parser.set('func', function () {
    // ... do something ...
});
parser.eval('3 * (2 + 1) - 1');
parser.eval('2 * func(2, 2)');
parser.eval('func("hello world", 0, 5) + " you"');

Functions can also be defined in the parser itself (currently only single-line functions):

parser.eval('function f(x, y) = x ^ y');
parser.eval('f(2, 3)'); // 8

See this tutorial for how to build arbitrary parsers/compilers. (Basically it automates the construction of recursive descent parsers from grammars, meaning you can change your expression syntax easily). The whole tutorial is done in JavaScript, so it applies directly to you.

http://www.bayfronttechnologies.com/mc_tutorial.html

Narcissus implements a proper JS parser in JS: http://mxr.mozilla.org/mozilla/source/js/narcissus/jsparse.js. Written by Brendan Eich (the JS creator) too!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top