handouts/ho05.tex
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Sun, 27 Oct 2013 00:08:57 +0100
changeset 161 bfcf838dda4d
parent 160 8134c3b981e0
child 162 edcd84c7b491
permissions -rw-r--r--
added
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
151
df229ec49b22 added ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     1
\documentclass{article}
df229ec49b22 added ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     2
\usepackage{charter}
df229ec49b22 added ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     3
\usepackage{hyperref}
df229ec49b22 added ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     4
\usepackage{amssymb}
df229ec49b22 added ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     5
\usepackage{amsmath}
df229ec49b22 added ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     6
\usepackage[T1]{fontenc}
df229ec49b22 added ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     7
\usepackage{listings}
df229ec49b22 added ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     8
\usepackage{xcolor}
df229ec49b22 added ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     9
\usepackage{tikz}
df229ec49b22 added ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    10
\usetikzlibrary{arrows}
df229ec49b22 added ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    11
\usetikzlibrary{automata}
df229ec49b22 added ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    12
\usetikzlibrary{shapes}
df229ec49b22 added ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    13
\usetikzlibrary{shadows}
df229ec49b22 added ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    14
\usetikzlibrary{positioning}
df229ec49b22 added ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    15
\usetikzlibrary{calc}
df229ec49b22 added ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    16
\usetikzlibrary{fit}
df229ec49b22 added ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    17
\usetikzlibrary{backgrounds}
153
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
    18
\usepackage{fontspec}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
    19
\setmonofont{Consolas}
151
df229ec49b22 added ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    20
df229ec49b22 added ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    21
\newcommand{\dn}{\stackrel{\mbox{\scriptsize def}}{=}}%
df229ec49b22 added ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    22
df229ec49b22 added ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    23
\definecolor{javared}{rgb}{0.6,0,0} % for strings
df229ec49b22 added ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    24
\definecolor{javagreen}{rgb}{0.25,0.5,0.35} % comments
df229ec49b22 added ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    25
\definecolor{javapurple}{rgb}{0.5,0,0.35} % keywords
df229ec49b22 added ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    26
\definecolor{javadocblue}{rgb}{0.25,0.35,0.75} % javadoc
df229ec49b22 added ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    27
df229ec49b22 added ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    28
\lstdefinelanguage{scala}{
df229ec49b22 added ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    29
  morekeywords={abstract,case,catch,class,def,%
df229ec49b22 added ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    30
    do,else,extends,false,final,finally,%
df229ec49b22 added ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    31
    for,if,implicit,import,match,mixin,%
df229ec49b22 added ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    32
    new,null,object,override,package,%
df229ec49b22 added ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    33
    private,protected,requires,return,sealed,%
df229ec49b22 added ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    34
    super,this,throw,trait,true,try,%
df229ec49b22 added ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    35
    type,val,var,while,with,yield},
df229ec49b22 added ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    36
  otherkeywords={=>,<-,<\%,<:,>:,\#,@},
df229ec49b22 added ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    37
  sensitive=true,
df229ec49b22 added ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    38
  morecomment=[l]{//},
df229ec49b22 added ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    39
  morecomment=[n]{/*}{*/},
df229ec49b22 added ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    40
  morestring=[b]",
df229ec49b22 added ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    41
  morestring=[b]',
df229ec49b22 added ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    42
  morestring=[b]"""
df229ec49b22 added ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    43
}
df229ec49b22 added ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    44
153
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
    45
\lstdefinelanguage{while}{
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
    46
  morekeywords={while, if, then. else, read, write},
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
    47
  otherkeywords={=>,<-,<\%,<:,>:,\#,@},
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
    48
  sensitive=true,
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
    49
  morecomment=[l]{//},
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
    50
  morecomment=[n]{/*}{*/},
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
    51
  morestring=[b]",
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
    52
  morestring=[b]',
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
    53
  morestring=[b]"""
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
    54
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
    55
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
    56
151
df229ec49b22 added ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    57
\lstset{language=Scala,
df229ec49b22 added ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    58
	basicstyle=\ttfamily,
df229ec49b22 added ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    59
	keywordstyle=\color{javapurple}\bfseries,
df229ec49b22 added ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    60
	stringstyle=\color{javagreen},
df229ec49b22 added ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    61
	commentstyle=\color{javagreen},
df229ec49b22 added ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    62
	morecomment=[s][\color{javadocblue}]{/**}{*/},
df229ec49b22 added ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    63
	numbers=left,
df229ec49b22 added ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    64
	numberstyle=\tiny\color{black},
df229ec49b22 added ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    65
	stepnumber=1,
df229ec49b22 added ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    66
	numbersep=10pt,
df229ec49b22 added ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    67
	tabsize=2,
df229ec49b22 added ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    68
	showspaces=false,
df229ec49b22 added ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    69
	showstringspaces=false}
df229ec49b22 added ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    70
	
153
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
    71
\newcommand\grid[1]{%
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
    72
\begin{tikzpicture}[baseline=(char.base)]
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
    73
  \path[use as bounding box]
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
    74
    (0,0) rectangle (1em,1em);
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
    75
  \draw[red!50, fill=red!20]
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
    76
    (0,0) rectangle (1em,1em);
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
    77
  \node[inner sep=1pt,anchor=base west]
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
    78
    (char) at (0em,\gridraiseamount) {#1};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
    79
\end{tikzpicture}}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
    80
\newcommand\gridraiseamount{0.12em}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
    81
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
    82
\makeatletter
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
    83
\newcommand\Grid[1]{%
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
    84
  \@tfor\z:=#1\do{\grid{\z}}}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
    85
\makeatother	
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
    86
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
    87
\newcommand\Vspace[1][.3em]{%
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
    88
  \mbox{\kern.06em\vrule height.3ex}%
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
    89
  \vbox{\hrule width#1}%
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
    90
  \hbox{\vrule height.3ex}}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
    91
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
    92
\def\VS{\Vspace[0.6em]}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
    93
	
151
df229ec49b22 added ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    94
\begin{document}
df229ec49b22 added ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    95
df229ec49b22 added ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    96
\section*{Handout 5}
df229ec49b22 added ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    97
155
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 154
diff changeset
    98
Whenever you want to design a new programming language or implement a compiler for an
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 154
diff changeset
    99
existing language, the first task is to fix the basic ``words'' of the language. For example what are the 
159
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   100
keywords, or reserved words, of the language, what are permitted identifiers, numbers, expressions and so 
153
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
   101
on. One convenient way to do this is, of 
155
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 154
diff changeset
   102
course, by using regular expressions. 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 154
diff changeset
   103
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 154
diff changeset
   104
In this course we want to take a closer look at the 
159
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   105
WHILE programming language. This is a simple imperative programming language consisting of arithmetic
155
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 154
diff changeset
   106
expressions, assignments, if-statements and loops. For example the Fibonacci program can be
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 154
diff changeset
   107
written in this language as follows:
151
df229ec49b22 added ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   108
153
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
   109
\begin{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
   110
\mbox{\lstinputlisting[language=while]{../progs/fib.while}}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
   111
\end{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
   112
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
   113
\noindent
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
   114
The keywords in this language will be
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
   115
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
   116
\begin{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
   117
\texttt{while}, \texttt{if}, \texttt{then}, \texttt{else}, \texttt{write}, \texttt{read}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
   118
\end{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
   119
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
   120
\noindent
155
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 154
diff changeset
   121
In addition we will have some common operators, such as \texttt{<}, \texttt{>}, \texttt{:=} and so on, as well as numbers
159
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   122
and strings (which we however ignore for the moment). We also need to specify what the ``whitespace''
155
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 154
diff changeset
   123
is in our programming language and what comments should look like.
159
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   124
As a first try, we might specify the regular expressions for our language roughly as follows
153
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
   125
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
   126
\begin{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
   127
\begin{tabular}{lcl}
159
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   128
$\textit{LETTER}$ & $:=$ & $\texttt{a} + \texttt{A} + \texttt{b} + \texttt{B} + \ldots$\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   129
$\textit{DIGIT}$ & $:=$ & $\texttt{0} + \texttt{1} + \texttt{2} + \ldots$\\
153
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
   130
$\textit{KEYWORD}$ & $:=$ & $\texttt{while}  + \texttt{if} + \texttt{then} + \texttt{else} + \dots$\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
   131
$\textit{IDENT}$ & $:=$ & $\textit{LETTER} \cdot (\textit{LETTER} + \textit{DIGIT} + {\_})^*$\\ 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
   132
$\textit{OP}$      &  $:=$ & $\texttt{:=} + \texttt{<} + \ldots$\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
   133
$\textit{NUM}$   &  $:=$ & $\textit{DIGIT}^+$\\
159
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   134
$\textit{WHITESPACE}$ & $:=$ & $("\hspace{2mm}" + \backslash\texttt{n})^+$
153
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
   135
\end{tabular}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
   136
\end{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
   137
155
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 154
diff changeset
   138
Having the regular expressions in place, the problem we have to solve is: 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 154
diff changeset
   139
given a string of our programming language, which regular expression 
159
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   140
matches which part of the string. By solving this problem, we can split up a string 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   141
of our language into components. 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   142
For example given the input string
153
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
   143
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
   144
\begin{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
   145
\texttt{\Grid{if\VS true\VS then\VS x+2\VS else\VS x+3}}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
   146
\end{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
   147
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
   148
\noindent
161
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   149
we expect it is split up as follows
153
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
   150
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
   151
\begin{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
   152
\tt
155
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 154
diff changeset
   153
\Grid{if}\,
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 154
diff changeset
   154
\Grid{\VS}\, 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 154
diff changeset
   155
\Grid{true}\, 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 154
diff changeset
   156
\Grid{\VS}\, 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 154
diff changeset
   157
\Grid{then}\, 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 154
diff changeset
   158
\Grid{\VS}\, 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 154
diff changeset
   159
\Grid{x}\, 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 154
diff changeset
   160
\Grid{+}\, 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 154
diff changeset
   161
\Grid{2}\,
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 154
diff changeset
   162
\Grid{\VS}\, 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 154
diff changeset
   163
\Grid{else}\,
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 154
diff changeset
   164
\Grid{\VS}\,
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 154
diff changeset
   165
\Grid{x}\,
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 154
diff changeset
   166
\Grid{+}\,
153
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
   167
\Grid{3}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
   168
\end{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
   169
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
   170
\noindent
161
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   171
This process of splitting up an input string into components is often called \emph{lexing} or \emph{scanning}.
153
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
   172
It is usually the first phase of a compiler. Note that the separation into words cannot, in general, 
161
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   173
be done by just looking at whitespaces: while \texttt{if} and \texttt{true} are separated by a whitespace
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   174
in the example above, this is not always the case. As can be seen the three components in \texttt{x+2} are 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   175
not separated by any whitespace. Another reason for recognising whitespaces explicitly is
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   176
that in some languages, for example Python, whitespaces matters, that is carry meaning. However in 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   177
our small language we will eventually just filter out all whitespaces and also all comments.
153
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
   178
161
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   179
Lexing not just separates a string into its components, but also classify the components, meaning explicitly record that \texttt{if} is a keyword,  \VS{} a whitespace, \texttt{true} an identifier and so on.
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   180
For the moment, though, we will only focus on the simpler problem of just splitting up 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   181
a string into components.
155
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 154
diff changeset
   182
159
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   183
There are a few subtleties  we need to consider first. For example, say the string is
154
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 153
diff changeset
   184
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 153
diff changeset
   185
\begin{center}
161
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   186
\texttt{\Grid{iffoo\VS}}\;\ldots
154
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 153
diff changeset
   187
\end{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 153
diff changeset
   188
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 153
diff changeset
   189
\noindent
159
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   190
then there are two possibilities for how it can be split up: either we regard the input as the keyword \texttt{if} followed
154
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 153
diff changeset
   191
by the identifier \texttt{foo} (both regular expressions match) or we regard \texttt{iffoo} as a 
155
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 154
diff changeset
   192
single identifier. The choice that is often made in lexers is to look for the longest possible match.
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 154
diff changeset
   193
This leaves  \texttt{iffoo} as the only match (since it is longer than \texttt{if}).
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 154
diff changeset
   194
161
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   195
Unfortunately, the convention about the longest match does not yet make the process 
159
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   196
of lexing completely deterministic. Consider the string
155
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 154
diff changeset
   197
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 154
diff changeset
   198
\begin{center}
161
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   199
\texttt{\Grid{then\VS}}\;\dots
155
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 154
diff changeset
   200
\end{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 154
diff changeset
   201
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 154
diff changeset
   202
\noindent
159
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   203
Clearly, this string should be identified as a keyword. The problem is that also the regular expression \textit{IDENT} for identifiers matches this string. To overcome this ambiguity we need to rank our 
156
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 155
diff changeset
   204
regular expressions. In our running example we just use the ranking
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 155
diff changeset
   205
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 155
diff changeset
   206
\[
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 155
diff changeset
   207
\textit{KEYWORD} < \textit{IDENT} < \textit{OP} < \ldots
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 155
diff changeset
   208
\]
155
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 154
diff changeset
   209
156
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 155
diff changeset
   210
\noindent
160
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 159
diff changeset
   211
So even if both regular expressions match in the example above,
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 159
diff changeset
   212
we give preference to the regular expression for keywords.
155
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 154
diff changeset
   213
161
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   214
Let us see how our algorithm for lexing works in detail. In addition to the functions $nullable$
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   215
and $der$, it will  use the function \emph{zeroable} defined as follows:
157
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   216
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   217
\begin{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   218
\begin{tabular}{@ {}l@ {\hspace{2mm}}c@ {\hspace{2mm}}l@ {}}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   219
$zeroable(\varnothing)$      & $\dn$ & $true$\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   220
$zeroable(\epsilon)$           & $\dn$ &  $f\!alse$\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   221
$zeroable (c)$                    & $\dn$ &  $f\!alse$\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   222
$zeroable (r_1 + r_2)$        & $\dn$ &  $zeroable(r_1) \wedge zeroable(r_2)$ \\ 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   223
$zeroable (r_1 \cdot r_2)$  & $\dn$ &  $zeroable(r_1) \vee zeroable(r_2)$ \\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   224
$zeroable (r^*)$                  & $\dn$ & $f\!alse$\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   225
\end{tabular}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   226
\end{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   227
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   228
\noindent
161
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   229
Recall that the function $nullable(r)$ tests whether a regular expression
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   230
can match the empty string. The function $zeroable$, on the other hand, tests whether a regular
158
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   231
expression cannot match anything at all. The mathematical way of stating this
160
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 159
diff changeset
   232
property is
157
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   233
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   234
\begin{center}
160
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 159
diff changeset
   235
$zeroable(r)$ if and only if $L(r) = \varnothing$
157
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   236
\end{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   237
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   238
\noindent
161
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   239
For what follows let us fix a set of regular expressions $rs$ as being 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   240
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   241
\begin{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   242
\textit{KEYWORD}, \textit{IDENT}, \textit{WHITESPACE}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   243
\end{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   244
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   245
\noindent
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   246
specifying the ``words'' 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   247
of our programming language. The algorithm takes as input the $rs$ and a string, say
158
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   248
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   249
\begin{center}
161
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   250
\texttt{\grid{$c_1$}\grid{$c_2$}\grid{$c_3$}\grid{$c_4$}}\;\ldots
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   251
\end{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   252
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   253
\noindent
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   254
and tries to chop off one word from the beginning of the string. If none of the
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   255
regular expression in $rs$ matches, we will just return
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   256
the empty string.
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   257
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   258
The crucial idea in the algorithm is to build the derivatives of all regular expressions in $rs$ with respect
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   259
to the first character $c_1$. Then we take the results and continue with 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   260
building the derivatives with respect to $c_2$ until we have either exhausted our 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   261
input string or all of the regular expressions are ``zeroable''.  If the input string is 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   262
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   263
\begin{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   264
\texttt{\Grid{if2\VS}}\;\dots
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   265
\end{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   266
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   267
\noindent
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   268
then building the derivatives with respect to \texttt{i} gives
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   269
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   270
\begin{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   271
\begin{tabular}{l|c}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   272
 & $zeroable$\\\hline
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   273
 $der\;\texttt{i}\;(\textit{KEYWORD})$      & no\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   274
 $der\;\texttt{i}\;(\textit{IDENT})$              & no\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   275
 $der\;\texttt{i}\;(\textit{WHITESPACE})$ & yes\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   276
\end{tabular}
158
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   277
\end{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   278
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   279
\noindent
161
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   280
We can eliminate then \textit{WHITESPACE} as a potential candidate, because
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   281
no derivative can go from $zeroable = \text{yes}$ to no. That leaves the other
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   282
two regular expressions as potential candidate and we have to consider the
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   283
next character from the input string
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   284
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   285
\begin{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   286
\begin{tabular}{l|c}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   287
 & $zeroable$\\\hline
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   288
 $der\;\texttt{f}\;(der\;\texttt{i}\;(\textit{KEYWORD}))$      & no\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   289
 $der\;\texttt{f}\;(der\;\texttt{i}\;(\textit{IDENT}))$              & no\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   290
\end{tabular}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   291
\end{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   292
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   293
\noindent
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   294
Since both are `no', we have to go on with \texttt{2} from the input string
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   295
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   296
\begin{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   297
\begin{tabular}{l|c}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   298
 & $zeroable$\\\hline
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   299
 $der\;\texttt{2}\;(der\;\texttt{f}\;(der\;\texttt{i}\;(\textit{KEYWORD})))$      & yes\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   300
 $der\;\texttt{2}\;(der\;\texttt{f}\;(der\;\texttt{i}\;(\textit{IDENT})))$              & no\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   301
\end{tabular}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   302
\end{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   303
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   304
\noindent
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   305
Although we now know that the beginning is definitely an \textit{IDENT}, we do not yet
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   306
know how much of the input string should be considered as an \textit{IDENT}. So we
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   307
also have to go on and consider the next derivative.
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   308
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   309
\begin{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   310
\begin{tabular}{l|c}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   311
 & $zeroable$\\\hline
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   312
 $der\;\texttt{\VS}\;(der\;\texttt{2}\;(der\;\texttt{f}\;(der\;\texttt{i}\;(\textit{IDENT}))))$              & yes\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   313
\end{tabular}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   314
\end{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   315
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   316
\noindent
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   317
Since the answer is now `yes' we can stop: once all derivatives are
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   318
zeroable, we know the regular expressions cannot match any more letters from
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   319
the input. In this case we only have to go back to the derivative that is nullable.  In this case it
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   320
is
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   321
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   322
\begin{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   323
$der\;\texttt{2}\;(der\;\texttt{f}\;(der\;\texttt{i}\;(\textit{IDENT})))$ 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   324
\end{center} 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   325
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   326
\noindent
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   327
which means we recognised an identifier. In case there is a choice of more than one
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   328
regular expressions that are nullable, then we choose the one with the highest precedence. 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   329
You can try out this case
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   330
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 160
diff changeset
   331
157
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   332
151
df229ec49b22 added ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   333
\end{document}
df229ec49b22 added ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   334
df229ec49b22 added ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   335
%%% Local Variables: 
df229ec49b22 added ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   336
%%% mode: latex
df229ec49b22 added ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   337
%%% TeX-master: t
df229ec49b22 added ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   338
%%% End: