handouts/ho01.tex
author Christian Urban <christian.urban@kcl.ac.uk>
Sat, 09 Nov 2024 06:31:45 +0000
changeset 973 f25d338d16c9
parent 965 94f5cce73a4f
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
621
cf287db8dc15 updated
Christian Urban <urbanc@in.tum.de>
parents: 618
diff changeset
     1
% !TEX program = xelatex
105
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     2
\documentclass{article}
237
370c0647a9bf more material
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 217
diff changeset
     3
\usepackage{../style}
217
cd6066f1056a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 123
diff changeset
     4
\usepackage{../langs}
871
94b84d880c2b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 830
diff changeset
     5
\usepackage{../graphicss}
242
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
     6
\usepackage{../data}
477
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 473
diff changeset
     7
\usepackage{lstlinebgrd}
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 473
diff changeset
     8
\definecolor{capri}{rgb}{0.0, 0.75, 1.0}
108
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
     9
306
fecffce112fa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 291
diff changeset
    10
%%http://regexcrossword.com/challenges/cities/puzzles/1
398
c8ce95067c1a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 395
diff changeset
    11
%%https://jex.im/regulex/
c8ce95067c1a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 395
diff changeset
    12
%%https://www.reddit.com/r/ProgrammingLanguages/comments/42dlem/mona_compiler_development_part_2_parsing/
c8ce95067c1a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 395
diff changeset
    13
%%https://www.reddit.com/r/ProgrammingLanguages/comments/43wlkq/formal_grammar_for_csh_tsch_sh_or_bash/
112
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 111
diff changeset
    14
403
564f7584eff1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 399
diff changeset
    15
%% regex displayers
564f7584eff1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 399
diff changeset
    16
%% https://regexper.com/#a%7Ca
564f7584eff1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 399
diff changeset
    17
%% https://www.debuggex.com
564f7584eff1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 399
diff changeset
    18
%% https://jex.im/regulex/#!embed=false&flags=&re=%5E(a%7Cb)*%3F%24
564f7584eff1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 399
diff changeset
    19
564f7584eff1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 399
diff changeset
    20
%% email validator
564f7584eff1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 399
diff changeset
    21
%% http://www.ex-parrot.com/%7Epdw/Mail-RFC822-Address.html
496
5c9de27a5b30 updated
Christian Urban <urbanc@in.tum.de>
parents: 492
diff changeset
    22
% https://jackfoxy.github.io/FsRegEx/emailregex.html
5c9de27a5b30 updated
Christian Urban <urbanc@in.tum.de>
parents: 492
diff changeset
    23
403
564f7584eff1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 399
diff changeset
    24
564f7584eff1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 399
diff changeset
    25
%% regex testers
564f7584eff1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 399
diff changeset
    26
% https://regex101.com
564f7584eff1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 399
diff changeset
    27
% http://regexr.com
564f7584eff1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 399
diff changeset
    28
564f7584eff1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 399
diff changeset
    29
%% emacs regexes
564f7584eff1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 399
diff changeset
    30
%% https://www.gnu.org/software/emacs/manual/html_node/elisp/Regular-Expressions.html
564f7584eff1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 399
diff changeset
    31
872
5f5e165c9a57 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 871
diff changeset
    32
%% reasons for a new programming language
403
564f7584eff1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 399
diff changeset
    33
%% http://beautifulracket.com
564f7584eff1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 399
diff changeset
    34
418
010c5a03dca2 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 417
diff changeset
    35
% compiler explorer
010c5a03dca2 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 417
diff changeset
    36
% https://gcc.godbolt.org
010c5a03dca2 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 417
diff changeset
    37
622
b47e140bcccd updated
Christian Urban <urbanc@in.tum.de>
parents: 621
diff changeset
    38
716
df7d47a507f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 706
diff changeset
    39
% good article how languages have been influenced
df7d47a507f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 706
diff changeset
    40
% 10 MOST(LY DEAD) INFLUENTIAL PROGRAMMING LANGUAGES
df7d47a507f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 706
diff changeset
    41
% https://www.hillelwayne.com/post/influential-dead-languages/
df7d47a507f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 706
diff changeset
    42
622
b47e140bcccd updated
Christian Urban <urbanc@in.tum.de>
parents: 621
diff changeset
    43
105
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    44
\begin{document}
924
6ad0f63e1968 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
    45
