Question

2. (10 points) Explain why associativity and precedence rules are necessary and how they are used to interpret the meaning of

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer:------------

Precedence and associativity rules are necessary for deterministic evaluation of expressions.
The operators are summarized in below Table:----------

Postfix operators ---------------------------- [] . (parameters) expression++ expression--

Unary prefix operators -------- ++expression --expression +expression -expression ~ !

Unary prefix creation and cast ---------------------------- new (type)

Multiplicative ---------------------------- * / %

Additive ---------------------------- + -

Shift ---------------------------- << >> >>>

Relational ---------------------------- < <= > >= instanceof

Equality ---------------------------- == !=

Bitwise/logical AND ---------------------------- &

Bitwise/logical XOR ---------------------------- ^

Bitwise/logical OR ---------------------------- |

Conditional AND ---------------------------- &&

Conditional OR ---------------------------- ||

Conditional ---------------------------- ?:

Assignment ---------------------------- = += -= *= /= %= <<= >>= >>>= &= ^= |=

Note:----

  • Operators within the same row have the same precedence.
  • Parentheses, ( ), can be used to override precedence and associativity.
  • The unary operators, which require one operand, include the postfix increment (++) and decrement (--) operators from the first row, all the prefix operators (+, -, ++, --, ~, !) in the second row, and the prefix operators (object creation operator new, cast operator (type)) in the third row.
  • The conditional operator (? :) is ternary, that is, requires three operands.
  • All operators not listed above as unary or ternary, are binary, that is, require two operands.
  • All binary operators, except for the relational and assignment operators, associate from left to right. The relational operators are non associative.
  • Except for unary postfix increment and decrement operators, all unary operators, all assignment operators, and the ternary conditional operator associate from right to left.

Precedence rules are used to determine which operator should be applied first if there are two operators with different precedence, and these follow each other in the expression.

In such a case, the operator with the highest precedence is applied first.
2 + 3 * 4 is evaluated as 2 + (3 * 4) (with the result 14) since * has higher precedence than +.

Associativity rules are used to determine which operator should be applied first if there are two operators with the same precedence, and these follow each other in the expression.

Left associativity implies grouping from left to right:

1 + 2 - 3 is interpreted as ((1 + 2) - 3), since the binary operators + and - both have same precedence and left associativity.

Right associativity implies grouping from right to left:

- - 4 is interpreted as (- (- 4)) (with the result 4), since the unary operator - has right associativity.

The precedence and associativity rules together determine the evaluation order of the operators.

Add a comment
Know the answer?
Add Answer to:
2. (10 points) Explain why associativity and precedence rules are necessary and how they are used...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
Active Questions
ADVERTISEMENT