Differences
This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
|
lfa:2024:lab03 [2024/10/20 12:46] cata_chiru |
lfa:2024:lab03 [2024/10/22 08:13] (current) cata_chiru |
||
|---|---|---|---|
| Line 50: | Line 50: | ||
| \\ | \\ | ||
| - | <hidden> | + | <hidden 3.2.1> |
| A = {00, 0000, 00000000 ...} | A = {00, 0000, 00000000 ...} | ||
| Line 69: | Line 69: | ||
| $ AB = ? $ \\ $ BA = ? $ | $ AB = ? $ \\ $ BA = ? $ | ||
| - | <hidden> | + | <hidden 3.2.2> |
| A is the language in which the words start with zero and end with one and the number of one is equal to the number of zeros (the same value for n is used). | A is the language in which the words start with zero and end with one and the number of one is equal to the number of zeros (the same value for n is used). | ||
| Line 94: | Line 94: | ||
| - | <hidden> | + | <hidden 3.2.3> |
| $math[AB = \{ 0^n 1^n 0^{m+k} \mid m \geq n \geq 1, k \geq 1 \}]. Deci $math[AB = A]. | $math[AB = \{ 0^n 1^n 0^{m+k} \mid m \geq n \geq 1, k \geq 1 \}]. Deci $math[AB = A]. | ||
| Line 116: | Line 116: | ||
| - | <hidden> | + | <hidden 3.2.4> |
| AB = ∅ (because A is empty, so the cartesian product leads to an empty set) | AB = ∅ (because A is empty, so the cartesian product leads to an empty set) | ||
| Line 126: | Line 126: | ||
| </hidden> | </hidden> | ||
| + | |||
| + | |||
| + | ===== 3.3 Regex Equivalence ===== | ||
| + | |||
| + | Are the following regex pair equivalent? | ||
| + | |||
| + | ** 3.3.1 ** | ||
| + | |||
| + | $ E1 = ab|a|b $ | ||
| + | \\ | ||
| + | $ E2 = (a|\epsilon)(b|\epsilon) $ | ||
| + | |||
| + | |||
| + | <hidden 3.3.1><note important> | ||
| + | We can observe that E2 accepts ε, while E1 does not so they are not equivalent. | ||
| + | \\ | ||
| + | Another approach is to compute the language of each expression (since they are finite) and check if they are equivalent. | ||
| + | </note></hidden> | ||
| + | |||
| + | |||
| + | ** 3.3.2 ** | ||
| + | |||
| + | $ E1 = a(b|c)(d|e)|abb|abc $ | ||
| + | \\ | ||
| + | $ E2 = ab(b|c|d|e)|acd|ace $ | ||
| + | |||
| + | |||
| + | <hidden 3.3.2><note important> | ||
| + | Since both E1 and E2 have a finite language, we could just compute the language and check if they are equivalent. | ||
| + | Language is L = {abb, abc, abd, abe, acd, ace}, therefore they are equivalent. | ||
| + | </note></hidden> | ||
| + | |||
| ** 3.3.3 ** | ** 3.3.3 ** | ||
| Line 134: | Line 166: | ||
| - | <hidden><note important> | + | <hidden 3.3.3><note important> |
| Both E1 and E2 have an infinite language, so comparing them is not an option. | Both E1 and E2 have an infinite language, so comparing them is not an option. | ||
| We can see that for example E2 accepts b, while E1 does not accept it, so the expressions are not equivalent. | We can see that for example E2 accepts b, while E1 does not accept it, so the expressions are not equivalent. | ||
| - | Fun fact: E1 was proposed by a student as a solution for 3.2.2 last year, while E2 is the actual solution. | + | Fun fact: E1 was proposed by a student as a solution for 3.4.1 some year ago, while E2 is the actual solution. |
| </note></hidden> | </note></hidden> | ||
| Line 150: | Line 182: | ||
| - | <hidden><note important> | + | <hidden 3.3.4><note important> |
| Both E1 and E2 have an infinite language, so comparing them is not an option. | Both E1 and E2 have an infinite language, so comparing them is not an option. | ||
| Line 164: | Line 196: | ||
| + | ===== 3.4. Writing Regular Expressions ===== | ||
| - | ===== 3.2. Writing Regular Expressions ===== | + | **3.4.0.** Write a regular expression for the language of arithmetic expressions containing +, * and numbers. |
| - | + | ||
| - | **3.2.1.** Write a regular expression for the language of arithmetic expressions containing +, * and numbers. | + | |
| **Hint:** you can abbreviate $ 0 \cup 1 \cup ... \cup 9 $ by $ [0-9] $ | **Hint:** you can abbreviate $ 0 \cup 1 \cup ... \cup 9 $ by $ [0-9] $ | ||
| - | <hidden> | + | <hidden 3.4.0> |
| We start by defining the regex for a number: | We start by defining the regex for a number: | ||
| Line 190: | Line 221: | ||
| - | **3.2.2.** Write a regular expression for $ L = \{ \omega \text{ in } \text{{0,1}} ^* \text{ | EVERY sequence of two or more consecutive zeros appears before ANY sequence of two or more consecutive ones} \} $ | + | **3.4.1.** Write a regular expression for $ L = \{ \omega \text{ in } \text{{0,1}} ^* \text{ | EVERY sequence of two or more consecutive zeros appears before ANY sequence of two or more consecutive ones} \} $ |
| - | <hidden> | + | <hidden 3.4.1> |
| > (1 U ε) ( 0 0* (1 U ε) )* (0 U ε) ( 1 1* (0 U ε) )* | > (1 U ε) ( 0 0* (1 U ε) )* (0 U ε) ( 1 1* (0 U ε) )* | ||
| Line 206: | Line 237: | ||
| - | **3.2.3.** Write a DFA for $ L(( 10 \cup 0) ^* ( 1 \cup \epsilon )) $ | + | **3.4.2.** |
| + | Find a regular expression for the set of all binary strings with the property that none of its prefixes has two more 0's than 1's nor two more 1's than 0's. | ||
| + | <hidden 3.4.2> | ||
| + | |||
| + | We the given property implies that the word is composed of repeated sequences of '10' and '01' (so elements at odd positions are different from their left neighbour), possibly followed by a final 1 or 0: | ||
| - | <hidden> | + | After any prefix of even length with an equal number of 0's and 1's (including $\epsilon$), you can either finish the word, add a single character and then finish the word (both of these keeping the rule true) or add at least 2 characters. In the last case, if the characters are equal the prefix ending at these two characters breaks the rule. |
| - | {{:lfa:2022:lfa2022_lab_3.2.3.png?300|}} | + | |
| - | </hidden> | + | |
| + | $(01\cup10)^*(0\cup1\cup\epsilon)$ | ||
| + | |||
| + | </hidden> | ||
| - | **3.2.4.** Write a regular expression which generates the accepted language of A. Then try to find the most simple and easy to understand way to write it. | + | **3.4.3.** Write a regular expression which generates the accepted language of A. Then try to find the most simple and easy to understand way to write it. |
| {{:lfa:graf1.png?400|}} | {{:lfa:graf1.png?400|}} | ||
| - | <hidden> | + | <hidden 3.4.3> |
| Looking at the DFA we can tell state 3 is a sink state. We can simplify the DFA's drawing by not looking at/ignoring it. | Looking at the DFA we can tell state 3 is a sink state. We can simplify the DFA's drawing by not looking at/ignoring it. | ||
| Line 240: | Line 276: | ||
| </hidden> | </hidden> | ||
| - | |||
| - | |||
| - | **3.2.5.** | ||
| - | Find a regular expression for the set of all binary strings with the property that none of its prefixes has two more 0's than 1's nor two more 1's than 0's. | ||
| - | |||
| - | |||
| - | |||
| - | <hidden> | ||
| - | |||
| - | We the given property implies that the word is composed of repeated sequences of '10' and '01' (so elements at odd positions are different from their left neighbour), possibly followed by a final 1 or 0: | ||
| - | |||
| - | After any prefix of even length with an equal number of 0's and 1's (including $\epsilon$), you can either finish the word, add a single character and then finish the word (both of these keeping the rule true) or add at least 2 characters. In the last case, if the characters are equal the prefix ending at these two characters breaks the rule. | ||
| - | |||
| - | $(01\cup10)^*(0\cup1\cup\epsilon)$ | ||
| - | |||
| - | </hidden> | ||
| - | |||
| - | |||
| - | /* this is the old exercise | ||
| - | Describe as precisely as possible the language generated by $math[((0(1 \cup 0)(1 \cup 0)) \cup 100)1((0(1 \cup 0)(1 \cup 0)) \cup 100)1(((0(1 \cup 0)(1 \cup 0)) \cup 100)0)*] | ||
| - | |||
| - | (hint: BCD) | ||
| - | |||
| - | |||
| - | <hidden> | ||
| - | |||
| - | OK, ok. We get it. It looks **bad**. BUT, | ||
| - | |||
| - | let's first find repeating patterns: ( ( 0 ( 1 U 0 ) ( 1 U 0 ) ) U 100 ) 1 | ||
| - | |||
| - | Ugly, yes. But let's see the language of this little regex: | ||
| - | |||
| - | 0001, 0011, 0101, 0111, 1001 | ||
| - | |||
| - | What are these in BCD? | ||
| - | |||
| - | 1, 3, 5, 7, 9. | ||
| - | |||
| - | So, we got so far BCD numbers that start with an odd digit, BUT this pattern repeats twice. So, we have BCD numbers starting with 2 odd digits. | ||
| - | |||
| - | NEXT: | ||
| - | |||
| - | ( ( 0 ( 1 U 0 ) ( 1 U 0 ) ) U 100 ) 0 )* | ||
| - | |||
| - | We have a Kleene star, so something repeats any number of times. | ||
| - | And, inside this Kleene star, there is ( 0 (1 U 0) (1 U 0) ) U 100 )0 that accepts the language: | ||
| - | |||
| - | 0000, 0010, 0100, 0110, 1000. | ||
| - | |||
| - | So, this ugly regex encodes BCD numbers that start with exactly 2 odd digits which are followed by 0 or more even digits. | ||
| - | </hidden> | ||
| - | */ | ||
| - | |||
| ==== Conclusion ==== | ==== Conclusion ==== | ||
| - | <hidden><note important> | + | <hidden Conclusion ><note important> |
| What have we learned today? | What have we learned today? | ||