Edit this page Backlinks This page is read only. You can view the source, but not change it. Ask your administrator if you think this is wrong. ====== 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}\} $. <hidden Solution 9.1.1> $ S \leftarrow aSa\ |\ bSb \ |\ a \ |\ b \ |\ \epsilon $ \\ </hidden> **9.1.2.** $ L = \{ a^{m} b^{m+n} c^{n} \ | \: n, m \geq 0 \} $ <hidden Solution 9.1.2> $ S \leftarrow AB $ \\ $ A \leftarrow aAb\ |\ \ \epsilon $ \\ $ B \leftarrow bBc\ |\ \ \epsilon $ \\ </hidden> **9.1.3.** $ L = \{w \in \{a, b\}^* | \#_a(w) = \#_b(w) \} $ <hidden Solution 9.1.3> $ S \leftarrow aBS\ |\ bAS\ |\ \epsilon $ \\ $ A \leftarrow a\ |\ bAA $ \\ $ B \leftarrow b\ |\ aBB $ \\ </hidden> **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 \} $ \\ <hidden Solution 9.1.5> $ S \leftarrow C\ |\ A $ \\ $ C \leftarrow Cc\ |\ B $ \\ $ B \leftarrow aBb\ |\ \ \epsilon $ \\ $ A \leftarrow aA\ |\ D $ \\ $ D \leftarrow bDc\ |\ \ \epsilon $ \\ </hidden> ===== 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 $ <hidden Solution 9.2.1> $ S \leftarrow A $ \\ $ A \leftarrow aA\ |\ B $ \\ $ B \leftarrow bB\ |\ \epsilon $ </hidden> **9.2.2.** $ S \leftarrow AS\ |\ \epsilon $ \\ $ A \leftarrow 0A1\ |\ 01\ |\ B $\\ $ B \leftarrow B1\ |\ \epsilon $ <hidden Solution 9.2.2> $ S \leftarrow AS\ |\ A $ \\ $ A \leftarrow 0A1\ |\ B $\\ $ B \leftarrow B1\ |\ \epsilon $ </hidden> **9.2.3.** $ S \leftarrow ASB\ |\ BSA\ |\ \epsilon $\\ $ A \leftarrow aA\ |\ \epsilon $\\ $ B \leftarrow bB\ |\ \epsilon $ <hidden Solution 9.2.3> $ S \leftarrow aS\ |\ bS\ |\ \epsilon $ \\ </hidden> **9.2.4.** Write an ambiguous grammar for $ L(a^*) $. <hidden Solution 9.2.4> $ S \leftarrow aS \ |\ A\ |\ \epsilon $\\ $ A \leftarrow aA\ |\ \epsilon $\\ </hidden> ===== 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 $ <hidden Solution 9.3.1> Y produces $ 1^* (0 | \epsilon) $ X produces $ 0^* 1Y \rightarrow 0^* 1^+ (0 | \epsilon) $ </hidden> **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