Re: JavaScript, Python

/*
 * expr ::= term | term + expr
 * term ::= factor | factor * term
 * factor ::= (expr) | num
 * num ::= 0..9
 */

var s = "((4+5)*3+2)*8";
var i = 0;

function gettoken () {
    return s[i++];
}

function tokenback () {
    i--;
}

function expr () {
    var x;
    var y = 0;

    x = term ();
    if (gettoken () == '+')
        y = expr ();
    else
        tokenback ();

return x+y;
}

function term () {
    var x;
    var y = 1;

    x = factor ();
    if (gettoken () == '*')
        y = term ();
    else
        tokenback ();

return x*y;
}

function factor () {
    var s = gettoken ();
    var x = parseInt (s);

    if (s == '(') {
        x = expr ();
        if (gettoken () != ')')
            console.log ("Error");
    }
    else if ((x >= 0) \&amp;\&amp; (x <= 9));
    else
        console.log ("Error");
return x;
}

console.log(expr());
console.log(((4+5)*3+2)*8);