GATE / CS ARCHIVE

SIMILAR QUESTIONS / COMPILER DESIGN

Trace yacc default shift conflict resolution

2 questions solved the same way, newest first. Back to Compiler Design

How these are solved

The linked subquestions share the ambiguous grammar E -> number | E+E | E*E with no precedence declarations. The first identifies the shift/reduce conflict and yacc default shift resolution; the second applies that resolution to 3*2+1 and asks precedence/associativity and value.

2 marks · MCQ
Report issue

Yacc default ambiguity resolution

Parsing & Syntax Analysis

Question 83a, 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(a): yacc A detects/eliminates recursion; B resolves reduce/reduce conflict; C resolves shift/reduce in favor of shift; D resolves shift/reduce in favor of reduce.
Question 83a, 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(a): yacc A detects/eliminates recursion; B resolves reduce/reduce conflict; C resolves shift/reduce in favor of shift; D resolves shift/reduce in favor of reduce.
Question 83a, 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(a): yacc A detects/eliminates recursion; B resolves reduce/reduce conflict; C resolves shift/reduce in favor of shift; D resolves shift/reduce in favor of reduce.
Tap to enlarge
Your answer
Answer & solution

Answer: C (no official key was published)

The grammar E→number | E+E | E×E is ambiguous. After recognizing E+E or E×E, a following operator permits either reduction of the completed expression or shifting to build the right operand further. This is a shift/reduce conflict. With no precedence/associativity declarations, yacc defaults to shifting, option C; left recursion itself is valid for LR parsing.

After E+E has been recognized and a further operator is seen, the parser can either reduce the completed expression now or shift the operator to extend its right operand. The grammar does not specify which grouping is intended, hence a shift/reduce conflict. yacc’s default selects shift. Left recursion is supported naturally by LR parsing and is not removed automatically; no two distinct completed production reductions cause this particular ambiguity.

Original paper, page 12

2 marks · MCQ
Report issue

Expression evaluation with default shifts

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