GATE / CS ARCHIVE

GATE CSE 2019 / QUESTION 43

Closure after an LR(0) transition

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

GATE 2019, Q43 2 marks · NAT
Report issue

Parsing & Syntax Analysis

Question 43, GATE 2019: Given augmented grammar S′→S; S→(L)|id; L→L,S|S, let I0=CLOSURE({S′→·S}). How many distinct LR(0) items are in GOTO(I0, '(')? The comma in L→L,S is a terminal.
Tap to enlarge
Answer & solution

Official answer: 5

Step through it (3 steps)

1. An LR(0) item is a production with a dot marking how much has been recognized. Closure adds productions of any nonterminal immediately after a dot, with their dots at the start.
2. I0 contains S′→·S, S→·(L), and S→·id. Only S→·(L) has an opening parenthesis immediately after its dot.
3. GOTO on '(' advances that dot, yielding the kernel item S→(·L). Its dot precedes L, so add L→·L,S and L→·S.
4. The first of these new items again has L after its dot, but adding L's productions repeats existing items. The second has S after its dot, so add S→·(L) and S→·id.
5. The last two dots precede terminals, so closure stops. The complete set is {S→(·L), L→·L,S, L→·S, S→·(L), S→·id}: exactly 5 items.
6. Do not carry S′→·S into this destination merely because it was in I0. GOTO keeps shifted matching items and then their closure, not the entire starting state.

Original paper, page 14 · Answer key