News for hackers. Stuff that matters.
Re:On the Complexity and Performance of Parsing with Derivat... by judofyr on Saturday June 25, 2016 @05:46PM (#11978142)
A Pratt-style parser makes expression precedence easy in a recursive descent parser:

    "use strict";
    var tokens = [2, '+', 5, '+', 9, '+', 4];
    var PREC = {'+': 1, '-': 1, '*': 2, '/': 2};

    function expr(prec) {
      var value = tokens.shift();
      while (true) {
        var nextToken = tokens[0];
        var nextPrec = PREC[nextToken];
        if (!nextPrec) break; // not an operator
        if (nextPrec <= prec) break;
        tokens.shift();
        var rest = expr(nextPrec);
        value = [nextToken, value, rest];
      }
      return value;
    }

    console.log(expr(0));
I find this technique useful because I can implement it in any language without being dependent on a parser generator.

There are no comments on this comment yet.