GATE / CS ARCHIVE

GATE CSE 2016 SET 1 / QUESTION 45

GATE CSE 2016 Set 1 Q45

Compiler Design · Parsing & Syntax Analysis · Full GATE CSE 2016 Set 1 paper

GATE 2016 Set 1, Q45 2 marks · NAT
Report issue

Parsing & Syntax Analysis

Question 45, GATE 2016: Three binary operators have these language-specific rules: + has highest precedence and is left-associative; − has medium precedence and is right-associative; * has lowest precedence and is left-associative. Evaluate 2−5+1−7*3.
Tap to enlarge
Answer & solution

Official answer: 9.0 : 9.0

Our answer: 9

Addition has highest precedence, giving 5+1=6. The remaining subtraction is right-associative, so 2−6−7 groups as 2−(6−7)=3. Multiplication has lowest precedence, producing 3*3=9.

Insert parentheses in precedence order before calculating: the expression becomes (2−(5+1)−7)*3, then right associativity changes the subtraction part to 2−((5+1)−7). The inner result 6−7 is−1, so the outer result is 3 and multiplying by 3 gives 9. Ordinary arithmetic precedence would put multiplication first and produce a different result; this question intentionally defines a different expression grammar.

Original paper, page 17 · Answer key