GATE / CS ARCHIVE

GATE CSE 2002 / QUESTION 22(b)

Operator precedence in the ambiguous expression grammar

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

GATE 2002, Q22(b) Descriptive
Report issue

Parsing & Syntax Analysis

Question 22(b), GATE 2002: Grammar E→E+E | E*E | id. Original22(b): What precedence, if any, does this grammar impose on * and +?
Question 22(b), GATE 2002: Grammar E→E+E | E*E | id. Original22(b): What precedence, if any, does this grammar impose on * and +?
Question 22(b), GATE 2002: Grammar E→E+E | E*E | id. Original22(b): What precedence, if any, does this grammar impose on * and +?
Tap to enlarge
Answer & solution

Answer: No relative precedence is specified or enforced by this grammar. (no official key was published)

Both + and * are produced by the same nonterminal E, and the grammar allows both i+(j*k) and (i+j)*k for the same token string. It therefore does not enforce multiplication-before-addition or addition-before-multiplication. Saying merely that the operators have equal precedence is also incomplete: the grammar does not enforce associativity, so it does not define a unique parse even under that description. External parser precedence declarations could resolve conflicts, but they are not part of the supplied grammar.

Original paper, page 10