Version without JavaScript, read-only: workshops, quizzes and running code are not available here. Full version · Catalogue · Sign in
Exercise sheet 2
Regular expressions
Reading, writing and comparing regular expressions; going from a specification in plain English to a precise expression. Chapter 2.
Unless stated otherwise, $\Sigma = \{a, b\}$.
Solution
- The words that contain the factor $aa$. Yes: $baab$. No: $abab$.
- The words that have an even number of $a$. Yes: $abab$. No: $abbb$.
- The words without the factor $aa$. Yes: $abab$. No: $baab$.
- The words whose length is a multiple of 3. No word of length 4 belongs to it; $aaaa$ does not belong to it.
- The words made of a single block of $a$ and a single block of $b$, in one order or the other (possibly empty). Yes: $aabb$, $bbba$. No: $abab$.
Solution
- $b^*ab^*$.
- $b^*(a + \varepsilon)b^*(a + \varepsilon)b^*$.
- $(b + ab)^*(\varepsilon + a)$: every $a$ is followed by a $b$, except perhaps the last one.
- $a + b + a(a + b)^*a + b(a + b)^*b$. Do not forget the one-letter words, which start and end with the same letter.
- $((a + b)(a + b)(a + b))^*$, or $((a + b)^3)^*$ with the abbreviation.
- $(a + b)^*a(a + b)^*b(a + b)^* + (a + b)^*b(a + b)^*a(a + b)^*$: an $a$ before a $b$, or a $b$ before an $a$. Shorter: $a^+b(a + b)^* + b^+a(a + b)^*$, according to the first letter.
Other answers are correct. To check yours, test short words, in particular $\varepsilon$ and one-letter words.
Solution
- Equivalent. $(a^*b^*)^* \subseteq (a + b)^*$ is clear. Conversely, a word of $(a + b)^*$ is a sequence of letters, and each letter is in $a^*b^*$.
- Equivalent: both denote $a$, $aba$, $ababa$… A word of $(ab)^*a$ is written $abab\cdots aba$, which can also be cut as $a \cdot ba \cdot ba \cdots$.
- No: $aab \in a^*b^*$ but $aab \notin (ab)^*$. And $abab \in (ab)^*$ but $abab \notin a^*b^*$.
- Equivalent: $a^* + b^*$ contains $a$ and $b$, so its star contains all words; the other inclusion is clear.
- Equivalent. A word that does not contain the factor $ab$ cannot have an $a$ followed (immediately) by a $b$: all its $b$ are before its $a$, so it is in $b^*a^*$. The two terms therefore cover all words.
Solution
- $\supseteq$: a word of $(e^*f)^*e^*$ is a sequence of words of $L(e)$ and $L(f)$, hence in $(L(e) \cup L(f))^*$. $\subseteq$: a word of $(L(e) \cup L(f))^*$ is a sequence $u_1 \cdots u_n$ of words, each in $L(e)$ or in $L(f)$. We cut the sequence after each $u_i$ taken from $L(f)$: each piece has the form (words of $L(e)$) then (one word of $L(f)$), except the last one, made only of words of $L(e)$. It is a word of $(e^*f)^*e^*$.
- Both sides denote the words $u_0 v_1 u_1 v_2 u_2 \cdots v_n u_n$ with $n \geq 0$, $u_i \in L(e)$, $v_i \in L(f)$: a word of $L(e)$, then pairs ($f$, $e$). On the left we group $u_0 \,(v_1 u_1) \cdots (v_n u_n)$, on the right $(u_0 v_1) \cdots (u_{n-1} v_n)\, u_n$.
- $L(e) \subseteq L(e)^*$ gives $(e + f)^* \subseteq (e^* + f)^*$. Conversely, a word of $L(e)^*$ is a sequence of words of $L(e)$; replacing each piece from $L(e)^*$ with this sequence, a word of $(L(e)^* \cup L(f))^*$ becomes a sequence of words of $L(e) \cup L(f)$.
Solution
- $0[xX][0-9a-fA-F]^+$
- $0 + -?[1-9][0-9]^*$. Zero is handled separately to forbid
-0and007. - $([01][0-9] + 2[0-3])\texttt{:}[0-5][0-9]$. The hour cannot be written $[0-2][0-9]$, which would accept
29.