The GATE Grind

GATE 2016 CS – Question 55

Compiler Design · Parsing and Syntax Analysis · 2 marks · Numerical answer

The attributes of three arithmetic operators in some programming language are given below.

OperatorPrecedenceAssociativityArity
+HighLeftBinary
-MediumRightBinary
*LowLeftBinary

The value of the expression $2 - 5 + 1 - 7 * 3$ in this language is ________.

Practise this question in The GATE Grind →

Show answer and explanation

Correct answer: 9

Explanation

Apply the highest precedence first, so $5 + 1 = 6$ and the expression becomes $2 - 6 - 7 * 3$. Next comes $-$, which is right associative, so $2 - 6 - 7$ means $2 - (6 - 7) = 2 - (-1) = 3$. Finally the lowest precedence operator $*$ gives $3 * 3 = 9$.