Differences

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

Link to this comparison view

Next revision
Previous revision
lfa:2024:lab09 [2024/12/01 13:10]
stefan.sterea created
lfa:2024:lab09 [2024/12/02 11:33] (current)
pdmatei
Line 1: Line 1:
-====== 9. Context-Free Languages ​======+====== 9. Push Down Automata ​======
  
-===== 9.1. Accepting and generating a CF language ​=====+===== 9.1. Writing PDAs =====
  
-Find PDAs that accept each of the languages below, both by empty stack and by final state. Identify which approach feels more suitable for each language. Find CFGs for the languages. 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).+Find PDAs that accept each of the languages below, both by empty stack and by final state. Identify which approach feels more suitable for each language. ​
  
 **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>​ 
-<note important>​ 
-$ S \leftarrow ASA | BSB | A | B | \epsilon $ 
-</​note>​ 
-{{ :​lfa:​2022:​lfa2022_lab9_1_1.png?​400 |}} 
-</​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>​ 
-<note important>​ 
-$ S \leftarrow XY $ \\ 
-$ X \leftarrow AXB | \epsilon $ \\ 
-$ Y \leftarrow BYC | \epsilon $ 
-</​note>​ 
-{{ :​lfa:​2022:​lfa2022_lab9_1_4.png?​400 |}} 
-</​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>​ 
-<note warning> 
-Try #1:\\ 
-$ S \leftarrow abS | baS | \epsilon $ \\ 
-incomplete because of: “aabb” \\ 
-Try #2:\\ 
-$ S \leftarrow aSb | bSa | SS | \epsilon $ \\ 
-incomplete because of: “abab” 
-</​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>​ 
-**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 $ \\    
-$ A \leftarrow a | bAA $ \\ 
-$ B \leftarrow b | aBB $ 
-</​note>​ 
-{{ :​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>​ 
- 
- 
  
 **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>​ 
-/-* 
-Solution 1: 
- 
-<note important>​ 
-$ S \leftarrow A | B $ \\ 
-$ A \leftarrow G |GA $ \\ 
-$ B \leftarrow H | HB $ \\ 
-$ G \leftarrow Ea $ \\ 
-$ H \leftarrow Eb $ \\ 
-$ E \leftarrow aME | bNE | \epsilon $ \\    
-$ M \leftarrow b | aMM $ \\ 
-$ 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>​ 
-{{ :​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>​ 
- 
- 
  
 **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 \} $ \\
  
-**9.1.6.** Prove that PDAs that accept by empty stack and PDAs that accept by final state are equivalent. ​Find a transformation ​from a PDA accepting ​by empty stack to one accepting by final state and a transformation from a PDA accepting by final state to one accepting by empty stack. +===== 9.2. Equivalence of the definitions ===== 
- +**9.2.1.** Prove that PDAs that accept by empty stack and PDAs that accept by final state are equivalent. ​More concretely:​ 
-<​hidden>​ +  * starting ​from a PDA $math[A] which accepts ​by empty stack, **create** a PDA $math[A'] which accepts by final state, such that $math[L(AL(A')]
-<note important>​ +  * perform ​the same task - vice-versa.
-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 $  \\ +
-</​note>​ +
-{{ :​lfa:​2022:​lfa2022_lab9_1_5.png?​600 |}} +
-</​hidden>​ +
- +
- +
- +
-===== 9.2. Ambiguous grammars ===== +
- +
-Which of the following grammars are ambiguous? Justify each answer. Modify the grammar to remove ambiguitywherever the case. +
- +
-**9.2.1.** +
- +
-S \leftarrow aA | A $ \\ +
-\leftarrow aA | B $ \\ +
-$ B \leftarrow bB | \epsilon $ +
- +
- +
- +
-<​hidden>​ +
-<note important>​ +
-Ambiguuous: yes +
- +
-  * S => aA => aB => abB => ab +
- +
-  * S => => aA => aB => abB => ab +
- +
-Repaired, unambiguous grammar: +
- +
-$ S \leftarrow A $  \\  +
-$ A \leftarrow aA | B $ \\ +
-$ B \leftarrow bB | \epsilon $  +
- +
-</​note>​ +
- +
-</​hidden>​ +
- +
- +
- +
-**9.2.2.** +
- +
-$ S \leftarrow AS | \epsilon $ \\ +
-$ A \leftarrow 0A1 | 01 | B $\\ +
-$ B \leftarrow B1 | \epsilon $ +
- +
- +
- +
-<​hidden><​note important>​ +
- +
-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 $  +
- +
- +
-</​note></​hidden>​ +
- +
- +
- +
-**9.2.3.** +
- +
-$ S \leftarrow ASB | BSA | \epsilon $\\ +
-$ A \leftarrow aA | \epsilon $\\ +
-$ B \leftarrow bB | \epsilon $ +
- +
- +
- +
-<​hidden>​ +
-<note important>​ +
-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 $ +
-</​note>​ +
-</​hidden>​ +
- +
- +
- +
-**9.2.4.** Write an ambiguous grammar for $ L(a^*) $. +
  
 +**9.2.2.** Prove that the language $math[L(A)] where $math[A] is a PDA which uses only a fixed number of $math[k] stack cells, is **regular**.
  
-<​hidden><​note important>​ 
-$ S \leftarrow aS | aaS | \epsilon $\\ 
-Ambiguuous: yes\\ 
-  * S => aS => aaS => aa 
-  * S => aaS => aa 
-</​note></​hidden>​