function* rpn2(expression) {
const stack = []
for (const token of tokenize(expression)) {
switch (token) {
case '(':
stack.push(token)
break
case ')':
while (stack.length) {
const n = stack.pop()
if (n === '(')
break
yield n
}
break
case '+':
case '*':
if (stack.length === 0 || stack[stack.length-1] === '(') {
stack.push(token)
} else {
while (stack.length) {
if (stack[stack.length-1] === '(' || (token === '+' && stack[stack.length-1] === '*'))
break
yield stack.pop()
}
stack.push(token)
}
break
default:
yield +token
}
}
while (stack.length)
yield stack.pop()
}