中文版第 78 页的一个关键错误:

来看一个例子,假设正在解析表达式语句 -1+2;

这里将 PREFIX 作为优先级传递给 parseExpression,将 PREFIX 变成了这个 parseExpression 调用的右约束能力。根据定义 PREFIX 的优先级很高。结果是 parseExpression(PREFIX) 不会解析 -1 中的 1,而会将其传递给另一个 infixParseFn。在这种情况下,precedence < p.peekPrecedence() 一直为 false,即 infixParseFn 不会将 1 作为表达式的左半边,而是将 1 作为前缀的“右半边”返回。这个右半边只有一个 1,没有随后需要解析的其他表达式。

找来原文:

This passes PREFIX to parseExpression as precedence, turning PREFIX into the right-binding power of that parseExpression invocation. PREFIX is a really high precedence, as per our definition. The result of this is that parseExpression(PREFIX) is never going to parse the 1 in -1 and pass it to another infixParseFn. The precedence < p.peekPrecedence() will never be true in this case, meaning that no other infixParseFn is going to get our 1 as the left arm. > Instead the 1 is returned as the “right” arm of our prefix expression. Just the 1, not some other expression that comes after and needs to be parsed.

其中这句

The result of this is that parseExpression(PREFIX) is never going to parse the 1 in -1 and pass it to another infixParseFn.

原文表述有歧义,而中文版则完全没有理解 Pratt 解析器的工作原理,翻译错误。这句话是理解 Pratt 工作原理最重要的一句。

更准确的英文表述应该是什么?

The result of this is that, although parseExpression(PREFIX) does parse the 1 in -1, it will never pass that 1 to another infixParseFn as the left-hand side of an infix expression.

所以更准确的中文:

结果是,parseExpression(PREFIX) 会解析 -1 中的 1,但不会将其传递给另一个 infixParseFn。因为在这种情况下,precedence < p.peekPrecedence() 一直为 false,不会有 infixParseFn1 作为表达式的左半边。而是会跳过循环,将 1 返回,成为前缀的“右半边”。这个右半边只有一个 1,没有随后需要解析的其他表达式。