简体   繁体   中英

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 .

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. 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.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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