handouts/ho06.tex
author Christian Urban <urbanc@in.tum.de>
Sat, 27 Oct 2018 12:17:03 +0100
changeset 594 d40d7d7b85bc
parent 593 bb24d4e207b6
child 595 4bf0096bc06b
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
584
529460073b24 updated
Christian Urban <urbanc@in.tum.de>
parents: 392
diff changeset
     1
173
7cfb7a6f7c99 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     2
\documentclass{article}
297
5c51839c88fd updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
     3
\usepackage{../style}
217
cd6066f1056a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 183
diff changeset
     4
\usepackage{../langs}
588
a4646557016d updated
Christian Urban <urbanc@in.tum.de>
parents: 587
diff changeset
     5
\usepackage{../grammar}
173
7cfb7a6f7c99 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     6
7cfb7a6f7c99 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     7
\begin{document}
7cfb7a6f7c99 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     8
292
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 217
diff changeset
     9
\section*{Handout 6 (Parser Combinators)}
173
7cfb7a6f7c99 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    10
584
529460073b24 updated
Christian Urban <urbanc@in.tum.de>
parents: 392
diff changeset
    11
This handout explains how \emph{parser combinators} work and how they
587
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
    12
can be implemented in Scala. Their most distinguishing feature is that
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
    13
they are very easy to implement (admittedly it is only easy in a
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
    14
functional programming language).  Another good point of parser
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
    15
combinators is that they can deal with any kind of input as long as
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
    16
this input is of ``sequence-kind'', for example a string or a list of
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
    17
tokens. The only two properties of the input we need is to be able to
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
    18
test when it is empty and ``sequentially'' take it apart. Strings and
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
    19
lists fit this bill. However, parser combinators also have their
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
    20
drawbacks. For example they require that the grammar to be parsed is
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
    21
\emph{not} left-recursive and they are efficient only when the grammar
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
    22
is unambiguous. It is the responsibility of the grammar designer to
591
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
    23
ensure these two properties hold.
173
7cfb7a6f7c99 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    24
587
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
    25
The general idea behind parser combinators is to transform the input
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
    26
into sets of pairs, like so
175
5801e8c0e528 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 173
diff changeset
    27
5801e8c0e528 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 173
diff changeset
    28
\begin{center}
385
7f8516ff408d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 297
diff changeset
    29
$\underbrace{\text{list of tokens}}_{\text{input}}$ 
594
d40d7d7b85bc updated
Christian Urban <urbanc@in.tum.de>
parents: 593
diff changeset
    30
$\quad\Rightarrow\quad$
591
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
    31
$\underbrace{\text{set of (parsed part, unprocessed part)}}_{\text{output}}$
385
7f8516ff408d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 297
diff changeset
    32
\end{center} 
175
5801e8c0e528 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 173
diff changeset
    33
587
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
    34
\noindent
590
c6a1e19e9801 updated
Christian Urban <urbanc@in.tum.de>
parents: 589
diff changeset
    35
Given the extended effort we have spent implementing a lexer in order
591
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
    36
to generate lists of tokens, it might be surprising that in what
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
    37
follows we shall often use strings as input, rather than lists of
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
    38
tokens. This is for making the explanation more lucid and for quick
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
    39
examples. It does not make our previous work on lexers obsolete
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
    40
(remember they transform a string into a list of tokens). Lexers will
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
    41
still be needed for building a somewhat realistic compiler.
584
529460073b24 updated
Christian Urban <urbanc@in.tum.de>
parents: 392
diff changeset
    42
590
c6a1e19e9801 updated
Christian Urban <urbanc@in.tum.de>
parents: 589
diff changeset
    43
As mentioned above, parser combinators are relatively agnostic about what
587
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
    44
kind of input they process. In my Scala code I use the following
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
    45
polymorphic types for parser combinators:
176
3c2653fc8b5a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 175
diff changeset
    46
3c2653fc8b5a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 175
diff changeset
    47
\begin{center}
584
529460073b24 updated
Christian Urban <urbanc@in.tum.de>
parents: 392
diff changeset
    48
input:\;\; \texttt{I}  \qquad output:\;\; \texttt{T}
176
3c2653fc8b5a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 175
diff changeset
    49
\end{center}
3c2653fc8b5a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 175
diff changeset
    50
587
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
    51
\noindent That is they take as input something of type \texttt{I} and
590
c6a1e19e9801 updated
Christian Urban <urbanc@in.tum.de>
parents: 589
diff changeset
    52
return a set of pairs of type \texttt{Set[(T, I)]}. Since the input
c6a1e19e9801 updated
Christian Urban <urbanc@in.tum.de>
parents: 589
diff changeset
    53
needs to be of ``sequence-kind'', I actually have to often write
591
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
    54
\texttt{I <\% Seq[\_]} for the input type. This ensures the
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
    55
input is a subtype of Scala sequences. The first component of the
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
    56
generated pairs corresponds to what the parser combinator was able to
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
    57
parse from the input and the second is the unprocessed, or
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
    58
leftover, part of the input (therefore the type of this unprocessed part is
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
    59
the same as the input). A parser combinator might return more than one
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
    60
such pair; the idea is that there are potentially several ways of how
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
    61
to parse the input.  As a concrete example, consider the string
183
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
    62
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
    63
\begin{center}
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
    64
\tt\Grid{iffoo\VS testbar}
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
    65
\end{center}
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
    66
385
7f8516ff408d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 297
diff changeset
    67
\noindent We might have a parser combinator which tries to
386
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 385
diff changeset
    68
interpret this string as a keyword (\texttt{if}) or as an
385
7f8516ff408d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 297
diff changeset
    69
identifier (\texttt{iffoo}). Then the output will be the set
177
53def1fbf472 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
    70
183
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
    71
\begin{center}
386
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 385
diff changeset
    72
$\left\{ \left(\texttt{\Grid{if}}\;,\; \texttt{\Grid{foo\VS testbar}}\right), 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 385
diff changeset
    73
           \left(\texttt{\Grid{iffoo}}\;,\; \texttt{\Grid{\VS testbar}}\right) \right\}$
183
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
    74
\end{center}
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
    75
587
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
    76
\noindent where the first pair means the parser could recognise
590
c6a1e19e9801 updated
Christian Urban <urbanc@in.tum.de>
parents: 589
diff changeset
    77
\texttt{if} from the input and leaves the \texttt{foo\VS testbar} as
591
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
    78
unprocessed part; in the other case it could recognise
587
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
    79
\texttt{iffoo} and leaves \texttt{\VS testbar} as unprocessed. If the
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
    80
parser cannot recognise anything from the input at all, then parser
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
    81
combinators just return the empty set $\{\}$. This will indicate
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
    82
something ``went wrong''\ldots or more precisely, nothing could be
385
7f8516ff408d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 297
diff changeset
    83
parsed.
183
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
    84
594
d40d7d7b85bc updated
Christian Urban <urbanc@in.tum.de>
parents: 593
diff changeset
    85
Also important to note is that the output type \texttt{T} for the
d40d7d7b85bc updated
Christian Urban <urbanc@in.tum.de>
parents: 593
diff changeset
    86
processed part can potentially be different from the input type
d40d7d7b85bc updated
Christian Urban <urbanc@in.tum.de>
parents: 593
diff changeset
    87
\texttt{I} in the parser. In the example above is just happens to be
d40d7d7b85bc updated
Christian Urban <urbanc@in.tum.de>
parents: 593
diff changeset
    88
the same. The reason for the difference is that in general we are
d40d7d7b85bc updated
Christian Urban <urbanc@in.tum.de>
parents: 593
diff changeset
    89
interested in transforming our input into something
d40d7d7b85bc updated
Christian Urban <urbanc@in.tum.de>
parents: 593
diff changeset
    90
``different''\ldots for example into a tree; or if we implement the
d40d7d7b85bc updated
Christian Urban <urbanc@in.tum.de>
parents: 593
diff changeset
    91
grammar for arithmetic expressions, we might be interested in the
d40d7d7b85bc updated
Christian Urban <urbanc@in.tum.de>
parents: 593
diff changeset
    92
actual integer number the arithmetic expression, say \texttt{1 + 2 *
d40d7d7b85bc updated
Christian Urban <urbanc@in.tum.de>
parents: 593
diff changeset
    93
  3}, stands for. In this way we can use parser combinators to
d40d7d7b85bc updated
Christian Urban <urbanc@in.tum.de>
parents: 593
diff changeset
    94
implement relatively easily a calculator, for instance (we shall do
d40d7d7b85bc updated
Christian Urban <urbanc@in.tum.de>
parents: 593
diff changeset
    95
this later on).
584
529460073b24 updated
Christian Urban <urbanc@in.tum.de>
parents: 392
diff changeset
    96
594
d40d7d7b85bc updated
Christian Urban <urbanc@in.tum.de>
parents: 593
diff changeset
    97
The main driving force behind parser combinators is that we can easily
d40d7d7b85bc updated
Christian Urban <urbanc@in.tum.de>
parents: 593
diff changeset
    98
build parser combinators out of smaller components following very
d40d7d7b85bc updated
Christian Urban <urbanc@in.tum.de>
parents: 593
diff changeset
    99
closely the structure of a grammar. In order to implement this in a
591
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   100
functional/object-oriented programming language, like Scala, we need
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   101
to specify an abstract class for parser combinators. In the abstract
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   102
class we specify that \texttt{I} is the \emph{input type} of the
593
bb24d4e207b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 592
diff changeset
   103
parser combinator and that \texttt{T} is the \emph{output type}.  This
591
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   104
implies that the function \texttt{parse} takes an argument of type
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   105
\texttt{I} and returns a set of type \mbox{\texttt{Set[(T, I)]}}.
177
53def1fbf472 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   106
53def1fbf472 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   107
\begin{center}
385
7f8516ff408d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 297
diff changeset
   108
\begin{lstlisting}[language=Scala]
177
53def1fbf472 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   109
abstract class Parser[I, T] {
590
c6a1e19e9801 updated
Christian Urban <urbanc@in.tum.de>
parents: 589
diff changeset
   110
  def parse(in: I) : Set[(T, I)]
177
53def1fbf472 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   111
590
c6a1e19e9801 updated
Christian Urban <urbanc@in.tum.de>
parents: 589
diff changeset
   112
  def parse_all(in: I) : Set[T] =
c6a1e19e9801 updated
Christian Urban <urbanc@in.tum.de>
parents: 589
diff changeset
   113
    for ((head, tail) <- parse(in); if (tail.isEmpty)) 
177
53def1fbf472 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   114
      yield head
53def1fbf472 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   115
}
53def1fbf472 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   116
\end{lstlisting}
53def1fbf472 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   117
\end{center}
176
3c2653fc8b5a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 175
diff changeset
   118
591
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   119
\noindent It is the obligation in each instance of this class to
584
529460073b24 updated
Christian Urban <urbanc@in.tum.de>
parents: 392
diff changeset
   120