\fnote{\copyright{} Christian Urban, King's College London, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2023}
105
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    46
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    47
\section*{Handout 1}
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    48
722
14914b57e207 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 716
diff changeset
    49
The purpose of a compiler is to transform a program a human can read and
830
dbf9d710ce65 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 745
diff changeset
    50
write into code machines can run as fast as possible. Developing a
722
14914b57e207 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 716
diff changeset
    51
compiler is an old craft going back to 1952 with the first compiler
14914b57e207 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 716
diff changeset
    52
written by Grace Hopper.\footnote{Who many years ago was invited on a
743
6acabeecdf75 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 742
diff changeset
    53
talk show hosted by David Letterman.
965
94f5cce73a4f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 926
diff changeset
    54
\here{https://youtu.be/oE2uls6iIEU}} Why studying compilers
924
6ad0f63e1968 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
    55
nowadays?  An interesting answer is given by John Regehr in his compiler
743
6acabeecdf75 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 742
diff changeset
    56
blog:\here{http://blog.regehr.org/archives/1419}
690
8d57433c7b5e updated
Christian Urban <urbanc@in.tum.de>
parents: 622
diff changeset
    57
8d57433c7b5e updated
Christian Urban <urbanc@in.tum.de>
parents: 622
diff changeset
    58
\begin{quote}\it{}``We can start off with a couple of observations
8d57433c7b5e updated
Christian Urban <urbanc@in.tum.de>
parents: 622
diff changeset
    59
  about the role of compilers. First, hardware is getting weirder
8d57433c7b5e updated
Christian Urban <urbanc@in.tum.de>
parents: 622
diff changeset
    60
  rather than getting clocked faster: almost all processors are
8d57433c7b5e updated
Christian Urban <urbanc@in.tum.de>
parents: 622
diff changeset
    61
  multicores and it looks like there is increasing asymmetry in
8d57433c7b5e updated
Christian Urban <urbanc@in.tum.de>
parents: 622
diff changeset
    62
  resources across cores. Processors come with vector units, crypto
8d57433c7b5e updated
Christian Urban <urbanc@in.tum.de>
parents: 622
diff changeset
    63
  accelerators, bit twiddling instructions, and lots of features to
8d57433c7b5e updated
Christian Urban <urbanc@in.tum.de>
parents: 622
diff changeset
    64
  make virtualization and concurrency work. We have DSPs, GPUs,
8d57433c7b5e updated
Christian Urban <urbanc@in.tum.de>
parents: 622
diff changeset
    65
  big.little, and Xeon Phi. This is only scratching the
8d57433c7b5e updated
Christian Urban <urbanc@in.tum.de>
parents: 622
diff changeset
    66
  surface. Second, we’re getting tired of low-level languages and
8d57433c7b5e updated
Christian Urban <urbanc@in.tum.de>
parents: 622
diff changeset
    67
  their associated security disasters, we want to write new code, to
8d57433c7b5e updated
Christian Urban <urbanc@in.tum.de>
parents: 622
diff changeset
    68
  whatever extent possible, in safer, higher-level
8d57433c7b5e updated
Christian Urban <urbanc@in.tum.de>
parents: 622
diff changeset
    69
  languages. Compilers are caught right in the middle of these
8d57433c7b5e updated
Christian Urban <urbanc@in.tum.de>
parents: 622
diff changeset
    70
  opposing trends: one of their main jobs is to help bridge the large
8d57433c7b5e updated
Christian Urban <urbanc@in.tum.de>
parents: 622
diff changeset
    71
  and growing gap between increasingly high-level languages and
8d57433c7b5e updated
Christian Urban <urbanc@in.tum.de>
parents: 622
diff changeset
    72
  increasingly wacky platforms. It’s effectively a perpetual
8d57433c7b5e updated
Christian Urban <urbanc@in.tum.de>
parents: 622
diff changeset
    73
  employment act for solid compiler hackers.''
8d57433c7b5e updated
Christian Urban <urbanc@in.tum.de>
parents: 622
diff changeset
    74
\end{quote}  
8d57433c7b5e updated
Christian Urban <urbanc@in.tum.de>
parents: 622
diff changeset
    75
722
14914b57e207 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 716
diff changeset
    76
\noindent
872
5f5e165c9a57 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 871
diff changeset
    77
Given this, the goal of this module is to become a solid (beginner) compiler
5f5e165c9a57 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 871
diff changeset
    78
hacker and as part of the coursework to implement two small
5f5e165c9a57 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 871
diff changeset
    79
compilers for two very small programming languages.
690
8d57433c7b5e updated
Christian Urban <urbanc@in.tum.de>
parents: 622
diff changeset
    80
722
14914b57e207 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 716
diff changeset
    81
The first part of the module is about the problem of text processing,
14914b57e207 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 716
diff changeset
    82
which is needed for compilers, but also for dictionaries, DNA-data,
14914b57e207 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 716
diff changeset
    83
spam-filters and so on. When looking for a particular string, say
14914b57e207 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 716
diff changeset
    84
\pcode{"foobar"}, in a large text we can use the Knuth-Morris-Pratt
14914b57e207 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 716
diff changeset
    85
algorithm, which is currently the most efficient general string search
14914b57e207 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 716
diff changeset
    86
algorithm. But often we do \emph{not} just look for a particular string,
14914b57e207 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 716
diff changeset
    87
but for string patterns. For example, in program code we need to
14914b57e207 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 716
diff changeset
    88
identify what are the keywords (\texttt{if}, \texttt{then},
14914b57e207 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 716
diff changeset
    89
\texttt{while}, \texttt{for}, etc) and what are the identifiers
14914b57e207 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 716
diff changeset
    90
(variable names). A pattern for identifiers could be stated as: they
14914b57e207 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 716
diff changeset
    91
start with a letter, followed by zero or more letters, numbers and
14914b57e207 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 716
diff changeset
    92
underscores. 
618
f4818c95a32e updated
Christian Urban <urbanc@in.tum.de>
parents: 570
diff changeset
    93
621
cf287db8dc15 updated
Christian Urban <urbanc@in.tum.de>
parents: 618
diff changeset
    94
%You might also be surprised what
cf287db8dc15 updated
Christian Urban <urbanc@in.tum.de>
parents: 618
diff changeset
    95
%constraints programming languages impose about numbers: for example
cf287db8dc15 updated
Christian Urban <urbanc@in.tum.de>
parents: 618
diff changeset
    96
%123 in JSON is OK, but 0123 is not.
706
b560f78781b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 690
diff changeset
    97
%
b560f78781b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 690
diff changeset
    98
% The regex for JASON numbers is
b560f78781b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 690
diff changeset
    99
%
b560f78781b9 updated
Christian Urban <urbanc@in.tum.de>
parents: 690
diff changeset
   100
% -?(0|[1-9][0-9]*)(\.[0-9]+)?([eE][+-]?[0-9]+)?
621
cf287db8dc15 updated
Christian Urban <urbanc@in.tum.de>
parents: 618
diff changeset
   101
cf287db8dc15 updated
Christian Urban <urbanc@in.tum.de>
parents: 618
diff changeset
   102
Often we also face the problem that we are given a string, for example
cf287db8dc15 updated
Christian Urban <urbanc@in.tum.de>
parents: 618
diff changeset
   103
some user input, and we want to know whether it matches a particular
622
b47e140bcccd updated
Christian Urban <urbanc@in.tum.de>
parents: 621
diff changeset
   104
pattern---is it an email address, for example. In this way we can
618
f4818c95a32e updated
Christian Urban <urbanc@in.tum.de>
parents: 570
diff changeset
   105
exclude user input that would otherwise have nasty effects on our
f4818c95a32e updated
Christian Urban <urbanc@in.tum.de>
parents: 570
diff changeset
   106
program (crashing it or making it go into an infinite loop, if not
872
5f5e165c9a57 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 871
diff changeset
   107
worse). This kind of ``vetting'' mechanism is also implemented in
622
b47e140bcccd updated
Christian Urban <urbanc@in.tum.de>
parents: 621
diff changeset
   108
popular network security tools such as Snort and
872
5f5e165c9a57 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 871
diff changeset
   109
Zeek.\here{www.snort.org}\here{www.bro.org} They scan incoming
622
b47e140bcccd updated
Christian Urban <urbanc@in.tum.de>
parents: 621
diff changeset
   110
network traffic for computer viruses or malicious packets. Similarly
b47e140bcccd updated
Christian Urban <urbanc@in.tum.de>
parents: 621
diff changeset
   111
filtering out spam usually involves looking for some signature
621
cf287db8dc15 updated
Christian Urban <urbanc@in.tum.de>
parents: 618
diff changeset
   112
(essentially a string pattern). The point is that the fast
618
f4818c95a32e updated
Christian Urban <urbanc@in.tum.de>
parents: 570
diff changeset
   113
Knuth-Morris-Pratt algorithm for strings is not good enough for such
f4818c95a32e updated
Christian Urban <urbanc@in.tum.de>
parents: 570
diff changeset
   114
string \emph{patterns}.\smallskip
291
201c2c6d8696 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 268
diff changeset
   115
201c2c6d8696 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 268
diff changeset
   116
\defn{Regular expressions} help with conveniently specifying
201c2c6d8696 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 268
diff changeset
   117
such patterns. The idea behind regular expressions is that
201c2c6d8696 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 268
diff changeset
   118
they are a simple method for describing languages (or sets of
201c2c6d8696 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 268
diff changeset
   119
strings)\ldots at least languages we are interested in in
926
42ecc3186944 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 925
diff changeset
   120
Computer Science. For example there is no convenient regular
291
201c2c6d8696 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 268
diff changeset
   121
expression for describing the English language short of
201c2c6d8696 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 268
diff changeset
   122
enumerating all English words. But they seem useful for
399
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 398
diff changeset
   123
describing for example simple email addresses.\footnote{See
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 398
diff changeset
   124
``8 Regular Expressions You Should Know''
291
201c2c6d8696 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 268
diff changeset
   125
\url{http://goo.gl/5LoVX7}} Consider the following regular
201c2c6d8696 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 268
diff changeset
   126
expression
242
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   127
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   128
\begin{equation}\label{email}
416
357c395ae838 updated
Christian Urban <urbanc@in.tum.de>
parents: 415
diff changeset
   129
\texttt{[a-z0-9\_.-]+} \;\;\texttt{@}\;\; \texttt{[a-z0-9.-]+} \;\;\texttt{.}\;\; \texttt{[a-z.]\{2,6\}}
242
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   130
\end{equation}
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   131
621
cf287db8dc15 updated
Christian Urban <urbanc@in.tum.de>
parents: 618
diff changeset
   132
\noindent where the first part, the user name, matches one or more
cf287db8dc15 updated
Christian Urban <urbanc@in.tum.de>
parents: 618
diff changeset
   133
lowercase letters (\pcode{a-z}), digits (\pcode{0-9}), underscores, dots
cf287db8dc15 updated
Christian Urban <urbanc@in.tum.de>
parents: 618
diff changeset
   134
and hyphens. The \pcode{+} at the end of the brackets ensures the ``one
cf287db8dc15 updated
Christian Urban <urbanc@in.tum.de>
parents: 618
diff changeset
   135
or more''. Then comes the email \pcode{@}-sign, followed by the domain
cf287db8dc15 updated
Christian Urban <urbanc@in.tum.de>
parents: 618
diff changeset
   136
name which must be one or more lowercase letters, digits, underscores,
cf287db8dc15 updated
Christian Urban <urbanc@in.tum.de>
parents: 618
diff changeset
   137
dots or hyphens (but no underscores).  Finally there must be a dot
cf287db8dc15 updated
Christian Urban <urbanc@in.tum.de>
parents: 618
diff changeset
   138
followed by the toplevel domain. This toplevel domain must be 2 to 6
cf287db8dc15 updated
Christian Urban <urbanc@in.tum.de>
parents: 618
diff changeset
   139
lowercase letters including the dot. Example strings which follow this
cf287db8dc15 updated
Christian Urban <urbanc@in.tum.de>
parents: 618
diff changeset
   140
pattern are:
242
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   141
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   142
\begin{lstlisting}[language={},numbers=none,keywordstyle=\color{black}]
243
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   143
niceandsimple@example.org
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   144
very.common@example.co.uk
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   145
a.little.lengthy.but.fine@dept.example.ac.uk
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   146
other.email-with-dash@example.edu
242
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   147
\end{lstlisting}
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   148
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   149
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   150
\noindent
399
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 398
diff changeset
   151
But for example the following two do not
242
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   152
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   153
\begin{lstlisting}[language={},numbers=none,keywordstyle=\color{black}]
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   154
user@localserver
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   155
disposable.style.email.with+symbol@example.com
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   156
\end{lstlisting}
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   157
471
9476086849ad updated
Christian Urban <urbanc@in.tum.de>
parents: 452
diff changeset
   158
\noindent according to the regular expression we specified in line
9476086849ad updated
Christian Urban <urbanc@in.tum.de>
parents: 452
diff changeset
   159
\eqref{email} above. Whether this is intended or not is a different
9476086849ad updated
Christian Urban <urbanc@in.tum.de>
parents: 452
diff changeset
   160
question (the second email above is actually an acceptable email
550
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 507
diff changeset
   161
address according to the RFC 5322 standard for email addresses).
399
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 398
diff changeset
   162
 
327
9470cd124667 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 318
diff changeset
   163
As mentioned above, identifiers, or variables, in program code
9470cd124667 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 318
diff changeset
   164
are often required to satisfy the constraints that they start
9470cd124667 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 318
diff changeset
   165
with a letter and then can be followed by zero or more letters
9470cd124667 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 318
diff changeset
   166
or numbers and also can include underscores, but not as the
9470cd124667 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 318
diff changeset
   167
first character. Such identifiers can be recognised with the
9470cd124667 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 318
diff changeset
   168
regular expression
242
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   169
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   170
\begin{center}
243
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   171
\pcode{[a-zA-Z] [a-zA-Z0-9_]*}
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   172
\end{center}
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   173
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   174
\noindent Possible identifiers that match this regular expression 
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   175
are \pcode{x}, \pcode{foo}, \pcode{foo_bar_1}, \pcode{A_very_42_long_object_name},
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   176
but not \pcode{_i} and also not \pcode{4you}.
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   177
404
245d302791c7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 403
diff changeset
   178
Many programming languages offer libraries that can be used to
243
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   179
validate such strings against regular expressions. Also there
248
ce767ca23244 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 245
diff changeset
   180
are some common, and I am sure very familiar, ways of how to
404
245d302791c7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 403
diff changeset
   181
construct regular expressions. For example in Scala we have
245d302791c7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 403
diff changeset
   182
a library implementing the following regular expressions: 
243
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   183
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   184
\begin{center}
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   185
\begin{tabular}{lp{9cm}}
242
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   186
\pcode{re*} & matches 0 or more occurrences of preceding 
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   187
expression\\
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   188
\pcode{re+} & matches 1 or more occurrences of preceding
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   189
expression\\
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   190
\pcode{re?} &	 matches 0 or 1 occurrence of preceding 
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   191
expression\\
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   192
\pcode{re\{n\}}	& matches exactly \pcode{n} number of 
243
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   193
occurrences of preceding  expression\\
242
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   194
\pcode{re\{n,m\}} & matches at least \pcode{n} and at most {\tt m}
550
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 507
diff changeset
   195
occurrences of the preceding expression\\
242
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   196
\pcode{[...]} & matches any single character inside the 
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   197
brackets\\
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   198
\pcode{[^...]} & matches any single character not inside the 
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   199
brackets\\
250
b79e704acb72 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 248
diff changeset
   200
\pcode{...-...} & character ranges\\
b79e704acb72 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 248
diff changeset
   201
\pcode{\\d} & matches digits; equivalent to \pcode{[0-9]}\\
b79e704acb72 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 248
diff changeset
   202
\pcode{.} & matches every character except newline\\
b79e704acb72 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 248
diff changeset
   203
\pcode{(re)}	& groups regular expressions and remembers 
b79e704acb72 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 248
diff changeset
   204
matched text
243
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   205
\end{tabular}
242
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   206
\end{center}
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   207
925
ddb521b57e0c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 924
diff changeset
   208
\noindent
924
6ad0f63e1968 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   209
The syntax is pretty universal and can be found in many regular
6ad0f63e1968 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   210
expression libraries. If you need a quick recap about regular
6ad0f63e1968 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   211
expressions and how the match strings, here is a quick video:
6ad0f63e1968 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   212
\url{https://youtu.be/bgBWp9EIlMM}.
243
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   213
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   214
\subsection*{Why Study Regular Expressions?}
242
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   215
471
9476086849ad updated
Christian Urban <urbanc@in.tum.de>
parents: 452
diff changeset
   216
Regular expressions were introduced by Kleene in the 1950ies and they
9476086849ad updated
Christian Urban <urbanc@in.tum.de>
parents: 452
diff changeset
   217
have been object of intense study since then. They are nowadays pretty
926
42ecc3186944 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 925
diff changeset
   218
much ubiquitous in Computer Science. There are many libraries
471
9476086849ad updated
Christian Urban <urbanc@in.tum.de>
parents: 452
diff changeset
   219
implementing regular expressions. I am sure you have come across them
622
b47e140bcccd updated
Christian Urban <urbanc@in.tum.de>
parents: 621
diff changeset
   220
before (remember the PRA or PEP modules?). 
621
cf287db8dc15 updated
Christian Urban <urbanc@in.tum.de>
parents: 618
diff changeset
   221
cf287db8dc15 updated
Christian Urban <urbanc@in.tum.de>
parents: 618
diff changeset
   222
Why on earth then is there any interest in studying them again in depth
cf287db8dc15 updated
Christian Urban <urbanc@in.tum.de>
parents: 618
diff changeset
   223
in this module? Well, one answer is in the following two graphs about
924
6ad0f63e1968 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   224
regular expression matching in Python, Ruby, JavaScript, Swift and Java
621
cf287db8dc15 updated
Christian Urban <urbanc@in.tum.de>
parents: 618
diff changeset
   225
(Version 8).
242
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   226
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   227
\begin{center}
621
cf287db8dc15 updated
Christian Urban <urbanc@in.tum.de>
parents: 618
diff changeset
   228
\begin{tabular}{@{\hspace{-1mm}}c@{\hspace{1.5mm}}c@{}}
477
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 473
diff changeset
   229
\begin{tikzpicture}
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 473
diff changeset
   230
\begin{axis}[
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 473
diff changeset
   231
    title={Graph: $\texttt{(a*)*\,b}$ and strings 
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 473
diff changeset
   232
           $\underbrace{\texttt{a}\ldots \texttt{a}}_{n}$},
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 473
diff changeset
   233
    xlabel={$n$},
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 473
diff changeset
   234
    x label style={at={(1.05,0.0)}},
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 473
diff changeset
   235
    ylabel={time in secs},
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 473
diff changeset
   236
    enlargelimits=false,
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 473
diff changeset
   237
    xtick={0,5,...,30},
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 473
diff changeset
   238
    xmax=33,
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 473
diff changeset
   239
    ymax=35,
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 473
diff changeset
   240
    ytick={0,5,...,30},
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 473
diff changeset
   241
    scaled ticks=false,
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 473
diff changeset
   242
    axis lines=left,
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 473
diff changeset
   243
    width=5.5cm,
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 473
diff changeset
   244
    height=4.5cm, 
924
6ad0f63e1968 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   245
    legend entries={Python, Java 8, JavaScript, Swift},  
477
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 473
diff changeset
   246
    legend pos=north west,
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 473
diff changeset
   247
    legend cell align=left]
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 473
diff changeset
   248
\addplot[blue,mark=*, mark options={fill=white}] table {re-python2.data};
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 473
diff changeset
   249
\addplot[cyan,mark=*, mark options={fill=white}] table {re-java.data};
618
f4818c95a32e updated
Christian Urban <urbanc@in.tum.de>
parents: 570
diff changeset
   250
\addplot[red,mark=*, mark options={fill=white}] table {re-js.data};
924
6ad0f63e1968 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   251
\addplot[magenta,mark=*, mark options={fill=white}] table {re-swift.data};
477
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 473
diff changeset
   252
\end{axis}
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 473
diff changeset
   253
\end{tikzpicture}
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 473
diff changeset
   254
&
291
201c2c6d8696 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 268
diff changeset
   255
\begin{tikzpicture}
399
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 398
diff changeset
   256
\begin{axis}[
415
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 407
diff changeset
   257
    title={Graph: $\texttt{a?\{n\}\,a{\{n\}}}$ and strings 
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 407
diff changeset
   258
           $\underbrace{\texttt{a}\ldots \texttt{a}}_{n}$},
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 407
diff changeset
   259
    xlabel={$n$},
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 407
diff changeset
   260
    x label style={at={(1.05,0.0)}},
399
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 398
diff changeset
   261
    ylabel={time in secs},
291
201c2c6d8696 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 268
diff changeset
   262
    enlargelimits=false,
201c2c6d8696 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 268
diff changeset
   263
    xtick={0,5,...,30},
201c2c6d8696 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 268
diff changeset
   264
    xmax=33,
201c2c6d8696 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 268
diff changeset
   265
    ymax=35,
201c2c6d8696 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 268
diff changeset
   266
    ytick={0,5,...,30},
201c2c6d8696 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 268
diff changeset
   267
    scaled ticks=false,
201c2c6d8696 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 268
diff changeset
   268
    axis lines=left,
415
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 407
diff changeset
   269
    width=5.5cm,
318
7975e4f0d4de updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 306
diff changeset
   270
    height=4.5cm, 
291
201c2c6d8696 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 268
diff changeset
   271
    legend entries={Python,Ruby},  
201c2c6d8696 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 268
diff changeset
   272
    legend pos=north west,
201c2c6d8696 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 268
diff changeset
   273
    legend cell align=left]
448
96129128d0f1 updated
Christian Urban <urbanc@in.tum.de>
parents: 443
diff changeset
   274
\addplot[blue,mark=*, mark options={fill=white}] table {re-python.data};
96129128d0f1 updated
Christian Urban <urbanc@in.tum.de>
parents: 443
diff changeset
   275
\addplot[brown,mark=triangle*, mark options={fill=white}] table {re-ruby.data};  
291
201c2c6d8696 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 268
diff changeset
   276
\end{axis}
242
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   277
\end{tikzpicture}
415
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 407
diff changeset
   278
\end{tabular}
242
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   279
\end{center}
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   280
924
6ad0f63e1968 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   281
\noindent The first graph shows that Python, JavaScript, Swift and Java 8 need
477
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 473
diff changeset
   282
approximately 30 seconds to find out that the regular expression
621
cf287db8dc15 updated
Christian Urban <urbanc@in.tum.de>
parents: 618
diff changeset
   283
$\texttt{(a*)*\,b}$ does not match strings of 28 \texttt{a}s. Similarly,
cf287db8dc15 updated
Christian Urban <urbanc@in.tum.de>
parents: 618
diff changeset
   284
the second shows that Python and Ruby need approximately 29 seconds for finding
cf287db8dc15 updated
Christian Urban <urbanc@in.tum.de>
parents: 618
diff changeset
   285
out whether a string of 28 \texttt{a}s matches the regular expression
cf287db8dc15 updated
Christian Urban <urbanc@in.tum.de>
parents: 618
diff changeset
   286
\texttt{a?\{28\}\,a\{28\}}.\footnote{In this
cf287db8dc15 updated
Christian Urban <urbanc@in.tum.de>
parents: 618
diff changeset
   287
example Ruby uses actually the slightly different regular expression
cf287db8dc15 updated
Christian Urban <urbanc@in.tum.de>
parents: 618
diff changeset
   288
\texttt{a?a?a?...a?a?aaa...aa}, where the \texttt{a?} and \texttt{a}
cf287db8dc15 updated
Christian Urban <urbanc@in.tum.de>
parents: 618
diff changeset
   289
each occur $n$ times. More such test cases can be found at
cf287db8dc15 updated
Christian Urban <urbanc@in.tum.de>
parents: 618
diff changeset
   290
\url{https://www.owasp.org/index.php/Regular_expression_Denial_of_Service_-_ReDoS}.}
477
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 473
diff changeset
   291
Admittedly, these regular expressions are carefully chosen to exhibit
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 473
diff changeset
   292
this exponential behaviour, but similar ones occur more often than one
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 473
diff changeset
   293
wants in ``real life''. For example, on 20 July 2016 a similar regular
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 473
diff changeset
   294
expression brought the webpage \href{http://stackexchange.com}{Stack
621
cf287db8dc15 updated
Christian Urban <urbanc@in.tum.de>
parents: 618
diff changeset
   295
Exchange} to its knees:
407
4b454a6d1814 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 404
diff changeset
   296
4b454a6d1814 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 404
diff changeset
   297
\begin{center}
4b454a6d1814 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 404
diff changeset
   298
\url{http://stackstatus.net/post/147710624694/outage-postmortem-july-20-2016}
4b454a6d1814 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 404
diff changeset
   299
\end{center}
4b454a6d1814 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 404
diff changeset
   300
417
e74c696821a2 updated
Christian Urban <urbanc@in.tum.de>
parents: 416
diff changeset
   301
\noindent I can also highly recommend a cool webtalk from an engineer
e74c696821a2 updated
Christian Urban <urbanc@in.tum.de>
parents: 416
diff changeset
   302
from Stack Exchange on the same subject:
e74c696821a2 updated
Christian Urban <urbanc@in.tum.de>
parents: 416
diff changeset
   303
e74c696821a2 updated
Christian Urban <urbanc@in.tum.de>
parents: 416
diff changeset
   304
\begin{center}
e74c696821a2 updated
Christian Urban <urbanc@in.tum.de>
parents: 416
diff changeset
   305
\url{https://vimeo.com/112065252}
e74c696821a2 updated
Christian Urban <urbanc@in.tum.de>
parents: 416
diff changeset
   306
\end{center}
e74c696821a2 updated
Christian Urban <urbanc@in.tum.de>
parents: 416
diff changeset
   307
407
4b454a6d1814 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 404
diff changeset
   308
\noindent
550
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 507
diff changeset
   309
A similar problem also occurred in the Atom editor:
416
357c395ae838 updated
Christian Urban <urbanc@in.tum.de>
parents: 415
diff changeset
   310
357c395ae838 updated
Christian Urban <urbanc@in.tum.de>
parents: 415
diff changeset
   311
\begin{center}
357c395ae838 updated
Christian Urban <urbanc@in.tum.de>
parents: 415
diff changeset
   312
\url{http://davidvgalbraith.com/how-i-fixed-atom/}
357c395ae838 updated
Christian Urban <urbanc@in.tum.de>
parents: 415
diff changeset
   313
\end{center}
357c395ae838 updated
Christian Urban <urbanc@in.tum.de>
parents: 415
diff changeset
   314
563
bddf14e026b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 562
diff changeset
   315
\noindent
621
cf287db8dc15 updated
Christian Urban <urbanc@in.tum.de>
parents: 618
diff changeset
   316
and also when somebody tried to match web-addresses using a 
cf287db8dc15 updated
Christian Urban <urbanc@in.tum.de>
parents: 618
diff changeset
   317
relatively simple regular expression
554
2509c670e3a2 updated
Christian Urban <urbanc@in.tum.de>
parents: 553
diff changeset
   318
2509c670e3a2 updated
Christian Urban <urbanc@in.tum.de>
parents: 553
diff changeset
   319
\begin{center}
924
6ad0f63e1968 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   320
\url{https://archive.ph/W5Ogx#selection-141.1-141.36}
554
2509c670e3a2 updated
Christian Urban <urbanc@in.tum.de>
parents: 553
diff changeset
   321
\end{center}  
2509c670e3a2 updated
Christian Urban <urbanc@in.tum.de>
parents: 553
diff changeset
   322
621
cf287db8dc15 updated
Christian Urban <urbanc@in.tum.de>
parents: 618
diff changeset
   323
\noindent
cf287db8dc15 updated
Christian Urban <urbanc@in.tum.de>
parents: 618
diff changeset
   324
Finally, on 2 July 2019 Cloudflare had a global outage because of a 
830
dbf9d710ce65 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 745
diff changeset
   325
regular expression (they had no outage for the 6 years before).  What
621
cf287db8dc15 updated
Christian Urban <urbanc@in.tum.de>
parents: 618
diff changeset
   326
happened is nicely explained in the blog:
cf287db8dc15 updated
Christian Urban <urbanc@in.tum.de>
parents: 618
diff changeset
   327
cf287db8dc15 updated
Christian Urban <urbanc@in.tum.de>
parents: 618
diff changeset
   328
\begin{center}
cf287db8dc15 updated
Christian Urban <urbanc@in.tum.de>
parents: 618
diff changeset
   329
\url{https://blog.cloudflare.com/details-of-the-cloudflare-outage-on-july-2-2019}
cf287db8dc15 updated
Christian Urban <urbanc@in.tum.de>
parents: 618
diff changeset
   330
\end{center}  
563
bddf14e026b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 562
diff changeset
   331
415
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 407
diff changeset
   332
Such troublesome regular expressions are sometimes called \emph{evil
722
14914b57e207 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 716
diff changeset
   333
regular expressions} because they have the potential to make regular
14914b57e207 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 716
diff changeset
   334
expression matching engines to topple over, like in Python, Ruby,
14914b57e207 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 716
diff changeset
   335
JavaScript and Java 8. This ``toppling over'' is also sometimes called
14914b57e207 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 716
diff changeset
   336
\emph{catastrophic backtracking}.  I have also seen the term
14914b57e207 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 716
diff changeset
   337
\emph{eternal matching} used for this.  The problem with evil regular
14914b57e207 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 716
diff changeset
   338
expressions and catastrophic backtracking is that they can have some
14914b57e207 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 716
diff changeset
   339
serious consequences, for example, if you use them in your
14914b57e207 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 716
diff changeset
   340
web-application. The reason is that hackers can look for these instances
14914b57e207 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 716
diff changeset
   341
where the matching engine behaves badly and then mount a nice DoS-attack
14914b57e207 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 716
diff changeset
   342
against your application. These attacks are already have their own name:
14914b57e207 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 716
diff changeset
   343
\emph{Regular Expression Denial of Service Attacks (ReDoS)}.
242
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   344
622
b47e140bcccd updated
Christian Urban <urbanc@in.tum.de>
parents: 621
diff changeset
   345
It will be instructive to look behind the ``scenes'' to find out why
b47e140bcccd updated
Christian Urban <urbanc@in.tum.de>
parents: 621
diff changeset
   346
Python and Ruby (and others) behave so badly when matching strings with
b47e140bcccd updated
Christian Urban <urbanc@in.tum.de>
parents: 621
diff changeset
   347
evil regular expressions. But we will also look at a relatively simple
b47e140bcccd updated
Christian Urban <urbanc@in.tum.de>
parents: 621
diff changeset
   348
algorithm that solves this problem much better than Python and Ruby
b47e140bcccd updated
Christian Urban <urbanc@in.tum.de>
parents: 621
diff changeset
   349
do\ldots actually it will be two versions of the algorithm: the first
b47e140bcccd updated
Christian Urban <urbanc@in.tum.de>
parents: 621
diff changeset
   350
one will be able in  the example \texttt{a?\{n\}\,a\{n\}} to process strings of
b47e140bcccd updated
Christian Urban <urbanc@in.tum.de>
parents: 621
diff changeset
   351
approximately 1,100 \texttt{a}s in 23 seconds, while the second version
b47e140bcccd updated
Christian Urban <urbanc@in.tum.de>
parents: 621
diff changeset
   352
will even be able to process up to 11,000(!) in 5 seconds, see the graph
b47e140bcccd updated
Christian Urban <urbanc@in.tum.de>
parents: 621
diff changeset
   353
below:
242
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   354
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   355
\begin{center}
291
201c2c6d8696 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 268
diff changeset
   356
\begin{tikzpicture}
399
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 398
diff changeset
   357
  \begin{axis}[
415
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 407
diff changeset
   358
    title={Graph: $\texttt{a?\{n\}\,a{\{n\}}}$ and strings 
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 407
diff changeset
   359
           $\underbrace{\texttt{a}\ldots \texttt{a}}_{n}$},
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 407
diff changeset
   360
    xlabel={$n$},
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 407
diff changeset
   361
    x label style={at={(1.05,0.0)}},
399
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 398
diff changeset
   362
    ylabel={time in secs},
291
201c2c6d8696 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 268
diff changeset
   363
    enlargelimits=false,
477
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 473
diff changeset
   364
    xtick={0,3000,...,12000},
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 473
diff changeset
   365
    xmax=13000,
443
cd43d8c6eb84 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 418
diff changeset
   366
    ymax=32,
cd43d8c6eb84 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 418
diff changeset
   367
    ytick={0,5,...,30},
291
201c2c6d8696 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 268
diff changeset
   368
    scaled ticks=false,
201c2c6d8696 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 268
diff changeset
   369
    axis lines=left,
415
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 407
diff changeset
   370
    width=7cm,
622
b47e140bcccd updated
Christian Urban <urbanc@in.tum.de>
parents: 621
diff changeset
   371
    height=4.4cm,
415
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 407
diff changeset
   372
    legend entries={Our Algorithm V1, Our Algorithm V2},
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 407
diff changeset
   373
    legend pos=outer north east]
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 407
diff changeset
   374
\addplot[green,mark=square*,mark options={fill=white}] table {re2.data};
291
201c2c6d8696 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 268
diff changeset
   375
\addplot[black,mark=square*,mark options={fill=white}] table {re3.data};
201c2c6d8696 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 268
diff changeset
   376
\end{axis}
242
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   377
\end{tikzpicture}
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   378
\end{center}
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   379
415
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 407
diff changeset
   380
\noindent And in the case of the regular expression $\texttt{(a*)*\,b}$
563
bddf14e026b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 562
diff changeset
   381
and strings of \texttt{a}s we will beat Java 8 by factor of
415
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 407
diff changeset
   382
approximately 1,000,000 (note the scale on the $x$-axis). 
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 407
diff changeset
   383
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 407
diff changeset
   384
\begin{center}
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 407
diff changeset
   385
\begin{tikzpicture}
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 407
diff changeset
   386
  \begin{axis}[
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 407
diff changeset
   387
    title={Graph: $\texttt{(a*)*\,b}$ and strings 
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 407
diff changeset
   388
           $\underbrace{\texttt{a}\ldots \texttt{a}}_{n}$},
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 407
diff changeset
   389
    xlabel={$n$},
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 407
diff changeset
   390
    x label style={at={(1.05,0.0)}},
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 407
diff changeset
   391
    ylabel={time in secs},
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 407
diff changeset
   392
    enlargelimits=false,
443
cd43d8c6eb84 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 418
diff changeset
   393
    ymax=32,
cd43d8c6eb84 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 418
diff changeset
   394
    ytick={0,5,...,30},
415
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 407
diff changeset
   395
    axis lines=left,
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 407
diff changeset
   396
    width=7cm,
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 407
diff changeset
   397
    height=4.5cm,
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 407
diff changeset
   398
    legend entries={Our Algorithm V2},
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 407
diff changeset
   399
    legend pos=outer north east]
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 407
diff changeset
   400
\addplot[black,mark=square*,mark options={fill=white}] table {re3a.data};
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 407
diff changeset
   401
\end{axis}
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 407
diff changeset
   402
\end{tikzpicture}
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 407
diff changeset
   403
\end{center}
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 407
diff changeset
   404
722
14914b57e207 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 716
diff changeset
   405
\noindent
14914b57e207 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 716
diff changeset
   406
You might have wondered above why I looked at the (now) old Java 8: the
14914b57e207 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 716
diff changeset
   407
reason is that Java 9 and later versions are a bit better, but we will
14914b57e207 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 716
diff changeset
   408
still beat them hands down with our regex matcher.
14914b57e207 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 716
diff changeset
   409
242
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   410
\subsection*{Basic Regular Expressions}
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   411
399
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 398
diff changeset
   412
The regular expressions shown earlier for Scala, we
925
ddb521b57e0c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 924
diff changeset
   413
will in this module call \emph{extended regular expressions}. The ones we
ddb521b57e0c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 924
diff changeset
   414
will mainly study are \emph{basic regular
243
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   415
expressions}, which by convention we will just call
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   416
\emph{regular expressions}, if it is clear what we mean. The
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   417
attraction of (basic) regular expressions is that many
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   418
features of the extended ones are just syntactic sugar.
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   419
(Basic) regular expressions are defined by the following
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   420
grammar:
242
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   421
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   422
\begin{center}
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   423
\begin{tabular}{r@{\hspace{1mm}}r@{\hspace{1mm}}l@{\hspace{13mm}}l}
399
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 398
diff changeset
   424
  $r$ & $::=$ &    $\ZERO$          & null language\\
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 398
diff changeset
   425
        & $\mid$ & $\ONE$           & empty string / \texttt{""} / []\\
242
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   426
        & $\mid$ & $c$                  & single character\\
243
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   427
        & $\mid$ & $r_1 + r_2$          & alternative / choice\\
242
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   428
        & $\mid$ & $r_1 \cdot r_2$      & sequence\\
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   429
        & $\mid$ & $r^*$                & star (zero or more)\\
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   430
  \end{tabular}
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   431
\end{center}
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   432
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   433
\noindent Because we overload our notation, there are some
243
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   434
subtleties you should be aware of. When regular expressions
404
245d302791c7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 403
diff changeset
   435
are referred to, then $\ZERO$ (in bold font) does not stand for
399
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 398
diff changeset
   436
the number zero: rather it is a particular pattern that does
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 398
diff changeset
   437
not match any string. Similarly, in the context of regular
925
ddb521b57e0c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 924
diff changeset
   438
expressions, $\ONE$ does not stand for the number one, but for
399
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 398
diff changeset
   439
a regular expression that matches the empty string. The letter
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 398
diff changeset
   440
$c$ stands for any character from the alphabet at hand. Again
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 398
diff changeset
   441
in the context of regular expressions, it is a particular
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 398
diff changeset
   442
pattern that can match the specified character. You should
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 398
diff changeset
   443
also be careful with our overloading of the star: assuming you
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 398
diff changeset
   444
have read the handout about our basic mathematical notation,
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 398
diff changeset
   445
you will see that in the context of languages (sets of
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 398
diff changeset
   446
strings) the star stands for an operation on languages. Here
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 398
diff changeset
   447
$r^*$ stands for a regular expression, which is different from
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 398
diff changeset
   448
the operation on sets is defined as
242
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   449
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   450
\[
399
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 398
diff changeset
   451
A\star\dn \bigcup_{0\le n} A^n
242
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   452
\]
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   453
334
fd89a63e9db3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 332
diff changeset
   454
242
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   455
We will use parentheses to disambiguate regular expressions.
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   456
Parentheses are not really part of a regular expression, and
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   457
indeed we do not need them in our code because there the tree
243
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   458
structure of regular expressions is always clear. But for
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   459
writing them down in a more mathematical fashion, parentheses
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   460
will be helpful. For example we will write $(r_1 + r_2)^*$,
742
b5b5583a3a08 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 723
diff changeset
   461
which is different from, say $r_1 + (r_2)^*$. This can be
b5b5583a3a08 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 723
diff changeset
   462
seen if we write regular expressions as trees:
b5b5583a3a08 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 723
diff changeset
   463
b5b5583a3a08 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 723
diff changeset
   464
\begin{center}
b5b5583a3a08 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 723
diff changeset
   465
\includegraphics[scale=0.65]{../pics/r1.pdf}
b5b5583a3a08 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 723
diff changeset
   466
\hspace{3cm}
b5b5583a3a08 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 723
diff changeset
   467
\includegraphics[scale=0.65]{../pics/r2.pdf}
b5b5583a3a08 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 723
diff changeset
   468
\end{center}
b5b5583a3a08 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 723
diff changeset
   469
b5b5583a3a08 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 723
diff changeset
   470
\noindent
b5b5583a3a08 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 723
diff changeset
   471
The regular expression on the left means
b5b5583a3a08 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 723
diff changeset
   472
roughly zero or more times $r_1$ or $r_2$, while the one on the right
550
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 507
diff changeset
   473
means $r_1$, or zero or more times $r_2$. This will turn out to
248
ce767ca23244 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 245
diff changeset
   474
be two different patterns, which match in general different
243
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   475
strings. We should also write $(r_1 + r_2) + r_3$, which is
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   476
different from the regular expression $r_1 + (r_2 + r_3)$, but
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   477
in case of $+$ and $\cdot$ we actually do not care about the
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   478
order and just write $r_1 + r_2 + r_3$, or $r_1 \cdot r_2
550
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 507
diff changeset
   479
\cdot r_3$, respectively. The reasons for this carelessness will become
399
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 398
diff changeset
   480
clear shortly. 
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 398
diff changeset
   481
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 398
diff changeset
   482
In the literature you will often find that the choice $r_1 +
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 398
diff changeset
   483
r_2$ is written as $r_1\mid{}r_2$ or $r_1\mid\mid{}r_2$. Also,
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 398
diff changeset
   484
often our $\ZERO$ and $\ONE$ are written $\varnothing$ and
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 398
diff changeset
   485
$\epsilon$, respectively. Following the convention in the
550
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 507
diff changeset
   486
literature, we will often omit the $\cdot$. This
243
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   487
is to make some concrete regular expressions more readable.
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   488
For example the regular expression for email addresses shown
550
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 507
diff changeset
   489
in \eqref{email} would fully expanded look like
242
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   490
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   491
\[
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   492
\texttt{[...]+} \;\cdot\;  \texttt{@} \;\cdot\; 
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   493
\texttt{[...]+} \;\cdot\; \texttt{.} \;\cdot\; 
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   494
\texttt{[...]\{2,6\}}
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   495
\]
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   496
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   497
\noindent
471
9476086849ad updated
Christian Urban <urbanc@in.tum.de>
parents: 452
diff changeset
   498
which is much less readable than the regular expression in
9476086849ad updated
Christian Urban <urbanc@in.tum.de>
parents: 452
diff changeset
   499
\eqref{email}. Similarly for the regular expression that matches the
9476086849ad updated
Christian Urban <urbanc@in.tum.de>
parents: 452
diff changeset
   500
string $hello$ we should write
242
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   501
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   502
\[
248
ce767ca23244 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 245
diff changeset
   503
h \cdot e \cdot l \cdot l \cdot o
242
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   504
\]
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   505
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   506
\noindent
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   507
but often just write {\it hello}.
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   508
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   509
If you prefer to think in terms of the implementation
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   510
of regular expressions in Scala, the constructors and
245
a5fade10c207 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 244
diff changeset
   511
classes relate as follows\footnote{More about Scala is 
830
dbf9d710ce65 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 745
diff changeset
   512
in the handout about \emph{A Crash-Course on Scala} from PEP.}
242
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   513
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   514
\begin{center}
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   515
\begin{tabular}{rcl}
399
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 398
diff changeset
   516
$\ZERO$       & $\mapsto$ & \texttt{ZERO}\\
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 398
diff changeset
   517
$\ONE$        & $\mapsto$ & \texttt{ONE}\\
242
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   518
$c$           & $\mapsto$ & \texttt{CHAR(c)}\\
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   519
$r_1 + r_2$   & $\mapsto$ & \texttt{ALT(r1, r2)}\\
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   520
$r_1 \cdot r_2$ & $\mapsto$ & \texttt{SEQ(r1, r2)}\\
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   521
$r^*$         & $\mapsto$ & \texttt{STAR(r)}
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   522
\end{tabular}
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   523
\end{center}
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   524
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   525
A source of confusion might arise from the fact that we
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   526
use the term \emph{basic regular expression} for the regular
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   527
expressions used in ``theory'' and defined above, and
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   528
\emph{extended regular expression} for the ones used in
243
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   529
``practice'', for example in Scala. If runtime is not an
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   530
issue, then the latter can be seen as syntactic sugar of
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   531
the former. For example we could replace
242
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   532
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   533
\begin{center}
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   534
\begin{tabular}{rcl}
471
9476086849ad updated
Christian Urban <urbanc@in.tum.de>
parents: 452
diff changeset
   535
$r^+$ & $\mapsto$ & $r\cdot r^*$\\
9476086849ad updated
Christian Urban <urbanc@in.tum.de>
parents: 452
diff changeset
   536
$r^?$ & $\mapsto$ & $\ONE + r$\\
242
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   537
$\backslash d$ & $\mapsto$ & $0 + 1 + 2 + \ldots + 9$\\
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   538
$[\text{\it a - z}]$ & $\mapsto$ & $a + b + \ldots + z$\\
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   539
\end{tabular}
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   540
\end{center}
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   541
243
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   542
242
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   543
\subsection*{The Meaning of Regular Expressions}
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   544
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   545
So far we have only considered informally what the
243
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   546
\emph{meaning} of a regular expression is. This is not good
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   547
enough for specifications of what algorithms are supposed to
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   548
do or which problems they are supposed to solve.
242
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   549
243
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   550
To define the meaning of a regular expression we will
872
5f5e165c9a57 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 871
diff changeset
   551
associate with every regular expression a language---a set of
243
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   552
strings. This language contains all the strings the regular
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   553
expression is supposed to match. To understand what is going
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   554
on here it is crucial that you have read the handout
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   555
about basic mathematical notations.
242
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   556
243
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   557
The \defn{meaning of a regular expression} can be defined
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   558
by a recursive function called $L$ (for language), which
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   559
is defined as follows
242
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   560
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   561
\begin{center}
399
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 398
diff changeset
   562
\begin{tabular}{rcll}
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 398
diff changeset
   563
$L(\ZERO)$         & $\dn$ & $\{\}$\\
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 398
diff changeset
   564
$L(\ONE)$          & $\dn$ & $\{[]\}$\\
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 398
diff changeset
   565
$L(c)$             & $\dn$ & $\{"c"\}$ & or equivalently $\dn \{[c]\}$\\
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 398
diff changeset
   566
$L(r_1+ r_2)$      & $\dn$ & $L(r_1) \cup L(r_2)$\\
243
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   567
$L(r_1 \cdot r_2)$ & $\dn$ & $L(r_1) \,@\, L(r_2)$\\
399
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 398
diff changeset
   568
$L(r^*)$           & $\dn$ & $(L(r))\star$\\
242
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   569
\end{tabular}
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   570
\end{center}
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   571
243
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   572
\noindent As a result we can now precisely state what the
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   573
meaning, for example, of the regular expression $h \cdot
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   574
e \cdot l \cdot l \cdot o$ is, namely
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   575
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   576
\[
248
ce767ca23244 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 245
diff changeset
   577
L(h \cdot e \cdot  l \cdot l \cdot o) = \{"hello"\}
243
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   578
\]
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   579
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   580
\noindent This is expected because this regular expression 
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   581
is only supposed to match the ``$hello$''-string. Similarly if
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   582
we have the choice-regular-expression $a + b$, its meaning is
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   583
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   584
\[
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   585
L(a + b) = \{"a", "b"\}
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   586
\]
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   587
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   588
\noindent You can now also see why we do not make a difference
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   589
between the different regular expressions $(r_1 + r_2) + r_3$
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   590
and $r_1 + (r_2 + r_3)$\ldots they are not the same regular
248
ce767ca23244 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 245
diff changeset
   591
expression, but they have the same meaning. For example
318
7975e4f0d4de updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 306
diff changeset
   592
you can do the following calculation which shows they
7975e4f0d4de updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 306
diff changeset
   593
have the same meaning:
243
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   594
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   595
\begin{eqnarray*}
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   596
L((r_1 + r_2) + r_3) & = & L(r_1 + r_2) \cup L(r_3)\\
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   597
                     & = & L(r_1) \cup L(r_2) \cup L(r_3)\\
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   598
                     & = & L(r_1) \cup L(r_2 + r_3)\\
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   599
                     & = & L(r_1 + (r_2 + r_3))
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   600
\end{eqnarray*}
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   601
830
dbf9d710ce65 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 745
diff changeset
   602
\noindent
dbf9d710ce65 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 745
diff changeset
   603
That means both languages are the same.
243
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   604
The point of the definition of $L$ is that we can use it to
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   605
precisely specify when a string $s$ is matched by a regular
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   606
expression $r$, namely if and only if $s \in L(r)$. In fact we
872
5f5e165c9a57 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 871
diff changeset
   607
will write a program \pcode{match} that takes a string $s$
5f5e165c9a57 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 871
diff changeset
   608
and a regular expression $r$ as arguments and returns
243
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   609
\emph{yes}, if $s \in L(r)$ and \emph{no}, if $s \not\in
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   610
L(r)$. We leave this for the next lecture.
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   611
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   612
There is one more feature of regular expressions that is worth
872
5f5e165c9a57 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 871
diff changeset
   613
mentioning here. Given some strings, there are in general many
243
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   614
different regular expressions that can recognise these
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   615
strings. This is obvious with the regular expression $a + b$
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   616
which can match the strings $a$ and $b$. But also the regular
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   617
expression $b + a$ would match the same strings. However,
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   618
sometimes it is not so obvious whether two regular expressions
399
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 398
diff changeset
   619
match the same strings: for example do $r^*$ and $\ONE + r
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 398
diff changeset
   620
\cdot r^*$ match the same strings? What about $\ZERO^*$
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 398
diff changeset
   621
and $\ONE^*$? This suggests the following relation between
243
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   622
\defn{equivalent regular expressions}: 
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   623
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   624
\[
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   625
r_1 \equiv r_2 \;\dn\; L(r_1) = L(r_2)
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   626
\]
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   627
248
ce767ca23244 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 245
diff changeset
   628
\noindent That means two regular expressions are said to be
550
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 507
diff changeset
   629
equivalent if they match the same set of strings. That is
872
5f5e165c9a57 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 871
diff changeset
   630
their meanings are the same. Therefore we
248
ce767ca23244 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 245
diff changeset
   631
do not really distinguish between the different regular
ce767ca23244 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 245
diff changeset
   632
expression $(r_1 + r_2) + r_3$ and $r_1 + (r_2 + r_3)$,
ce767ca23244 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 245
diff changeset
   633
because they are equivalent. I leave you to the question
ce767ca23244 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 245
diff changeset
   634
whether
243
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   635
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   636
\[
399
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 398
diff changeset
   637
\ZERO^* \equiv \ONE^*
243
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   638
\]
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   639
399
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 398
diff changeset
   640
\noindent holds or not? Such equivalences will be important
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 398
diff changeset
   641
for our matching algorithm, because we can use them to
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 398
diff changeset
   642
simplify regular expressions, which will mean we can speed
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 398
diff changeset
   643
up the calculations. 
243
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   644
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   645
\subsection*{My Fascination for Regular Expressions}
242
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   646
471
9476086849ad updated
Christian Urban <urbanc@in.tum.de>
parents: 452
diff changeset
   647
Up until a few years ago I was not really interested in regular
9476086849ad updated
Christian Urban <urbanc@in.tum.de>
parents: 452
diff changeset
   648
expressions. They have been studied for the last 60 years (by smarter
9476086849ad updated
Christian Urban <urbanc@in.tum.de>
parents: 452
diff changeset
   649
people than me)---surely nothing new can be found out about them. I
9476086849ad updated
Christian Urban <urbanc@in.tum.de>
parents: 452
diff changeset
   650
even have the vague recollection that I did not quite understand them
9476086849ad updated
Christian Urban <urbanc@in.tum.de>
parents: 452
diff changeset
   651
during my undergraduate study. If I remember correctly,\footnote{That
9476086849ad updated
Christian Urban <urbanc@in.tum.de>
parents: 452
diff changeset
   652
  was really a long time ago.} I got utterly confused about $\ONE$
9476086849ad updated
Christian Urban <urbanc@in.tum.de>
parents: 452
diff changeset
   653
(which my lecturer wrote as $\epsilon$) and the empty string (which he
9476086849ad updated
Christian Urban <urbanc@in.tum.de>
parents: 452
diff changeset
   654
also wrote as $\epsilon$).\footnote{Obviously the lecturer must have
550
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 507
diff changeset
   655
  been bad ;o)} Since then, I have used regular expressions for
471
9476086849ad updated
Christian Urban <urbanc@in.tum.de>
parents: 452
diff changeset
   656
implementing lexers and parsers as I have always been interested in
9476086849ad updated
Christian Urban <urbanc@in.tum.de>
parents: 452
diff changeset
   657
all kinds of programming languages and compilers, which invariably
9476086849ad updated
Christian Urban <urbanc@in.tum.de>
parents: 452
diff changeset
   658
need regular expressions in some form or shape.
243
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   659
399
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 398
diff changeset
   660
To understand my fascination \emph{nowadays} with regular
243
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   661
expressions, you need to know that my main scientific interest
471
9476086849ad updated
Christian Urban <urbanc@in.tum.de>
parents: 452
diff changeset
   662
for the last 17 years has been with theorem provers. I am a
243
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   663
core developer of one of
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   664
them.\footnote{\url{http://isabelle.in.tum.de}} Theorem
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   665
provers are systems in which you can formally reason about
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   666
mathematical concepts, but also about programs. In this way
550
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 507
diff changeset
   667
theorem provers can help with the menacing problem of writing bug-free code. Theorem provers have
416
357c395ae838 updated
Christian Urban <urbanc@in.tum.de>
parents: 415
diff changeset
   668
proved already their value in a number of cases (even in
243
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   669
terms of hard cash), but they are still clunky and difficult
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   670
to use by average programmers.
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   671
471
9476086849ad updated
Christian Urban <urbanc@in.tum.de>
parents: 452
diff changeset
   672
Anyway, in about 2011 I came across the notion of \defn{derivatives of
9476086849ad updated
Christian Urban <urbanc@in.tum.de>
parents: 452
diff changeset
   673
  regular expressions}. This notion allows one to do almost all
9476086849ad updated
Christian Urban <urbanc@in.tum.de>
parents: 452
diff changeset
   674
calculations with regular expressions on the level of regular
9476086849ad updated
Christian Urban <urbanc@in.tum.de>
parents: 452
diff changeset
   675
expressions, not needing any automata (you will see we only touch
9476086849ad updated
Christian Urban <urbanc@in.tum.de>
parents: 452
diff changeset
   676
briefly on automata in lecture 3). Automata are usually the main
9476086849ad updated
Christian Urban <urbanc@in.tum.de>
parents: 452
diff changeset
   677
object of study in formal language courses.  The avoidance of automata
924
6ad0f63e1968 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   678
is crucial for me because automata are graphs and it is rather
6ad0f63e1968 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   679
difficult to reason about graphs in theorem provers. In contrast,
6ad0f63e1968 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   680
reasoning about regular expressions is easy-peasy in theorem
6ad0f63e1968 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   681
provers. Is this important? I think yes, because according to
6ad0f63e1968 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   682
Kuklewicz nearly all POSIX-based regular expression matchers are
243
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   683
buggy.\footnote{\url{http://www.haskell.org/haskellwiki/Regex_Posix}}
924
6ad0f63e1968 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   684
With my PhD students Fahad Ausaf and Chengsong Tan, I proved the
6ad0f63e1968 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   685
correctness for two such matchers that were proposed by Sulzmann and Lu
6ad0f63e1968 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   686
in 2014.\footnote{\url{http://goo.gl/bz0eHp}} A variant of which you have
6ad0f63e1968 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   687
already seen in PEP as CW3 and you will see again in the CFL in the first
6ad0f63e1968 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   688
two CWs. What we have not yet figured out that our matchers are
6ad0f63e1968 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   689
universally fast, meaning they do not explode on any input.
6ad0f63e1968 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   690
Hopefully we can also prove
6ad0f63e1968 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   691
that the machine code(!)  that implements our matchers efficiently is
6ad0f63e1968 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   692
correct also. Writing programs in this way does not leave any room for
6ad0f63e1968 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   693
any errors or bugs. How nice!
243
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   694
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   695
What also helped with my fascination with regular expressions
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   696
is that we could indeed find out new things about them that
416
357c395ae838 updated
Christian Urban <urbanc@in.tum.de>
parents: 415
diff changeset
   697
have surprised some experts. Together with two colleagues from China, I was
243
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   698
able to prove the Myhill-Nerode theorem by only using regular
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   699
expressions and the notion of derivatives. Earlier versions of
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   700
this theorem used always automata in the proof. Using this
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   701
theorem we can show that regular languages are closed under
830
dbf9d710ce65 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 745
diff changeset
   702
complementation, something which Bill Gasarch in his Computational Complexity
243
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   703
blog\footnote{\url{http://goo.gl/2R11Fw}} assumed can only be
550
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 507
diff changeset
   704
shown via automata. So even somebody who has written a 700+-page
243
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   705
book\footnote{\url{http://goo.gl/fD0eHx}} on regular
550
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 507
diff changeset
   706
expressions did not know better. Well, we showed it can also be
924
6ad0f63e1968 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   707
done with regular expressions only.\footnote{\url{https://nms.kcl.ac.uk/christian.urban/Publications/rexp.pdf}}
471
9476086849ad updated
Christian Urban <urbanc@in.tum.de>
parents: 452
diff changeset
   708
What a feeling when you are an outsider to the subject!
243
8d5aaf5b0031 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
   709
471
9476086849ad updated
Christian Urban <urbanc@in.tum.de>
parents: 452
diff changeset
   710
To conclude: Despite my early ignorance about regular expressions, I
550
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 507
diff changeset
   711
find them now extremely interesting. They have practical importance
471
9476086849ad updated
Christian Urban <urbanc@in.tum.de>
parents: 452
diff changeset
   712
(remember the shocking runtime of the regular expression matchers in
924
6ad0f63e1968 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   713
Python, Ruby, Swift and Java in some instances and the problems in Stack
6ad0f63e1968 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   714
Exchange and the Atom editor---even regex libraries in more modern programming languages, like Rust, have their problems). They are used in tools like Snort and
872
5f5e165c9a57 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 871
diff changeset
   715
Zeek in order to monitor network traffic. They have a beautiful mathematical
550
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 507
diff changeset
   716
theory behind them, which can be sometimes quite deep and which
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 507
diff changeset
   717
sometimes contains hidden snares.  People who are not very familiar
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 507
diff changeset
   718
with the mathematical background of regular expressions get them
492
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
   719
consistently wrong (this is surprising given they are a supposed to be
872
5f5e165c9a57 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 871
diff changeset
   720
a core skill for computer scientists). The hope is that we can do better
492
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
   721
in the future---for example by proving that the algorithms actually
471
9476086849ad updated
Christian Urban <urbanc@in.tum.de>
parents: 452
diff changeset
   722
satisfy their specification and that the corresponding implementations
9476086849ad updated
Christian Urban <urbanc@in.tum.de>
parents: 452
diff changeset
   723
do not contain any bugs. We are close, but not yet quite there.
242
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   724
332
4755ad4b457b updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 327
diff changeset
   725
Notwithstanding my fascination, I am also happy to admit that regular
244
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 243
diff changeset
   726
expressions have their shortcomings. There are some well-known
471
9476086849ad updated
Christian Urban <urbanc@in.tum.de>
parents: 452
diff changeset
   727
``theoretical'' shortcomings, for example recognising strings of the
9476086849ad updated
Christian Urban <urbanc@in.tum.de>
parents: 452
diff changeset
   728
form $a^{n}b^{n}$ is not possible with regular expressions. This means
550
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 507
diff changeset
   729
for example if we try to recognise whether parentheses are well-nested
492
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
   730
in an expression is impossible with (basic) regular expressions.  I am
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
   731
not so bothered by these shortcomings. What I am bothered about is
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
   732
when regular expressions are in the way of practical programming. For
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
   733
example, it turns out that the regular expression for email addresses
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
   734
shown in \eqref{email} is hopelessly inadequate for recognising all of
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
   735
them (despite being touted as something every computer scientist
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
   736
should know about). The W3 Consortium (which standardises the Web)
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
   737
proposes to use the following, already more complicated regular
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
   738
expressions for email addresses:
244
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 243
diff changeset
   739
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 243
diff changeset
   740
{\small\begin{lstlisting}[language={},keywordstyle=\color{black},numbers=none]
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 243
diff changeset
   741
[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 243
diff changeset
   742
\end{lstlisting}}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 243
diff changeset
   743
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 243
diff changeset
   744
\noindent But they admit that by using this regular expression
471
9476086849ad updated
Christian Urban <urbanc@in.tum.de>
parents: 452
diff changeset
   745
they wilfully violate the RFC 5322 standard, which specifies
244
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 243
diff changeset
   746
the syntax of email addresses. With their proposed regular
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 243
diff changeset
   747
expression they are too strict in some cases and too lax in
872
5f5e165c9a57 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 871
diff changeset
   748
others\ldots{}not a good situation to be in. A regular expression
244
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 243
diff changeset
   749
that is claimed to be closer to the standard is shown in
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 243
diff changeset
   750
Figure~\ref{monster}. Whether this claim is true or not, I
416
357c395ae838 updated
Christian Urban <urbanc@in.tum.de>
parents: 415
diff changeset
   751
would not know---the only thing I can say about this regular
248
ce767ca23244 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 245
diff changeset
   752
expression is it is a monstrosity. However, this might
ce767ca23244 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 245
diff changeset
   753
actually be an argument against the RFC standard, rather than
399
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 398
diff changeset
   754
against regular expressions. A similar argument is made in
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 398
diff changeset
   755
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 398
diff changeset
   756
\begin{center}
570
Christian Urban <urbanc@in.tum.de>
parents: 563
diff changeset
   757
\url{http://elliot.land/post/its-impossible-to-validate-an-email-address}
399
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 398
diff changeset
   758
\end{center}
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 398
diff changeset
   759
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 398
diff changeset
   760
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 398
diff changeset
   761
\noindent which explains some of the crazier parts of email
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 398
diff changeset
   762
addresses. Still it is good to know that some tasks in text
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 398
diff changeset
   763
processing just cannot be achieved by using regular
471
9476086849ad updated
Christian Urban <urbanc@in.tum.de>
parents: 452
diff changeset
   764
expressions. But for what we want to use them (lexing) they are
473
dc528091eb70 updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   765
pretty good.\medskip
dc528091eb70 updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   766
dc528091eb70 updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   767
\noindent
dc528091eb70 updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   768
Finally there is a joke about regular expressions:
dc528091eb70 updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   769
dc528091eb70 updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   770
\begin{quote}\it
dc528091eb70 updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   771
  ``Sometimes you have a programming problem and it seems like the
dc528091eb70 updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   772
  best solution is to use regular expressions; now you have two
dc528091eb70 updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   773
  problems.''
dc528091eb70 updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   774
\end{quote}  
244
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 243
diff changeset
   775
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 243
diff changeset
   776
924
6ad0f63e1968 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   777
%\begin{figure}[p]\small
6ad0f63e1968 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   778
%  \lstinputlisting[numbers=left,linebackgroundcolor={\ifodd\value{lstnumber}\color{capri!3}\fi}]
6ad0f63e1968 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   779
%                  {../progs/crawler1.scala}
6ad0f63e1968 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   780
%
6ad0f63e1968 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   781
%\caption{The Scala code for a simple web-crawler that checks
6ad0f63e1968 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   782
%for broken links in web-pages. It uses the regular expression
6ad0f63e1968 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   783
%\texttt{http\_pattern} in Line~\ref{httpline} for recognising
6ad0f63e1968 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   784
%URL-addresses. It finds all links using the library function
6ad0f63e1968 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   785
%\texttt{findAllIn} in Line~\ref{findallline} (this function 
6ad0f63e1968 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   786
%is part of Scala's regular expression library).\label{crawler1}}
6ad0f63e1968 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   787
%
6ad0f63e1968 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   788
%\end{figure}
242
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   789
722
14914b57e207 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 716
diff changeset
   790
 
242
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   791
722
14914b57e207 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 716
diff changeset
   792
%\begin{figure}[p]\small
14914b57e207 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 716
diff changeset
   793
%  \lstinputlisting[numbers=left,linebackgroundcolor={\ifodd\value{lstnumber}\color{capri!3}\fi}]
14914b57e207 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 716
diff changeset
   794
%                  {../progs/crawler2.scala}
14914b57e207 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 716
diff changeset
   795
%
14914b57e207 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 716
diff changeset
   796
%\caption{A version of the web-crawler that only follows links
14914b57e207 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 716
diff changeset
   797
%in ``my'' domain---since these are the ones I am interested in
14914b57e207 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 716
diff changeset
   798
%to fix. It uses the regular expression \texttt{my\_urls} in
14914b57e207 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 716
diff changeset
   799
%Line~\ref{myurlline} to check for my name in the links. The
14914b57e207 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 716
diff changeset
   800
%main change is in
14914b57e207 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 716
diff changeset
   801
%Lines~\ref{changestartline}--\ref{changeendline} where there
14914b57e207 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 716
diff changeset
   802
%is a test whether URL is in ``my'' domain or
14914b57e207 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 716
diff changeset
   803
%not.\label{crawler2}}
14914b57e207 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 716
diff changeset
   804
%\end{figure}
477
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 473
diff changeset
   805
924
6ad0f63e1968 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   806
%\begin{figure}[p]\small
6ad0f63e1968 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   807
%  \lstinputlisting[numbers=left,linebackgroundcolor={\ifodd\value{lstnumber}\co%lor{capri!3}\fi}]
6ad0f63e1968 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   808
%                  {../progs/crawler2.scala}
6ad0f63e1968 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   809
%
6ad0f63e1968 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   810
%\caption{A small email harvester---whenever we download a
6ad0f63e1968 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   811
%web-page, we also check whether it contains any email
6ad0f63e1968 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   812
%addresses. For this we use the regular expression
6ad0f63e1968 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   813
%\texttt{email\_pattern} in Line~\ref{emailline}. The main
6ad0f63e1968 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   814
%change is in Line~\ref{mainline} where all email addresses
6ad0f63e1968 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   815
%that can be found in a page are printed.\label{crawler3}}
6ad0f63e1968 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   816
%
6ad0f63e1968 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   817
%\end{figure}
242
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 237
diff changeset
   818
244
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 243
diff changeset
   819
\begin{figure}[p]
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 243
diff changeset
   820
\tiny
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 243
diff changeset
   821
\begin{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 243
diff changeset
   822
\begin{minipage}{0.8\textwidth}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 243
diff changeset
   823
\lstinputlisting[language={},keywordstyle=\color{black},numbers=none]{../progs/email-rexp}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 243
diff changeset
   824
\end{minipage}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 243
diff changeset
   825
\end{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 243
diff changeset
   826
404
245d302791c7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 403
diff changeset
   827
\caption{Nothing that can be said about this regular
416
357c395ae838 updated
Christian Urban <urbanc@in.tum.de>
parents: 415
diff changeset
   828
expression\ldots{}except it is a monstrosity.\label{monster}}
244
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 243
diff changeset
   829
\end{figure}
108
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   830
112
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 111
diff changeset
   831
105
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   832
\end{document}
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   833
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   834
%%% Local Variables: 
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   835
%%% mode: latex
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   836
%%% TeX-master: t
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   837
%%% End: