Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
lfa:2022:lab09-cfl [2022/12/15 15:02]
pdmatei
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 $ \\
 incomplete because of: “aabb” \\ incomplete because of: “aabb” \\
 Try #2:\\ Try #2:\\
-$ S \leftarrow ​aSb | bSa | SS | \epsilon $ \\+$ S \leftarrow aSb | bSa | SS | \epsilon $ \\
 incomplete 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 exactly one letter '​a'​ is extra; B = rule that promises that exactly one letter '​b'​ is extra \\ **Idea:** A = rule that promises that exactly one letter '​a'​ is extra; B = rule that promises that exactly one letter '​b'​ is extra \\
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)]. ​ 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]. 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 62: Line 70:
  
  
-/*+
 <​hidden>​ <​hidden>​
 +/*
 Solution 1: Solution 1:
  
Line 81: Line 90:
  
 Solution 2: 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. 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.
  
Line 86: Line 96:
  
 </​hidden>​ </​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 107: Line 117:
 </​hidden>​ </​hidden>​
  
-*/+
  
 ===== 9.2. Ambiguous grammars ===== ===== 9.2. Ambiguous grammars =====
Line 119: Line 129:
 $ B \leftarrow bB | \epsilon $ $ B \leftarrow bB | \epsilon $
  
-/*+
  
 <​hidden>​ <​hidden>​
Line 139: Line 149:
 </​hidden>​ </​hidden>​
  
-*/+
  
 **9.2.2.** **9.2.2.**
Line 147: Line 157:
 $ B \leftarrow B1 | \epsilon $ $ B \leftarrow B1 | \epsilon $
  
-/*+
  
 <​hidden><​note important>​ <​hidden><​note important>​
Line 164: Line 174:
 </​note></​hidden>​ </​note></​hidden>​
  
-*/+
  
 **9.2.3.** **9.2.3.**
Line 172: Line 182:
 $ B \leftarrow bB | \epsilon $ $ B \leftarrow bB | \epsilon $
  
-/*+
  
 <​hidden>​ <​hidden>​
Line 190: 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 203: Line 213:
 </​note></​hidden>​ </​note></​hidden>​
  
-*/+