9. Context-Free Grammars
9.1. Generating a CF language
Write CF grammar for the following languages. For each grammar, make sure it is not ambiguous. (Start with any CF grammar that accepts L. Then write another non-ambiguous grammar for the same language).
9.1.1. $ L = \{\: w \in \{a,b\}^* \ | \:w \text{ is a palindrome}\} $.
9.1.2. $ L = \{ a^{m} b^{m+n} c^{n} \ | \: n, m \geq 0 \} $
9.1.3. $ L = \{w \in \{a, b\}^* | \#_a(w) = \#_b(w) \} $
9.1.4. $ L = \{w \in \{a, b\}^* | \#_a(w) \neq \#_b(w) \} $
9.1.5. $ L = \{a^ib^jc^k | i = j \lor j = k \} $
9.2. Ambiguity
Which of the following grammars are ambiguous? Justify each answer. Modify the grammar to remove ambiguity, wherever the case.
9.2.1.
$ S \leftarrow aA\ |\ A $
$ A \leftarrow aA\ |\ B $
$ B \leftarrow bB\ |\ \epsilon $
9.2.2.
$ S \leftarrow AS\ |\ \epsilon $
$ A \leftarrow 0A1\ |\ 01\ |\ B $
$ B \leftarrow B1\ |\ \epsilon $
9.2.3.
$ S \leftarrow ASB\ |\ BSA\ |\ \epsilon $
$ A \leftarrow aA\ |\ \epsilon $
$ B \leftarrow bB\ |\ \epsilon $
9.2.4. Write an ambiguous grammar for $ L(a^*) $.
9.3 Regular Grammars
Definition: A regular grammar is a CF grammar where the production rules follow one of these 2 patterns:
- all of them are of the form:
- $ X \leftarrow aY $
- $ X \leftarrow Y $
- $ X \leftarrow a $
- $ X \leftarrow \epsilon $
- OR all of them are of the form:
- $ X \leftarrow Ya $
- $ X \leftarrow Y $
- $ X \leftarrow a $
- $ X \leftarrow \epsilon $
9.3.1. (warmup) Find a DFA or an NFA that accepts the language generated by the following regular grammar:
$ X \leftarrow 0X\ |\ 1Y $
$ Y \leftarrow 1Y\ |\ 0\ |\ \epsilon $
9.4.1. Prove that the languages accepted by regular grammars are exactly the regular languages
- prove that any regular grammar generates a regular language: start from a regular grammar G and construct an NFA $ N $ such that $ L(N) = L(G) $
- prove that any regular language is generated by a regular grammar: start from a DFA $ A $ and construct a regular grammar such that $ L(G) = L(A) $
- write the proofs for both definitions (the one with $ X \leftarrow aY $ and the one with $ X \leftarrow Ya $).
- Hint: for the first definition, think top-down; for the second one, think bottom-up;
- Hint 2: use induction in the proofs