Differences
This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
|
lfa:2022:lab09-cfl [2022/12/15 14:48] alexandra.udrescu01 |
lfa:2022:lab09-cfl [2022/12/17 11:05] (current) alexandra.udrescu01 |
||
|---|---|---|---|
| Line 7: | Line 7: | ||
| **9.1.1.** $ L = \{\: w \in \{A,B\}^* \ | \:w \text{ is a palindrome}\} $. | **9.1.1.** $ L = \{\: w \in \{A,B\}^* \ | \:w \text{ is a palindrome}\} $. | ||
| - | /* | ||
| <hidden> | <hidden> | ||
| Line 16: | Line 15: | ||
| </hidden> | </hidden> | ||
| - | */ | + | |
| **9.1.2.** $ L = \{ A^{m} B^{m+n} C^{n} \ | \: n, m \geq 0 \} $ | **9.1.2.** $ L = \{ A^{m} B^{m+n} C^{n} \ | \: n, m \geq 0 \} $ | ||
| - | /* | + | |
| <hidden> | <hidden> | ||
| Line 31: | Line 30: | ||
| </hidden> | </hidden> | ||
| - | */ | + | |
| **9.1.3.** $ L = \{w \in \{a, b\}^* | \#_a(w) = \#_b(w) \} $ | **9.1.3.** $ L = \{w \in \{a, b\}^* | \#_a(w) = \#_b(w) \} $ | ||
| - | /* | + | |
| <hidden> | <hidden> | ||
| <note warning> | <note warning> | ||
| Try #1:\\ | Try #1:\\ | ||
| - | $ S \leftarrow aBS | baS | \epsilon $ \\ | + | $ S \leftarrow abS | baS | \epsilon $ \\ |
| - | ambiguuous because of: “ababab” \\ | + | incomplete because of: “aabb” \\ |
| Try #2:\\ | Try #2:\\ | ||
| - | $ S \leftarrow $ aSb | bSa | SS | $ \epsilon $ \\ | + | $ S \leftarrow aSb | bSa | SS | \epsilon $ \\ |
| - | ambiguuous because of: “abab” | + | incomplete because of: “abab” |
| </note> | </note> | ||
| + | |||
| + | **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: | ||
| + | $math[S \leftarrow aSbS \mid bSaS \mid \epsilon] | ||
| + | /* | ||
| + | **Solution 2:** | ||
| + | |||
| <note important> | <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 \\ | + | **Idea:** A = rule that promises that exactly one letter 'a' is extra; B = rule that promises that exactly one letter 'b' is extra \\ |
| $ S \leftarrow aBS | bAS | \epsilon $ \\ | $ S \leftarrow aBS | bAS | \epsilon $ \\ | ||
| $ A \leftarrow a | bAA $ \\ | $ A \leftarrow a | bAA $ \\ | ||
| Line 53: | Line 59: | ||
| </note> | </note> | ||
| {{ :lfa:2022:lfa2022_lab9_1_3.png?300 |}} | {{ :lfa:2022:lfa2022_lab9_1_3.png?300 |}} | ||
| + | |||
| + | We can easily check by induction (over the length of the derivation sequence) that $math[A \Rightarrow^* w] iff $math[\#_A(w) = 1 + \#_B(w)]. | ||
| + | The basis case (length 1) is straightforward as $math[A \Rightarrow a]. Now suppose $math[A \Rightarrow bAA \Rightarrow^* bw_1w_2]. By induction hypothesis: $math[\#_A(bw_1w_2) = \#_A(w_1) + \#_A(w_2) = \#_B(w_1) + 1 + \#_B(w_2) + 1 = \#_B(bw_1w_2)+1]. | ||
| + | */ | ||
| </hidden> | </hidden> | ||
| Line 60: | Line 70: | ||
| - | /* | + | |
| <hidden> | <hidden> | ||
| + | /* | ||
| + | Solution 1: | ||
| + | |||
| <note important> | <note important> | ||
| $ S \leftarrow A | B $ \\ | $ S \leftarrow A | B $ \\ | ||
| Line 75: | Line 88: | ||
| </note> | </note> | ||
| {{ :lfa:2022:lfa2022_lab9_1_4.png?400 |}} | {{ :lfa:2022:lfa2022_lab9_1_4.png?400 |}} | ||
| - | </hidden> | + | |
| + | Solution 2: | ||
| */ | */ | ||
| + | Start with a grammar that generates $math[L_= = \{w \in \{a, b\}^* | \#_a(w) = \#_b(w) \}], for instance $math[S \leftarrow aSbS | bSaS | \epsilon]. Next, we generate two grammars, one for $math[L_> = \{w \in \{a, b\}^* | \#_a(w) > \#_b(w) \}], and another for $math[L_< = \{w \in \{a, b\}^* | \#_a(w) < \#_b(w) \}], and we can combine them into our response. We illustrate writing a rule for the former. | ||
| + | |||
| + | The language $math[L_>] can be described by the following language operations: $math[L_> = L_=(\{a\}L_=)^+]. Note that $math[\epsilon] is member of $math[L_=]. If $math[S] is the start symbol for $math[L_=], then our grammar is: $math[S_> \leftarrow SaST, T \leftarrow aST | \epsilon]. | ||
| + | |||
| + | </hidden> | ||
| + | |||
| **9.1.5.** $ L = \{a^ib^jc^k | i = j \lor j = k \} $ \\ | **9.1.5.** $ L = \{a^ib^jc^k | i = j \lor j = k \} $ \\ | ||
| - | /* | + | |
| <hidden> | <hidden> | ||
| Line 97: | Line 117: | ||
| </hidden> | </hidden> | ||
| - | */ | + | |
| ===== 9.2. Ambiguous grammars ===== | ===== 9.2. Ambiguous grammars ===== | ||
| Line 109: | Line 129: | ||
| $ B \leftarrow bB | \epsilon $ | $ B \leftarrow bB | \epsilon $ | ||
| - | /* | + | |
| <hidden> | <hidden> | ||
| Line 129: | Line 149: | ||
| </hidden> | </hidden> | ||
| - | */ | + | |
| **9.2.2.** | **9.2.2.** | ||
| Line 137: | Line 157: | ||
| $ B \leftarrow B1 | \epsilon $ | $ B \leftarrow B1 | \epsilon $ | ||
| - | /* | + | |
| <hidden><note important> | <hidden><note important> | ||
| Line 154: | Line 174: | ||
| </note></hidden> | </note></hidden> | ||
| - | */ | + | |
| **9.2.3.** | **9.2.3.** | ||
| Line 162: | Line 182: | ||
| $ B \leftarrow bB | \epsilon $ | $ B \leftarrow bB | \epsilon $ | ||
| - | /* | + | |
| <hidden> | <hidden> | ||
| Line 180: | Line 200: | ||
| </hidden> | </hidden> | ||
| - | */ | + | |
| **9.2.4.** Write an ambiguous grammar for $ L(a^*) $. | **9.2.4.** Write an ambiguous grammar for $ L(a^*) $. | ||
| - | /* | + | |
| <hidden><note important> | <hidden><note important> | ||
| Line 193: | Line 213: | ||
| </note></hidden> | </note></hidden> | ||
| - | */ | + | |