handouts/ho05.tex
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Fri, 25 Oct 2013 17:55:35 +0100
changeset 154 51d6b8b828c4
parent 153 70ab41cb610e
child 155 9b2d128765e1
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
152
90e27fafc5c7 added ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 151
diff changeset
    98
Whenever you want to design a programming language or implement a compiler for an
153
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
    99
existing language, the first task is to fix the basic ``words'' of the language, like what are the 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
   100
keywords or reserved words of the language, what are permitted identifiers, numbers and so 
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 
152
90e27fafc5c7 added ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 151
diff changeset
   102
course, to use regular expressions. In this course we want to take a closer look at the 
90e27fafc5c7 added ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 151
diff changeset
   103
WHILE-language. This is a simple imperative language consisting of arithmetic
90e27fafc5c7 added ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 151
diff changeset
   104
expressions, assignments and loops only. For example the Fibonacci program can be
90e27fafc5c7 added ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 151
diff changeset
   105
written in this language as follows
151
df229ec49b22 added ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   106
153
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
   107
\begin{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
   108
\mbox{\lstinputlisting[language=while]{../progs/fib.while}}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
   109
\end{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
   110
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
   111
\noindent
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
   112
The keywords in this language will be
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
   113
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
   114
\begin{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
   115
\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
   116
\end{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
   117
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
   118
\noindent
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
   119
In addition we will have some typical operators, like \texttt{<}, \texttt{>}, \texttt{:=} and so on; numbers
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
   120
and strings (which we however ignore for the moment). We also need to specify what the ``white space''
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
   121
is in our programming language as well as what comments should look like.
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
   122
We might specify the regular expressions for our language roughly as follows
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
   123
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
   124
\begin{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
   125
\begin{tabular}{lcl}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
   126
$\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
   127
$\textit{IDENT}$ & $:=$ & $\textit{LETTER} \cdot (\textit{LETTER} + \textit{DIGIT} + {\_})^*$\\ 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
   128
$\textit{OP}$      &  $:=$ & $\texttt{:=} + \texttt{<} + \ldots$\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
   129
$\textit{NUM}$   &  $:=$ & $\textit{DIGIT}^+$\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
   130
$\textit{WHITESPACE}$ & $:=$ & $"\hspace{2mm}" + \backslash\texttt{n}$
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
   131
\end{tabular}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
   132
\end{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
   133
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
   134
\noindent
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
   135
with the usual meanings for the regular expressions $\textit{LETTER}$ and $\textit{DIGIT}$.
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
   136
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
   137
The problem we have to solve is given a string of our programming language, which regular expression 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
   138
matches which part of the string. For example the input string
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
   139
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
   140
\begin{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
   141
\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
   142
\end{center}
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
\noindent
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
   145
needs to be recognised as
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
   146
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
   147
\begin{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
   148
\tt
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
   149
\Grid{if} 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
   150
\Grid{\VS} 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
   151
\Grid{true} 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
   152
\Grid{\VS} 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
   153
\Grid{then} 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
   154
\Grid{\VS} 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
   155
\Grid{x} 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
   156
\Grid{+} 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
   157
\Grid{2}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
   158
\Grid{\VS} 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
   159
\Grid{else}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
   160
\Grid{\VS}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
   161
\Grid{x}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
   162
\Grid{+}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
   163
\Grid{3}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
   164
\end{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
   165
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
   166
\noindent
154
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 153
diff changeset
   167
Since \texttt{if} matches the \textit{KEYWORD} regular expression, \VS{}  is a whitespace and so on. This process of separating 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
   168
It is usually the first phase of a compiler. Note that the separation into words cannot, in general, 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
   169
be done by 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: 152
diff changeset
   170
the components in \texttt{x+2} are not. Another reason for recognising whitespaces explicitly is
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
   171
that in some languages, for example Python, whitespace matters. However in our small language we will eventually filter out all whitespaces and also comments.
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 152
diff changeset
   172
154
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 153
diff changeset
   173
Lexing will not just separate the input into its components, but also classify the components, that
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 153
diff changeset
   174
is explicitly record that \texttt{it} 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: 153
diff changeset
   175
But for the moment we will only focus on the simpler problem of separating a string into components.
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 153
diff changeset
   176
There are a few subtleties  we need to consider first. For example if the input string is
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 153
diff changeset
   177
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 153
diff changeset
   178
\begin{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 153
diff changeset
   179
\texttt{\Grid{iffoo\VS\ldots}}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 153
diff changeset
   180
\end{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 153
diff changeset
   181
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 153
diff changeset
   182
\noindent
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 153
diff changeset
   183
then there are two possibilities: either we regard the input as the keyword \texttt{if} followed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 153
diff changeset
   184
by the identifier \texttt{foo} (both regular expressions match) or we regard \texttt{iffoo} as a 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 153
diff changeset
   185
single identifier. The choice that is often made in lexers to look for the longest possible match,
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 153
diff changeset
   186
that is regard the input as a single identifier  \texttt{iffoo} (since it is longer than \texttt{if}).
151
df229ec49b22 added ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   187
df229ec49b22 added ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   188
\end{document}
df229ec49b22 added ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   189
df229ec49b22 added ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   190
%%% Local Variables: 
df229ec49b22 added ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   191
%%% mode: latex
df229ec49b22 added ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   192
%%% TeX-master: t
df229ec49b22 added ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   193
%%% End: