Related Articles
GATE | GATE CS Mock 2018 | Set 2 | Question 40
• Last Updated : 08 Mar, 2018

Consider the following grammars G1 and G2 respectively.

Grammar – G1:

```A1 → A2A3
A2 → A3A1 | b
A3 → A1A1 | a
```

Grammar – G2:

```S → AA | 0
A → SS | 1
```

Consider the following statements regarding above grammars:

(I): This grammar is not left recursive and equivalent to grammar – G1:

```A1 → A2A3
A2 →  A3A1 | b
A3 → a | bA3A1 | aK | bA3A1K
k → A1A3A1 | A1A3A1K
```

(II): This grammar is not left recursive and equivalent to grammar – G2:

```S → A A | 0
A → 0S | 1 | 0SK | 1K
K → AS | ASK
```

(A) Only statement (I) is correct
(B) Only statement (II) is correct
(C) Both statements (I) and (II) are correct
(D) Neither statement (I) not (II) is correct

Answer: (C)

Explanation: Grammar – G1:

```A1 → A2A3
A2 → A3A1 | b
A3 → A1A1 | a
```

Replace A3 → A1 A1 by A3 → A2A3A1 and then replace this by A3 → A3A1A3A1 and A3 → bA3A1.

Eliminating direct left recursion in the above, gives:

```A3 → a | bA3A1 | aK | b A3A1K
k → A1 A3 A1 | A1 A3 A1K
```

The resulting grammar is then:

```A1 → A2A3
A2 →  A3A1 | b
A3 → a | bA3A1 | aK | bA3A1K
k → A1A3A1 | A1A3A1K
```

Grammar – G2:

```S → AA | 0
A → SS | 1
```

Considering the ordering S, A, we get:

```S → AA | 0
A → AAS | 0S | 1
```

and removing immediate left recursion, we get

```S → A A | 0
A → 0S | 1 | 0SK | 1K
K → AS | ASK
```

So, both statements (I) and (II) are correct.

Quiz of this Question My Personal Notes arrow_drop_up