Which of the following statements is TRUE for the grammar given below? S ->…

2017

Which of the following statements is TRUE for the grammar given below?

S -> (L) | a
L -> L,S | S

  1. A.

    The grammar can be parsed by LR(0) parser only

  2. B.

    The grammar can't be parsed by LR(0) but it can be parsed by SLR(1)

  3. C.

    The grammar can be parsed by LL(1) and LR(0) parsers

  4. D.

    The grammar can be parsed by LL(1) parser only

Attempted by 39 students.

Show answer & explanation

Correct answer: A

Concept

Parser-class hierarchy: For the LR family, LR(0) ⊆ SLR(1) ⊆ LALR(1) ⊆ LR(1). A grammar is LR(0) when its LR(0) item-set (canonical collection) has no shift–reduce or reduce–reduce conflict in any state — i.e. no state holds a completed item [A → α·] together with a shift item, and no state holds two completed items. Separately, a grammar with left recursion (e.g. L → L , S) can never be LL(1).

Application to this grammar

  1. LL(1) check: The production L L , S is immediately left-recursive, so the grammar is not LL(1). Any statement asserting LL(1) is therefore false.

  2. Build the LR(0) item sets. Augment the grammar with S → S and walk CLOSURE/GOTO state by state (dot · marks how far each item has been matched):

  1. I0 = {S′ → ·S, S → ·(L), S → ·a} — no completed item; pure shift/goto state.

  2. On '(' from I0: I2 = {S → (·L), L → ·L,S, L → ·S, S → ·(L), S → ·a} — no completed item; pure shift/goto state.

  3. On S from I2: I5 = {L → S·} — the only item in this state, so it is reduce-only; no shift item shares the state, hence no shift–reduce conflict.

  4. On L from I2: I4 = {S → (L·), L → L·,S} — both items have the dot immediately before a terminal (either ')' or ','), so this is a pure shift state with no completed item.

  5. On ')' from I4: I6 = {S → (L)·} — single completed item, reduce-only.

  6. On ',' from I4: I7 = {L → L,·S, S → ·(L), S → ·a} — no completed item; pure shift/goto state.

  7. On S from I7: I8 = {L → L,S·} — single completed item, reduce-only.

  8. On 'a' (from I0, I2, or I7): I3 = {S → a·} — single completed item, reduce-only.

  9. On S from I0: I1 = {S′ → S·} — the accept state.

Across all nine states, every state is either a pure shift/goto state (no completed item) or holds exactly one completed item (reduce-only) — no state ever mixes a completed item with a shift, and no state holds two completed items. So there is no LR(0) conflict anywhere: the grammar is LR(0).

Cross-check

Because LR(0) ⊆ SLR(1) ⊆ LR(1), an LR(0) grammar is automatically SLR(1) and LR(1) too — FOLLOW(L) = { ) , , } and FOLLOW(S) = { $ , ) , , } are never even needed to resolve a conflict, since none exists. Among the four statements, the only one consistent with “LR(0) = yes, LL(1) = no” is “The grammar can be parsed by LR(0) parser”. The word “only” is loose — every LR(0) grammar is trivially also SLR/LALR/LR(1) — but it is the intended reading, since it is the lone option that does not contradict the automaton just built.

Explore the full course: Niacl Ao It Specialist