15 \pgfplotsset{compat=1.12} |
15 \pgfplotsset{compat=1.12} |
16 |
16 |
17 \newcommand{\bl}[1]{\textcolor{blue}{#1}} |
17 \newcommand{\bl}[1]{\textcolor{blue}{#1}} |
18 |
18 |
19 % beamer stuff |
19 % beamer stuff |
20 \renewcommand{\slidecaption}{AFL 02, King's College London} |
20 \renewcommand{\slidecaption}{CFL 02, King's College London} |
21 |
21 |
22 |
22 |
23 \begin{document} |
23 \begin{document} |
24 |
24 |
25 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
25 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
26 \begin{frame}[t] |
26 \begin{frame}[t] |
27 \frametitle{% |
27 \frametitle{% |
28 \begin{tabular}{@ {}c@ {}} |
28 \begin{tabular}{@ {}c@ {}} |
29 \\[-3mm] |
29 \\[-3mm] |
30 \LARGE Automata and \\[-2mm] |
30 \LARGE Compilers and \\[-1mm] |
31 \LARGE Formal Languages (2)\\[3mm] |
31 \LARGE Formal Languages (2)\\[3mm] |
32 \end{tabular}} |
32 \end{tabular}} |
33 |
33 |
34 \normalsize |
34 \normalsize |
35 \begin{center} |
35 \begin{center} |
234 |
234 |
235 \begin{center} |
235 \begin{center} |
236 \bl{\begin{tabular}{l@{\hspace{2mm}}c@{\hspace{2mm}}l} |
236 \bl{\begin{tabular}{l@{\hspace{2mm}}c@{\hspace{2mm}}l} |
237 $Der\,f\,A$ & $=$ & $\{\textit{oo}, \textit{rak}\}$\\ |
237 $Der\,f\,A$ & $=$ & $\{\textit{oo}, \textit{rak}\}$\\ |
238 $Der\,b\,A$ & $=$ & $\{\textit{ar}\}$\\ |
238 $Der\,b\,A$ & $=$ & $\{\textit{ar}\}$\\ |
239 $Der\,a\,A$ & $=$ & $\varnothing$\\\pause |
239 $Der\,a\,A$ & $=$ & $\{\}$\\\pause |
240 \end{tabular}} |
240 \end{tabular}} |
241 \end{center} |
241 \end{center} |
242 |
242 |
243 \small |
243 \small |
244 We can extend this definition to strings |
244 We can extend this definition to strings |
258 Their inductive definition: |
258 Their inductive definition: |
259 |
259 |
260 |
260 |
261 \begin{textblock}{6}(2,7.5) |
261 \begin{textblock}{6}(2,7.5) |
262 \begin{tabular}{@ {}rrl@ {\hspace{13mm}}l} |
262 \begin{tabular}{@ {}rrl@ {\hspace{13mm}}l} |
263 \bl{$r$} & \bl{$::=$} & \bl{$\varnothing$} & null\\ |
263 \bl{$r$} & \bl{$::=$} & \bl{$\ZERO$} & null\\ |
264 & \bl{$\mid$} & \bl{$\epsilon$} & empty string / \pcode{""} / $[]$\\ |
264 & \bl{$\mid$} & \bl{$\ONE$} & empty string / \pcode{""} / $[]$\\ |
265 & \bl{$\mid$} & \bl{$c$} & character\\ |
265 & \bl{$\mid$} & \bl{$c$} & character\\ |
266 & \bl{$\mid$} & \bl{$r_1 \cdot r_2$} & sequence\\ |
266 & \bl{$\mid$} & \bl{$r_1 \cdot r_2$} & sequence\\ |
267 & \bl{$\mid$} & \bl{$r_1 + r_2$} & alternative / choice\\ |
267 & \bl{$\mid$} & \bl{$r_1 + r_2$} & alternative / choice\\ |
268 & \bl{$\mid$} & \bl{$r^*$} & star (zero or more)\\ |
268 & \bl{$\mid$} & \bl{$r^*$} & star (zero or more)\\ |
269 \end{tabular} |
269 \end{tabular} |
284 \begin{frame}[c] |
284 \begin{frame}[c] |
285 \frametitle{\begin{tabular}{c}The Meaning of a\\[-2mm] Regular Expression\end{tabular}} |
285 \frametitle{\begin{tabular}{c}The Meaning of a\\[-2mm] Regular Expression\end{tabular}} |
286 |
286 |
287 \begin{textblock}{15}(1,4) |
287 \begin{textblock}{15}(1,4) |
288 \begin{tabular}{@ {}rcl} |
288 \begin{tabular}{@ {}rcl} |
289 \bl{$L(\varnothing)$} & \bl{$\dn$} & \bl{$\varnothing$}\\ |
289 \bl{$L(\ZERO)$} & \bl{$\dn$} & \bl{$\{\}$}\\ |
290 \bl{$L(\epsilon)$} & \bl{$\dn$} & \bl{$\{[]\}$}\\ |
290 \bl{$L(\ONE)$} & \bl{$\dn$} & \bl{$\{[]\}$}\\ |
291 \bl{$L(c)$} & \bl{$\dn$} & \bl{$\{[c]\}$}\\ |
291 \bl{$L(c)$} & \bl{$\dn$} & \bl{$\{[c]\}$}\\ |
292 \bl{$L(r_1 + r_2)$} & \bl{$\dn$} & \bl{$L(r_1) \cup L(r_2)$}\\ |
292 \bl{$L(r_1 + r_2)$} & \bl{$\dn$} & \bl{$L(r_1) \cup L(r_2)$}\\ |
293 \bl{$L(r_1 \cdot r_2)$} & \bl{$\dn$} & \bl{$L(r_1) \,@\, L(r_2)$}\\ |
293 \bl{$L(r_1 \cdot r_2)$} & \bl{$\dn$} & \bl{$L(r_1) \,@\, L(r_2)$}\\ |
294 \bl{$L(r^*)$} & \bl{$\dn$} & \bl{$(L(r))^*$}\\ |
294 \bl{$L(r^*)$} & \bl{$\dn$} & \bl{$(L(r))^*$}\\ |
295 \end{tabular} |
295 \end{tabular} |
351 \begin{frame}[t] |
351 \begin{frame}[t] |
352 \frametitle{Corner Cases} |
352 \frametitle{Corner Cases} |
353 |
353 |
354 \begin{center} |
354 \begin{center} |
355 \bl{\begin{tabular}{rcl} |
355 \bl{\begin{tabular}{rcl} |
356 $a \cdot \varnothing$ & $\not\equiv$ & $a$\\ |
356 $a \cdot \ZERO$ & $\not\equiv$ & $a$\\ |
357 $a + \epsilon$ & $\not\equiv$ & $a$\\ |
357 $a + \ONE$ & $\not\equiv$ & $a$\\ |
358 $\epsilon$ & $\equiv$ & $\varnothing^*$\\ |
358 $\ONE$ & $\equiv$ & $\ZERO^*$\\ |
359 $\epsilon^*$ & $\equiv$ & $\epsilon$\\ |
359 $\ONE^*$ & $\equiv$ & $\ONE$\\ |
360 $\varnothing^*$ & $\not\equiv$ & $\varnothing$\\ |
360 $\ZERO^*$ & $\not\equiv$ & $\ZERO$\\ |
361 \end{tabular}} |
361 \end{tabular}} |
362 \end{center} |
362 \end{center} |
363 |
363 |
364 \end{frame} |
364 \end{frame} |
365 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
365 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
368 \begin{frame}[t] |
368 \begin{frame}[t] |
369 \frametitle{Simplification Rules} |
369 \frametitle{Simplification Rules} |
370 |
370 |
371 \begin{center} |
371 \begin{center} |
372 \bl{\begin{tabular}{rcl} |
372 \bl{\begin{tabular}{rcl} |
373 $r + \varnothing$ & $\equiv$ & $r$\\ |
373 $r + \ZERO$ & $\equiv$ & $r$\\ |
374 $\varnothing + r$ & $\equiv$ & $r$\\ |
374 $\ZERO + r$ & $\equiv$ & $r$\\ |
375 $r \cdot \epsilon$ & $\equiv$ & $r$\\ |
375 $r \cdot \ONE$ & $\equiv$ & $r$\\ |
376 $\epsilon \cdot r$ & $\equiv$ & $r$\\ |
376 $\ONE \cdot r$ & $\equiv$ & $r$\\ |
377 $r \cdot \varnothing$ & $\equiv$ & $\varnothing$\\ |
377 $r \cdot \ZERO$ & $\equiv$ & $\ZERO$\\ |
378 $\varnothing \cdot r$ & $\equiv$ & $\varnothing$\\ |
378 $\ZERO \cdot r$ & $\equiv$ & $\ZERO$\\ |
379 $r + r$ & $\equiv$ & $r$ |
379 $r + r$ & $\equiv$ & $r$ |
380 \end{tabular}} |
380 \end{tabular}} |
381 \end{center} |
381 \end{center} |
382 |
382 |
383 \end{frame} |
383 \end{frame} |
440 |
440 |
441 \begin{itemize} |
441 \begin{itemize} |
442 \item \alert{R}egular \alert{e}xpression \alert{D}enial \alert{o}f \alert{S}ervice (ReDoS)\bigskip |
442 \item \alert{R}egular \alert{e}xpression \alert{D}enial \alert{o}f \alert{S}ervice (ReDoS)\bigskip |
443 \item Evil regular expressions\medskip |
443 \item Evil regular expressions\medskip |
444 \begin{itemize} |
444 \begin{itemize} |
445 \item \bl{$(a^?^{\{n\}}) \cdot a^{\{n\}}$} |
445 \item \bl{$({a^?}^{\{n\}}) \cdot a^{\{n\}}$} |
446 \item \bl{$(a^+)^+$} |
446 \item \bl{$(a^+)^+$} |
447 \item \bl{$([a$\,-\,$z]^+)^*$} |
447 \item \bl{$([a$\,-\,$z]^+)^*$} |
448 \item \bl{$(a + a \cdot a)^+$} |
448 \item \bl{$(a + a \cdot a)^+$} |
449 \item \bl{$(a + a?)^+$} |
449 \item \bl{$(a + a?)^+$} |
450 \end{itemize} |
450 \end{itemize} |
459 |
459 |
460 |
460 |
461 \ldots{}whether a regular expression can match the empty string: |
461 \ldots{}whether a regular expression can match the empty string: |
462 \begin{center} |
462 \begin{center} |
463 \begin{tabular}{@ {}l@ {\hspace{2mm}}c@ {\hspace{2mm}}l@ {}} |
463 \begin{tabular}{@ {}l@ {\hspace{2mm}}c@ {\hspace{2mm}}l@ {}} |
464 \bl{$nullable(\varnothing)$} & \bl{$\dn$} & \bl{\textit{false}}\\ |
464 \bl{$nullable(\ZERO)$} & \bl{$\dn$} & \bl{\textit{false}}\\ |
465 \bl{$nullable(\epsilon)$} & \bl{$\dn$} & \bl{\textit{true}}\\ |
465 \bl{$nullable(\ONE)$} & \bl{$\dn$} & \bl{\textit{true}}\\ |
466 \bl{$nullable (c)$} & \bl{$\dn$} & \bl{\textit{false}}\\ |
466 \bl{$nullable (c)$} & \bl{$\dn$} & \bl{\textit{false}}\\ |
467 \bl{$nullable (r_1 + r_2)$} & \bl{$\dn$} & \bl{$nullable(r_1) \vee nullable(r_2)$} \\ |
467 \bl{$nullable (r_1 + r_2)$} & \bl{$\dn$} & \bl{$nullable(r_1) \vee nullable(r_2)$} \\ |
468 \bl{$nullable (r_1 \cdot r_2)$} & \bl{$\dn$} & \bl{$nullable(r_1) \wedge nullable(r_2)$} \\ |
468 \bl{$nullable (r_1 \cdot r_2)$} & \bl{$\dn$} & \bl{$nullable(r_1) \wedge nullable(r_2)$} \\ |
469 \bl{$nullable (r^*)$} & \bl{$\dn$} & \bl{\textit{true}}\\ |
469 \bl{$nullable (r^*)$} & \bl{$\dn$} & \bl{\textit{true}}\\ |
470 \end{tabular} |
470 \end{tabular} |
491 \begin{frame}[c] |
491 \begin{frame}[c] |
492 \frametitle{The Derivative of a Rexp} |
492 \frametitle{The Derivative of a Rexp} |
493 |
493 |
494 \begin{center} |
494 \begin{center} |
495 \begin{tabular}{@ {}l@ {\hspace{2mm}}c@ {\hspace{2mm}}l@ {\hspace{-10mm}}l@ {}} |
495 \begin{tabular}{@ {}l@ {\hspace{2mm}}c@ {\hspace{2mm}}l@ {\hspace{-10mm}}l@ {}} |
496 \bl{$der\, c\, (\varnothing)$} & \bl{$\dn$} & \bl{$\varnothing$} & \\ |
496 \bl{$der\, c\, (\ZERO)$} & \bl{$\dn$} & \bl{$\ZERO$} & \\ |
497 \bl{$der\, c\, (\epsilon)$} & \bl{$\dn$} & \bl{$\varnothing$} & \\ |
497 \bl{$der\, c\, (\ONE)$} & \bl{$\dn$} & \bl{$\ZERO$} & \\ |
498 \bl{$der\, c\, (d)$} & \bl{$\dn$} & \bl{if $c = d$ then $\epsilon$ else $\varnothing$} & \\ |
498 \bl{$der\, c\, (d)$} & \bl{$\dn$} & \bl{if $c = d$ then $\ONE$ else $\ZERO$} & \\ |
499 \bl{$der\, c\, (r_1 + r_2)$} & \bl{$\dn$} & \bl{$der\, c\, r_1 + der\, c\, r_2$} & \\ |
499 \bl{$der\, c\, (r_1 + r_2)$} & \bl{$\dn$} & \bl{$der\, c\, r_1 + der\, c\, r_2$} & \\ |
500 \bl{$der\, c\, (r_1 \cdot r_2)$} & \bl{$\dn$} & \bl{if $nullable (r_1)$}\\ |
500 \bl{$der\, c\, (r_1 \cdot r_2)$} & \bl{$\dn$} & \bl{if $nullable (r_1)$}\\ |
501 & & \bl{then $(der\,c\,r_1) \cdot r_2 + der\, c\, r_2$}\\ |
501 & & \bl{then $(der\,c\,r_1) \cdot r_2 + der\, c\, r_2$}\\ |
502 & & \bl{else $(der\, c\, r_1) \cdot r_2$}\\ |
502 & & \bl{else $(der\, c\, r_1) \cdot r_2$}\\ |
503 \bl{$der\, c\, (r^*)$} & \bl{$\dn$} & \bl{$(der\,c\,r) \cdot (r^*)$} &\bigskip\\\pause |
503 \bl{$der\, c\, (r^*)$} & \bl{$\dn$} & \bl{$(der\,c\,r) \cdot (r^*)$} &\bigskip\\\pause |
690 |
690 |
691 Recall the example of \bl{$r \dn ((a \cdot b) + b)^*$} with |
691 Recall the example of \bl{$r \dn ((a \cdot b) + b)^*$} with |
692 |
692 |
693 \begin{center} |
693 \begin{center} |
694 \begin{tabular}{l} |
694 \begin{tabular}{l} |
695 \bl{$der\,a\,r = ((\epsilon \cdot b) + \varnothing) \cdot r$}\\ |
695 \bl{$der\,a\,r = ((\ONE \cdot b) + \ZERO) \cdot r$}\\ |
696 \bl{$der\,b\,r = ((\varnothing \cdot b) + \epsilon)\cdot r$}\\ |
696 \bl{$der\,b\,r = ((\ZERO \cdot b) + \ONE)\cdot r$}\\ |
697 \bl{$der\,c\,r = ((\varnothing \cdot b) + \varnothing)\cdot r$} |
697 \bl{$der\,c\,r = ((\ZERO \cdot b) + \ZERO)\cdot r$} |
698 \end{tabular} |
698 \end{tabular} |
699 \end{center} |
699 \end{center} |
700 |
700 |
701 What are these regular expressions equivalent to? |
701 What are these regular expressions equivalent to? |
702 |
702 |
758 |
758 |
759 Remember their inductive definition:\\[5cm] |
759 Remember their inductive definition:\\[5cm] |
760 |
760 |
761 \begin{textblock}{6}(5,5) |
761 \begin{textblock}{6}(5,5) |
762 \begin{tabular}{@ {}rrl} |
762 \begin{tabular}{@ {}rrl} |
763 \bl{$r$} & \bl{$::=$} & \bl{$\varnothing$}\\ |
763 \bl{$r$} & \bl{$::=$} & \bl{$\ZERO$}\\ |
764 & \bl{$\mid$} & \bl{$\epsilon$} \\ |
764 & \bl{$\mid$} & \bl{$\ONE$} \\ |
765 & \bl{$\mid$} & \bl{$c$} \\ |
765 & \bl{$\mid$} & \bl{$c$} \\ |
766 & \bl{$\mid$} & \bl{$r_1 \cdot r_2$}\\ |
766 & \bl{$\mid$} & \bl{$r_1 \cdot r_2$}\\ |
767 & \bl{$\mid$} & \bl{$r_1 + r_2$} \\ |
767 & \bl{$\mid$} & \bl{$r_1 + r_2$} \\ |
768 & \bl{$\mid$} & \bl{$r^*$} \\ |
768 & \bl{$\mid$} & \bl{$r^*$} \\ |
769 \end{tabular} |
769 \end{tabular} |
777 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
777 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
778 \begin{frame}[c] |
778 \begin{frame}[c] |
779 \frametitle{Proofs about Rexp (2)} |
779 \frametitle{Proofs about Rexp (2)} |
780 |
780 |
781 \begin{itemize} |
781 \begin{itemize} |
782 \item \bl{$P$} holds for \bl{$\varnothing$}, \bl{$\epsilon$} and \bl{c}\bigskip |
782 \item \bl{$P$} holds for \bl{$\ZERO$}, \bl{$\ONE$} and \bl{c}\bigskip |
783 \item \bl{$P$} holds for \bl{$r_1 + r_2$} under the assumption that \bl{$P$} already |
783 \item \bl{$P$} holds for \bl{$r_1 + r_2$} under the assumption that \bl{$P$} already |
784 holds for \bl{$r_1$} and \bl{$r_2$}.\bigskip |
784 holds for \bl{$r_1$} and \bl{$r_2$}.\bigskip |
785 \item \bl{$P$} holds for \bl{$r_1 \cdot r_2$} under the assumption that \bl{$P$} already |
785 \item \bl{$P$} holds for \bl{$r_1 \cdot r_2$} under the assumption that \bl{$P$} already |
786 holds for \bl{$r_1$} and \bl{$r_2$}.\bigskip |
786 holds for \bl{$r_1$} and \bl{$r_2$}.\bigskip |
787 \item \bl{$P$} holds for \bl{$r^*$} under the assumption that \bl{$P$} already |
787 \item \bl{$P$} holds for \bl{$r^*$} under the assumption that \bl{$P$} already |
808 \begin{frame}[c] |
808 \begin{frame}[c] |
809 \frametitle{Proofs about Rexp (4)} |
809 \frametitle{Proofs about Rexp (4)} |
810 |
810 |
811 \begin{center} |
811 \begin{center} |
812 \bl{\begin{tabular}{r@{\hspace{2mm}}c@{\hspace{2mm}}l} |
812 \bl{\begin{tabular}{r@{\hspace{2mm}}c@{\hspace{2mm}}l} |
813 $rev(\varnothing)$ & $\dn$ & $\varnothing$\\ |
813 $rev(\ZERO)$ & $\dn$ & $\ZERO$\\ |
814 $rev(\epsilon)$ & $\dn$ & $\epsilon$\\ |
814 $rev(\ONE)$ & $\dn$ & $\ONE$\\ |
815 $rev(c)$ & $\dn$ & $c$\\ |
815 $rev(c)$ & $\dn$ & $c$\\ |
816 $rev(r_1 + r_2)$ & $\dn$ & $rev(r_1) + rev(r_2)$\\ |
816 $rev(r_1 + r_2)$ & $\dn$ & $rev(r_1) + rev(r_2)$\\ |
817 $rev(r_1 \cdot r_2)$ & $\dn$ & $rev(r_2) \cdot rev(r_1)$\\ |
817 $rev(r_1 \cdot r_2)$ & $\dn$ & $rev(r_2) \cdot rev(r_1)$\\ |
818 $rev(r^*)$ & $\dn$ & $rev(r)^*$\\ |
818 $rev(r^*)$ & $\dn$ & $rev(r)^*$\\ |
819 \end{tabular}} |
819 \end{tabular}} |