Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
lfa:2022:lab11-cfl_3 [2022/12/18 21:26] mihai.udubasa [11.4. Short Exercises] |
lfa:2022:lab11-cfl_3 [2022/12/18 23:26] (current) mihai.udubasa add some notes to 11.4.2 |
||
---|---|---|---|
Line 45: | Line 45: | ||
</hidden> | </hidden> | ||
* {{:lfa:2022:lfa2022_lab6_mindfa2_1.png?200|}} | * {{:lfa:2022:lfa2022_lab6_mindfa2_1.png?200|}} | ||
- | <hidden><note tip> $ S_1 \leftarrow 1S_2 | 0S_1 | \epsilon \\ S_2 \leftarrow 0S_1 | 1S_3 | \epsilon \\ S_3 \leftarrow 0S_3 | 1S_3$ </note></hidden | + | <hidden><note tip> $ S_1 \leftarrow 1S_2 | 0S_1 | \epsilon \\ S_2 \leftarrow 0S_1 | 1S_3 | \epsilon \\ S_3 \leftarrow 0S_3 | 1S_3$ </note></hidden> |
- | > | + | |
===== 11.4. Short Exercises ===== | ===== 11.4. Short Exercises ===== | ||
Line 54: | Line 53: | ||
Create a graph $math[G=(V,E)] where the nodes are **terminals and non-terminals**. For each rule of the form $math[X \leftarrow aY] create a directed edge $math[(X,Y)]. For each rule $math[X \leftarrow a], also generate an edge $math[(X,a)]. | Create a graph $math[G=(V,E)] where the nodes are **terminals and non-terminals**. For each rule of the form $math[X \leftarrow aY] create a directed edge $math[(X,Y)]. For each rule $math[X \leftarrow a], also generate an edge $math[(X,a)]. | ||
- First, we need to check first if the grammar generates a language different from $math[\emptyset]. So we check if there is a path from the start symbol to some terminal. | - First, we need to check first if the grammar generates a language different from $math[\emptyset]. So we check if there is a path from the start symbol to some terminal. | ||
- | - Second, we need to check if there are //loops//. It suffices to check if the graph is a tree. | + | - Second, we need to check if there are //loops//. It suffices to check if the graph is a tree. MU: the condition is not sufficient as a grammar could generate a finite language while also having looping rules (if the grammar comes from the dfa transformation, these would be from the sink states of the dfa; see state 3 of the second dfa from 11.3.1) |
</hidden> | </hidden> | ||
**11.4.3.** Prove that any DFA can be converted to a regular grammar. | **11.4.3.** Prove that any DFA can be converted to a regular grammar. | ||
- | <hidden> See lecture</hidden> | + | <hidden> See lecture </hidden> |
**11.4.4.** Is there a decidable algorithm to remove ambiguity from regular grammars? | **11.4.4.** Is there a decidable algorithm to remove ambiguity from regular grammars? | ||
<hidden> | <hidden> | ||
Line 64: | Line 63: | ||
We can determinize the NFA. In the resulting DFA (in any DFA for that matter), it is not possible to derive the same word using two different transition sequences (each state and character uniquely determines the next-state). Hence, if we convert this DFA back to a regular grammar, we will get an unambiguous one. | We can determinize the NFA. In the resulting DFA (in any DFA for that matter), it is not possible to derive the same word using two different transition sequences (each state and character uniquely determines the next-state). Hence, if we convert this DFA back to a regular grammar, we will get an unambiguous one. | ||
</hidden> | </hidden> | ||
- | **11.4.5.** Show the following grammar is ambiguous: $math[S \leftarrow aSbS | bSaS | epsilon]. Write a non-ambiguous equivalent. | + | **11.4.5.** Show the following grammar is ambiguous: $ S \leftarrow aSbS | bSaS | \epsilon $. Write a non-ambiguous equivalent. |
<hidden> | <hidden> | ||
The grammar is indeed ambiguous: $math[S \Rightarrow aSbS \Rightarrow abS \Rightarrow abaSbS \Rightarrow ^* abab]. And also $math[S \Rightarrow aSbS \Rightarrow abSaSbS \Rightarrow^* abab] | The grammar is indeed ambiguous: $math[S \Rightarrow aSbS \Rightarrow abS \Rightarrow abaSbS \Rightarrow ^* abab]. And also $math[S \Rightarrow aSbS \Rightarrow abSaSbS \Rightarrow^* abab] | ||
This grammar generates $math[\{ w \in \{0,1\}^* \mid \#_A(w) = \#_B(w) \}]. See the solution from the previous lab. | This grammar generates $math[\{ w \in \{0,1\}^* \mid \#_A(w) = \#_B(w) \}]. See the solution from the previous lab. | ||
See also [[https://cs.stackexchange.com/questions/64569/unambiguous-grammar-that-produce-equal-number-of-a-and-b | this]] for more details. | See also [[https://cs.stackexchange.com/questions/64569/unambiguous-grammar-that-produce-equal-number-of-a-and-b | this]] for more details. | ||
+ | \\ | ||
+ | **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 $ | ||
+ | |||
</hidden> | </hidden> | ||
+ | |||
+ |