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 Languages ====== ===== 9.1. Accepting and generating a CF language ===== Write a PDA as well as a CF grammar for the following languages. Specify - for each PDA, the way it accepts (by empty-stack or by final state). 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> <note important> $ S \leftarrow ASA | BSB | A | B | \epsilon $ </note> {{ :lfa:2022:lfa2022_lab9_1_1.png?400 |}} </hidden> **9.1.2.** $ L = \{ A^{m} B^{m+n} C^{n} \ | \: n, m \geq 0 \} $ <hidden> <note important> $ S \leftarrow XY $ \\ $ X \leftarrow AXB | \epsilon $ \\ $ Y \leftarrow BYC | \epsilon $ </note> {{ :lfa:2022:lfa2022_lab9_1_4.png?400 |}} </hidden> **9.1.3.** $ L = \{w \in \{a, b\}^* | \#_a(w) = \#_b(w) \} $ <hidden> <note warning> Try #1:\\ $ S \leftarrow aBS | baS | \epsilon $ \\ ambiguuous because of: “ababab” \\ Try #2:\\ $ S \leftarrow $ aSb | bSa | SS | $ \epsilon $ \\ ambiguuous because of: “abab” </note> <note important> **Idea:** A = rule that promises that one letter 'a' should come without a pair; B = rule that promises that one letter 'b' should come without a pair \\ $ S \leftarrow aBS | bAS | \epsilon $ \\ $ A \leftarrow a | bAA $ \\ $ B \leftarrow b | aBB $ </note> {{ :lfa:2022:lfa2022_lab9_1_3.png?300 |}} </hidden> **9.1.4.** $ L = \{w \in \{a, b\}^* | \#_a(w) \neq \#_b(w) \} $ <hidden> <note important> $ S \leftarrow A | B $ \\ $ A \leftarrow G |GG $ \\ $ B \leftarrow H | HH $ \\ $ G \leftarrow Ea $ \\ $ H \leftarrow Eb $ \\ $ E \leftarrow aME | bNE | \epsilon $ \\ $ M \leftarrow b | aMM $ \\ $ N \leftarrow a | bNN $ </note> {{ :lfa:2022:lfa2022_lab9_1_4.png?400 |}} </hidden> **9.1.5.** $ L = \{a^ib^jc^k | i = j \lor j = k \} $ \\ <hidden> <note important> Inherently ambiguous grammar because of the case i = j = k \\ $ S \leftarrow X | Y $ \\ $ X \leftarrow ZC $ \\ $ Z \leftarrow aZb | \epsilon $ \\ $ C \leftarrow cC | \epsilon $ \\ $ Y \leftarrow AT $ \\ $ A \leftarrow aA | \epsilon $ \\ $ T \leftarrow bTc | \epsilon $ \\ </note> {{ :lfa:2022:lfa2022_lab9_1_5.png?600 |}} </hidden> ===== 9.2. Ambiguous grammars ===== 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> <note important> Ambiguuous: yes * S => aA => aB => abB => ab * S => A => aA => aB => abB => ab Repaired, unambiguous grammar: $ S \leftarrow P_1 P_2 | B $ (this matches strings like (aa*b*)*a*b*) \\ $ P_1 \leftarrow aAbBP_1 | \epsilon $ (this matches strings like (a+b+)*) \\ $ P_2 \leftarrow aAB | \epsilon $ (this matches strings like (a+b*) | $\epsilon$) \\ $ A \leftarrow aA | \epsilon $ \\ $ B \leftarrow bB | \epsilon $ </note> </hidden> **9.2.2.** $ S \leftarrow AS | \epsilon $ \\ $ A \leftarrow 0A1 | 01 | B $\\ $ B \leftarrow B1 | \epsilon $ <hidden><note important> Ambiguuous: yes * S ⇒ $ \epsilon $ * S ⇒ AS ⇒ BS ⇒ S ⇒ $ \epsilon $ Repaired: \\ $ S \leftarrow AS | \epsilon $ \\ $ A \leftarrow A’B $ \\ $ A’ \leftarrow 0A’1 | \epsilon $ \\ $ B \leftarrow B1 | \epsilon $ </note></hidden> **9.2.3.** $ S \leftarrow ASB | BSA | \epsilon $\\ $ A \leftarrow aA | \epsilon $\\ $ B \leftarrow bB | \epsilon $ <hidden> <note important> Ambiguuous: yes * S ⇒ ε * S ⇒ ASB ⇒ SB ⇒ B ⇒ $ \epsilon $ The grammar actually generates the language {a,b}*: $ S \Rightarrow ASB \Rightarrow aASB \Rightarrow aSB \Rightarrow aS $ \\ $ S \Rightarrow BSA \Rightarrow bBSA \Rightarrow bSA \Rightarrow bS $ Repaired: \\ $ S \leftarrow aS | bS | \epsilon $ </note> </hidden> **9.2.4.** Write an ambiguous grammar for $ L(a^*) $. <hidden><note important> $ S \leftarrow aS | aaS | \epsilon $\\ Ambiguuous: yes\\ * S => aS => aaS => aa * S => aaS => aa </note></hidden>