GATE / CS ARCHIVE

SIMILAR QUESTIONS / COMPILER DESIGN

Compute LR item closure and goto sets

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

How these are solved

Move the dot for the specified transition then repeatedly add productions for a nonterminal after the dot. Initial versus supplied noninitial item sets and requested counts differ; preserve augmentation.

2 marks · NAT
Report issue

Compute an LR(0) GOTO closure

Parsing & Syntax Analysis

Question 65, GATE 2024: Augmented grammar: S′→S; S→SS | Aa | bAc | Bc | bBa; A→d#; B→@. Terminals are a,b,c,d,#,@. I0=CLOSURE({S′→·S}). For an SLR parser, how many items are in GOTO(I0,S)?
Tap to enlarge
Answer & solution

Official answer: 9

Step through it (3 steps)

Approach: Move dots over S only where S immediately follows the dot, then close the resulting item set.

1. Derivation. The initial closure contains S′→·S and S→·SS, plus the other four S-start items and A→·d#, B→·@. GOTO on S shifts the first two into S′→S· and S→S·S. The latter requires closure under S, adding all five S-start items: ·SS, ·Aa, ·bAc, ·Bc, ·bBa. Closure of their leading A/B adds A→·d# and B→·@. The total is 2+5+2=9 distinct LR(0) items; SLR FOLLOW sets do not change this item-set construction.

2. Explanation and checks. The full resulting set is S′→S·; S→S·S; S→·SS; S→·Aa; S→·bAc; S→·Bc; S→·bBa; A→·d#; B→·@. The item S→·SS does not generate endlessly many items: closure is a set and revisiting the same production adds no duplicate. Terminals b,d,@ do not cause further closure expansion. Both # and @ are explicitly terminals here, so neither denotes epsilon or the end marker. FOLLOW sets govern SLR reduction entries, not this GOTO construction.

Original paper, page 40 · Answer key

1 mark · NAT
Report issue

Parsing & Syntax Analysis

Question 29, GATE 2022: Grammar: S′→S; S→S+R|R; R→R*P|P; P→(S)|id. Let I0={S′→S·, S→S·+R}. Count items in goto(closure(I0),+).
Tap to enlarge
Answer & solution

Official answer: 5

Step through it (3 steps)

Taking goto on + advances S→S·+R to S→S+·R. Closure then adds R→·R*P, R→·P, P→·(S), and P→·id. No dot precedes S in these items, so no further closure is needed. Total 5 items.

Closure only expands a nonterminal immediately after a dot. Initially the dots precede end-of-rule or terminal +, so closure(I0)=I0. After shifting +, R is exposed; recursively expanding R and P yields the five items listed. The left-recursive R production is added once, so closure terminates.

Original paper, page 22 · Answer key

2 marks · NAT
Report issue

Parsing & Syntax Analysis

Question 51, GATE 2021: Augmented grammar S′→S; S→S#cS | SS | S@ | <S> | a | b | c. I0=CLOSURE({S′→·S}). Count LR(0) items in GOTO(GOTO(I0,<),<).
Tap to enlarge
Answer & solution

Official answer: 8

Step through it (4 steps)

On reading < from the initial closure the kernel is S→<·S>. Its closure adds all seven S productions with the dot at the beginning: S→·S#cS, ·SS, ·S@, ·<S>, ·a, ·b, ·c. Reading a second < reproduces exactly the same kernel and closure. Thus GOTO(GOTO(I0,<),<) has 1+7=8 items; the augmented S-prime item is not added because closure expands only S.

The resulting item set is {S→<·S>, S→·S#cS, S→·SS, S→·S@, S→·<S>, S→·a, S→·b, S→·c}. Recursive closure does not create infinitely many copies: items are set members. In particular, productions beginning with S are added once and their expansion adds no new productions beyond the seven already present.

Original paper, page 86 · Answer key

2 marks · NAT
Report issue

Closure after an LR(0) transition

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

1 mark · MCQ
Report issue

Canonical LR zero item sets

Parsing & Syntax Analysis

Question 7, GATE 2006: Grammar: S→S*E | E; E→F+E | F; F→id. Consider LR(0) items 1: S→S*·E; 2: E→F·+E; 3: E→F+·E. Which pair appears together in a canonical item set?
Options:
(A) 1 and 2
(B) 2 and 3
(C) 1 and 3
(D) None of these pairs
Tap to enlarge
Your answer
Answer & solution

Answer: D (no official key was published)

Step through it (12 steps)

1. The state after recognizing S* contains S→S*·E plus closure items E→·F+E,E→·F,F→·id. The item E→F·+E occurs after a transition on F, together with E→F·.

2. The item E→F+·E occurs after a further transition on +, again with the initial E/F closure. These three kernels are different states; closure does not introduce either of the other listed advanced items.

3. Thus none of the listed pairs coexist, D.

4. Teaching check: LR closure inserts productions with the dot at their beginning when a nonterminal follows the dot. It never advances an existing dot. Thus an item beginning E→·F can coexist with item 1, but the advanced E→F·+E requires a GOTO on F, producing another state.

Original paper, page 1