Javascript parser for simple expression

后端 未结 6 2238
再見小時候
再見小時候 2020-12-05 03:57

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 sup

相关标签:
6条回答
  • 2020-12-05 04:03

    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.

    0 讨论(0)
  • 2020-12-05 04:03

    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!

    0 讨论(0)
  • 2020-12-05 04:05

    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.

    0 讨论(0)
  • 2020-12-05 04:07

    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.evaluate('3 * (2 + 1) - 1');
    parser.evaluate('2 * func(2, 2)');
    parser.evaluate('func("hello world", 0, 5) + " you"');
    

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

    parser.evaluate('function f(x, y) = x ^ y');
    parser.evaluate('f(2, 3)'); // 8
    
    0 讨论(0)
  • 2020-12-05 04:14

    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

    0 讨论(0)
  • 2020-12-05 04:23

    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.

    0 讨论(0)
提交回复
热议问题