GATE / CS ARCHIVE

GATE CSE 2017 SET 2 / QUESTION 32

GATE CSE 2017 Set 2 Q32

Compiler Design · Parsing & Syntax Analysis · Full GATE CSE 2017 Set 2 paper

GATE 2017 Set 2, Q32 2 marks · MCQ
Report issue

Parsing & Syntax Analysis

Question 32, GATE 2017: Grammar G is E→E−T|T, T→T+F|F, F→(E)|id. Choose an equivalent grammar without left recursion. A:the same grammar. B:E→TE′, E′→−TE′|ε, T→T+F|F, F→(E)|id. C:E→TX, X→−TX|ε, T→FY, Y→+FY|ε, F→(E)|id. D:E→TX|(TX), X→−TX|+TX|ε, T→id.
Tap to enlarge
Your answer
Answer & solution

Official answer: C

1. Apply the standard direct-left-recursion transformation A→Aα|β becomes A→βA′, A′→αA′|ε. For E, α is −T and β is T, giving E→TX and X→−TX|ε.

2. Apply it separately to T: α is +F and β is F, giving T→FY and Y→+FY|ε. Keep F→(E)|id so arbitrarily nested factors remain possible. These are exactly option C.

3. A leaves both original recursions intact. B fixes E but leaves T→T+F directly left-recursive. D restricts T to id and cannot generate nested expressions such as ((id)) or parentheses as arbitrary factors. The transformation preserves the generated strings, although semantic associativity must be handled when constructing evaluation actions.

Original paper, page 12 · Answer key