Grammars

Writing Grammars

1. Write grammars for the following languages:

1.1. $ L = \{a^{m}b^{m + n}c^{n} | n, m \geq 0 \} $
1.2. $ L = \{a^ib^jc^k | i = j \lor j = k \} $
1.3. $ L = \{w \in \{a, b\}^* | \#_a(w) \neq \#_b(w) \} $

Ambiguity

2. Which of the following grammars are ambiguous? Justify each answer. Modify the grammar to remove ambiguity, wherever the case.

2.1.

$ S \leftarrow aA | A $
$ A \leftarrow aA | B $
$ B \leftarrow bB | \epsilon $

2.2.

$ S \leftarrow AS | \epsilon $
$ A \leftarrow 0A1 | 01 | B $
$ B \leftarrow B1 | \epsilon $

2.3.

$ S \leftarrow ASB | BSA | \epsilon $
$ A \leftarrow aA | \epsilon $
$ B \leftarrow bB | \epsilon $

3. Write an ambiguous grammar for $ L(a^*) $.

Regular Grammars

4. Is the language described by the following grammar regular? If so, write a regular expression for it.

$ S \leftarrow aA $
$ A \leftarrow aA | B $
$ B \leftarrow Bb | \epsilon $

5. Write a regular expression for the language described by:

$ S \leftarrow aX $
$ X \leftarrow bY | S $
$ Y \leftarrow aX | bS | \epsilon $

6. Write a regular grammar for $ L((0 \cup 1^*)^*01^*) $.

Chomsky Normal Form

7. Remove “$ \epsilon \text{-rules} $” from the following grammar:

$ A \leftarrow \epsilon | B $
$ B \leftarrow b $
$ B \leftarrow ABC | BAC $
$ C \leftarrow AC | c$

8. Remove the “unit rules” from the previous grammar, after “$ \epsilon \text{-rules} $” have been removed.

9. Apply the CNF conversion rules to the solution for 1.1. Does the accepted language stay the same?