97 $\textit{der}\;c\;(r_1 \cdot r_2)$ & $\dn$ & $\textit{if}\;\textit{nullable}(r_1)$\\ |
98 $\textit{der}\;c\;(r_1 \cdot r_2)$ & $\dn$ & $\textit{if}\;\textit{nullable}(r_1)$\\ |
98 & & $\textit{then}\;((\textit{der}\;c\;r_1)\cdot r_2) + (\textit{der}\;c\;r_2)$\\ |
99 & & $\textit{then}\;((\textit{der}\;c\;r_1)\cdot r_2) + (\textit{der}\;c\;r_2)$\\ |
99 & & $\textit{else}\;(\textit{der}\;c\;r_1)\cdot r_2$\\ |
100 & & $\textit{else}\;(\textit{der}\;c\;r_1)\cdot r_2$\\ |
100 $\textit{der}\;c\;(r^*)$ & $\dn$ & $(\textit{der}\;c\;r)\cdot (r^*)$\\ |
101 $\textit{der}\;c\;(r^*)$ & $\dn$ & $(\textit{der}\;c\;r)\cdot (r^*)$\\ |
101 \end{tabular} |
102 \end{tabular} |
102 \end{center}\hfill[1 Mark] |
103 \end{center} |
|
104 |
|
105 For example given the regular expression $r = (a \cdot b) \cdot c$, the derivatives |
|
106 w.r.t.~the characters $a$, $b$ and $c$ are |
|
107 |
|
108 \begin{center} |
|
109 \begin{tabular}{lcll} |
|
110 $\textit{der}\;a\;r$ & $=$ & $(\ONE \cdot b)\cdot c$ & ($= r'$)\\ |
|
111 $\textit{der}\;b\;r$ & $=$ & $(\ZERO \cdot b)\cdot c$\\ |
|
112 $\textit{der}\;c\;r$ & $=$ & $(\ZERO \cdot b)\cdot c$ |
|
113 \end{tabular} |
|
114 \end{center} |
|
115 |
|
116 Let $r'$ stand for the first derivative, then taking the derivatives of $r'$ |
|
117 w.r.t.~the characters $a$, $b$ and $c$ gives |
|
118 |
|
119 \begin{center} |
|
120 \begin{tabular}{lcll} |
|
121 $\textit{der}\;a\;r'$ & $=$ & $((\ZERO \cdot b) + \ZERO)\cdot c$ \\ |
|
122 $\textit{der}\;b\;r'$ & $=$ & $((\ZERO \cdot b) + \ONE)\cdot c$ & ($= r''$)\\ |
|
123 $\textit{der}\;c\;r'$ & $=$ & $((\ZERO \cdot b) + \ZERO)\cdot c$ |
|
124 \end{tabular} |
|
125 \end{center} |
|
126 |
|
127 One more example: Let $r''$ stand for the second derivative above, |
|
128 then taking the derivatives of $r''$ w.r.t.~the characters $a$, $b$ |
|
129 and $c$ gives |
|
130 |
|
131 \begin{center} |
|
132 \begin{tabular}{lcll} |
|
133 $\textit{der}\;a\;r''$ & $=$ & $((\ZERO \cdot b) + \ZERO) \cdot c + \ZERO$ \\ |
|
134 $\textit{der}\;b\;r''$ & $=$ & $((\ZERO \cdot b) + \ZERO) \cdot c + \ZERO$\\ |
|
135 $\textit{der}\;c\;r''$ & $=$ & $((\ZERO \cdot b) + \ZERO) \cdot c + \ONE$ |
|
136 \end{tabular} |
|
137 \end{center} |
|
138 |
|
139 Note, the last derivative can match the empty string, that is it is \textit{nullable}.\\ |
|
140 \mbox{}\hfill\mbox{[1 Mark]} |
103 |
141 |
104 \item[(1c)] Implement the function \textit{simp}, which recursively |
142 \item[(1c)] Implement the function \textit{simp}, which recursively |
105 traverses a regular expression from inside to outside, and |
143 traverses a regular expression from the inside to the outside, and |
106 simplifies every sub-regular-expressions on the left to |
144 simplifies every sub-regular-expression on the left (see below) to |
107 the regular expression on the right, except it does not simplify inside |
145 the regular expression on the right, except it does not simplify inside |
108 ${}^*$-regular expressions. |
146 ${}^*$-regular expressions. |
109 |
147 |
110 \begin{center} |
148 \begin{center} |
111 \begin{tabular}{l@{\hspace{2mm}}c@{\hspace{2mm}}ll} |
149 \begin{tabular}{l@{\hspace{4mm}}c@{\hspace{4mm}}ll} |
112 $r \cdot \ZERO$ & $\mapsto$ & $\ZERO$\\ |
150 $r \cdot \ZERO$ & $\mapsto$ & $\ZERO$\\ |
113 $\ZERO \cdot r$ & $\mapsto$ & $\ZERO$\\ |
151 $\ZERO \cdot r$ & $\mapsto$ & $\ZERO$\\ |
114 $r \cdot \ONE$ & $\mapsto$ & $r$\\ |
152 $r \cdot \ONE$ & $\mapsto$ & $r$\\ |
115 $\ONE \cdot r$ & $\mapsto$ & $r$\\ |
153 $\ONE \cdot r$ & $\mapsto$ & $r$\\ |
116 $r + \ZERO$ & $\mapsto$ & $r$\\ |
154 $r + \ZERO$ & $\mapsto$ & $r$\\ |
117 $\ZERO + r$ & $\mapsto$ & $r$\\ |
155 $\ZERO + r$ & $\mapsto$ & $r$\\ |
118 $r + r$ & $\mapsto$ & $r$\\ |
156 $r + r$ & $\mapsto$ & $r$\\ |
119 \end{tabular} |
157 \end{tabular} |
120 \end{center} |
158 \end{center} |
121 |
159 |
122 For example |
160 For example the regular expression |
123 \[(r_1 + \ZERO) \cdot \ONE + ((\ONE + r_2) + r_3) \cdot (r_4 \cdot \ZERO)\] |
161 \[(r_1 + \ZERO) \cdot \ONE + ((\ONE + r_2) + r_3) \cdot (r_4 \cdot \ZERO)\] |
124 |
162 |
125 simplifies to just $r_1$. |
163 simplifies to just $r_1$. |
126 \hfill[1 Mark] |
164 \hfill[1 Mark] |
127 |
165 |
128 \item[(1d)] Implement two functions: The first, called \textit{ders}, |
166 \item[(1d)] Implement two functions: The first, called \textit{ders}, |
129 takes a list of characters as arguments and a regular expression and |
167 takes a list of characters and a regular expression as arguments, and |
130 buids the derivative as follows: |
168 builds the derivative w.r.t.~the list as follows: |
131 |
169 |
132 \begin{center} |
170 \begin{center} |
133 \begin{tabular}{lcl} |
171 \begin{tabular}{lcl} |
134 $\textit{ders}\;Nil\;(r)$ & $\dn$ & $r$\\ |
172 $\textit{ders}\;(Nil)\;r$ & $\dn$ & $r$\\ |
135 $\textit{ders}\;c::cs\;(r)$ & $\dn$ & |
173 $\textit{ders}\;(c::cs)\;r$ & $\dn$ & |
136 $\textit{ders}\;cs\;(\textit{simp}(\textit{der}\;c\;r))$\\ |
174 $\textit{ders}\;cs\;(\textit{simp}(\textit{der}\;c\;r))$\\ |
137 \end{tabular} |
175 \end{tabular} |
138 \end{center} |
176 \end{center} |
139 |
177 |
140 The second, called \textit{matcher}, takes a string and a regular expression |
178 The second, called \textit{matcher}, takes a string and a regular expression |
141 as arguments. It builds first the derivatives according to \textit{ders} |
179 as arguments. It builds first the derivatives according to \textit{ders} |
142 and at the end tests whether the resulting redular expression can match |
180 and after that tests whether the resulting derivative regular expression can match |
143 the empty string (using \textit{nullable}). |
181 the empty string (using \textit{nullable}). |
144 For example the \textit{matcher} will produce true if given the |
182 For example the \textit{matcher} will produce true given the |
145 regular expression $a\cdot b\cdot c$ and the string $abc$. |
183 regular expression $(a\cdot b)\cdot c$ and the string $abc$. |
146 \hfill[1 Mark] |
184 \hfill[1 Mark] |
147 |
185 |
148 \item[(1e)] Implement the function \textit{replace}: it searches (from the left to |
186 \item[(1e)] Implement the function $\textit{replace}\;r\;s_1\;s_2$: it searches |
149 right) in string $s_1$ all the non-empty substrings that match the |
187 (from the left to |
150 regular expression---these substrings are assumed to be |
188 right) in the string $s_1$ all the non-empty substrings that match the |
|
189 regular expression $r$---these substrings are assumed to be |
151 the longest substrings matched by the regular expression and |
190 the longest substrings matched by the regular expression and |
152 assumed to be non-overlapping. All these substrings in $s_1$ are replaced |
191 assumed to be non-overlapping. All these substrings in $s_1$ matched by $r$ |
153 by $s_2$. For example given the regular expression |
192 are replaced by $s_2$. For example given the regular expression |
154 |
193 |
155 \[(a \cdot a)^* + (b \cdot b)\] |
194 \[(a \cdot a)^* + (b \cdot b)\] |
156 |
195 |
157 \noindent the string $aabbbaaaaaaabaaaaabbaaaabb$ and |
196 \noindent the string $s_1 = aabbbaaaaaaabaaaaabbaaaabb$ and |
158 replacement string $c$ yields the string |
197 replacement string $s_2 = c$ yields the string |
159 |
198 |
160 \[ |
199 \[ |
161 ccbcabcaccc |
200 ccbcabcaccc |
162 \] |
201 \] |
163 |
202 |