2
我已經想通了如何實現二元運算符與優先順序,這樣的(僞):遞歸下降解析:高優先級一元運算符
method plus
times()
while(consume(plus_t)) do
times()
end
end
method times
number()
while(consume(times_t))
number()
end
end
// plus() is the root operation
// omitted: number() consumes a number token
所以,當我分析4 + 5 * 6
它會:
- 加
- 乘法
- 號(4消耗)
- plus_t消耗
- 乘法
- 數(5消耗)
- times_t消耗
- 數(6消耗)
- 乘法
然而,當我嘗試加入一種minus
方法d(前綴minusing像-4
,未綴minusing像4 - 5
):
method minus
consume(minus_t)
plus()
end
這需要非常低的優先級,所以-4 + 5
變得-(4 + 5)
而非(-4) + 5
,這是不希望的。
我該怎麼做高優先級的一元運算符?