supply an implementation for \texttt{parse}.  From this function we
529460073b24 updated
Christian Urban <urbanc@in.tum.de>
parents: 392
diff changeset
   121
can then ``centrally'' derive the function \texttt{parse\_all}, which
529460073b24 updated
Christian Urban <urbanc@in.tum.de>
parents: 392
diff changeset
   122
just filters out all pairs whose second component is not empty (that
529460073b24 updated
Christian Urban <urbanc@in.tum.de>
parents: 392
diff changeset
   123
is has still some unprocessed part). The reason is that at the end of
529460073b24 updated
Christian Urban <urbanc@in.tum.de>
parents: 392
diff changeset
   124
the parsing we are only interested in the results where all the input
529460073b24 updated
Christian Urban <urbanc@in.tum.de>
parents: 392
diff changeset
   125
has been consumed and no unprocessed part is left over.
183
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   126
385
7f8516ff408d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 297
diff changeset
   127
One of the simplest parser combinators recognises just a
584
529460073b24 updated
Christian Urban <urbanc@in.tum.de>
parents: 392
diff changeset
   128
single character, say $c$, from the beginning of strings. Its
385
7f8516ff408d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 297
diff changeset
   129
behaviour can be described as follows:
176
3c2653fc8b5a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 175
diff changeset
   130
177
53def1fbf472 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   131
\begin{itemize}
584
529460073b24 updated
Christian Urban <urbanc@in.tum.de>
parents: 392
diff changeset
   132
\item If the head of the input string starts with a $c$, then return 
529460073b24 updated
Christian Urban <urbanc@in.tum.de>
parents: 392
diff changeset
   133
  the set
529460073b24 updated
Christian Urban <urbanc@in.tum.de>
parents: 392
diff changeset
   134
  \[\{(c, \textit{tail of}\; s)\}\]
529460073b24 updated
Christian Urban <urbanc@in.tum.de>
parents: 392
diff changeset
   135
  where \textit{tail of} 
529460073b24 updated
Christian Urban <urbanc@in.tum.de>
parents: 392
diff changeset
   136
  $s$ is the unprocessed part of the input string.
529460073b24 updated
Christian Urban <urbanc@in.tum.de>
parents: 392
diff changeset
   137
\item Otherwise return the empty set $\{\}$.	
177
53def1fbf472 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   138
\end{itemize}
53def1fbf472 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   139
53def1fbf472 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   140
\noindent
590
c6a1e19e9801 updated
Christian Urban <urbanc@in.tum.de>
parents: 589
diff changeset
   141
The input type of this simple parser combinator is \texttt{String} and
c6a1e19e9801 updated
Christian Urban <urbanc@in.tum.de>
parents: 589
diff changeset
   142
the output type is \texttt{Char}. This means \texttt{parse} returns
c6a1e19e9801 updated
Christian Urban <urbanc@in.tum.de>
parents: 589
diff changeset
   143
\mbox{\texttt{Set[(Char, String)]}}.  The code in Scala is as follows:
177
53def1fbf472 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   144
53def1fbf472 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   145
\begin{center}
53def1fbf472 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   146
\begin{lstlisting}[language=Scala,basicstyle=\small\ttfamily, numbers=none]
53def1fbf472 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   147
case class CharParser(c: Char) extends Parser[String, Char] {
587
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   148
  def parse(in: String) = 
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   149
    if (in.head == c) Set((c, in.tail)) else Set()
177
53def1fbf472 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   150
}
53def1fbf472 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   151
\end{lstlisting}
53def1fbf472 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   152
\end{center}
176
3c2653fc8b5a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 175
diff changeset
   153
589
0451b8b67f62 updated
Christian Urban <urbanc@in.tum.de>
parents: 588
diff changeset
   154
\noindent You can see \texttt{parse} tests whether the
587
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   155
first character of the input string \texttt{in} is equal to
584
529460073b24 updated
Christian Urban <urbanc@in.tum.de>
parents: 392
diff changeset
   156
\texttt{c}. If yes, then it splits the string into the recognised part
587
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   157
\texttt{c} and the unprocessed part \texttt{in.tail}. In case
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   158
\texttt{in} does not start with \texttt{c} then the parser returns the
584
529460073b24 updated
Christian Urban <urbanc@in.tum.de>
parents: 392
diff changeset
   159
empty set (in Scala \texttt{Set()}). Since this parser recognises
529460073b24 updated
Christian Urban <urbanc@in.tum.de>
parents: 392
diff changeset
   160
characters and just returns characters as the processed part, the
529460073b24 updated
Christian Urban <urbanc@in.tum.de>
parents: 392
diff changeset
   161
output type of the parser is \texttt{Char}.
529460073b24 updated
Christian Urban <urbanc@in.tum.de>
parents: 392
diff changeset
   162
529460073b24 updated
Christian Urban <urbanc@in.tum.de>
parents: 392
diff changeset
   163
If we want to parse a list of tokens and interested in recognising a
590
c6a1e19e9801 updated
Christian Urban <urbanc@in.tum.de>
parents: 589
diff changeset
   164
number token, for example, we could write something like this
584
529460073b24 updated
Christian Urban <urbanc@in.tum.de>
parents: 392
diff changeset
   165
529460073b24 updated
Christian Urban <urbanc@in.tum.de>
parents: 392
diff changeset
   166
\begin{center}
529460073b24 updated
Christian Urban <urbanc@in.tum.de>
parents: 392
diff changeset
   167
\begin{lstlisting}[language=Scala,basicstyle=\small\ttfamily,numbers=none]
529460073b24 updated
Christian Urban <urbanc@in.tum.de>
parents: 392
diff changeset
   168
case object NumParser extends Parser[List[Token], Int] {
529460073b24 updated
Christian Urban <urbanc@in.tum.de>
parents: 392
diff changeset
   169
  def parse(ts: List[Token]) = ts match {
529460073b24 updated
Christian Urban <urbanc@in.tum.de>
parents: 392
diff changeset
   170
    case Num_token(s)::ts => Set((s.toInt, ts)) 
529460073b24 updated
Christian Urban <urbanc@in.tum.de>
parents: 392
diff changeset
   171
    case _ => Set ()
529460073b24 updated
Christian Urban <urbanc@in.tum.de>
parents: 392
diff changeset
   172
  }
529460073b24 updated
Christian Urban <urbanc@in.tum.de>
parents: 392
diff changeset
   173
}
529460073b24 updated
Christian Urban <urbanc@in.tum.de>
parents: 392
diff changeset
   174
\end{lstlisting}
529460073b24 updated
Christian Urban <urbanc@in.tum.de>
parents: 392
diff changeset
   175
\end{center}
529460073b24 updated
Christian Urban <urbanc@in.tum.de>
parents: 392
diff changeset
   176
529460073b24 updated
Christian Urban <urbanc@in.tum.de>
parents: 392
diff changeset
   177
\noindent
529460073b24 updated
Christian Urban <urbanc@in.tum.de>
parents: 392
diff changeset
   178
In this parser the input is of type \texttt{List[Token]}. The function
529460073b24 updated
Christian Urban <urbanc@in.tum.de>
parents: 392
diff changeset
   179
parse looks at the input \texttt{ts} and checks whether the first
589
0451b8b67f62 updated
Christian Urban <urbanc@in.tum.de>
parents: 588
diff changeset
   180
token is a \texttt{Num\_token} (let us assume our lexer generated
0451b8b67f62 updated
Christian Urban <urbanc@in.tum.de>
parents: 588
diff changeset
   181
these tokens for numbers). But this parser does not just return this
584
529460073b24 updated
Christian Urban <urbanc@in.tum.de>
parents: 392
diff changeset
   182
token (and the rest of the list), like the \texttt{CharParser} above,
590
c6a1e19e9801 updated
Christian Urban <urbanc@in.tum.de>
parents: 589
diff changeset
   183
rather it extracts also the string \texttt{s} from the token and
c6a1e19e9801 updated
Christian Urban <urbanc@in.tum.de>
parents: 589
diff changeset
   184
converts it into an integer. The hope is that the lexer did its work
c6a1e19e9801 updated
Christian Urban <urbanc@in.tum.de>
parents: 589
diff changeset
   185
well and this conversion always succeeds. The consequence of this is
c6a1e19e9801 updated
Christian Urban <urbanc@in.tum.de>
parents: 589
diff changeset
   186
that the output type for this parser is \texttt{Int}, not
c6a1e19e9801 updated
Christian Urban <urbanc@in.tum.de>
parents: 589
diff changeset
   187
\texttt{Token}. Such a conversion would be needed if we want to
c6a1e19e9801 updated
Christian Urban <urbanc@in.tum.de>
parents: 589
diff changeset
   188
implement a simple calculator program, because string-numbers need to
c6a1e19e9801 updated
Christian Urban <urbanc@in.tum.de>
parents: 589
diff changeset
   189
be transformed into \texttt{Int}-numbers in order to do the
c6a1e19e9801 updated
Christian Urban <urbanc@in.tum.de>
parents: 589
diff changeset
   190
calculations.
385
7f8516ff408d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 297
diff changeset
   191
7f8516ff408d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 297
diff changeset
   192
584
529460073b24 updated
Christian Urban <urbanc@in.tum.de>
parents: 392
diff changeset
   193
These simple parsers that just look at the input and do a simple
529460073b24 updated
Christian Urban <urbanc@in.tum.de>
parents: 392
diff changeset
   194
transformation are often called \emph{atomic} parser combinators.
529460073b24 updated
Christian Urban <urbanc@in.tum.de>
parents: 392
diff changeset
   195
More interesting are the parser combinators that build larger parsers
587
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   196
out of smaller component parsers. There are three such parser
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   197
combinators that can be implemented generically. The \emph{alternative
584
529460073b24 updated
Christian Urban <urbanc@in.tum.de>
parents: 392
diff changeset
   198
  parser combinator} is as follows: given two parsers, say, $p$ and
529460073b24 updated
Christian Urban <urbanc@in.tum.de>
parents: 392
diff changeset
   199
$q$, we apply both parsers to the input (remember parsers are
587
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   200
functions) and combine the output (remember they are sets of pairs):
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   201
 
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   202
\begin{center}
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   203
$p(\text{input}) \cup q(\text{input})$
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   204
\end{center}
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   205
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   206
\noindent In Scala we can implement alternative parser
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   207
combinator as follows
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   208
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   209
\begin{center}
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   210
\begin{lstlisting}[language=Scala, numbers=none]
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   211
class AltParser[I, T]
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   212
       (p: => Parser[I, T], 
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   213
        q: => Parser[I, T]) extends Parser[I, T] {
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   214
  def parse(in: I) = p.parse(in) ++ q.parse(in)
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   215
}
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   216
\end{lstlisting}
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   217
\end{center}
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   218
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   219
\noindent The types of this parser combinator are again generic (we
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   220
have \texttt{I} for the input type, and \texttt{T} for the output
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   221
type). The alternative parser builds a new parser out of two existing
590
c6a1e19e9801 updated
Christian Urban <urbanc@in.tum.de>
parents: 589
diff changeset
   222
