简体   繁体   English

“优先爬升方法”中这种语法产生的目的是什么?

[英]What is the purpose of this grammar production in “Precedence Climbing Method”?

I saw an example of the so called "Precedence Climbing Method" for representing operator precedence in a formal grammar on Wikipedia here . 我在此处看到了一个所谓的“优先爬升方法”的示例,该示例用于在Wikipedia上的正式语法中表示运算符优先级。

It gives the following grammar as an example: 它以以下语法为例:

expression ::= equality-expression
equality-expression ::= additive-expression ( ( '==' | '!=' ) additive-expression ) *
additive-expression ::= multiplicative-expression ( ( '+' | '-' ) multiplicative-expression ) *
multiplicative-expression ::= primary ( ( '*' | '/' ) primary ) *
primary ::= '(' expression ')' | NUMBER | VARIABLE | '-' primary

I don't understand what the purpose of the '-' primary at the end of the last production is. 我不明白最后一次制作结束时'-' primary的目的是什么。 Can anyone explain why it is constructed this way? 谁能解释为什么以这种方式构造它?

It's unary minus, which binds more tightly than multiplicative operators: 它是一元负号,与乘法运算符的绑定更紧密:

-a * 7

(Not that it makes much difference in that case.) (并不是在那种情况下有很大的区别。)

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

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