简体   繁体   English

ecmascript规范的语法令人困惑

[英]Confusing on the syntax of ecmascript specification

I am confusing on the following specification rules. 我对以下规范规则感到困惑。 Why is an "AdditiveExpression" should consist of another "AdditiveExpression" and "MultiplicativeExpression"? 为什么“ AdditiveExpression”应由另一个“ AdditiveExpression”和“ MultiplicativeExpression”组成?

As the following rules say, we should evaluate the "AdditiveExpression" on the left side and the "MultiplicativeExpression" on the right side first, but what should the rule do if the right side of the operand is not the evaluating of "MultiplicativeExpression" but a "AdditiveExpression"? 如以下规则所述,我们应该首先在左侧评估“ AdditiveExpression”,在右侧评估“ MultiplicativeExpression”,但是如果操作数的右侧不是“ MultiplicativeExpression”的评估,那么该规则应该怎么做?一个“ AdditiveExpression”?

电子书规范

The syntax says the right operand of + must be a MultiplicativeExpression. 语法表明+的正确操作数必须是MultiplicativeExpression。 If it isn't, you get a syntax error and nothing is evaluated. 如果不是,则会出现语法错误,并且不会评估任何内容。

For example, the expression 1 + 2 + 3 is parsed like this: 例如,表达式1 + 2 + 3的解析如下:

  • 1 is a NumericLiteral , which is a Literal , which is a PrimaryExpression . 1NumericLiteral ,它是Literal ,它是PrimaryExpression

  • A PrimaryExpression is a MemberExpression , which is a NewExpression , which is a LeftHandSideExpression , which is an UpdateExpression , which is a UnaryExpression , which is an ExponentiationExpression , which is a MultiplicativeExpression . PrimaryExpressionMemberExpression ,这是一个NewExpression ,这是一个LeftHandSideExpression ,这是一个UpdateExpression ,这是一个UnaryExpression ,这是一个ExponentiationExpression ,这是一个MultiplicativeExpression

  • A MultiplicativeExpression is an AdditiveExpression . MultiplicativeExpressionAdditiveExpression

  • + is ... + . +是... +

  • 2 is a NumericLiteral , which is (by the chain listed above) a MultiplicativeExpression . 2NumericLiteral ,根据上面列出的链,它是MultiplicativeExpression

  • At this point the rule AdditiveExpression : AdditiveExpression + MultiplicativeExpression (quoted in your question) matches: 1 + 2 is parsed as an AdditiveExpression . 此时,规则AdditiveExpression : AdditiveExpression + MultiplicativeExpression (在您的问题中引用)匹配: 1 + 2被解析为AdditiveExpression

  • We have another + , which is just + . 我们还有另一个+ ,就是+

  • 3 is a NumericLiteral , which is (by the chain listed above) a MultiplicativeExpression . 3NumericLiteral ,根据上面列出的链,它是一个MultiplicativeExpression

  • Now AdditiveExpression : AdditiveExpression + MultiplicativeExpression applies again (with 1 + 2 being the AdditiveExpression and 3 being the MultiplicativeExpression ). 现在AdditiveExpression : AdditiveExpression + MultiplicativeExpression再次适用( 1 + 2AdditiveExpression3MultiplicativeExpression )。

  • The whole thing is now an AdditiveExpression . 现在整个事情就是一个AdditiveExpression

Why does the "AdditiveExpression" should consist of another "AdditiveExpression" and "MultiplicativeExpression"? 为什么“ AdditiveExpression”应该由另一个“ AdditiveExpression”和“ MultiplicativeExpression”组成?

So that you can nest chain additive expression indefinitely: 1 + 1 + 1 + 1 + … 这样您就可以无限期嵌套链加法表达式: 1 + 1 + 1 + 1 + …

what should the rule do if the right side of the operand is not the evaluating of "MultiplicativeExpression" but a "AdditiveExpression"? 如果操作数的右侧不是对“ MultiplicativeExpression”的求值而是对“ AdditiveExpression”的求值,该规则应该怎么做?

That just doesn't happen. 那只是没有发生。 The expression 1 + 1 + 1 does parse as (1 + 1) + 1 (where the right side is a MultiplicativeExpression), not as 1 + (1 + 1) . 表达式1 + 1 + 1解析为(1 + 1) + 1 (其中右侧 MultiplicativeExpression),而不是1 + (1 + 1)

The single number can be parsed as a "MultiplicativeExpression"? 单个数字可以解析为“ MultiplicativeExpression”吗?

Yes. 是。 The single number on the right side is a multiplicative expression, the single number on the lift side is an addititive expression : 右侧的单个数字是一个乘法表达式,升降机一侧的单个数字是一个可加表达式

AdditiveExpression[Yield, Await]: MultiplicativeExpression[?Yield, ?Await]

that is also a multiplicative expression , which is an ExponentiationExpression , which is an UnaryExpression , which is a UpdateExpression which is a LeftHandSideExpression , which is a NewExpression , which is a MemberExpression , which is a PrimaryExpression , which is a Literal , which consists of NumericLiteral lexem. 这也是一个乘法表达 ,这是一种ExponentiationExpression ,这是一个UnaryExpression ,这是一个UpdateExpression其是LeftHandSideExpression ,这是一个NewExpression ,这是一个MemberExpression ,这是一个PrimaryExpression ,这是一个文字 ,它由NumericLiteral lexem。

This kind of nesting is how operator precedence is determined in the JS grammar. 这种嵌套是在JS语法中确定运算符优先级的方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM