GATE / CS ARCHIVE

GATE CSE 2005 / QUESTION 83b

Expression evaluation with default shifts

Compiler Design · Parsing & Syntax Analysis · Full GATE CSE 2005 paper

GATE 2005, Q83b 2 marks · MCQ
Report issue

Parsing & Syntax Analysis

Question 83b, GATE 2005: Expression grammar E→number | E+E | E×E has the usual synthesized arithmetic values and is given to yacc without precedence/associativity declarations. Printed 83(b): under its default resolution, how is 3×2+1 interpreted? A equal precedence, left associative, result 7; B equal precedence, right associative, result 9; C ×higher, left associative, result 7; D +higher, left associative, result 9.
Question 83b, GATE 2005: Expression grammar E→number | E+E | E×E has the usual synthesized arithmetic values and is given to yacc without precedence/associativity declarations. Printed 83(b): under its default resolution, how is 3×2+1 interpreted? A equal precedence, left associative, result 7; B equal precedence, right associative, result 9; C ×higher, left associative, result 7; D +higher, left associative, result 9.
Question 83b, GATE 2005: Expression grammar E→number | E+E | E×E has the usual synthesized arithmetic values and is given to yacc without precedence/associativity declarations. Printed 83(b): under its default resolution, how is 3×2+1 interpreted? A equal precedence, left associative, result 7; B equal precedence, right associative, result 9; C ×higher, left associative, result 7; D +higher, left associative, result 9.
Question 83b, GATE 2005: Expression grammar E→number | E+E | E×E has the usual synthesized arithmetic values and is given to yacc without precedence/associativity declarations. Printed 83(b): under its default resolution, how is 3×2+1 interpreted? A equal precedence, left associative, result 7; B equal precedence, right associative, result 9; C ×higher, left associative, result 7; D +higher, left associative, result 9.
Tap to enlarge
Your answer
Answer & solution

Answer: B (no official key was published)

Using the default shift resolution from part(a), neither operator receives a special precedence declaration and pending operators are reduced from the right. Thus 3×2+1 is parsed as 3×(2+1)=9. This behaves as equal precedence and right associativity, option B. This conclusion is conditional on the default shift resolution, not on adding conventional arithmetic precedence declarations.

At the + following 3×2, shifting postpones reducing 3×2. The parser first combines 2+1, then multiplies that result by 3, giving 9. Applying the same shift default to repeated operators creates right association. This is not evidence of a declared higher precedence for+: the behavior is the equal-precedence default arising from unresolved conflicts. A user-supplied precedence declaration could intentionally change the result.

Original paper, page 12