parsers \texttt{p} and \texttt{q} which are given as arguments.  Both
c6a1e19e9801 updated
Christian Urban <urbanc@in.tum.de>
parents: 589
diff changeset
   223
parsers need to be able to process input of type \texttt{I} and return
c6a1e19e9801 updated
Christian Urban <urbanc@in.tum.de>
parents: 589
diff changeset
   224
in \texttt{parse} the same output type \texttt{Set[(T,
587
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   225
  I)]}.\footnote{There is an interesting detail of Scala, namely the
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   226
  \texttt{=>} in front of the types of \texttt{p} and \texttt{q}. They
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   227
  will prevent the evaluation of the arguments before they are
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   228
  used. This is often called \emph{lazy evaluation} of the
590
c6a1e19e9801 updated
Christian Urban <urbanc@in.tum.de>
parents: 589
diff changeset
   229
  arguments. We will explain this later.}  The alternative parser runs
c6a1e19e9801 updated
Christian Urban <urbanc@in.tum.de>
parents: 589
diff changeset
   230
the input with the first parser \texttt{p} (producing a set of pairs)
c6a1e19e9801 updated
Christian Urban <urbanc@in.tum.de>
parents: 589
diff changeset
   231
and then runs the same input with \texttt{q} (producing another set of
c6a1e19e9801 updated
Christian Urban <urbanc@in.tum.de>
parents: 589
diff changeset
   232
pairs).  The result should be then just the union of both sets, which
c6a1e19e9801 updated
Christian Urban <urbanc@in.tum.de>
parents: 589
diff changeset
   233
is the operation \texttt{++} in Scala.
587
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   234
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   235
The alternative parser combinator allows us to construct a parser that
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   236
parses either a character \texttt{a} or \texttt{b} using the
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   237
\texttt{CharParser} shown above. For this we can write
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   238
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   239
\begin{center}
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   240
\begin{lstlisting}[language=Scala, numbers=none]
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   241
new AltParser(CharParser('a'), CharParser('b'))
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   242
\end{lstlisting}
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   243
\end{center}
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   244
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   245
\noindent Later on we will use Scala mechanism for introducing some
589
0451b8b67f62 updated
Christian Urban <urbanc@in.tum.de>
parents: 588
diff changeset
   246
more readable shorthand notation for this, like \texttt{"a" |
587
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   247
  "b"}. Let us look in detail at what this parser combinator produces
590
c6a1e19e9801 updated
Christian Urban <urbanc@in.tum.de>
parents: 589
diff changeset
   248
with some sample strings.
587
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   249
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   250
\begin{center}
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   251
\begin{tabular}{rcl}
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   252
input strings & & output\medskip\\
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   253
\texttt{\Grid{acde}} & $\rightarrow$ & $\left\{(\texttt{\Grid{a}}, \texttt{\Grid{cde}})\right\}$\\
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   254
\texttt{\Grid{bcde}} & $\rightarrow$ & $\left\{(\texttt{\Grid{b}}, \texttt{\Grid{cde}})\right\}$\\
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   255
\texttt{\Grid{ccde}} & $\rightarrow$ & $\{\}$
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   256
\end{tabular}
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   257
\end{center}
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   258
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   259
\noindent We receive in the first two cases a successful
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   260
output (that is a non-empty set). In each case, either
591
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   261
\pcode{a} or \pcode{b} is in the parsed part, and
587
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   262
\pcode{cde} in the unprocessed part. Clearly this parser cannot
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   263
parse anything with \pcode{ccde}, therefore the empty
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   264
set is returned.
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   265
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   266
A bit more interesting is the \emph{sequence parser combinator}. Given
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   267
two parsers, say again, $p$ and $q$, we want to apply first the input
590
c6a1e19e9801 updated
Christian Urban <urbanc@in.tum.de>
parents: 589
diff changeset
   268
to $p$ producing a set of pairs; then apply $q$ to all the unparsed  
587
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   269
parts in the pairs; and then combine the results. Mathematically we would
591
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   270
write something like this for the set of pairs:
587
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   271
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   272
\begin{center}
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   273
\begin{tabular}{lcl}
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   274
$\{((\textit{output}_1, \textit{output}_2), u_2)$ & $\,|\,$ & 
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   275
$(\textit{output}_1, u_1) \in p(\text{input}) 
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   276
\;\wedge\;$\\
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   277
&& $(\textit{output}_2, u_2) \in q(u_1)\}$
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   278
\end{tabular}
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   279
\end{center}
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   280
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   281
\noindent Notice that the $p$ will first be run on the input,
590
c6a1e19e9801 updated
Christian Urban <urbanc@in.tum.de>
parents: 589
diff changeset
   282
producing pairs of the form $(\textit{output}_1, u_1)$ where the $u_1$
591
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   283
stands for the unprocessed, or leftover, parts of $p$. We want that
590
c6a1e19e9801 updated
Christian Urban <urbanc@in.tum.de>
parents: 589
diff changeset
   284
$q$ runs on all these unprocessed parts $u_1$. Therefore these
c6a1e19e9801 updated
Christian Urban <urbanc@in.tum.de>
parents: 589
diff changeset
   285
unprocessed parts are fed into the second parser $q$. The overall
c6a1e19e9801 updated
Christian Urban <urbanc@in.tum.de>
parents: 589
diff changeset
   286
result of the sequence parser combinator is pairs of the form
584
529460073b24 updated
Christian Urban <urbanc@in.tum.de>
parents: 392
diff changeset
   287
$((\textit{output}_1, \textit{output}_2), u_2)$. This means the
593
bb24d4e207b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 592
diff changeset
   288
unprocessed part of the sequence parser combinator is the unprocessed
591
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   289
part the second parser $q$ leaves as leftover. The parsed parts of the
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   290
component parsers are combined in a pair, namely
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   291
$(\textit{output}_1, \textit{output}_2)$. The reason is we want to
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   292
know what $p$ and $q$ were able to parse. This behaviour can be
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   293
implemented in Scala as follows:
183
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   294
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   295
\begin{center}
385
7f8516ff408d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 297
diff changeset
   296
\begin{lstlisting}[language=Scala,numbers=none]
183
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   297
class SeqParser[I, T, S]
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   298
       (p: => Parser[I, T], 
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   299
        q: => Parser[I, S]) extends Parser[I, (T, S)] {
587
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   300
  def parse(in: I) = 
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   301
    for ((output1, u1) <- p.parse(in); 
386
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 385
diff changeset
   302
         (output2, u2) <- q.parse(u1)) 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 385
diff changeset
   303
            yield ((output1, output2), u2)
183
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   304
}
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   305
\end{lstlisting}
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   306
\end{center}
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   307
587
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   308
\noindent This parser takes again as arguments two parsers, \texttt{p}
591
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   309
and \texttt{q}. It implements \texttt{parse} as follows: first run the
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   310
parser \texttt{p} on the input producing a set of pairs
587
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   311
(\texttt{output1}, \texttt{u1}). The \texttt{u1} stands for the
591
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   312
unprocessed parts left over by \texttt{p} (recall that there can be
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   313
several such pairs). Let then \texttt{q} run on these unprocessed
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   314
parts producing again a set of pairs. The output of the sequence
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   315
parser combinator is then a set containing pairs where the first
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   316
components are again pairs, namely what the first parser could parse
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   317
together with what the second parser could parse; the second component
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   318
is the unprocessed part left over after running the second parser
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   319
\texttt{q}. Note that the input type of the sequence parser combinator
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   320
is as usual \texttt{I}, but the output type is
183
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   321
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   322
\begin{center}
590
c6a1e19e9801 updated
Christian Urban <urbanc@in.tum.de>
parents: 589
diff changeset
   323
\texttt{(T, S)}
183
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   324
\end{center}
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   325
584
529460073b24 updated
Christian Urban <urbanc@in.tum.de>
parents: 392
diff changeset
   326
\noindent
591
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   327
Consequently, the function \texttt{parse} in the sequence parser
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   328
combinator returns sets of type \texttt{Set[((T, S), I)]}.  That means
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   329
we have essentially two output types for the sequence parser
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   330
combinator (packaged in a pair), because in general \textit{p} and
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   331
\textit{q} might produce different things (for example we recognise a
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   332
number with \texttt{p} and then with \texttt{q} a string corresponding
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   333
to an operator).  If any of the runs of \textit{p} and \textit{q}
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   334
fail, that is produce the empty set, then \texttt{parse} will also
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   335
produce the empty set.
584
529460073b24 updated
Christian Urban <urbanc@in.tum.de>
parents: 392
diff changeset
   336
587
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   337
With the shorthand notation we shall introduce later for the sequence
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   338
parser combinator, we can write for example \pcode{"a" ~ "b"}, which
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   339
is the parser combinator that first recognises the character
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   340
\texttt{a} from a string and then \texttt{b}. Let us look again at
591
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   341
some examples of how this parser combinator processes some strings:
385
7f8516ff408d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 297
diff changeset
   342
7f8516ff408d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 297
diff changeset
   343
\begin{center}
7f8516ff408d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 297
diff changeset
   344
\begin{tabular}{rcl}
7f8516ff408d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 297
diff changeset
   345
input strings & & output\medskip\\
584
529460073b24 updated
Christian Urban <urbanc@in.tum.de>
parents: 392
diff changeset
   346
\texttt{\Grid{abcde}} & $\rightarrow$ & $\left\{((\texttt{\Grid{a}}, \texttt{\Grid{b}}), \texttt{\Grid{cde}})\right\}$\\
529460073b24 updated
Christian Urban <urbanc@in.tum.de>
parents: 392
diff changeset
   347
\texttt{\Grid{bacde}} & $\rightarrow$ & $\{\}$\\
529460073b24 updated
Christian Urban <urbanc@in.tum.de>
parents: 392
diff changeset
   348
\texttt{\Grid{cccde}} & $\rightarrow$ & $\{\}$
385
7f8516ff408d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 297
diff changeset
   349
\end{tabular}
7f8516ff408d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 297
diff changeset
   350
\end{center}
7f8516ff408d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 297
diff changeset
   351
586
Christian Urban <urbanc@in.tum.de>
parents: 585
diff changeset
   352
\noindent In the first line we have a successful parse, because the
587
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   353
string starts with \texttt{ab}, which is the prefix we are looking
584
529460073b24 updated
Christian Urban <urbanc@in.tum.de>
parents: 392
diff changeset
   354
for. But since the parsing combinator is constructed as sequence of
529460073b24 updated
Christian Urban <urbanc@in.tum.de>
parents: 392
diff changeset
   355
the two simple (atomic) parsers for \texttt{a} and \texttt{b}, the
529460073b24 updated
Christian Urban <urbanc@in.tum.de>
parents: 392
diff changeset
   356
result is a nested pair of the form \texttt{((a, b), cde)}. It is
586
Christian Urban <urbanc@in.tum.de>
parents: 585
diff changeset
   357
\emph{not} a simple pair \texttt{(ab, cde)} as one might erroneously
587
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   358
expect.  The parser returns the empty set in the other examples,
584
529460073b24 updated
Christian Urban <urbanc@in.tum.de>
parents: 392
diff changeset
   359
because they do not fit with what the parser is supposed to parse.
529460073b24 updated
Christian Urban <urbanc@in.tum.de>
parents: 392
diff changeset
   360
529460073b24 updated
Christian Urban <urbanc@in.tum.de>
parents: 392
diff changeset
   361
589
0451b8b67f62 updated
Christian Urban <urbanc@in.tum.de>
parents: 588
diff changeset
   362
A slightly more complicated parser is \pcode{("a" | "b") ~ "c"} which
587
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   363
parses as first character either an \texttt{a} or \texttt{b}, followed
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   364
by a \texttt{c}. This parser produces the following outputs.
385
7f8516ff408d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 297
diff changeset
   365
7f8516ff408d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 297
diff changeset
   366
\begin{center}
7f8516ff408d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 297
diff changeset
   367
\begin{tabular}{rcl}
7f8516ff408d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 297
diff changeset
   368
input strings & & output\medskip\\
585
6ee22f196884 updated
Christian Urban <urbanc@in.tum.de>
parents: 584
diff changeset
   369
\texttt{\Grid{acde}} & $\rightarrow$ & $\left\{((\texttt{\Grid{a}}, \texttt{\Grid{c}}), \texttt{\Grid{de}})\right\}$\\
6ee22f196884 updated
Christian Urban <urbanc@in.tum.de>
parents: 584
diff changeset
   370
\texttt{\Grid{bcde}} & $\rightarrow$ & $\left\{((\texttt{\Grid{b}}, \texttt{\Grid{c}}), \texttt{\Grid{de}})\right\}$\\
6ee22f196884 updated
Christian Urban <urbanc@in.tum.de>
parents: 584
diff changeset
   371
\texttt{\Grid{abde}} & $\rightarrow$ & $\{\}$
385
7f8516ff408d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 297
diff changeset
   372
\end{tabular}
7f8516ff408d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 297
diff changeset
   373
\end{center}
7f8516ff408d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 297
diff changeset
   374
585
6ee22f196884 updated
Christian Urban <urbanc@in.tum.de>
parents: 584
diff changeset
   375
\noindent
6ee22f196884 updated
Christian Urban <urbanc@in.tum.de>
parents: 584
diff changeset
   376
Now consider the parser \pcode{("a" ~ "b") ~ "c"} which parses
6ee22f196884 updated
Christian Urban <urbanc@in.tum.de>
parents: 584
diff changeset
   377
\texttt{a}, \texttt{b}, \texttt{c} in sequence. This parser produces
6ee22f196884 updated
Christian Urban <urbanc@in.tum.de>
parents: 584
diff changeset
   378
the following outputs.
6ee22f196884 updated
Christian Urban <urbanc@in.tum.de>
parents: 584
diff changeset
   379
6ee22f196884 updated
Christian Urban <urbanc@in.tum.de>
parents: 584
diff changeset
   380
\begin{center}
6ee22f196884 updated
Christian Urban <urbanc@in.tum.de>
parents: 584
diff changeset
   381
\begin{tabular}{rcl}
6ee22f196884 updated
Christian Urban <urbanc@in.tum.de>
parents: 584
diff changeset
   382
input strings & & output\medskip\\
6ee22f196884 updated
Christian Urban <urbanc@in.tum.de>
parents: 584
diff changeset
   383
\texttt{\Grid{abcde}} & $\rightarrow$ & $\left\{(((\texttt{\Grid{a}},\texttt{\Grid{b}}), \texttt{\Grid{c}}), \texttt{\Grid{de}})\right\}$\\
6ee22f196884 updated
Christian Urban <urbanc@in.tum.de>
parents: 584
diff changeset
   384
\texttt{\Grid{abde}} & $\rightarrow$ & $\{\}$\\
6ee22f196884 updated
Christian Urban <urbanc@in.tum.de>
parents: 584
diff changeset
   385
\texttt{\Grid{bcde}} & $\rightarrow$ & $\{\}$
6ee22f196884 updated
Christian Urban <urbanc@in.tum.de>
parents: 584
diff changeset
   386
\end{tabular}
6ee22f196884 updated
Christian Urban <urbanc@in.tum.de>
parents: 584
diff changeset
   387
\end{center}
6ee22f196884 updated
Christian Urban <urbanc@in.tum.de>
parents: 584
diff changeset
   388
6ee22f196884 updated
Christian Urban <urbanc@in.tum.de>
parents: 584
diff changeset
   389
6ee22f196884 updated
Christian Urban <urbanc@in.tum.de>
parents: 584
diff changeset
   390
\noindent The second and third example fail, because something is
590
c6a1e19e9801 updated
Christian Urban <urbanc@in.tum.de>
parents: 589
diff changeset
   391
``missing'' in the sequence we are looking for. The first succeeds but
c6a1e19e9801 updated
Christian Urban <urbanc@in.tum.de>
parents: 589
diff changeset
   392
notice how the results nest with sequences: the parsed part is a
c6a1e19e9801 updated
Christian Urban <urbanc@in.tum.de>
parents: 589
diff changeset
   393
nested pair of the form \pcode{((a, b), c)}. If we nest the sequence
591
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   394
parser differently, say \pcode{"a" ~ ("b" ~ "c")}, then also
590
c6a1e19e9801 updated
Christian Urban <urbanc@in.tum.de>
parents: 589
diff changeset
   395
our output pairs nest differently
589
0451b8b67f62 updated
Christian Urban <urbanc@in.tum.de>
parents: 588
diff changeset
   396
0451b8b67f62 updated
Christian Urban <urbanc@in.tum.de>
parents: 588
diff changeset
   397
\begin{center}
0451b8b67f62 updated
Christian Urban <urbanc@in.tum.de>
parents: 588
diff changeset
   398
\begin{tabular}{rcl}
0451b8b67f62 updated
Christian Urban <urbanc@in.tum.de>
parents: 588
diff changeset
   399
input strings & & output\medskip\\
0451b8b67f62 updated
Christian Urban <urbanc@in.tum.de>
parents: 588
diff changeset
   400
\texttt{\Grid{abcde}} & $\rightarrow$ & $\left\{((\texttt{\Grid{a}},(\texttt{\Grid{b}}, \texttt{\Grid{c}})), \texttt{\Grid{de}})\right\}$\\
0451b8b67f62 updated
Christian Urban <urbanc@in.tum.de>
parents: 588
diff changeset
   401
\end{tabular}
0451b8b67f62 updated
Christian Urban <urbanc@in.tum.de>
parents: 588
diff changeset
   402
\end{center}
0451b8b67f62 updated
Christian Urban <urbanc@in.tum.de>
parents: 588
diff changeset
   403
0451b8b67f62 updated
Christian Urban <urbanc@in.tum.de>
parents: 588
diff changeset
   404
\noindent
0451b8b67f62 updated
Christian Urban <urbanc@in.tum.de>
parents: 588
diff changeset
   405
Two more examples: first consider the parser
585
6ee22f196884 updated
Christian Urban <urbanc@in.tum.de>
parents: 584
diff changeset
   406
\pcode{("a" ~ "a") ~ "a"} and the input \pcode{aaaa}:
183
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   407
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   408
\begin{center}
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   409
\begin{tabular}{rcl}
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   410
input string & & output\medskip\\
385
7f8516ff408d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 297
diff changeset
   411
\texttt{\Grid{aaaa}} & $\rightarrow$ & 
7f8516ff408d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 297
diff changeset
   412
$\left\{(((\texttt{\Grid{a}}, \texttt{\Grid{a}}), \texttt{\Grid{a}}), \texttt{\Grid{a}})\right\}$\\
183
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   413
\end{tabular}
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   414
\end{center}
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   415
591
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   416
\noindent Notice again how the results nest deeper and deeper as pairs (the
585
6ee22f196884 updated
Christian Urban <urbanc@in.tum.de>
parents: 584
diff changeset
   417
last \pcode{a} is in the unprocessed part). To consume everything of
6ee22f196884 updated
Christian Urban <urbanc@in.tum.de>
parents: 584
diff changeset
   418
this string we can use the parser \pcode{(("a" ~ "a") ~ "a") ~
6ee22f196884 updated
Christian Urban <urbanc@in.tum.de>
parents: 584
diff changeset
   419
  "a"}. Then the output is as follows:
183
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   420
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   421
\begin{center}
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   422
\begin{tabular}{rcl}
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   423
input string & & output\medskip\\
385
7f8516ff408d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 297
diff changeset
   424
\texttt{\Grid{aaaa}} & $\rightarrow$ & 
7f8516ff408d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 297
diff changeset
   425
$\left\{((((\texttt{\Grid{a}}, \texttt{\Grid{a}}), \texttt{\Grid{a}}), \texttt{\Grid{a}}), \texttt{""})\right\}$\\
183
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   426
\end{tabular}
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   427
\end{center}
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   428
385
7f8516ff408d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 297
diff changeset
   429
\noindent This is an instance where the parser consumed
7f8516ff408d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 297
diff changeset
   430
completely the input, meaning the unprocessed part is just the
587
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   431
empty string. So if we called \pcode{parse_all}, instead of \pcode{parse},
585
6ee22f196884 updated
Christian Urban <urbanc@in.tum.de>
parents: 584
diff changeset
   432
we would get back the result
6ee22f196884 updated
Christian Urban <urbanc@in.tum.de>
parents: 584
diff changeset
   433
6ee22f196884 updated
Christian Urban <urbanc@in.tum.de>
parents: 584
diff changeset
   434
\[
6ee22f196884 updated
Christian Urban <urbanc@in.tum.de>
parents: 584
diff changeset
   435
\left\{(((\texttt{\Grid{a}}, \texttt{\Grid{a}}), \texttt{\Grid{a}}), \texttt{\Grid{a}})\right\}
6ee22f196884 updated
Christian Urban <urbanc@in.tum.de>
parents: 584
diff changeset
   436
\]
6ee22f196884 updated
Christian Urban <urbanc@in.tum.de>
parents: 584
diff changeset
   437
6ee22f196884 updated
Christian Urban <urbanc@in.tum.de>
parents: 584
diff changeset
   438
\noindent where the unprocessed (empty) parts have been stripped away
6ee22f196884 updated
Christian Urban <urbanc@in.tum.de>
parents: 584
diff changeset
   439
from the pairs; everything where the second part was not empty has
587
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   440
been thrown away as well, because they represent
590
c6a1e19e9801 updated
Christian Urban <urbanc@in.tum.de>
parents: 589
diff changeset
   441
ultimately-unsuccessful-parses. The main point is that the sequence
c6a1e19e9801 updated
Christian Urban <urbanc@in.tum.de>
parents: 589
diff changeset
   442
parser combinator returns pairs that can nest according to the
c6a1e19e9801 updated
Christian Urban <urbanc@in.tum.de>
parents: 589
diff changeset
   443
nesting of the component parsers.
385
7f8516ff408d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 297
diff changeset
   444
183
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   445
590
c6a1e19e9801 updated
Christian Urban <urbanc@in.tum.de>
parents: 589
diff changeset
   446
Consider also carefully that constructing a parser such \pcode{"a" |
c6a1e19e9801 updated
Christian Urban <urbanc@in.tum.de>
parents: 589
diff changeset
   447
  ("a" ~ "b")} will result in a typing error. The intention with this
591
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   448
parser is that we want to parse either an \texttt{a}, or an \texttt{a}
590
c6a1e19e9801 updated
Christian Urban <urbanc@in.tum.de>
parents: 589
diff changeset
   449
followed by a \texttt{b}. However, the first parser has as output type
c6a1e19e9801 updated
Christian Urban <urbanc@in.tum.de>
parents: 589
diff changeset
   450
a single character (recall the type of \texttt{CharParser}), but the
c6a1e19e9801 updated
Christian Urban <urbanc@in.tum.de>
parents: 589
diff changeset
   451
second parser produces a pair of characters as output. The alternative
c6a1e19e9801 updated
Christian Urban <urbanc@in.tum.de>
parents: 589
diff changeset
   452
parser is required to have both component parsers to have the same
591
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   453
type---the reason is that we need to be able to build the union of two
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   454
sets, which requires in Scala that the sets have the same type.  Since
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   455
they are not in this case, there is a typing error.  We will see later
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   456
how we can build this parser without the typing error.
385
7f8516ff408d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 297
diff changeset
   457
587
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   458
The next parser combinator, called \emph{semantic action}, does not
591
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   459
actually combine two smaller parsers, but applies a function to the result
587
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   460
of a parser.  It is implemented in Scala as follows
183
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   461
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   462
\begin{center}
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   463
\begin{lstlisting}[language=Scala,basicstyle=\small\ttfamily, numbers=none]
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   464
class FunParser[I, T, S]
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   465
         (p: => Parser[I, T], 
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   466
          f: T => S) extends Parser[I, S] {
587
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   467
  def parse(in: I) = 
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   468
    for ((head, tail) <- p.parse(in)) yield (f(head), tail)
183
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   469
}
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   470
\end{lstlisting}
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   471
\end{center}
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   472
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   473
590
c6a1e19e9801 updated
Christian Urban <urbanc@in.tum.de>
parents: 589
diff changeset
   474
\noindent This parser combinator takes a parser \texttt{p} (with input
c6a1e19e9801 updated
Christian Urban <urbanc@in.tum.de>
parents: 589
diff changeset
   475
type \texttt{I} and output type \texttt{T}) as one argument but also a
c6a1e19e9801 updated
Christian Urban <urbanc@in.tum.de>
parents: 589
diff changeset
   476
function \texttt{f} (with type \texttt{T => S}). The parser \texttt{p}
c6a1e19e9801 updated
Christian Urban <urbanc@in.tum.de>
parents: 589
diff changeset
   477
produces sets of type \texttt{Set[(T, I)]}. The semantic action
c6a1e19e9801 updated
Christian Urban <urbanc@in.tum.de>
parents: 589
diff changeset
   478
combinator then applies the function \texttt{f} to all the `processed'
c6a1e19e9801 updated
Christian Urban <urbanc@in.tum.de>
parents: 589
diff changeset
   479
parser outputs. Since this function is of type \texttt{T => S}, we
c6a1e19e9801 updated
Christian Urban <urbanc@in.tum.de>
parents: 589
diff changeset
   480
obtain a parser with output type \texttt{S}. Again Scala lets us
c6a1e19e9801 updated
Christian Urban <urbanc@in.tum.de>
parents: 589
diff changeset
   481
introduce some shorthand notation for this parser
591
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   482
combinator. Therefore we will write short \texttt{p ==> f} for it.
386
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 385
diff changeset
   483
589
0451b8b67f62 updated
Christian Urban <urbanc@in.tum.de>
parents: 588
diff changeset
   484
What are semantic actions good for? Well, they allow you to transform
590
c6a1e19e9801 updated
Christian Urban <urbanc@in.tum.de>
parents: 589
diff changeset
   485
the parsed input into datastructures you can use for further
591
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   486
processing. A simple (contrived) example would be to transform parsed
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   487
characters into ASCII numbers. Suppose we define a function \texttt{f}
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   488
(from characters to \texttt{Int}s) and use a \texttt{CharParser} for parsing
589
0451b8b67f62 updated
Christian Urban <urbanc@in.tum.de>
parents: 588
diff changeset
   489
the character \texttt{c}.
587
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   490
591
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   491
587
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   492
\begin{center}
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   493
\begin{lstlisting}[language=Scala,basicstyle=\small\ttfamily, numbers=none]
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   494
val f = (c: Char) => c.toInt
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   495
val c = new CharParser('c')
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   496
\end{lstlisting}
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   497
\end{center}
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   498
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   499
\noindent
589
0451b8b67f62 updated
Christian Urban <urbanc@in.tum.de>
parents: 588
diff changeset
   500
We then can run the following two parsers on the input \texttt{cbd}:
587
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   501
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   502
\begin{center}
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   503
\begin{lstlisting}[language=Scala,basicstyle=\small\ttfamily, numbers=none]
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   504
c.parse("cbd")
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   505
(c ==> f).parse("cbd")
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   506
\end{lstlisting}
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   507
\end{center}
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   508
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   509
\noindent
589
0451b8b67f62 updated
Christian Urban <urbanc@in.tum.de>
parents: 588
diff changeset
   510
In the first line we obtain the expected result \texttt{Set(('c',
0451b8b67f62 updated
Christian Urban <urbanc@in.tum.de>
parents: 588
diff changeset
   511
  "bd"))}, whereas the second produces \texttt{Set((99, "bd"))}---the
0451b8b67f62 updated
Christian Urban <urbanc@in.tum.de>
parents: 588
diff changeset
   512
character has been transformed into an ASCII number.
588
a4646557016d updated
Christian Urban <urbanc@in.tum.de>
parents: 587
diff changeset
   513
a4646557016d updated
Christian Urban <urbanc@in.tum.de>
parents: 587
diff changeset
   514
A slightly less contrived example is about parsing numbers (recall
591
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   515
\texttt{NumParser} above). However, we want to do this here for
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   516
strings, not for tokens.  For this assume we have the following
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   517
(atomic) \texttt{RegexParser}.
588
a4646557016d updated
Christian Urban <urbanc@in.tum.de>
parents: 587
diff changeset
   518
a4646557016d updated
Christian Urban <urbanc@in.tum.de>
parents: 587
diff changeset
   519
\begin{center}
a4646557016d updated
Christian Urban <urbanc@in.tum.de>
parents: 587
diff changeset
   520
  \begin{lstlisting}[language=Scala,xleftmargin=0mm,
a4646557016d updated
Christian Urban <urbanc@in.tum.de>
parents: 587
diff changeset
   521
    basicstyle=\small\ttfamily, numbers=none]
a4646557016d updated
Christian Urban <urbanc@in.tum.de>
parents: 587
diff changeset
   522
import scala.util.matching.Regex
a4646557016d updated
Christian Urban <urbanc@in.tum.de>
parents: 587
diff changeset
   523
    
a4646557016d updated
Christian Urban <urbanc@in.tum.de>
parents: 587
diff changeset
   524
case class RegexParser(reg: Regex) extends Parser[String, String] {
a4646557016d updated
Christian Urban <urbanc@in.tum.de>
parents: 587
diff changeset
   525
  def parse(in: String) = reg.findPrefixMatchOf(in) match {
a4646557016d updated
Christian Urban <urbanc@in.tum.de>
parents: 587
diff changeset
   526
    case None => Set()
a4646557016d updated
Christian Urban <urbanc@in.tum.de>
parents: 587
diff changeset
   527
    case Some(m) => Set((m.matched, m.after.toString))  
a4646557016d updated
Christian Urban <urbanc@in.tum.de>
parents: 587
diff changeset
   528
  }
a4646557016d updated
Christian Urban <urbanc@in.tum.de>
parents: 587
diff changeset
   529
}
a4646557016d updated
Christian Urban <urbanc@in.tum.de>
parents: 587
diff changeset
   530
\end{lstlisting}
a4646557016d updated
Christian Urban <urbanc@in.tum.de>
parents: 587
diff changeset
   531
\end{center}
a4646557016d updated
Christian Urban <urbanc@in.tum.de>
parents: 587
diff changeset
   532
a4646557016d updated
Christian Urban <urbanc@in.tum.de>
parents: 587
diff changeset
   533
\noindent
a4646557016d updated
Christian Urban <urbanc@in.tum.de>
parents: 587
diff changeset
   534
This parser takes a regex as argument and splits up a string into a
a4646557016d updated
Christian Urban <urbanc@in.tum.de>
parents: 587
diff changeset
   535
prefix and the rest according to this regex
a4646557016d updated
Christian Urban <urbanc@in.tum.de>
parents: 587
diff changeset
   536
(\texttt{reg.findPrefixMatchOf} generates a match---in the successful
a4646557016d updated
Christian Urban <urbanc@in.tum.de>
parents: 587
diff changeset
   537
case---and the corresponding strings can be extracted with
591
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   538
\texttt{matched} and \texttt{after}). The input and output type for
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   539
this parser is \texttt{String}. Using \texttt{RegexParser} we can
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   540
define a \texttt{NumParser} for \texttt{Strings} to \texttt{Int} as
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   541
follows:
588
a4646557016d updated
Christian Urban <urbanc@in.tum.de>
parents: 587
diff changeset
   542
a4646557016d updated
Christian Urban <urbanc@in.tum.de>
parents: 587
diff changeset
   543
\begin{center}
a4646557016d updated
Christian Urban <urbanc@in.tum.de>
parents: 587
diff changeset
   544
\begin{lstlisting}[language=Scala,basicstyle=\small\ttfamily, numbers=none]
a4646557016d updated
Christian Urban <urbanc@in.tum.de>
parents: 587
diff changeset
   545
val NumParser = RegexParser("[0-9]+".r)
a4646557016d updated
Christian Urban <urbanc@in.tum.de>
parents: 587
diff changeset
   546
\end{lstlisting}
a4646557016d updated
Christian Urban <urbanc@in.tum.de>
parents: 587
diff changeset
   547
\end{center}
a4646557016d updated
Christian Urban <urbanc@in.tum.de>
parents: 587
diff changeset
   548
a4646557016d updated
Christian Urban <urbanc@in.tum.de>
parents: 587
diff changeset
   549
\noindent
591
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   550
This parser will recognise a number at the beginning of a string. For
588
a4646557016d updated
Christian Urban <urbanc@in.tum.de>
parents: 587
diff changeset
   551
example
a4646557016d updated
Christian Urban <urbanc@in.tum.de>
parents: 587
diff changeset
   552
a4646557016d updated
Christian Urban <urbanc@in.tum.de>
parents: 587
diff changeset
   553
\begin{center}
a4646557016d updated
Christian Urban <urbanc@in.tum.de>
parents: 587
diff changeset
   554
\begin{lstlisting}[language=Scala,basicstyle=\small\ttfamily, numbers=none]
a4646557016d updated
Christian Urban <urbanc@in.tum.de>
parents: 587
diff changeset
   555
NumParser.parse("123abc")
a4646557016d updated
Christian Urban <urbanc@in.tum.de>
parents: 587
diff changeset
   556
\end{lstlisting}
a4646557016d updated
Christian Urban <urbanc@in.tum.de>
parents: 587
diff changeset
   557
\end{center}  
a4646557016d updated
Christian Urban <urbanc@in.tum.de>
parents: 587
diff changeset
   558
a4646557016d updated
Christian Urban <urbanc@in.tum.de>
parents: 587
diff changeset
   559
\noindent
a4646557016d updated
Christian Urban <urbanc@in.tum.de>
parents: 587
diff changeset
   560
produces \texttt{Set((123,abc))}. The problem is that \texttt{123} is
590
c6a1e19e9801 updated
Christian Urban <urbanc@in.tum.de>
parents: 589
diff changeset
   561
still a string (the required double-quotes are not printed by
c6a1e19e9801 updated
Christian Urban <urbanc@in.tum.de>
parents: 589
diff changeset
   562
Scala). We want to convert this string into the corresponding
c6a1e19e9801 updated
Christian Urban <urbanc@in.tum.de>
parents: 589
diff changeset
   563
\texttt{Int}. We can do this as follows using a semantic action
588
a4646557016d updated
Christian Urban <urbanc@in.tum.de>
parents: 587
diff changeset
   564
a4646557016d updated
Christian Urban <urbanc@in.tum.de>
parents: 587
diff changeset
   565
\begin{center}
a4646557016d updated
Christian Urban <urbanc@in.tum.de>
parents: 587
diff changeset
   566
\begin{lstlisting}[language=Scala,basicstyle=\small\ttfamily, numbers=none]
a4646557016d updated
Christian Urban <urbanc@in.tum.de>
parents: 587
diff changeset
   567
(NumParser ==> (s => s.toInt)).parse("123abc")
a4646557016d updated
Christian Urban <urbanc@in.tum.de>
parents: 587
diff changeset
   568
\end{lstlisting}
a4646557016d updated
Christian Urban <urbanc@in.tum.de>
parents: 587
diff changeset
   569
\end{center}  
a4646557016d updated
Christian Urban <urbanc@in.tum.de>
parents: 587
diff changeset
   570
a4646557016d updated
Christian Urban <urbanc@in.tum.de>
parents: 587
diff changeset
   571
\noindent
589
0451b8b67f62 updated
Christian Urban <urbanc@in.tum.de>
parents: 588
diff changeset
   572
The function in the semantic action converts a string into an
591
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   573
\texttt{Int}. Now \texttt{parse} generates \texttt{Set((123,abc))},
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   574
but this time \texttt{123} is an \texttt{Int}. Let us come back to
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   575
semantic actions when we are going to implement actual context-free
593
bb24d4e207b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 592
diff changeset
   576
grammars.
587
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   577
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   578
\subsubsection*{Shorthand notation for parser combinators}
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   579
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   580
Before we proceed, let us just explain the shorthand notation for
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   581
parser combinators. Like for regular expressions, the shorthand notation
590
c6a1e19e9801 updated
Christian Urban <urbanc@in.tum.de>
parents: 589
diff changeset
   582
will make our life much easier when writing actual parsers. We can define
591
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   583
some implicits which allow us to write
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   584
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   585
\begin{center}
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   586
\begin{tabular}{ll}  
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   587
  \pcode{p | q} & alternative parser\\
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   588
  \pcode{p ~ q} & sequence parser\\ 
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   589
  \pcode{p ==> f} & semantic action parser
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   590
\end{tabular}
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   591
\end{center}
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   592
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   593
\noindent
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   594
as well as to use plain strings for specifying simple string parsers.
590
c6a1e19e9801 updated
Christian Urban <urbanc@in.tum.de>
parents: 589
diff changeset
   595
c6a1e19e9801 updated
Christian Urban <urbanc@in.tum.de>
parents: 589
diff changeset
   596
The idea is that this shorthand notation allows us to easily translate
c6a1e19e9801 updated
Christian Urban <urbanc@in.tum.de>
parents: 589
diff changeset
   597
context-free grammars into code. For example recall our context-free
c6a1e19e9801 updated
Christian Urban <urbanc@in.tum.de>
parents: 589
diff changeset
   598
grammar for palindromes:
c6a1e19e9801 updated
Christian Urban <urbanc@in.tum.de>
parents: 589
diff changeset
   599
c6a1e19e9801 updated
Christian Urban <urbanc@in.tum.de>
parents: 589
diff changeset
   600
\begin{plstx}[margin=3cm]
591
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   601
: \meta{Pal} ::=  a\cdot \meta{Pal}\cdot a | b\cdot \meta{Pal}\cdot b | a | b | \epsilon\\
590
c6a1e19e9801 updated
Christian Urban <urbanc@in.tum.de>
parents: 589
diff changeset
   602
\end{plstx}
c6a1e19e9801 updated
Christian Urban <urbanc@in.tum.de>
parents: 589
diff changeset
   603
c6a1e19e9801 updated
Christian Urban <urbanc@in.tum.de>
parents: 589
diff changeset
   604
\noindent
c6a1e19e9801 updated
Christian Urban <urbanc@in.tum.de>
parents: 589
diff changeset
   605
Each alternative in this grammar translates into an alternative parser
c6a1e19e9801 updated
Christian Urban <urbanc@in.tum.de>
parents: 589
diff changeset
   606
combinator.  The $\cdot$ can be translated to a sequence parser
c6a1e19e9801 updated
Christian Urban <urbanc@in.tum.de>
parents: 589
diff changeset
   607
combinator. The parsers for $a$, $b$ and $\epsilon$ can be simply
c6a1e19e9801 updated
Christian Urban <urbanc@in.tum.de>
parents: 589
diff changeset
   608
written as \texttt{"a"}, \texttt{"b"} and \texttt{""}.
c6a1e19e9801 updated
Christian Urban <urbanc@in.tum.de>
parents: 589
diff changeset
   609
587
5ddedcd92d84 updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   610
386
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 385
diff changeset
   611
\subsubsection*{How to build parsers using parser combinators?}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 385
diff changeset
   612
588
a4646557016d updated
Christian Urban <urbanc@in.tum.de>
parents: 587
diff changeset
   613
The beauty of parser combinators is the ease with which they can be
a4646557016d updated
Christian Urban <urbanc@in.tum.de>
parents: 587
diff changeset
   614
implemented and how easy it is to translate context-free grammars into
a4646557016d updated
Christian Urban <urbanc@in.tum.de>
parents: 587
diff changeset
   615
code (though the grammars need to be non-left-recursive). To
591
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   616
demonstrate this consider again the grammar for palindromes from above.
590
c6a1e19e9801 updated
Christian Urban <urbanc@in.tum.de>
parents: 589
diff changeset
   617
The first idea would be to translate it into the following code
588
a4646557016d updated
Christian Urban <urbanc@in.tum.de>
parents: 587
diff changeset
   618
a4646557016d updated
Christian Urban <urbanc@in.tum.de>
parents: 587
diff changeset
   619
\begin{center}
a4646557016d updated
Christian Urban <urbanc@in.tum.de>
parents: 587
diff changeset
   620
\begin{lstlisting}[language=Scala,basicstyle=\small\ttfamily, numbers=none]
a4646557016d updated
Christian Urban <urbanc@in.tum.de>
parents: 587
diff changeset
   621
lazy val Pal : Parser[String, String] = 
a4646557016d updated
Christian Urban <urbanc@in.tum.de>
parents: 587
diff changeset
   622
  (("a" ~ Pal ~ "a") | ("b" ~ Pal ~ "b") | "a" | "b" | "")
a4646557016d updated
Christian Urban <urbanc@in.tum.de>
parents: 587
diff changeset
   623
\end{lstlisting}
a4646557016d updated
Christian Urban <urbanc@in.tum.de>
parents: 587
diff changeset
   624
\end{center}
a4646557016d updated
Christian Urban <urbanc@in.tum.de>
parents: 587
diff changeset
   625
a4646557016d updated
Christian Urban <urbanc@in.tum.de>
parents: 587
diff changeset
   626
\noindent
590
c6a1e19e9801 updated
Christian Urban <urbanc@in.tum.de>
parents: 589
diff changeset
   627
Unfortunately, this does not quite work yet as it produces a typing
c6a1e19e9801 updated
Christian Urban <urbanc@in.tum.de>
parents: 589
diff changeset
   628
error. The reason is that the parsers \texttt{"a"}, \texttt{"b"} and
c6a1e19e9801 updated
Christian Urban <urbanc@in.tum.de>
parents: 589
diff changeset
   629
\texttt{""} all produce strings as output type and therefore can be
c6a1e19e9801 updated
Christian Urban <urbanc@in.tum.de>
parents: 589
diff changeset
   630
put into an alternative \texttt{...| "a" | "b" | ""}. But both
591
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   631
sequence parsers \pcode{"a" ~ Pal ~ "a"} and \pcode{"b" ~ Pal ~ "b"}
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   632
produce pairs of the form
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   633
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   634
\begin{center}
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   635
(((\texttt{a}-part, \texttt{Pal}-part), \texttt{a}-part), unprocessed part)
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   636
\end{center}
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   637
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   638
\noindent That is how the
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   639
sequence parser combinator nests results when \pcode{\~} is used
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   640
between two components. The solution is to use a semantic action that
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   641
``flattens'' these pairs and appends the corresponding strings, like
588
a4646557016d updated
Christian Urban <urbanc@in.tum.de>
parents: 587
diff changeset
   642
a4646557016d updated
Christian Urban <urbanc@in.tum.de>
parents: 587
diff changeset
   643
\begin{center}
a4646557016d updated
Christian Urban <urbanc@in.tum.de>
parents: 587
diff changeset
   644
\begin{lstlisting}[language=Scala,basicstyle=\small\ttfamily, numbers=none]
a4646557016d updated
Christian Urban <urbanc@in.tum.de>
parents: 587
diff changeset
   645
lazy val Pal : Parser[String, String] =  
a4646557016d updated
Christian Urban <urbanc@in.tum.de>
parents: 587
diff changeset
   646
  (("a" ~ Pal ~ "a") ==> { case ((x, y), z) => x + y + z } |
a4646557016d updated
Christian Urban <urbanc@in.tum.de>
parents: 587
diff changeset
   647
   ("b" ~ Pal ~ "b") ==> { case ((x, y), z) => x + y + z } |
a4646557016d updated
Christian Urban <urbanc@in.tum.de>
parents: 587
diff changeset
   648
    "a" | "b" | "")
a4646557016d updated
Christian Urban <urbanc@in.tum.de>
parents: 587
diff changeset
   649
\end{lstlisting}
a4646557016d updated
Christian Urban <urbanc@in.tum.de>
parents: 587
diff changeset
   650
\end{center}
a4646557016d updated
Christian Urban <urbanc@in.tum.de>
parents: 587
diff changeset
   651
589
0451b8b67f62 updated
Christian Urban <urbanc@in.tum.de>
parents: 588
diff changeset
   652
\noindent
591
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   653
How does this work? Well, recall again what the pairs look like for
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   654
the parser \pcode{"a" ~ Pal ~ "a"}.  The pattern in the semantic
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   655
action matches the nested pairs (the \texttt{x} with the
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   656
\texttt{a}-part and so on).  Unfortunately when we have such nested
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   657
pairs, Scala requires us to define the function using the
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   658
\pcode{case}-syntax
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   659
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   660
\begin{center}
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   661
\begin{lstlisting}[language=Scala,basicstyle=\small\ttfamily, numbers=none]
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   662
{ case ((x, y), z) => ... }
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   663
\end{lstlisting}
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   664
\end{center}
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   665
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   666
\noindent
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   667
If we have more sequence parser combinators or have them differently nested,
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   668
then the pattern in the semantic action needs to be adjusted accordingly.
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   669
The action we implement above is to concatenate all three strings, which
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   670
means after the semantic action is applied the output type of the parser 
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   671
is \texttt{String}, which means it fits with the alternative parsers
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   672
\texttt{...| "a" | "b" | ""}.
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   673
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   674
If we run the parser above with \pcode{Pal.parse_all("abaaaba")} we obtain
593
bb24d4e207b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 592
diff changeset
   675
as result the \pcode{Set(abaaaba)}, which indicates that the string is a palindrome
591
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   676
(an empty set would mean something is wrong). But also notice what the
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   677
intermediate results are generated by \pcode{Pal.parse("abaaaba")}
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   678
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   679
\begin{center}
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   680
\begin{lstlisting}[language=Scala,basicstyle=\small\ttfamily, numbers=none]
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   681
Set((abaaaba,""),(aba,aaba), (a,baaaba), ("",abaaaba))
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   682
\end{lstlisting}
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   683
\end{center}
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   684
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   685
\noindent
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   686
That there are more than one output might be slightly unexpected, but
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   687
can be explained as follows: the pairs represent all possible
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   688
(partial) parses of the string \pcode{"abaaaba"}. The first pair above
593
bb24d4e207b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 592
diff changeset
   689
corresponds to a complete parse (all output is consumed) and this is
591
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   690
what \pcode{Pal.parse_all} returns. The second pair is a small
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   691
``sub-palindrome'' that can also be parsed, but the parse fails with
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   692
the rest \pcode{aaba}, which is therefore left as unprocessed. The
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   693
third one is an attempt to parse the whole string with the
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   694
single-character parser \pcode{a}. That of course only partially
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   695
succeeds, by leaving \pcode{"baaaba"} as the unprocessed
593
bb24d4e207b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 592
diff changeset
   696
part. Finally, since we allow the empty string to be a palindrome we
591
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   697
also obtain the last pair, where actually nothing is consumed from the
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   698
input string. While all this works as intended, we need to be careful
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   699
with this (especially with including the \pcode{""} parser in our
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   700
grammar): if during parsing the set of parsing attempts gets too big,
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   701
then the parsing process can become very slow as the potential
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   702
candidates for applying rules can snowball.
589
0451b8b67f62 updated
Christian Urban <urbanc@in.tum.de>
parents: 588
diff changeset
   703
0451b8b67f62 updated
Christian Urban <urbanc@in.tum.de>
parents: 588
diff changeset
   704
591
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   705
Important is also to note is that we must define the
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   706
\texttt{Pal}-parser as a \emph{lazy} value in Scala. Look again at the
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   707
code: \texttt{Pal} occurs on the right-hand side of the definition. If we had
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   708
just written
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   709
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   710
\begin{center}
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   711
\begin{lstlisting}[language=Scala,basicstyle=\small\ttfamily, numbers=none]
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   712
val Pal : Parser[String, String] =  ...rhs...
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   713
\end{lstlisting}
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   714
\end{center}
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   715
589
0451b8b67f62 updated
Christian Urban <urbanc@in.tum.de>
parents: 588
diff changeset
   716
\noindent
593
bb24d4e207b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 592
diff changeset
   717
then Scala before making this assignment to \texttt{Pal} attempts to
591
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   718
find out what the expression on the right-hand side evaluates to. This
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   719
is straightforward in case of simple expressions \texttt{2 + 3}, but
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   720
the expression above contains \texttt{Pal} in the right-hand
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   721
side. Without \pcode{lazy} it would try to evaluate what \texttt{Pal}
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   722
evaluates to and start a new recursion, which means it falls into an
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   723
infinite loop. The definition of \texttt{Pal} is recursive and the
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   724
\pcode{lazy} key-word prevents it from being fully evaluated. Therefore
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   725
whenever we want to define a recursive parser we have to write
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   726
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   727
\begin{center}
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   728
\begin{lstlisting}[language=Scala,basicstyle=\small\ttfamily, numbers=none]
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   729
lazy val SomeParser : Parser[...,...] =  ...rhs...
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   730
\end{lstlisting}
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   731
\end{center}
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   732
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   733
\noindent That was not necessary for our atomic parsers, like
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   734
\texttt{RegexParser} or \texttt{CharParser}, because they are not recursive.
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   735
Note that this is also the reason why we had to write
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   736
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   737
\begin{center}
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   738
\begin{lstlisting}[language=Scala,basicstyle=\small\ttfamily, numbers=none]
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   739
class AltParser[I, T]
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   740
       (p: => Parser[I, T], 
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   741
        q: => Parser[I, T]) extends Parser[I, T] {...}
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   742
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   743
class SeqParser[I, T, S]
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   744
       (p: => Parser[I, T], 
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   745
        q: => Parser[I, S]) extends Parser[I, (T, S)] {...}
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   746
\end{lstlisting}
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   747
\end{center}
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   748
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   749
\noindent where the \texttt{\textbf{\textcolor{codepurple}{=>}}} in front of
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   750
the argument types for \texttt{p} and \texttt{q} prevent Scala from
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   751
evaluating the arguments. Normally, Scala would first evaluate what
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   752
kind of parsers \texttt{p} and \texttt{q} are, and only then generate
593
bb24d4e207b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 592
diff changeset
   753
the alternative parser combinator, respectively sequence parser
bb24d4e207b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 592
diff changeset
   754
combinator. Since the arguments can be recursive parsers, such as
591
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   755
\texttt{Pal}, this would lead again to an infinite loop.
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   756
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   757
As a final example in this section, let us consider the grammar for
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   758
well-nested parentheses:
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   759
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   760
\begin{plstx}[margin=3cm]
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   761
: \meta{P} ::=  (\cdot \meta{P}\cdot ) \cdot \meta{P} | \epsilon\\
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   762
\end{plstx}
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   763
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   764
\noindent
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   765
Let us assume we want to not just recognise strings of
593
bb24d4e207b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 592
diff changeset
   766
well-nested parentheses but also transform round parentheses
591
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   767
into curly braces. We can do this by using a semantic
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   768
action:
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   769
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   770
\begin{center}
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   771
  \begin{lstlisting}[language=Scala,basicstyle=\small\ttfamily,
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   772
    xleftmargin=0mm, numbers=none]
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   773
lazy val P : Parser[String, String] = 
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   774
  "(" ~ P ~ ")" ~ P ==> { case (((_,x),_),y) => "{" + x + "}" + y } | ""
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   775
\end{lstlisting}
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   776
\end{center}
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   777
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   778
\noindent
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   779
Here we define a function where which ignores the parentheses in the
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   780
pairs, but replaces them in the right places with curly braces when
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   781
assembling the new string in the right-hand side. If we run
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   782
\pcode{P.parse_all("(((()()))())")} we obtain
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   783
\texttt{Set(\{\{\{\{\}\{\}\}\}\{\}\})} as expected.
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   784
863e502f6a5c updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   785
588
a4646557016d updated
Christian Urban <urbanc@in.tum.de>
parents: 587
diff changeset
   786
386
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 385
diff changeset
   787
\subsubsection*{Implementing an Interpreter}
183
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   788
593
bb24d4e207b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 592
diff changeset
   789
The first step before implementing an interpreter for a full-blown
592
0d309fafa9f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 591
diff changeset
   790
language is to implement a simple calculator for arithmetic
0d309fafa9f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 591
diff changeset
   791
expressions. Suppose our arithmetic expressions are given by the
0d309fafa9f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 591
diff changeset
   792
grammar:
0d309fafa9f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 591
diff changeset
   793
0d309fafa9f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 591
diff changeset
   794
\begin{plstx}[margin=3cm,one per line]
593
bb24d4e207b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 592
diff changeset
   795
: \meta{E} ::= \meta{E} \cdot + \cdot \meta{E} 
592
0d309fafa9f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 591
diff changeset
   796
   | \meta{E} \cdot - \cdot \meta{E} 
0d309fafa9f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 591
diff changeset
   797
   | \meta{E} \cdot * \cdot \meta{E} 
0d309fafa9f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 591
diff changeset
   798
   | ( \cdot \meta{E} \cdot )
0d309fafa9f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 591
diff changeset
   799
   | Number \\
0d309fafa9f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 591
diff changeset
   800
\end{plstx}
0d309fafa9f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 591
diff changeset
   801
0d309fafa9f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 591
diff changeset
   802
\noindent
0d309fafa9f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 591
diff changeset
   803
Naturally we want to implement the grammar in such a way that we can
593
bb24d4e207b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 592
diff changeset
   804
calculate what the result of, for example, \texttt{4*2+3} is---we are
bb24d4e207b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 592
diff changeset
   805
interested in an \texttt{Int} rather than a string. This means every
bb24d4e207b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 592
diff changeset
   806
component parser needs to have as output type \texttt{Int} and when we
bb24d4e207b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 592
diff changeset
   807
assemble the intermediate results, strings like \texttt{"+"},
bb24d4e207b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 592
diff changeset
   808
\texttt{"*"} and so on, need to be translated into the appropriate
bb24d4e207b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 592
diff changeset
   809
Scala operation of adding, multiplying and so on.  Being inspired by
bb24d4e207b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 592
diff changeset
   810
the parser for well-nested parentheses above and ignoring the fact
bb24d4e207b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 592
diff changeset
   811
that we want $*$ to take precedence over $+$ and $-$, we might want to
bb24d4e207b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 592
diff changeset
   812
write something like
592
0d309fafa9f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 591
diff changeset
   813
0d309fafa9f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 591
diff changeset
   814
\begin{center}
0d309fafa9f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 591
diff changeset
   815
\begin{lstlisting}[language=Scala,basicstyle=\small\ttfamily, numbers=none]
0d309fafa9f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 591
diff changeset
   816
lazy val E: Parser[String, Int] = 
0d309fafa9f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 591
diff changeset
   817
  (E ~ "+" ~ E ==> { case ((x, y), z) => x + z} |
0d309fafa9f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 591
diff changeset
   818
   E ~ "-" ~ E ==> { case ((x, y), z) => x - z} |
0d309fafa9f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 591
diff changeset
   819
   E ~ "*" ~ E ==> { case ((x, y), z) => x * z} |
0d309fafa9f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 591
diff changeset
   820
   "(" ~ E ~ ")" ==> { case ((x, y), z) => y} |
0d309fafa9f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 591
diff changeset
   821
   NumParserInt)
0d309fafa9f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 591
diff changeset
   822
\end{lstlisting}
0d309fafa9f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 591
diff changeset
   823
\end{center}
0d309fafa9f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 591
diff changeset
   824
0d309fafa9f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 591
diff changeset
   825
\noindent
593
bb24d4e207b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 592
diff changeset
   826
Consider again carefully how the semantic actions pick out the correct
bb24d4e207b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 592
diff changeset
   827
arguments for the calculation. In case of plus, we need \texttt{x} and
bb24d4e207b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 592
diff changeset
   828
\texttt{z}, because they correspond to the results of the component
bb24d4e207b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 592
diff changeset
   829
parser \texttt{E}. We can just add \texttt{x + z} in order to obtain
bb24d4e207b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 592
diff changeset
   830
an \texttt{Int} because the output type of \texttt{E} is
bb24d4e207b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 592
diff changeset
   831
\texttt{Int}.  Similarly with subtraction and multiplication. In
bb24d4e207b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 592
diff changeset
   832
contrast in the fourth clause we need to return \texttt{y}, because it
bb24d4e207b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 592
diff changeset
   833
is the result enclosed inside the parentheses. The information about
bb24d4e207b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 592
diff changeset
   834
parentheses, roughly speaking, we just throw away.
592
0d309fafa9f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 591
diff changeset
   835
0d309fafa9f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 591
diff changeset
   836
So far so good. The problem arises when we try to call \pcode{parse_all} with the
0d309fafa9f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 591
diff changeset
   837
expression \texttt{"1+2+3"}. Lets try it
0d309fafa9f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 591
diff changeset
   838
0d309fafa9f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 591
diff changeset
   839
\begin{center}
0d309fafa9f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 591
diff changeset
   840
\begin{lstlisting}[language=Scala,basicstyle=\small\ttfamily, numbers=none]
0d309fafa9f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 591
diff changeset
   841
E.parse_all("1+2+3")
0d309fafa9f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 591
diff changeset
   842
\end{lstlisting}
0d309fafa9f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 591
diff changeset
   843
\end{center}
0d309fafa9f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 591
diff changeset
   844
0d309fafa9f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 591
diff changeset
   845
\noindent
593
bb24d4e207b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 592
diff changeset
   846
\ldots and we wait and wait and \ldots still wait. What is the
bb24d4e207b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 592
diff changeset
   847
problem? Actually, the parser just fell into an infinite loop! The
bb24d4e207b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 592
diff changeset
   848
reason is that the above grammar is left-recursive and recall that our
bb24d4e207b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 592
diff changeset
   849
parser combinators cannot deal with such left-recursive
bb24d4e207b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 592
diff changeset
   850
grammars. Fortunately, every left-recursive context-free grammar can be
bb24d4e207b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 592
diff changeset
   851
transformed into a non-left-recursive grammars that still recognises
bb24d4e207b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 592
diff changeset
   852
the same strings. This allows us to design the following grammar
bb24d4e207b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 592
diff changeset
   853
bb24d4e207b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 592
diff changeset
   854
\begin{plstx}[margin=3cm]
bb24d4e207b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 592
diff changeset
   855
  : \meta{E} ::=  \meta{T} \cdot + \cdot \meta{E} |  \meta{T} \cdot - \cdot \meta{E} | \meta{T}\\
bb24d4e207b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 592
diff changeset
   856
: \meta{T} ::=  \meta{F} \cdot * \cdot \meta{T} | \meta{F}\\
bb24d4e207b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 592
diff changeset
   857
: \meta{F} ::= ( \cdot \meta{E} \cdot ) | Number\\
bb24d4e207b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 592
diff changeset
   858
\end{plstx}
bb24d4e207b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 592
diff changeset
   859
bb24d4e207b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 592
diff changeset
   860
\noindent
bb24d4e207b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 592
diff changeset
   861
Recall what left-recursive means from Handout 5 and make sure you see
bb24d4e207b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 592
diff changeset
   862
why this grammar is \emph{non} left-recursive. This version of the grammar
bb24d4e207b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 592
diff changeset
   863
also deals with the fact that $*$ should have a higher precedence. This does not
bb24d4e207b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 592
diff changeset
   864
affect which strings this grammar can recognise, but in which order we are going
bb24d4e207b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 592
diff changeset
   865
to evaluate any arithmetic expression. We can translate this grammar into
bb24d4e207b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 592
diff changeset
   866
parsing combinators as follows:
592
0d309fafa9f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 591
diff changeset
   867
0d309fafa9f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 591
diff changeset
   868
593
bb24d4e207b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 592
diff changeset
   869
\begin{center}
bb24d4e207b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 592
diff changeset
   870
\begin{lstlisting}[language=Scala,basicstyle=\small\ttfamily, numbers=none]
bb24d4e207b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 592
diff changeset
   871
lazy val E: Parser[String, Int] = 
bb24d4e207b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 592
diff changeset
   872
  (T ~ "+" ~ E) ==> { case ((x, y), z) => x + z } |
bb24d4e207b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 592
diff changeset
   873
  (T ~ "-" ~ E) ==> { case ((x, y), z) => x - z } | T 
bb24d4e207b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 592
diff changeset
   874
lazy val T: Parser[String, Int] = 
bb24d4e207b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 592
diff changeset
   875
  (F ~ "*" ~ T) ==> { case ((x, y), z) => x * z } | F
bb24d4e207b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 592
diff changeset
   876
lazy val F: Parser[String, Int] = 
bb24d4e207b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 592
diff changeset
   877
  ("(" ~ E ~ ")") ==> { case ((x, y), z) => y } | NumParserInt
bb24d4e207b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 592
diff changeset
   878
\end{lstlisting}
bb24d4e207b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 592
diff changeset
   879
\end{center}
592
0d309fafa9f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 591
diff changeset
   880
593
bb24d4e207b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 592
diff changeset
   881
\noindent
594
d40d7d7b85bc updated
Christian Urban <urbanc@in.tum.de>
parents: 593
diff changeset
   882
Let us try out some examples:
592
0d309fafa9f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 591
diff changeset
   883
593
bb24d4e207b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 592
diff changeset
   884
\begin{center}
bb24d4e207b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 592
diff changeset
   885
\begin{tabular}{rcl}
bb24d4e207b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 592
diff changeset
   886
  input strings & & output of \pcode{parse_all}\medskip\\
bb24d4e207b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 592
diff changeset
   887
  \texttt{\Grid{1+2+3}} & $\rightarrow$ & \texttt{Set(6)}\\
bb24d4e207b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 592
diff changeset
   888
  \texttt{\Grid{4*2+3}} & $\rightarrow$ & \texttt{Set(11)}\\
bb24d4e207b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 592
diff changeset
   889
  \texttt{\Grid{4*(2+3)}} & $\rightarrow$ & \texttt{Set(20)}\\
594
d40d7d7b85bc updated
Christian Urban <urbanc@in.tum.de>
parents: 593
diff changeset
   890
  \texttt{\Grid{(4)*((2+3))}} & $\rightarrow$ & \texttt{Set(20)}\\
593
bb24d4e207b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 592
diff changeset
   891
  \texttt{\Grid{4/2+3}} & $\rightarrow$ & \texttt{Set()}\\
bb24d4e207b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 592
diff changeset
   892
  \texttt{\Grid{1\VS +\VS 2\VS +\VS 3}} & $\rightarrow$ & \texttt{Set()}\\                      
bb24d4e207b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 592
diff changeset
   893
\end{tabular}
bb24d4e207b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 592
diff changeset
   894
\end{center}
183
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   895
593
bb24d4e207b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 592
diff changeset
   896
\noindent
594
d40d7d7b85bc updated
Christian Urban <urbanc@in.tum.de>
parents: 593
diff changeset
   897
Note that we call \pcode{parse_all}, not \pcode{parse}.  The examples
d40d7d7b85bc updated
Christian Urban <urbanc@in.tum.de>
parents: 593
diff changeset
   898
should be quite self-explanatory. The last two example do not produce
d40d7d7b85bc updated
Christian Urban <urbanc@in.tum.de>
parents: 593
diff changeset
   899
any integer result because our parser does not define what to do in
d40d7d7b85bc updated
Christian Urban <urbanc@in.tum.de>
parents: 593
diff changeset
   900
case of division (could be easily added), but also has no idea what to
d40d7d7b85bc updated
Christian Urban <urbanc@in.tum.de>
parents: 593
diff changeset
   901
do with whitescpaces. To deal with them is the task of the lexer! Yes,
d40d7d7b85bc updated
Christian Urban <urbanc@in.tum.de>
parents: 593
diff changeset
   902
we can deal with them inside the grammar, but that would render many
d40d7d7b85bc updated
Christian Urban <urbanc@in.tum.de>
parents: 593
diff changeset
   903
grammars becoming unintelligible, including this one.\footnote{If you
d40d7d7b85bc updated
Christian Urban <urbanc@in.tum.de>
parents: 593
diff changeset
   904
  think an easy solution is to extend the notion of what a number
d40d7d7b85bc updated
Christian Urban <urbanc@in.tum.de>
parents: 593
diff changeset
   905
  should be, then think again---you still would have to deal with
d40d7d7b85bc updated
Christian Urban <urbanc@in.tum.de>
parents: 593
diff changeset
   906
  cases like \texttt{\Grid{(\VS (\VS 2+3)\VS )}}. Jusat think you have
d40d7d7b85bc updated
Christian Urban <urbanc@in.tum.de>
parents: 593
diff changeset
   907
  a grammar for a full-blown language where there are numerous such cases.}
173
7cfb7a6f7c99 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   908
7cfb7a6f7c99 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   909
\end{document}
7cfb7a6f7c99 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   910
7cfb7a6f7c99 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   911
%%% Local Variables: 
7cfb7a6f7c99 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   912
%%% mode: latex  
7cfb7a6f7c99 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   913
%%% TeX-master: t
7cfb7a6f7c99 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   914
%%% End: