GATE CS 2024 Set 2 — Question 65

NAT+2 / -0HardLR(0) & SLR(1)Syntax AnalysisCompiler Design

Compiler Design → Syntax Analysis → LR(0) & SLR(1)

Last updated

Question

Consider the following augmented grammar, which is to be parsed with a SLR parser. The set of terminals is {a,b,c,d,#,@}\{a, b, c, d, \#, @\}SSS' \rightarrow SSSSAabAcBcbBaS \rightarrow SS \mid Aa \mid bAc \mid Bc \mid bBaAd#A \rightarrow d\#B@B \rightarrow @Let I0=CLOSURE({SS})I_0 = CLOSURE(\{S' \rightarrow \bullet S\}). The number of items in the set GOTO(I0,S)GOTO(I_0, S) is ___________

Correct answer

9 to 9

Solution

To find the number of items in GOTO(I0,S)GOTO(I_0, S), we first determine the set I0I_0 and then compute the GOTOGOTO transition for the symbol SS.
1.Compute I0=CLOSURE({SS})I_0 = CLOSURE(\{S' \rightarrow \bullet S\}):
  • Start with the initial item: SSS' \rightarrow \bullet S.
  • Since the dot is before SS, add all productions of SS with the dot at the beginning:
  • SSSS \rightarrow \bullet SS
  • SAaS \rightarrow \bullet Aa
  • SbAcS \rightarrow \bullet bAc
  • SBcS \rightarrow \bullet Bc
  • SbBaS \rightarrow \bullet bBa
  • Now, check for non-terminals after the dot in the newly added items:
  • In SAaS \rightarrow \bullet Aa, the dot is before AA. Add AA productions: Ad#A \rightarrow \bullet d\#.
  • In SBcS \rightarrow \bullet Bc, the dot is before BB. Add BB productions: B@B \rightarrow \bullet @.
  • Thus, I0={SS,SSS,SAa,SbAc,SBc,SbBa,Ad#,B@}I_0 = \{S' \rightarrow \bullet S, S \rightarrow \bullet SS, S \rightarrow \bullet Aa, S \rightarrow \bullet bAc, S \rightarrow \bullet Bc, S \rightarrow \bullet bBa, A \rightarrow \bullet d\#, B \rightarrow \bullet @\}.
2. Compute GOTO(I0,S)GOTO(I_0, S):
  • GOTO(I0,S)=CLOSURE({AαSβAαSβI0})GOTO(I_0, S) = CLOSURE(\{A \rightarrow \alpha S \bullet \beta \mid A \rightarrow \alpha \bullet S \beta \in I_0\}).
  • The items in I0I_0 with SS immediately after the dot are SSS' \rightarrow \bullet S and SSSS \rightarrow \bullet SS.
  • Moving the dot over SS gives the kernel of the new state: {SS,SSS}\{S' \rightarrow S \bullet, S \rightarrow S \bullet S\}.
  • Now, compute the closure of this kernel:
  • SSS' \rightarrow S \bullet is already complete.
  • For SSSS \rightarrow S \bullet S, the dot is before SS, so we add all SS productions with the dot at the beginning:
  • SSSS \rightarrow \bullet SS
  • SAaS \rightarrow \bullet Aa
  • SbAcS \rightarrow \bullet bAc
  • SBcS \rightarrow \bullet Bc
  • SbBaS \rightarrow \bullet bBa
  • In SAaS \rightarrow \bullet Aa, the dot is before AA, so add Ad#A \rightarrow \bullet d\#.
  • In SBcS \rightarrow \bullet Bc, the dot is before BB, so add B@B \rightarrow \bullet @.
3. Count the items in GOTO(I0,S)GOTO(I_0, S):
The complete set of items is:
1. SSS' \rightarrow S \bullet
2. SSSS \rightarrow S \bullet S
3. SSSS \rightarrow \bullet SS
4. SAaS \rightarrow \bullet Aa
5. SbAcS \rightarrow \bullet bAc
6. SBcS \rightarrow \bullet Bc
7. SbBaS \rightarrow \bullet bBa
8. Ad#A \rightarrow \bullet d\#
9. B@B \rightarrow \bullet @There are 9 items in total.

More questions on Syntax Analysis

Practice GATE CS PYQs with adaptive difficulty

Timed practice, skill tracking, and AI explanations — free to start.

Start practicing free