Table of Contents

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}\} $.

Click to display ⇲

Click to hide ⇱

$ S \leftarrow ASA | BSB | A | B | \epsilon $

9.1.2. $ L = \{ A^{m} B^{m+n} C^{n} \ | \: n, m \geq 0 \} $

Click to display ⇲

Click to hide ⇱

$ S \leftarrow XY $
$ X \leftarrow AXB | \epsilon $
$ Y \leftarrow BYC | \epsilon $

9.1.3. $ L = \{w \in \{a, b\}^* | \#_a(w) = \#_b(w) \} $

Click to display ⇲

Click to hide ⇱

Try #1:
$ S \leftarrow abS | baS | \epsilon $
incomplete because of: “aabb”
Try #2:
$ S \leftarrow aSb | bSa | SS | \epsilon $
incomplete because of: “abab”

Solution 1: We can either generate one a followed by one b, as well as one b followed by one a. At the same time, other sequences of equal number of a and b can appear freely, hence one solution is: $ S \leftarrow aSbS \mid bSaS \mid \epsilon$

9.1.4. $ L = \{w \in \{a, b\}^* | \#_a(w) \neq \#_b(w) \} $

Click to display ⇲

Click to hide ⇱

Start with a grammar that generates $ L_= = \{w \in \{a, b\}^* $ , for instance $ S \leftarrow aSbS $ . Next, we generate two grammars, one for $ L_> = \{w \in \{a, b\}^* $ , and another for $ L_< = \{w \in \{a, b\}^* $ , and we can combine them into our response. We illustrate writing a rule for the former.

The language $ L_>$ can be described by the following language operations: $ L_> = L_=(\{a\}L_=)^+$ . Note that $ \epsilon$ is member of $ L_=$ . If $ S$ is the start symbol for $ L_=$ , then our grammar is: $ S_> \leftarrow SaST, T \leftarrow aST $ .

9.1.5. $ L = \{a^ib^jc^k | i = j \lor j = k \} $

Click to display ⇲

Click to hide ⇱

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 $

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 $

Click to display ⇲

Click to hide ⇱

Ambiguuous: yes
  • S ⇒ aA ⇒ aB ⇒ abB ⇒ ab
  • S ⇒ A ⇒ aA ⇒ aB ⇒ abB ⇒ ab

Repaired, unambiguous grammar:

$ S \leftarrow 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 $

Click to display ⇲

Click to hide ⇱

Ambiguuous: yes
  • S ⇒ $ \epsilon $
  • S ⇒ AS ⇒ BS ⇒ S ⇒ $ \epsilon $

Repaired:
$ S \leftarrow 1BS' | S' $
$ S' \leftarrow ABS' | \epsilon $
$ A \leftarrow 0A1 | 01 $
$ B \leftarrow B1 | \epsilon $

9.2.3.

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

Click to display ⇲

Click to hide ⇱

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 $

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

Click to display ⇲

Click to hide ⇱

$ S \leftarrow aS | aaS | \epsilon $
Ambiguuous: yes
  • S ⇒ aS ⇒ aaS ⇒ aa
  • S ⇒ aaS ⇒ aa