Question

Consider the following two sets of LR(1) items of an LR(1) grammar.

X→c.X,c/dX→c.X,$X→.cX,c/dX→.cX,$X→.d,c/dX→.d,$\begin{array}{ll} X \rightarrow c.X, c/d & X \rightarrow c.X, \$ \\ X \rightarrow .cX, c/d & X \rightarrow .cX, \$ \\ X \rightarrow .d, c/d & X \rightarrow .d, \$ \end{array}

Which of the following statements related to merging of the two sets in the corresponding LALR parser is/are FALSE?

  1. Cannot be merged since look aheads are different.
  2. Can be merged but will result in S-R conflict.
  3. Can be merged but will result in R-R conflict.
  4. Cannot be merged since goto on c will lead to two different sets.
Choose one option
Official answer and solution
Official answer
D
From
Official GATE 2013 answer key

Solution

Examine the rules for merging LR(1) item sets in LALR parser generation and check for shift-reduce and reduce-reduce conflicts.

Two LR(1) states can be merged in an LALR(1) parser if and only if they have the identical core (the set of first components of items, ignoring lookaheads).

Here, the two sets of LR(1) items are:

  • Set 1:
    X→c.X,c/dX \rightarrow c.X, c/d
    X→.cX,c/dX \rightarrow .cX, c/d
    X→.d,c/dX \rightarrow .d, c/d
  • Set 2:
    X→c.X,$X \rightarrow c.X, \text{\$}
    X→.cX,$X \rightarrow .cX, \text{\$}
    X→.d,$X \rightarrow .d, \text{\$}

Both sets have the exact same core:

{X→c.X,X→.cX,X→.d}\{X \rightarrow c.X,\quad X \rightarrow .cX,\quad X \rightarrow .d\}

Merging the two sets yields:

X→c.X,c/d/$X \rightarrow c.X, c/d/\text{\$}
X→.cX,c/d/$X \rightarrow .cX, c/d/\text{\$}
X→.d,c/d/$X \rightarrow .d, c/d/\text{\$}

Let us evaluate each statement:

  1. "Cannot be merged since lookaheads are different": FALSE. LALR specifically merges sets with the same core regardless of lookahead differences.
  2. "Can be merged but will result in S-R conflict": FALSE. A shift-reduce conflict requires at least one complete item (A→α.A \rightarrow \alpha.) where the dot is at the end, and a shift item on the same terminal. Here, none of the items has the dot at the end (X→c.XX \rightarrow c.X has dot before non-terminal XX). There are no reduce items, so no S-R conflict can occur.
  3. "Can be merged but will result in R-R conflict": FALSE. A reduce-reduce conflict requires two distinct complete items. With no reduce items present, no R-R conflict can occur.
  4. "Cannot be merged since goto on c will lead to two different sets": FALSE. The goto on cc from both sets will produce sets having identical cores, which are themselves merged in the LALR parser.

Since all statements 1, 2, 3, and 4 are FALSE, option (D) is correct.

Final answer

D

The answer is the one in the official GATE answer key. The solution is this site's own explanation.

Source

Paper
GATE 2013 Computer Science and Information Technology (CS)
Question
40 of 65, printed as CS Q.40
Section
Computer Science and Information Technology

Report an error in this question

GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.