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
,这是不希望的。
我该怎么做高优先级的一元运算符?