Differences

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

Link to this comparison view

Next revision
Previous revision
lfa:2023:lab09 [2023/12/11 23:24]
mihai.udubasa created
lfa:2023:lab09 [2024/01/16 22:31] (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>​
 <note important>​ <note important>​
Line 14: Line 14:
 {{ :​lfa:​2022:​lfa2022_lab9_1_1.png?​400 |}} {{ :​lfa:​2022:​lfa2022_lab9_1_1.png?​400 |}}
 </​hidden>​ </​hidden>​
-*/+
  
  
Line 20: Line 20:
  
  
-/*+
 <​hidden>​ <​hidden>​
 <note important>​ <note important>​
Line 29: Line 29:
 {{ :​lfa:​2022:​lfa2022_lab9_1_4.png?​400 |}} {{ :​lfa:​2022:​lfa2022_lab9_1_4.png?​400 |}}
 </​hidden>​ </​hidden>​
-*/+
  
  
Line 35: Line 35:
  
  
-/*+
 <​hidden>​ <​hidden>​
 <note warning> <note warning>
Line 49: Line 49:
 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: 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] $math[S \leftarrow aSbS \mid bSaS \mid \epsilon]
-/*+
 **Solution 2:** **Solution 2:**
  
Line 62: Line 62:
 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 70: Line 70:
  
  
-/*+
 <​hidden>​ <​hidden>​
-/*+/-*
 Solution 1: Solution 1:
  
Line 90: 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 96: Line 96:
  
 </​hidden>​ </​hidden>​
-*/+
  
  
Line 102: Line 102:
  
  
-/*+
 <​hidden>​ <​hidden>​
 <note important>​ <note important>​
Line 116: Line 116:
 {{ :​lfa:​2022:​lfa2022_lab9_1_5.png?​600 |}} {{ :​lfa:​2022:​lfa2022_lab9_1_5.png?​600 |}}
 </​hidden>​ </​hidden>​
-*/+
  
  
Line 130: Line 130:
  
  
-/*+
 <​hidden>​ <​hidden>​
 <note important>​ <note important>​
Line 148: Line 148:
  
 </​hidden>​ </​hidden>​
-*/+
  
  
Line 158: Line 158:
  
  
-/*+
 <​hidden><​note important>​ <​hidden><​note important>​
  
Line 173: Line 173:
  
 </​note></​hidden>​ </​note></​hidden>​
-*/+
  
  
Line 183: Line 183:
  
  
-/*+
 <​hidden>​ <​hidden>​
 <note important>​ <note important>​
Line 199: Line 199:
 </​note>​ </​note>​
 </​hidden>​ </​hidden>​
-*/+
  
  
Line 205: Line 205:
  
  
-/*+
 <​hidden><​note important>​ <​hidden><​note important>​
 $ S \leftarrow aS | aaS | \epsilon $\\ $ S \leftarrow aS | aaS | \epsilon $\\
Line 212: Line 212:
   * S => aaS => aa   * S => aaS => aa
 </​note></​hidden>​ </​note></​hidden>​
-*/+