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/04 20:46]
alexandra.udrescu01
lfa:2022:lab09-cfl [2022/12/17 11:05] (current)
alexandra.udrescu01
Line 6: Line 6:
  
 **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 11: Line 12:
 $ S \leftarrow ASA | BSB | A | B | \epsilon $ $ S \leftarrow ASA | BSB | A | B | \epsilon $
 </​note>​ </​note>​
 +{{ :​lfa:​2022:​lfa2022_lab9_1_1.png?​400 |}}
 </​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 21: Line 27:
 $ Y \leftarrow BYC | \epsilon $ $ Y \leftarrow BYC | \epsilon $
 </​note>​ </​note>​
 +{{ :​lfa:​2022:​lfa2022_lab9_1_4.png?​400 |}}
 </​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 $ \\
 $ B \leftarrow b | aBB $ $ B \leftarrow b | aBB $
 </​note>​ </​note>​
-{{ :​lfa:​2022:​lfa2022_lab9_1_3.png?​300 |}} accept ​by empty stack+{{ :​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>​
 +
 +
  
 **9.1.4.** $ L = \{w \in \{a, b\}^* | \#_a(w) \neq \#_b(w) \} $ **9.1.4.** $ L = \{w \in \{a, b\}^* | \#_a(w) \neq \#_b(w) \} $
 +
 +
  
 <​hidden>​ <​hidden>​
 +/*
 +Solution 1:
 +
 <note important>​ <note important>​
 $ S \leftarrow A | B $ \\ $ S \leftarrow A | B $ \\
-$ A \leftarrow G |GG $ \\ +$ A \leftarrow G |GA $ \\ 
-$ B \leftarrow H | HH $ \\+$ B \leftarrow H | HB $ \\
 $ G \leftarrow Ea $ \\ $ G \leftarrow Ea $ \\
 $ H \leftarrow Eb $ \\ $ H \leftarrow Eb $ \\
 $ E \leftarrow aME | bNE | \epsilon $ \\    $ E \leftarrow aME | bNE | \epsilon $ \\   
 $ M \leftarrow b | aMM $ \\ $ M \leftarrow b | aMM $ \\
-$ N \leftarrow a | bNN $+$ N \leftarrow a | bNN $ \\ 
 +Non-terminal E generates words with the same number of '​a'​ and '​b',​ as it was presented in the previous exercise. \\ 
 +Non-terminal A generates words that have more "​a",​ while B generates words with fewer "​a"​. \\ Then S generates words with either more, or fewer '​a'​ than '​b'​.
 </​note>​ </​note>​
-{{ :​lfa:​2022:​lfa2022_lab9_1_4.png?​400 |}} accept ​by empty stack+{{ :​lfa:​2022:​lfa2022_lab9_1_4.png?​400 |}} 
 + 
 +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>​ </​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 74: Line 116:
 {{ :​lfa:​2022:​lfa2022_lab9_1_5.png?​600 |}} {{ :​lfa:​2022:​lfa2022_lab9_1_5.png?​600 |}}
 </​hidden>​ </​hidden>​
 +
 +
  
 ===== 9.2. Ambiguous grammars ===== ===== 9.2. Ambiguous grammars =====
Line 84: Line 128:
 $ A \leftarrow aA | B $ \\ $ A \leftarrow aA | B $ \\
 $ B \leftarrow bB | \epsilon $ $ B \leftarrow bB | \epsilon $
 +
 +
  
 <​hidden>​ <​hidden>​
Line 95: Line 141:
 Repaired, unambiguous grammar: Repaired, unambiguous grammar:
  
-$ S \leftarrow A $ \\+$ S \leftarrow A $  \\ 
 $ A \leftarrow aA | B $ \\ $ A \leftarrow aA | B $ \\
-$ B \leftarrow bB | \epsilon $+$ B \leftarrow bB | \epsilon $ 
  
 </​note>​ </​note>​
  
 </​hidden>​ </​hidden>​
 +
 +
  
 **9.2.2.** **9.2.2.**
Line 108: Line 156:
 $ A \leftarrow 0A1 | 01 | B $\\ $ A \leftarrow 0A1 | 01 | B $\\
 $ B \leftarrow B1 | \epsilon $ $ B \leftarrow B1 | \epsilon $
 +
  
  
Line 117: Line 166:
  
 Repaired: \\ Repaired: \\
-$ S \leftarrow ​AS \epsilon ​$ \\ +$ S \leftarrow ​1BS' ​S' ​$ \\ 
-\leftarrow ​0A’1B ​$ \\ +S' ​\leftarrow ​ABS' | \epsilon ​$ \\ 
-$ A’ \leftarrow ​0A’1 ​\epsilon ​$ \\+$ A \leftarrow ​0A1 01 $ \\
 $ B \leftarrow B1 | \epsilon $  $ B \leftarrow B1 | \epsilon $ 
  
  
 </​note></​hidden>​ </​note></​hidden>​
 +
 +
  
 **9.2.3.** **9.2.3.**
Line 130: Line 181:
 $ A \leftarrow aA | \epsilon $\\ $ A \leftarrow aA | \epsilon $\\
 $ B \leftarrow bB | \epsilon $ $ B \leftarrow bB | \epsilon $
 +
  
  
Line 138: Line 190:
   * S ⇒ ASB ⇒ SB ⇒ B ⇒ $ \epsilon $   * 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: \\ Repaired: \\
-$ S \leftarrow ​aSb bSa | b | a | \epsilon $+$ S \leftarrow ​aS bS | \epsilon $
 </​note>​ </​note>​
 </​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 153: Line 212:
   * S => aaS => aa   * S => aaS => aa
 </​note></​hidden>​ </​note></​hidden>​
 +
 +