handouts/ho05.tex
author Christian Urban <christian.urban@kcl.ac.uk>
Sat, 08 Nov 2025 09:51:49 +0000
changeset 1022 53889239df4a
parent 1021 5990e03b2ba8
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
665
81bb2db5e658 updated
Christian Urban <urbanc@in.tum.de>
parents: 618
diff changeset
     1
% !TEX program = xelatex
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}
459
858f62bc930a updated
Christian Urban <urbanc@in.tum.de>
parents: 385
diff changeset
     5
\usepackage{../grammar}
798
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
     6
\usepackage{../graphics}
173
7cfb7a6f7c99 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     7
545
56e054d84be2 updated
Christian Urban <urbanc@in.tum.de>
parents: 459
diff changeset
     8
% epsilon and left-recursion elimination
56e054d84be2 updated
Christian Urban <urbanc@in.tum.de>
parents: 459
diff changeset
     9
% http://www.mollypages.org/page/grammar/index.mp
56e054d84be2 updated
Christian Urban <urbanc@in.tum.de>
parents: 459
diff changeset
    10
618
1c7cca56fadf updated
Christian Urban <urbanc@in.tum.de>
parents: 582
diff changeset
    11
%% parsing scala files
691
6d32ba9280bf updated
Christian Urban <urbanc@in.tum.de>
parents: 686
diff changeset
    12
%% https://scalameta.org/
6d32ba9280bf updated
Christian Urban <urbanc@in.tum.de>
parents: 686
diff changeset
    13
6d32ba9280bf updated
Christian Urban <urbanc@in.tum.de>
parents: 686
diff changeset
    14
% chomsky normal form transformation
6d32ba9280bf updated
Christian Urban <urbanc@in.tum.de>
parents: 686
diff changeset
    15
% https://youtu.be/FNPSlnj3Vt0
618
1c7cca56fadf updated
Christian Urban <urbanc@in.tum.de>
parents: 582
diff changeset
    16
710
ba35058db273 updated
Christian Urban <urbanc@in.tum.de>
parents: 691
diff changeset
    17
% Language hierachy is about complexity
ba35058db273 updated
Christian Urban <urbanc@in.tum.de>
parents: 691
diff changeset
    18
%    How hard is it to recognise an element in a language
ba35058db273 updated
Christian Urban <urbanc@in.tum.de>
parents: 691
diff changeset
    19
722
7c09b7eadc6b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 710
diff changeset
    20
% Pratt parsing
7c09b7eadc6b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 710
diff changeset
    21
% https://matklad.github.io/2020/04/13/simple-but-powerful-pratt-parsing.html
7c09b7eadc6b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 710
diff changeset
    22
% https://www.oilshell.org/blog/2017/03/31.html
7c09b7eadc6b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 710
diff changeset
    23
173
7cfb7a6f7c99 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    24
\begin{document}
1021
5990e03b2ba8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 940
diff changeset
    25
\fnote{\copyright{} Christian Urban, King's College London, 2023, 2025}
173
7cfb7a6f7c99 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    26
385
7f8516ff408d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 362
diff changeset
    27
\section*{Handout 5 (Grammars \& Parser)}
173
7cfb7a6f7c99 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    28
727
a526f7f20cad updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 722
diff changeset
    29
So far we have focused on regular expressions as well as matching and
a526f7f20cad updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 722
diff changeset
    30
lexing algorithms. While regular expressions are very useful for lexing
a526f7f20cad updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 722
diff changeset
    31
and for recognising many patterns in strings (like email addresses),
a526f7f20cad updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 722
diff changeset
    32
they have their limitations. For example there is no regular expression
a526f7f20cad updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 722
diff changeset
    33
that can recognise the language $a^nb^n$ (where you have strings
a526f7f20cad updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 722
diff changeset
    34
starting with $n$ $a$'s followed by the same amount of $b$'s). Another
a526f7f20cad updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 722
diff changeset
    35
example for which there exists no regular expression is the language of
a526f7f20cad updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 722
diff changeset
    36
well-parenthesised expressions. In languages like Lisp, which use
a526f7f20cad updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 722
diff changeset
    37
parentheses rather extensively, it might be of interest to know whether
a526f7f20cad updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 722
diff changeset
    38
the following two expressions are well-parenthesised or not (the left
a526f7f20cad updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 722
diff changeset
    39
one is, the right one is not):
173
7cfb7a6f7c99 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    40
7cfb7a6f7c99 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    41
\begin{center}
7cfb7a6f7c99 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    42
$(((()()))())$  \hspace{10mm} $(((()()))()))$
7cfb7a6f7c99 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    43
\end{center}
7cfb7a6f7c99 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    44
362
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
    45
\noindent Not being able to solve such recognition problems is
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
    46
a serious limitation. In order to solve such recognition
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
    47
problems, we need more powerful techniques than regular
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
    48
expressions. We will in particular look at \emph{context-free
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
    49
languages}. They include the regular languages as the picture
582
cd7a08469b3c updated
Christian Urban <urbanc@in.tum.de>
parents: 545
diff changeset
    50
below about language classes shows:
173
7cfb7a6f7c99 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    51
7cfb7a6f7c99 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    52
7cfb7a6f7c99 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    53
\begin{center}
7cfb7a6f7c99 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    54
\begin{tikzpicture}
297
5c51839c88fd updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    55
[rect/.style={draw=black!50, 
5c51839c88fd updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    56
 top color=white,bottom color=black!20, 
5c51839c88fd updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    57
 rectangle, very thick, rounded corners}]
173
7cfb7a6f7c99 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    58
297
5c51839c88fd updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    59
\draw (0,0) node [rect, text depth=30mm, text width=46mm] {\small all languages};
5c51839c88fd updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    60
\draw (0,-0.4) node [rect, text depth=20mm, text width=44mm] {\small decidable languages};
5c51839c88fd updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    61
\draw (0,-0.65) node [rect, text depth=13mm] {\small context sensitive languages};
5c51839c88fd updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    62
\draw (0,-0.84) node [rect, text depth=7mm, text width=35mm] {\small context-free languages};
5c51839c88fd updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    63
\draw (0,-1.05) node [rect] {\small regular languages};
173
7cfb7a6f7c99 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    64
\end{tikzpicture}
7cfb7a6f7c99 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    65
\end{center}
7cfb7a6f7c99 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    66
582
cd7a08469b3c updated
Christian Urban <urbanc@in.tum.de>
parents: 545
diff changeset
    67
\noindent Each ``bubble'' stands for sets of languages (remember
cd7a08469b3c updated
Christian Urban <urbanc@in.tum.de>
parents: 545
diff changeset
    68
languages are sets of strings). As indicated the set of regular
680
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
    69
languages is fully included inside the context-free languages,
582
cd7a08469b3c updated
Christian Urban <urbanc@in.tum.de>
parents: 545
diff changeset
    70
meaning every regular language is also context-free, but not vice
665
81bb2db5e658 updated
Christian Urban <urbanc@in.tum.de>
parents: 618
diff changeset
    71
versa. Below I will let you think, for example, what the context-free
582
cd7a08469b3c updated
Christian Urban <urbanc@in.tum.de>
parents: 545
diff changeset
    72
grammar is for the language corresponding to the regular expression
cd7a08469b3c updated
Christian Urban <urbanc@in.tum.de>
parents: 545
diff changeset
    73
$(aaa)^*a$.
cd7a08469b3c updated
Christian Urban <urbanc@in.tum.de>
parents: 545
diff changeset
    74
cd7a08469b3c updated
Christian Urban <urbanc@in.tum.de>
parents: 545
diff changeset
    75
Because of their convenience, context-free languages play an important
cd7a08469b3c updated
Christian Urban <urbanc@in.tum.de>
parents: 545
diff changeset
    76
role in `day-to-day' text processing and in programming
cd7a08469b3c updated
Christian Urban <urbanc@in.tum.de>
parents: 545
diff changeset
    77
languages. Context-free in this setting means that ``words'' have one
680
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
    78
meaning only and this meaning is independent from the context
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
    79
the ``words'' appear in. For example ambiguity issues like
582
cd7a08469b3c updated
Christian Urban <urbanc@in.tum.de>
parents: 545
diff changeset
    80
cd7a08469b3c updated
Christian Urban <urbanc@in.tum.de>
parents: 545
diff changeset
    81
\begin{center}
682
612976492d25 updated
Christian Urban <urbanc@in.tum.de>
parents: 681
diff changeset
    82
\tt Time flies like an arrow. Fruit flies like bananas.
582
cd7a08469b3c updated
Christian Urban <urbanc@in.tum.de>
parents: 545
diff changeset
    83
\end{center}  
cd7a08469b3c updated
Christian Urban <urbanc@in.tum.de>
parents: 545
diff changeset
    84
cd7a08469b3c updated
Christian Urban <urbanc@in.tum.de>
parents: 545
diff changeset
    85
\noindent
940
1c1fbf45a03c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 936
diff changeset
    86
from natural languages where the meaning of \emph{flies} depends on the
686
5fe95ea0bad0 updated
Christian Urban <urbanc@in.tum.de>
parents: 682
diff changeset
    87
surrounding \emph{context} are avoided as much as possible. Here is
936
aabd9168c7ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 798
diff changeset
    88
an interesting video about C++ not being a context-free language
686
5fe95ea0bad0 updated
Christian Urban <urbanc@in.tum.de>
parents: 682
diff changeset
    89
5fe95ea0bad0 updated
Christian Urban <urbanc@in.tum.de>
parents: 682
diff changeset
    90
\begin{center}
5fe95ea0bad0 updated
Christian Urban <urbanc@in.tum.de>
parents: 682
diff changeset
    91
\url{https://www.youtube.com/watch?v=OzK8pUu4UfM}
5fe95ea0bad0 updated
Christian Urban <urbanc@in.tum.de>
parents: 682
diff changeset
    92
\end{center}  
582
cd7a08469b3c updated
Christian Urban <urbanc@in.tum.de>
parents: 545
diff changeset
    93
cd7a08469b3c updated
Christian Urban <urbanc@in.tum.de>
parents: 545
diff changeset
    94
Context-free languages are usually specified by grammars. For example
cd7a08469b3c updated
Christian Urban <urbanc@in.tum.de>
parents: 545
diff changeset
    95
a grammar for well-parenthesised expressions can be given as follows:
173
7cfb7a6f7c99 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    96
459
858f62bc930a updated
Christian Urban <urbanc@in.tum.de>
parents: 385
diff changeset
    97
\begin{plstx}[margin=3cm]
858f62bc930a updated
Christian Urban <urbanc@in.tum.de>
parents: 385
diff changeset
    98
: \meta{P} ::=  ( \cdot  \meta{P} \cdot ) \cdot \meta{P}
858f62bc930a updated
Christian Urban <urbanc@in.tum.de>
parents: 385
diff changeset
    99
             | \epsilon\\ 
858f62bc930a updated
Christian Urban <urbanc@in.tum.de>
parents: 385
diff changeset
   100
\end{plstx}
858f62bc930a updated
Christian Urban <urbanc@in.tum.de>
parents: 385
diff changeset
   101
362
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
   102
\noindent 
936
aabd9168c7ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 798
diff changeset
   103
or a grammar for recognising strings consisting of ones (at least one) is
362
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
   104
459
858f62bc930a updated
Christian Urban <urbanc@in.tum.de>
parents: 385
diff changeset
   105
\begin{plstx}[margin=3cm]
858f62bc930a updated
Christian Urban <urbanc@in.tum.de>
parents: 385
diff changeset
   106
: \meta{O} ::= 1 \cdot  \meta{O} 
858f62bc930a updated
Christian Urban <urbanc@in.tum.de>
parents: 385
diff changeset
   107
             | 1\\
858f62bc930a updated
Christian Urban <urbanc@in.tum.de>
parents: 385
diff changeset
   108
\end{plstx}
362
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
   109
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
   110
In general grammars consist of finitely many rules built up
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
   111
from \emph{terminal symbols} (usually lower-case letters) and
582
cd7a08469b3c updated
Christian Urban <urbanc@in.tum.de>
parents: 545
diff changeset
   112
\emph{non-terminal symbols} (upper-case letters written in
cd7a08469b3c updated
Christian Urban <urbanc@in.tum.de>
parents: 545
diff changeset
   113
bold like \meta{A}, \meta{N} and so on). Rules have
362
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
   114
the shape
173
7cfb7a6f7c99 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   115
459
858f62bc930a updated
Christian Urban <urbanc@in.tum.de>
parents: 385
diff changeset
   116
\begin{plstx}[margin=3cm]
858f62bc930a updated
Christian Urban <urbanc@in.tum.de>
parents: 385
diff changeset
   117
: \meta{NT} ::= rhs\\
858f62bc930a updated
Christian Urban <urbanc@in.tum.de>
parents: 385
diff changeset
   118
\end{plstx}
173
7cfb7a6f7c99 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   119
 
362
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
   120
\noindent where on the left-hand side is a single non-terminal
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
   121
and on the right a string consisting of both terminals and
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
   122
non-terminals including the $\epsilon$-symbol for indicating
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
   123
the empty string. We use the convention to separate components
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
   124
on the right hand-side by using the $\cdot$ symbol, as in the
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
   125
grammar for well-parenthesised expressions. We also use the
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
   126
convention to use $|$ as a shorthand notation for several
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
   127
rules. For example
173
7cfb7a6f7c99 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   128
459
858f62bc930a updated
Christian Urban <urbanc@in.tum.de>
parents: 385
diff changeset
   129
\begin{plstx}[margin=3cm]
858f62bc930a updated
Christian Urban <urbanc@in.tum.de>
parents: 385
diff changeset
   130
: \meta{NT} ::= rhs_1
858f62bc930a updated
Christian Urban <urbanc@in.tum.de>
parents: 385
diff changeset
   131
   | rhs_2\\
858f62bc930a updated
Christian Urban <urbanc@in.tum.de>
parents: 385
diff changeset
   132
\end{plstx}
173
7cfb7a6f7c99 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   133
459
858f62bc930a updated
Christian Urban <urbanc@in.tum.de>
parents: 385
diff changeset
   134
\noindent means that the non-terminal \meta{NT} can be replaced by
362
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
   135
either $\textit{rhs}_1$ or $\textit{rhs}_2$. If there are more
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
   136
than one non-terminal on the left-hand side of the rules, then
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
   137
we need to indicate what is the \emph{starting} symbol of the
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
   138
grammar. For example the grammar for arithmetic expressions
173
7cfb7a6f7c99 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   139
can be given as follows
7cfb7a6f7c99 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   140
459
858f62bc930a updated
Christian Urban <urbanc@in.tum.de>
parents: 385
diff changeset
   141
\begin{plstx}[margin=3cm,one per line]
858f62bc930a updated
Christian Urban <urbanc@in.tum.de>
parents: 385
diff changeset
   142
\mbox{\rm (1)}: \meta{E} ::= \meta{N}\\
858f62bc930a updated
Christian Urban <urbanc@in.tum.de>
parents: 385
diff changeset
   143
\mbox{\rm (2)}: \meta{E} ::= \meta{E} \cdot + \cdot \meta{E}\\
858f62bc930a updated
Christian Urban <urbanc@in.tum.de>
parents: 385
diff changeset
   144
\mbox{\rm (3)}: \meta{E} ::= \meta{E} \cdot - \cdot \meta{E}\\
858f62bc930a updated
Christian Urban <urbanc@in.tum.de>
parents: 385
diff changeset
   145
\mbox{\rm (4)}: \meta{E} ::= \meta{E} \cdot * \cdot \meta{E}\\
858f62bc930a updated
Christian Urban <urbanc@in.tum.de>
parents: 385
diff changeset
   146
\mbox{\rm (5)}: \meta{E} ::= ( \cdot \meta{E} \cdot )\\
858f62bc930a updated
Christian Urban <urbanc@in.tum.de>
parents: 385
diff changeset
   147
\mbox{\rm (6\ldots)}: \meta{N} ::= \meta{N} \cdot \meta{N} 
858f62bc930a updated
Christian Urban <urbanc@in.tum.de>
parents: 385
diff changeset
   148
  \mid 0 \mid 1 \mid \ldots \mid 9\\
858f62bc930a updated
Christian Urban <urbanc@in.tum.de>
parents: 385
diff changeset
   149
\end{plstx}
173
7cfb7a6f7c99 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   150
459
858f62bc930a updated
Christian Urban <urbanc@in.tum.de>
parents: 385
diff changeset
   151
\noindent where \meta{E} is the starting symbol. A
362
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
   152
\emph{derivation} for a grammar starts with the starting
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
   153
symbol of the grammar and in each step replaces one
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
   154
non-terminal by a right-hand side of a rule. A derivation ends
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
   155
with a string in which only terminal symbols are left. For
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
   156
example a derivation for the string $(1 + 2) + 3$ is as
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
   157
follows:
175
5801e8c0e528 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 173
diff changeset
   158
5801e8c0e528 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 173
diff changeset
   159
\begin{center}
362
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
   160
\begin{tabular}{lll@{\hspace{2cm}}l}
459
858f62bc930a updated
Christian Urban <urbanc@in.tum.de>
parents: 385
diff changeset
   161
\meta{E} & $\rightarrow$ & $\meta{E}+\meta{E}$          & by (2)\\
858f62bc930a updated
Christian Urban <urbanc@in.tum.de>
parents: 385
diff changeset
   162
       & $\rightarrow$ & $(\meta{E})+\meta{E}$     & by (5)\\
858f62bc930a updated
Christian Urban <urbanc@in.tum.de>
parents: 385
diff changeset
   163
       & $\rightarrow$ & $(\meta{E}+\meta{E})+\meta{E}$   & by (2)\\
858f62bc930a updated
Christian Urban <urbanc@in.tum.de>
parents: 385
diff changeset
   164
       & $\rightarrow$ & $(\meta{E}+\meta{E})+\meta{N}$   & by (1)\\
858f62bc930a updated
Christian Urban <urbanc@in.tum.de>
parents: 385
diff changeset
   165
       & $\rightarrow$ & $(\meta{E}+\meta{E})+3$   & by (6\dots)\\
858f62bc930a updated
Christian Urban <urbanc@in.tum.de>
parents: 385
diff changeset
   166
       & $\rightarrow$ & $(\meta{N}+\meta{E})+3$   & by (1)\\
362
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
   167
       & $\rightarrow^+$ & $(1+2)+3$ & by (1, 6\ldots)\\
175
5801e8c0e528 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 173
diff changeset
   168
\end{tabular} 
5801e8c0e528 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 173
diff changeset
   169
\end{center}
5801e8c0e528 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 173
diff changeset
   170
362
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
   171
\noindent where on the right it is indicated which 
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
   172
grammar rule has been applied. In the last step we
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
   173
merged several steps into one.
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
   174
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
   175
The \emph{language} of a context-free grammar $G$
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
   176
with start symbol $S$ is defined as the set of strings
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
   177
derivable by a derivation, that is
175
5801e8c0e528 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 173
diff changeset
   178
5801e8c0e528 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 173
diff changeset
   179
\begin{center}
5801e8c0e528 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 173
diff changeset
   180
$\{c_1\ldots c_n \;|\; S \rightarrow^* c_1\ldots c_n \;\;\text{with all} \; c_i \;\text{being non-terminals}\}$
5801e8c0e528 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 173
diff changeset
   181
\end{center}
5801e8c0e528 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 173
diff changeset
   182
5801e8c0e528 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 173
diff changeset
   183
\noindent
680
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   184
A \emph{parse-tree} encodes how a string is derived with the starting
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   185
symbol on top and each non-terminal containing a subtree for how it is
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   186
replaced in a derivation. The parse tree for the string $(1 + 23)+4$ is
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   187
as follows:
175
5801e8c0e528 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 173
diff changeset
   188
5801e8c0e528 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 173
diff changeset
   189
\begin{center}
5801e8c0e528 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 173
diff changeset
   190
\begin{tikzpicture}[level distance=8mm, black]
665
81bb2db5e658 updated
Christian Urban <urbanc@in.tum.de>
parents: 618
diff changeset
   191
  \node {\meta{E}}
81bb2db5e658 updated
Christian Urban <urbanc@in.tum.de>
parents: 618
diff changeset
   192
    child {node {\meta{E} } 
175
5801e8c0e528 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 173
diff changeset
   193
       child {node {$($}}
665
81bb2db5e658 updated
Christian Urban <urbanc@in.tum.de>
parents: 618
diff changeset
   194
       child {node {\meta{E} }       
81bb2db5e658 updated
Christian Urban <urbanc@in.tum.de>
parents: 618
diff changeset
   195
         child {node {\meta{E} } child {node {\meta{N} } child {node {$1$}}}}
175
5801e8c0e528 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 173
diff changeset
   196
         child {node {$+$}}
665
81bb2db5e658 updated
Christian Urban <urbanc@in.tum.de>
parents: 618
diff changeset
   197
         child {node {\meta{E} } 
81bb2db5e658 updated
Christian Urban <urbanc@in.tum.de>
parents: 618
diff changeset
   198
            child {node {\meta{N} } child {node {$2$}}}
81bb2db5e658 updated
Christian Urban <urbanc@in.tum.de>
parents: 618
diff changeset
   199
            child {node {\meta{N} } child {node {$3$}}}
175
5801e8c0e528 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 173
diff changeset
   200
            } 
5801e8c0e528 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 173
diff changeset
   201
        }
5801e8c0e528 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 173
diff changeset
   202
       child {node {$)$}}
5801e8c0e528 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 173
diff changeset
   203
     }
5801e8c0e528 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 173
diff changeset
   204
     child {node {$+$}}
665
81bb2db5e658 updated
Christian Urban <urbanc@in.tum.de>
parents: 618
diff changeset
   205
     child {node {\meta{E} }
81bb2db5e658 updated
Christian Urban <urbanc@in.tum.de>
parents: 618
diff changeset
   206
        child {node {\meta{N} } child {node {$4$}}}
175
5801e8c0e528 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 173
diff changeset
   207
     };
5801e8c0e528 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 173
diff changeset
   208
\end{tikzpicture}
5801e8c0e528 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 173
diff changeset
   209
\end{center}
5801e8c0e528 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 173
diff changeset
   210
362
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
   211
\noindent We are often interested in these parse-trees since
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
   212
they encode the structure of how a string is derived by a
680
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   213
grammar. 
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   214
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   215
Before we come to the problem of constructing such parse-trees, we need
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   216
to consider the following two properties of grammars. A grammar is
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   217
\emph{left-recursive} if there is a derivation starting from a
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   218
non-terminal, say \meta{NT} which leads to a string which again starts
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   219
with \meta{NT}. This means a derivation of the form.
173
7cfb7a6f7c99 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   220
175
5801e8c0e528 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 173
diff changeset
   221
\begin{center}
665
81bb2db5e658 updated
Christian Urban <urbanc@in.tum.de>
parents: 618
diff changeset
   222
$\meta{NT} \rightarrow \ldots \rightarrow \meta{NT} \cdot \ldots$
175
5801e8c0e528 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 173
diff changeset
   223
\end{center}
5801e8c0e528 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 173
diff changeset
   224
680
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   225
\noindent It can be easily seen that the grammar above for arithmetic
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   226
expressions is left-recursive: for example the rules $\meta{E}
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   227
\rightarrow \meta{E}\cdot + \cdot \meta{E}$ and $\meta{N} \rightarrow
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   228
\meta{N}\cdot \meta{N}$ show that this grammar is left-recursive. But
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   229
note that left-recursiveness can involve more than one step in the
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   230
derivation. The problem with left-recursive grammars is that some
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   231
algorithms cannot cope with them: with left-recursive grammars they will
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   232
fall into a loop. Fortunately every left-recursive grammar can be
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   233
transformed into one that is not left-recursive, although this
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   234
transformation might make the grammar less ``human-readable''. For
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   235
example if we want to give a non-left-recursive grammar for numbers we
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   236
might specify
175
5801e8c0e528 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 173
diff changeset
   237
5801e8c0e528 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 173
diff changeset
   238
\begin{center}
665
81bb2db5e658 updated
Christian Urban <urbanc@in.tum.de>
parents: 618
diff changeset
   239
$\meta{N} \;\;\rightarrow\;\; 0\;|\;\ldots\;|\;9\;|\;
81bb2db5e658 updated
Christian Urban <urbanc@in.tum.de>
parents: 618
diff changeset
   240
1\cdot \meta{N}\;|\;2\cdot \meta{N}\;|\;\ldots\;|\;9\cdot \meta{N}$
175
5801e8c0e528 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 173
diff changeset
   241
\end{center}
5801e8c0e528 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 173
diff changeset
   242
362
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
   243
\noindent Using this grammar we can still derive every number
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
   244
string, but we will never be able to derive a string of the
665
81bb2db5e658 updated
Christian Urban <urbanc@in.tum.de>
parents: 618
diff changeset
   245
form $\meta{N} \to \ldots \to \meta{N} \cdot \ldots$.
175
5801e8c0e528 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 173
diff changeset
   246
362
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
   247
The other property we have to watch out for is when a grammar
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
   248
is \emph{ambiguous}. A grammar is said to be ambiguous if
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
   249
there are two parse-trees for one string. Again the grammar
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
   250
for arithmetic expressions shown above is ambiguous. While the
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
   251
shown parse tree for the string $(1 + 23) + 4$ is unique, this
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
   252
is not the case in general. For example there are two parse
175
5801e8c0e528 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 173
diff changeset
   253
trees for the string $1 + 2 + 3$, namely
5801e8c0e528 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 173
diff changeset
   254
5801e8c0e528 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 173
diff changeset
   255
\begin{center}
5801e8c0e528 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 173
diff changeset
   256
\begin{tabular}{c@{\hspace{10mm}}c}
5801e8c0e528 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 173
diff changeset
   257
\begin{tikzpicture}[level distance=8mm, black]
665
81bb2db5e658 updated
Christian Urban <urbanc@in.tum.de>
parents: 618
diff changeset
   258
  \node {\meta{E} }
81bb2db5e658 updated
Christian Urban <urbanc@in.tum.de>
parents: 618
diff changeset
   259
    child {node {\meta{E} } child {node {\meta{N} } child {node {$1$}}}}
175
5801e8c0e528 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 173
diff changeset
   260
    child {node {$+$}}
665
81bb2db5e658 updated
Christian Urban <urbanc@in.tum.de>
parents: 618
diff changeset
   261
    child {node {\meta{E} }
81bb2db5e658 updated
Christian Urban <urbanc@in.tum.de>
parents: 618
diff changeset
   262
       child {node {\meta{E} } child {node {\meta{N} } child {node {$2$}}}}
175
5801e8c0e528 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 173
diff changeset
   263
       child {node {$+$}}
665
81bb2db5e658 updated
Christian Urban <urbanc@in.tum.de>
parents: 618
diff changeset
   264
       child {node {\meta{E} } child {node {\meta{N} } child {node {$3$}}}}
175
5801e8c0e528 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 173
diff changeset
   265
    }
5801e8c0e528 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 173
diff changeset
   266
    ;
5801e8c0e528 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 173
diff changeset
   267
\end{tikzpicture} 
5801e8c0e528 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 173
diff changeset
   268
&
5801e8c0e528 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 173
diff changeset
   269
\begin{tikzpicture}[level distance=8mm, black]
665
81bb2db5e658 updated
Christian Urban <urbanc@in.tum.de>
parents: 618
diff changeset
   270
  \node {\meta{E} }
81bb2db5e658 updated
Christian Urban <urbanc@in.tum.de>
parents: 618
diff changeset
   271
    child {node {\meta{E} }
81bb2db5e658 updated
Christian Urban <urbanc@in.tum.de>
parents: 618
diff changeset
   272
       child {node {\meta{E} } child {node {\meta{N} } child {node {$1$}}}}
175
5801e8c0e528 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 173
diff changeset
   273
       child {node {$+$}}
665
81bb2db5e658 updated
Christian Urban <urbanc@in.tum.de>
parents: 618
diff changeset
   274
       child {node {\meta{E} } child {node {\meta{N} } child {node {$2$}}}} 
175
5801e8c0e528 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 173
diff changeset
   275
    }
5801e8c0e528 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 173
diff changeset
   276
    child {node {$+$}}
665
81bb2db5e658 updated
Christian Urban <urbanc@in.tum.de>
parents: 618
diff changeset
   277
    child {node {\meta{E} } child {node {\meta{N} } child {node {$3$}}}}
175
5801e8c0e528 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 173
diff changeset
   278
    ;
5801e8c0e528 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 173
diff changeset
   279
\end{tikzpicture}
5801e8c0e528 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 173
diff changeset
   280
\end{tabular} 
5801e8c0e528 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 173
diff changeset
   281
\end{center}
5801e8c0e528 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 173
diff changeset
   282
680
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   283
\noindent In particular in programming languages we will try to avoid
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   284
ambiguous grammars because two different parse-trees for a string mean a
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   285
program can be interpreted in two different ways. In such cases we have
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   286
to somehow make sure the two different ways do not matter, or
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   287
disambiguate the grammar in some other way (for example making the $+$
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   288
left-associative). Unfortunately already the problem of deciding whether
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   289
a grammar is ambiguous or not is in general undecidable. But in simple
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   290
instance (the ones we deal with in this module) one can usually see when
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   291
a grammar is ambiguous.
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   292
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   293
\subsection*{Removing Left-Recursion}
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   294
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   295
Let us come back to the problem of left-recursion and consider the 
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   296
following grammar for binary numbers:
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   297
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   298
\begin{plstx}[margin=1cm]
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   299
: \meta{B} ::= \meta{B} \cdot \meta{B} | 0 | 1\\
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   300
\end{plstx}
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   301
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   302
\noindent
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   303
It is clear that this grammar can create all binary numbers, but
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   304
it is also clear that this grammar is left-recursive. Giving this
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   305
grammar as is to parser combinators will result in an infinite 
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   306
loop. Fortunately, every left-recursive grammar can be translated
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   307
into one that is not left-recursive with the help of some
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   308
transformation rules. Suppose we identified the ``offensive'' 
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   309
rule, then we can separate the grammar into this offensive rule
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   310
and the ``rest'':
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   311
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   312
\begin{plstx}[margin=1cm]
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   313
  : \meta{B} ::= \underbrace{\meta{B} \cdot \meta{B}}_{\textit{lft-rec}} 
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   314
  | \underbrace{0 \;\;|\;\; 1}_{\textit{rest}}\\
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   315
\end{plstx}
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   316
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   317
\noindent
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   318
To make the idea of the transformation clearer, suppose the left-recursive
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   319
rule is of the form $\meta{B}\alpha$ (the left-recursive non-terminal 
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   320
followed by something called $\alpha$) and the ``rest'' is called $\beta$.
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   321
That means our grammar looks schematically as follows
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   322
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   323
\begin{plstx}[margin=1cm]
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   324
  : \meta{B} ::= \meta{B} \cdot \alpha | \beta\\
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   325
\end{plstx}
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   326
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   327
\noindent
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   328
To get rid of the left-recursion, we are required to introduce 
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   329
a new non-terminal, say $\meta{B'}$ and transform the rule
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   330
as follows:
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   331
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   332
\begin{plstx}[margin=1cm]
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   333
  : \meta{B} ::= \beta \cdot \meta{B'}\\
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   334
  : \meta{B'} ::= \alpha \cdot \meta{B'} | \epsilon\\
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   335
\end{plstx}
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   336
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   337
\noindent
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   338
In our example of binary numbers we would after the transformation 
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   339
end up with the rules
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   340
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   341
\begin{plstx}[margin=1cm]
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   342
  : \meta{B} ::= 0 \cdot \meta{B'} | 1 \cdot \meta{B'}\\
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   343
  : \meta{B'} ::= \meta{B} \cdot \meta{B'} | \epsilon\\
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   344
\end{plstx}
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   345
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   346
\noindent
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   347
A little thought should convince you that this grammar still derives
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   348
all the binary numbers (for example 0 and 1 are derivable because $\meta{B'}$
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   349
can be $\epsilon$). Less clear might be why this grammar is non-left recursive.
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   350
For $\meta{B'}$ it is relatively clear because we will never be 
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   351
able to derive things like
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   352
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   353
\begin{center}
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   354
$\meta{B'} \rightarrow\ldots\rightarrow \meta{B'}\cdot\ldots$
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   355
\end{center}  
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   356
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   357
\noindent
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   358
because there will always be a $\meta{B}$ in front of a $\meta{B'}$, and
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   359
$\meta{B}$ now has always a $0$ or $1$ in front, so a $\meta{B'}$ can
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   360
never be in the first place. The reasoning is similar for $\meta{B}$:
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   361
the $0$ and $1$ in the rule for $\meta{B}$ ``protect'' it from becoming
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   362
left-recursive. This transformation does not mean the grammar is the
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   363
simplest left-recursive grammar for binary numbers. For example the
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   364
following grammar would do as well
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   365
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   366
\begin{plstx}[margin=1cm]
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   367
  : \meta{B} ::= 0 \cdot \meta{B} | 1 \cdot \meta{B} | 0 | 1\\
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   368
\end{plstx}
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   369
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   370
\noindent
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   371
The point is that we can in principle transform every left-recursive
940
1c1fbf45a03c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 936
diff changeset
   372
grammar into one that is non-left-recursive. This explains why often
680
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   373
the following grammar is used for arithmetic expressions:
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   374
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   375
\begin{plstx}[margin=1cm]
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   376
  : \meta{E} ::= \meta{T} | \meta{T} \cdot + \cdot \meta{E} |  \meta{T} \cdot - \cdot \meta{E}\\
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   377
  : \meta{T} ::= \meta{F} | \meta{F} \cdot * \cdot \meta{T}\\
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   378
  : \meta{F} ::= num\_token | ( \cdot \meta{E} \cdot )\\
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   379
\end{plstx}
176
3c2653fc8b5a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 175
diff changeset
   380
680
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   381
\noindent
936
aabd9168c7ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 798
diff changeset
   382
In this grammar all $\meta{E}$xpressions, $\meta{T}$erms and
aabd9168c7ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 798
diff changeset
   383
$\meta{F}$actors are in some way protected from being
940
1c1fbf45a03c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 936
diff changeset
   384
left-recursive. For example if you start $\meta{E}$ you can derive
936
aabd9168c7ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 798
diff changeset
   385
another one by going through $\meta{T}$, then $\meta{F}$, but then
aabd9168c7ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 798
diff changeset
   386
$\meta{E}$ is protected by the open-parenthesis in the last rule.
680
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   387
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   388
\subsection*{Removing $\epsilon$-Rules and CYK-Algorithm}
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   389
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   390
I showed above that the non-left-recursive grammar for binary numbers is
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   391
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   392
\begin{plstx}[margin=1cm]
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   393
  : \meta{B} ::= 0 \cdot \meta{B'} | 1 \cdot \meta{B'}\\
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   394
  : \meta{B'} ::= \meta{B} \cdot \meta{B'} | \epsilon\\
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   395
\end{plstx}
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   396
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   397
\noindent
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   398
The transformation made the original grammar non-left-recursive, but at
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   399
the expense of introducing an $\epsilon$ in the second rule. Having an
936
aabd9168c7ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 798
diff changeset
   400
explicit $\epsilon$-rule is annoying, not in terms of looping, but in
680
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   401
terms of efficiency. The reason is that the $\epsilon$-rule always
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   402
applies but since it recognises the empty string, it does not make any
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   403
progress with recognising a string. Better are rules like $( \cdot
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   404
\meta{E} \cdot )$ where something of the input is consumed. Getting
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   405
rid of $\epsilon$-rules is also important for the CYK parsing algorithm,
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   406
which can give us an insight into the complexity class of parsing.
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   407
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   408
It turns out we can also by some generic transformations eliminate
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   409
$\epsilon$-rules from grammars. Consider again the grammar above for
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   410
binary numbers where have a rule $\meta{B'} ::= \epsilon$. In this case
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   411
we look for rules of the (generic) form \mbox{$\meta{A} :=
940
1c1fbf45a03c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 936
diff changeset
   412
\alpha\cdot\meta{B'}\cdot\beta$}. That is, there are rules that use
680
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   413
$\meta{B'}$ and something ($\alpha$) is in front of $\meta{B'}$ and
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   414
something follows ($\beta$). Such rules need to be replaced by
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   415
additional rules of the form \mbox{$\meta{A} := \alpha\cdot\beta$}.
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   416
In our running example there are the two rules for $\meta{B}$ which
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   417
fall into this category
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   418
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   419
\begin{plstx}[margin=1cm]
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   420
  : \meta{B} ::= 0 \cdot \meta{B'} | 1 \cdot \meta{B'}\\
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   421
\end{plstx} 
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   422
940
1c1fbf45a03c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 936
diff changeset
   423
\noindent To follow the general scheme of the transformation,
680
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   424
the $\alpha$ is either is either $0$ or $1$, and the $\beta$ happens
798
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   425
to be empty. So we need to generate new rules for the form 
680
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   426
\mbox{$\meta{A} := \alpha\cdot\beta$}, which in our particular 
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   427
example means we obtain
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   428
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   429
\begin{plstx}[margin=1cm]
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   430
  : \meta{B} ::= 0 \cdot \meta{B'} | 1 \cdot \meta{B'} | 0 | 1\\
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   431
\end{plstx} 
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   432
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   433
\noindent
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   434
Unfortunately $\meta{B'}$ is also used in the rule 
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   435
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   436
\begin{plstx}[margin=1cm]
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   437
  : \meta{B'} ::= \meta{B} \cdot \meta{B'}\\
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   438
\end{plstx}
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   439
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   440
\noindent
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   441
For this we repeat the transformation, giving 
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   442
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   443
\begin{plstx}[margin=1cm]
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   444
  : \meta{B'} ::= \meta{B} \cdot \meta{B'} | \meta{B}\\
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   445
\end{plstx}
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   446
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   447
\noindent
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   448
In this case $\alpha$ was substituted with $\meta{B}$ and $\beta$
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   449
was again empty. Once no rule is left over, we can simply throw
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   450
away the $\epsilon$ rule.  This gives the grammar
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   451
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   452
\begin{plstx}[margin=1cm]
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   453
  : \meta{B} ::= 0 \cdot \meta{B'} | 1 \cdot \meta{B'} | 0 | 1\\
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   454
  : \meta{B'} ::= \meta{B} \cdot \meta{B'} | \meta{B}\\
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   455
\end{plstx}
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   456
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   457
\noindent
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   458
I let you think about whether this grammar can still recognise all
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   459
binary numbers and whether this grammar is non-left-recursive. The
936
aabd9168c7ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 798
diff changeset
   460
precise statement for the transformation of removing $\epsilon$-rules
aabd9168c7ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 798
diff changeset
   461
is that if the original grammar was able to recognise only non-empty
680
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   462
strings, then the transformed grammar will be equivalent (matching the
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   463
same set of strings); if the original grammar was able to match the
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   464
empty string, then the transformed grammar will be able to match the
936
aabd9168c7ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 798
diff changeset
   465
same strings, \emph{except} the empty string. So the
aabd9168c7ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 798
diff changeset
   466
$\epsilon$-removal does not preserve equivalence of grammars in
aabd9168c7ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 798
diff changeset
   467
general, but the small defect with the empty string is not important
aabd9168c7ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 798
diff changeset
   468
for practical purposes.
680
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   469
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   470
So why are these transformations all useful? Well apart from making the 
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   471
parser combinators work (remember they cannot deal with left-recursion and
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   472
are inefficient with $\epsilon$-rules), a second reason is that they help
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   473
with getting any insight into the complexity of the parsing problem. 
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   474
The parser combinators are very easy to implement, but are far from the 
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   475
most efficient way of processing input (they can blow up exponentially
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   476
with ambiguous grammars). The question remains what is the best possible
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   477
complexity for parsing? It turns out that this is $O(n^3)$ for context-free
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   478
languages. 
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   479
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   480
To answer the question about complexity, let me describe next the CYK
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   481
algorithm (named after the authors Cocke–Younger–Kasami). This algorithm
681
9efdee02c95e updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   482
works with grammars that are in \emph{Chomsky normalform}. In Chomsky
9efdee02c95e updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   483
normalform all rules must be of the form $\meta{A} ::= a$, where $a$ is 
9efdee02c95e updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   484
a terminal, or $\meta{A} ::= \meta{B}\cdot \meta{C}$, where $\meta{B}$ and
9efdee02c95e updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   485
$\meta{B}$ need to be non-terminals. And no rule can contain $\epsilon$.
9efdee02c95e updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   486
The following grammar is in Chomsky normalform:
9efdee02c95e updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   487
9efdee02c95e updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   488
\begin{plstx}[margin=1cm]
682
612976492d25 updated
Christian Urban <urbanc@in.tum.de>
parents: 681
diff changeset
   489
  : \meta{S} ::= \meta{N}\cdot \meta{P}\\
612976492d25 updated
Christian Urban <urbanc@in.tum.de>
parents: 681
diff changeset
   490
  : \meta{P} ::= \meta{V}\cdot \meta{N}\\
612976492d25 updated
Christian Urban <urbanc@in.tum.de>
parents: 681
diff changeset
   491
  : \meta{N} ::= \meta{N}\cdot \meta{N}\\
612976492d25 updated
Christian Urban <urbanc@in.tum.de>
parents: 681
diff changeset
   492
  : \meta{N} ::= \meta{A}\cdot \meta{N}\\
612976492d25 updated
Christian Urban <urbanc@in.tum.de>
parents: 681
diff changeset
   493
  : \meta{N} ::= \texttt{student} | \texttt{trainer} | \texttt{team} 
612976492d25 updated
Christian Urban <urbanc@in.tum.de>
parents: 681
diff changeset
   494
                | \texttt{trains}\\
612976492d25 updated
Christian Urban <urbanc@in.tum.de>
parents: 681
diff changeset
   495
  : \meta{V} ::= \texttt{trains} | \texttt{team}\\
612976492d25 updated
Christian Urban <urbanc@in.tum.de>
parents: 681
diff changeset
   496
  : \meta{A} ::= \texttt{The} | \texttt{the}\\
681
9efdee02c95e updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   497
\end{plstx}
9efdee02c95e updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   498
9efdee02c95e updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   499
\noindent
9efdee02c95e updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   500
where $\meta{S}$ is the start symbol and $\meta{S}$, $\meta{P}$,
9efdee02c95e updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   501
$\meta{N}$, $\meta{V}$ and $\meta{A}$ are non-terminals. The ``words''
9efdee02c95e updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   502
are terminals. The rough idea behind this grammar is that $\meta{S}$
9efdee02c95e updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   503
stands for a sentence, $\meta{P}$ is a predicate, $\meta{N}$ is a noun
9efdee02c95e updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   504
and so on. For example the rule \mbox{$\meta{P} ::= \meta{V}\cdot
9efdee02c95e updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   505
\meta{N}$} states that a predicate can be a verb followed by a noun.
9efdee02c95e updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   506
Now the question is whether the string 
9efdee02c95e updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   507
9efdee02c95e updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   508
\begin{center}
9efdee02c95e updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   509
  \texttt{The trainer trains the student team}
9efdee02c95e updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   510
\end{center}
9efdee02c95e updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   511
9efdee02c95e updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   512
\noindent
9efdee02c95e updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   513
is recognised by the grammar. The CYK algorithm starts with the
9efdee02c95e updated
Christian Urban <urbanc@in.tum.de>
parents: 680
diff changeset
   514
following triangular data structure.
680
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   515
798
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   516
%%\begin{figure}[t]
682
612976492d25 updated
Christian Urban <urbanc@in.tum.de>
parents: 681
diff changeset
   517
\begin{center}
798
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   518
  \begin{tikzpicture}[scale=0.7,line width=0.8mm]
682
612976492d25 updated
Christian Urban <urbanc@in.tum.de>
parents: 681
diff changeset
   519
  \draw (-2,0) -- (4,0);
612976492d25 updated
Christian Urban <urbanc@in.tum.de>
parents: 681
diff changeset
   520
  \draw (-2,1) -- (4,1);
612976492d25 updated
Christian Urban <urbanc@in.tum.de>
parents: 681
diff changeset
   521
  \draw (-2,2) -- (3,2);
612976492d25 updated
Christian Urban <urbanc@in.tum.de>
parents: 681
diff changeset
   522
  \draw (-2,3) -- (2,3);
612976492d25 updated
Christian Urban <urbanc@in.tum.de>
parents: 681
diff changeset
   523
  \draw (-2,4) -- (1,4);
612976492d25 updated
Christian Urban <urbanc@in.tum.de>
parents: 681
diff changeset
   524
  \draw (-2,5) -- (0,5);
612976492d25 updated
Christian Urban <urbanc@in.tum.de>
parents: 681
diff changeset
   525
  \draw (-2,6) -- (-1,6);
612976492d25 updated
Christian Urban <urbanc@in.tum.de>
parents: 681
diff changeset
   526
  
612976492d25 updated
Christian Urban <urbanc@in.tum.de>
parents: 681
diff changeset
   527
  \draw (0,0) -- (0, 5);
612976492d25 updated
Christian Urban <urbanc@in.tum.de>
parents: 681
diff changeset
   528
  \draw (1,0) -- (1, 4);
612976492d25 updated
Christian Urban <urbanc@in.tum.de>
parents: 681
diff changeset
   529
  \draw (2,0) -- (2, 3);
612976492d25 updated
Christian Urban <urbanc@in.tum.de>
parents: 681
diff changeset
   530
  \draw (3,0) -- (3, 2);
612976492d25 updated
Christian Urban <urbanc@in.tum.de>
parents: 681
diff changeset
   531
  \draw (4,0) -- (4, 1);
612976492d25 updated
Christian Urban <urbanc@in.tum.de>
parents: 681
diff changeset
   532
  \draw (-1,0) -- (-1, 6);
612976492d25 updated
Christian Urban <urbanc@in.tum.de>
parents: 681
diff changeset
   533
  \draw (-2,0) -- (-2, 6);
612976492d25 updated
Christian Urban <urbanc@in.tum.de>
parents: 681
diff changeset
   534
  
612976492d25 updated
Christian Urban <urbanc@in.tum.de>
parents: 681
diff changeset
   535
  \draw (-1.5,-0.5) node {\footnotesize{}\texttt{The}}; 
612976492d25 updated
Christian Urban <urbanc@in.tum.de>
parents: 681
diff changeset
   536
  \draw (-0.5,-1.0) node {\footnotesize{}\texttt{trainer}}; 
612976492d25 updated
Christian Urban <urbanc@in.tum.de>
parents: 681
diff changeset
   537
  \draw ( 0.5,-0.5) node {\footnotesize{}\texttt{trains}}; 
612976492d25 updated
Christian Urban <urbanc@in.tum.de>
parents: 681
diff changeset
   538
  \draw ( 1.5,-1.0) node {\footnotesize{}\texttt{the}}; 
612976492d25 updated
Christian Urban <urbanc@in.tum.de>
parents: 681
diff changeset
   539
  \draw ( 2.5,-0.5) node {\footnotesize{}\texttt{student}}; 
612976492d25 updated
Christian Urban <urbanc@in.tum.de>
parents: 681
diff changeset
   540
  \draw ( 3.5,-1.0) node {\footnotesize{}\texttt{team}};
612976492d25 updated
Christian Urban <urbanc@in.tum.de>
parents: 681
diff changeset
   541
  
612976492d25 updated
Christian Urban <urbanc@in.tum.de>
parents: 681
diff changeset
   542
  \draw (-1.5,0.5) node {$A$}; 
612976492d25 updated
Christian Urban <urbanc@in.tum.de>
parents: 681
diff changeset
   543
  \draw (-0.5,0.5) node {$N$}; 
612976492d25 updated
Christian Urban <urbanc@in.tum.de>
parents: 681
diff changeset
   544
  \draw ( 0.5,0.5) node {$N,V$}; 
612976492d25 updated
Christian Urban <urbanc@in.tum.de>
parents: 681
diff changeset
   545
  \draw ( 1.5,0.5) node {$A$}; 
612976492d25 updated
Christian Urban <urbanc@in.tum.de>
parents: 681
diff changeset
   546
  \draw ( 2.5,0.5) node {$N$}; 
612976492d25 updated
Christian Urban <urbanc@in.tum.de>
parents: 681
diff changeset
   547
  \draw ( 3.5,0.5) node {$N,V$};
612976492d25 updated
Christian Urban <urbanc@in.tum.de>
parents: 681
diff changeset
   548
798
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   549
%  \draw (-1.5,1.5) node {\small{}a}; 
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   550
%  \draw (-0.5,1.5) node {\small{}b}; 
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   551
%  \draw ( 0.5,1.5) node {\small{}c}; 
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   552
%  \draw ( 1.5,1.5) node {\small{}d}; 
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   553
%  \draw ( 2.5,1.5) node {\small{}e}; 
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   554
  
682
612976492d25 updated
Christian Urban <urbanc@in.tum.de>
parents: 681
diff changeset
   555
  \draw (-2.4, 5.5) node {$1$}; 
612976492d25 updated
Christian Urban <urbanc@in.tum.de>
parents: 681
diff changeset
   556
  \draw (-2.4, 4.5) node {$2$}; 
612976492d25 updated
Christian Urban <urbanc@in.tum.de>
parents: 681
diff changeset
   557
  \draw (-2.4, 3.5) node {$3$}; 
612976492d25 updated
Christian Urban <urbanc@in.tum.de>
parents: 681
diff changeset
   558
  \draw (-2.4, 2.5) node {$4$}; 
612976492d25 updated
Christian Urban <urbanc@in.tum.de>
parents: 681
diff changeset
   559
  \draw (-2.4, 1.5) node {$5$}; 
612976492d25 updated
Christian Urban <urbanc@in.tum.de>
parents: 681
diff changeset
   560
  \draw (-2.4, 0.5) node {$6$}; 
612976492d25 updated
Christian Urban <urbanc@in.tum.de>
parents: 681
diff changeset
   561
  \end{tikzpicture}
612976492d25 updated
Christian Urban <urbanc@in.tum.de>
parents: 681
diff changeset
   562
  \end{center}
798
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   563
%%\end{figure}
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   564
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   565
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   566
\noindent
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   567
The last row contains the information about all words and their
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   568
corresponding non-terminals. For example the field for \texttt{trains}
936
aabd9168c7ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 798
diff changeset
   569
contains the information $\meta{N}$ and $\meta{V}$ because \texttt{trains} can be a
798
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   570
``verb'' and a ``noun'' according to the grammar.  The row above,
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   571
let's call the corresponding fields 5a to 5e, contains information
936
aabd9168c7ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 798
diff changeset
   572
about \underline{2-word} parts of the sentence, namely
798
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   573
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   574
\begin{center}
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   575
\begin{tabular}{llll}
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   576
  5a) & $\underbrace{\texttt{The}}_{A}$ $\mid$ $\underbrace{\texttt{trainer}}_{N}$   \\
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   577
  5b) & $\underbrace{\texttt{trainer}}_{N}$ $\mid$ $\underbrace{\texttt{trains}}_{N,V}$\\
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   578
  5c) & \texttt{trains} $\mid$ \texttt{the}    \\
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   579
  5d) & \texttt{the} $\mid$ \texttt{student}   \\
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   580
  5e) & \texttt{student} $\mid$ \texttt{team}  \\
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   581
\end{tabular}  
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   582
\end{center}  
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   583
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   584
\noindent
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   585
For each of them, we look up in row 6 which non-terminals it belongs to
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   586
(indicated above for 5a and 5b). For 5a, with the non-terminals
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   587
\meta{A} and \meta{N}, we find the grammar rule
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   588
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   589
\begin{plstx}[margin=1cm]
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   590
  : \meta{N} ::= \meta{A}\cdot \meta{N}\\
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   591
\end{plstx}
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   592
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   593
\noindent
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   594
which means into field 5a we put the left-hand side of this rule,
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   595
which in this case is the non-terminal \meta{N}. For 5b we have to check
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   596
for both $\meta{N}\cdot\meta{N}$ and $\meta{N}\cdot\meta{V}$ whether there
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   597
is a right-hand side of this form in the grammar. But only the
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   598
grammar rule 
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   599
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   600
\begin{plstx}[margin=1cm]
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   601
  : \meta{N} ::= \meta{N}\cdot \meta{N}\\
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   602
\end{plstx}
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   603
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   604
\noindent
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   605
matches, which means 5b gets also an \meta{N}. Continuing for all
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   606
fields in row 5 gives:
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   607
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   608
\begin{center}
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   609
  \begin{tikzpicture}[scale=0.7,line width=0.8mm]
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   610
  \draw (-2,0) -- (4,0);
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   611
  \draw (-2,1) -- (4,1);
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   612
  \draw (-2,2) -- (3,2);
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   613
  \draw (-2,3) -- (2,3);
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   614
  \draw (-2,4) -- (1,4);
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   615
  \draw (-2,5) -- (0,5);
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   616
  \draw (-2,6) -- (-1,6);
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   617
  
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   618
  \draw (0,0) -- (0, 5);
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   619
  \draw (1,0) -- (1, 4);
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   620
  \draw (2,0) -- (2, 3);
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   621
  \draw (3,0) -- (3, 2);
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   622
  \draw (4,0) -- (4, 1);
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   623
  \draw (-1,0) -- (-1, 6);
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   624
  \draw (-2,0) -- (-2, 6);
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   625
  
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   626
  \draw (-1.5,-0.5) node {\footnotesize{}\texttt{The}}; 
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   627
  \draw (-0.5,-1.0) node {\footnotesize{}\texttt{trainer}}; 
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   628
  \draw ( 0.5,-0.5) node {\footnotesize{}\texttt{trains}}; 
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   629
  \draw ( 1.5,-1.0) node {\footnotesize{}\texttt{the}}; 
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   630
  \draw ( 2.5,-0.5) node {\footnotesize{}\texttt{student}}; 
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   631
  \draw ( 3.5,-1.0) node {\footnotesize{}\texttt{team}};
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   632
  
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   633
  \draw (-1.5,0.5) node {$A$}; 
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   634
  \draw (-0.5,0.5) node {$N$}; 
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   635
  \draw ( 0.5,0.5) node {$N,V$}; 
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   636
  \draw ( 1.5,0.5) node {$A$}; 
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   637
  \draw ( 2.5,0.5) node {$N$}; 
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   638
  \draw ( 3.5,0.5) node {$N,V$};
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   639
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   640
  \draw (-1.5,1.5) node {$N$}; 
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   641
  \draw (-0.5,1.5) node {$N$}; 
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   642
  \draw ( 0.5,1.5) node {$$}; 
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   643
  \draw ( 1.5,1.5) node {$N$}; 
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   644
  \draw ( 2.5,1.5) node {$N$}; 
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   645
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   646
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   647
%  \draw (-1.5,1.5) node {\small{}a}; 
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   648
%  \draw (-0.5,1.5) node {\small{}b}; 
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   649
%  \draw ( 0.5,1.5) node {\small{}c}; 
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   650
%  \draw ( 1.5,1.5) node {\small{}d}; 
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   651
%  \draw ( 2.5,1.5) node {\small{}e}; 
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   652
  
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   653
  \draw (-2.4, 5.5) node {$1$}; 
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   654
  \draw (-2.4, 4.5) node {$2$}; 
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   655
  \draw (-2.4, 3.5) node {$3$}; 
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   656
  \draw (-2.4, 2.5) node {$4$}; 
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   657
  \draw (-2.4, 1.5) node {$5$}; 
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   658
  \draw (-2.4, 0.5) node {$6$}; 
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   659
  \end{tikzpicture}
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   660
  \end{center}
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   661
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   662
\noindent
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   663
Now row 4 is in charge of all 3-word parts of the sentence, namely
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   664
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   665
\begin{center}
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   666
\begin{tabular}{llll}
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   667
  4a) & The $\mid$ trainer trains\\
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   668
      & The trainer $\mid$ trains\\
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   669
  4b) & trainer $\mid$ trains the\\
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   670
      & trainer trains $\mid$ the\\
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   671
  4c) & trains $\mid$ the student\\
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   672
      & trains the $\mid$ student\\
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   673
  4d) & the $\mid$ student team\\
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   674
      & the student $\mid$ team\\
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   675
\end{tabular}  
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   676
\end{center}
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   677
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   678
\noindent
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   679
Note that in case of 3-word parts we have two splits. For example for
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   680
4a: $\underbrace{\texttt{The}}_{A}$ and
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   681
$\underbrace{\texttt{trainer trains}}_{N}$; and also
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   682
$\underbrace{\texttt{The trainer}}_{N}$ and
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   683
$\underbrace{\texttt{trains}}_{N,V}$. For each of these splits we have
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   684
to look up in the rows below, which non-terminals we already
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   685
computed. This allows us to look for right-hand sides in our grammar:
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   686
$\meta{A}\cdot\meta{N}$, $\meta{N}\cdot\meta{N}$ and
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   687
$\meta{N}\cdot\meta{V}$, which yield the only left-hand side
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   688
\meta{N}. This is what we fill in for 4a. And so on for row 4.
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   689
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   690
Row 3 is about all 4-word parts in the sentence, namely
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   691
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   692
\begin{center}
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   693
\begin{tabular}{llll}
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   694
  3a) & The trainer trains the\\
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   695
  3b) & trainer trains the student\\
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   696
  3c) & trains the student team\\
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   697
\end{tabular}  
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   698
\end{center}
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   699
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   700
\noindent
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   701
Each of them can be split up in 3 ways, for example
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   702
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   703
\begin{center}
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   704
\begin{tabular}{llll}
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   705
  3a) & The $\mid$ trainer trains the\\
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   706
      & The trainer $\mid$ trains the\\
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   707
      & The trainer trains $\mid$ the\\
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   708
\end{tabular}  
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   709
\end{center}
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   710
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   711
\noindent
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   712
and we have to consider them all in turn to fill in the non-terminals for
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   713
3a. You can guess how it continues: row 2 is for all 5-word parts, and finally
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   714
the field on the top is for the whole sentence.
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   715
The idea of the CYK algorithm is that if in the top-field the starting
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   716
symbol \meta{S} appears (possibly together with other non-terminals),
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   717
then the sentence is accepted by the grammar. If it does not, then the
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   718
sentence is not accepted.
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   719
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   720
Let us very quickly calculate the complexity of the CYK
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   721
algorithm. Lookup operations inside the triangle and in the grammar
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   722
are assumed to be of constant time, $O(1)$, meaning they do not
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   723
matter. How many fields are in the triangle\ldots
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   724
$\frac{n}{2} * (n+1)$, where $n$ is the size of the input. That means
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   725
roughly $O(n^2)$ fields. How much work do we have to do for each
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   726
field? Well, for the top-most we have to consider $n-1$ splits, which
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   727
means roughly $O(n)$ for each field. The overall result is a $O(n^3)$
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   728
time-complexity for CYK. It turns out that this is the best worst-time
409e5014edde updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 727
diff changeset
   729
complexity for parsing with context-free grammars.
680
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   730
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   731
\end{document}
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   732
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   733
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   734
%%% Parser combinators are now part of handout 6
459
858f62bc930a updated
Christian Urban <urbanc@in.tum.de>
parents: 385
diff changeset
   735
858f62bc930a updated
Christian Urban <urbanc@in.tum.de>
parents: 385
diff changeset
   736
\subsection*{Parser Combinators}
858f62bc930a updated
Christian Urban <urbanc@in.tum.de>
parents: 385
diff changeset
   737
362
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
   738
Let us now turn to the problem of generating a parse-tree for
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
   739
a grammar and string. In what follows we explain \emph{parser
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
   740
combinators}, because they are easy to implement and closely
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
   741
resemble grammar rules. Imagine that a grammar describes the
665
81bb2db5e658 updated
Christian Urban <urbanc@in.tum.de>
parents: 618
diff changeset
   742
strings of natural numbers, such as the grammar \meta{N}  shown
362
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
   743
above. For all such strings we want to generate the
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
   744
parse-trees or later on we actually want to extract the
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
   745
meaning of these strings, that is the concrete integers
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
   746
``behind'' these strings. In Scala the parser combinators will
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
   747
be functions of type
176
3c2653fc8b5a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 175
diff changeset
   748
3c2653fc8b5a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 175
diff changeset
   749
\begin{center}
177
53def1fbf472 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   750
\texttt{I $\Rightarrow$ Set[(T, I)]}
176
3c2653fc8b5a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 175
diff changeset
   751
\end{center}
3c2653fc8b5a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 175
diff changeset
   752
362
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
   753
\noindent that is they take as input something of type
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
   754
\texttt{I}, typically a list of tokens or a string, and return
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
   755
a set of pairs. The first component of these pairs corresponds
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
   756
to what the parser combinator was able to process from the
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
   757
input and the second is the unprocessed part of the input. As
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
   758
we shall see shortly, a parser combinator might return more
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
   759
than one such pair, with the idea that there are potentially
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
   760
several ways how to interpret the input. As a concrete
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
   761
example, consider the case where the input is of type string,
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
   762
say the string
183
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   763
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   764
\begin{center}
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   765
\tt\Grid{iffoo\VS testbar}
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   766
\end{center}
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   767
362
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
   768
\noindent We might have a parser combinator which tries to
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
   769
interpret this string as a keyword (\texttt{if}) or an
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
   770
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
   771
183
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   772
\begin{center}
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   773
$\left\{ \left(\texttt{\Grid{if}}\,,\, \texttt{\Grid{foo\VS testbar}}\right), 
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   774
           \left(\texttt{\Grid{iffoo}}\,,\, \texttt{\Grid{\VS testbar}}\right) \right\}$
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   775
\end{center}
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   776
362
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
   777
\noindent where the first pair means the parser could
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
   778
recognise \texttt{if} from the input and leaves the rest as
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
   779
`unprocessed' as the second component of the pair; in the
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
   780
other case it could recognise \texttt{iffoo} and leaves
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
   781
\texttt{\VS testbar} as unprocessed. If the parser cannot
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
   782
recognise anything from the input then parser combinators just
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
   783
return the empty set $\{\}$. This will indicate
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
   784
something ``went wrong''.
183
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   785
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   786
The main attraction is that we can easily build parser combinators out of smaller components
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   787
following very closely the structure of a grammar. In order to implement this in an object
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   788
oriented programming language, like Scala, we need to specify an abstract class for parser 
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   789
combinators. This abstract class requires the implementation of the function
177
53def1fbf472 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   790
\texttt{parse} taking an argument of type \texttt{I} and returns a set of type  
53def1fbf472 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   791
\mbox{\texttt{Set[(T, I)]}}.
53def1fbf472 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   792
53def1fbf472 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   793
\begin{center}
53def1fbf472 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   794
\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
   795
abstract class Parser[I, T] {
53def1fbf472 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   796
  def parse(ts: I): Set[(T, I)]
53def1fbf472 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   797
53def1fbf472 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   798
  def parse_all(ts: I): Set[T] =
53def1fbf472 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   799
    for ((head, tail) <- parse(ts); if (tail.isEmpty)) 
53def1fbf472 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   800
      yield head
53def1fbf472 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   801
}
53def1fbf472 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   802
\end{lstlisting}
53def1fbf472 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   803
\end{center}
176
3c2653fc8b5a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 175
diff changeset
   804
177
53def1fbf472 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   805
\noindent
183
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   806
From the function \texttt{parse} we can then ``centrally'' derive the function \texttt{parse\_all},
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   807
which just filters out all pairs whose second component is not empty (that is has still some
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   808
unprocessed part). The reason is that at the end of parsing we are only interested in the
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   809
results where all the input has been consumed and no unprocessed part is left.
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   810
177
53def1fbf472 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   811
One of the simplest parser combinators recognises just a character, say $c$, 
53def1fbf472 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   812
from the beginning of strings. Its behaviour is as follows:
176
3c2653fc8b5a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 175
diff changeset
   813
177
53def1fbf472 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   814
\begin{itemize}
53def1fbf472 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   815
\item if the head of the input string starts with a $c$, it returns 
53def1fbf472 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   816
	the set $\{(c, \textit{tail of}\; s)\}$
53def1fbf472 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   817
\item otherwise it returns the empty set $\varnothing$	
53def1fbf472 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   818
\end{itemize}
53def1fbf472 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   819
53def1fbf472 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   820
\noindent
53def1fbf472 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   821
The input type of this simple parser combinator for characters is
53def1fbf472 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   822
\texttt{String} and the output type \mbox{\texttt{Set[(Char, String)]}}. 
53def1fbf472 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   823
The code in Scala is as follows:
53def1fbf472 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   824
53def1fbf472 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   825
\begin{center}
53def1fbf472 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   826
\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
   827
case class CharParser(c: Char) extends Parser[String, Char] {
53def1fbf472 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   828
  def parse(sb: String) = 
53def1fbf472 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   829
    if (sb.head == c) Set((c, sb.tail)) else Set()
53def1fbf472 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   830
}
53def1fbf472 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   831
\end{lstlisting}
53def1fbf472 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 176
diff changeset
   832
\end{center}
176
3c2653fc8b5a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 175
diff changeset
   833
183
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   834
\noindent
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   835
The \texttt{parse} function tests whether the first character of the 
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   836
input string \texttt{sb} is equal to \texttt{c}. If yes, then it splits the
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   837
string into the recognised part \texttt{c} and the unprocessed part
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   838
\texttt{sb.tail}. In case \texttt{sb} does not start with \texttt{c} then
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   839
the parser returns the empty set (in Scala \texttt{Set()}).
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   840
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   841
More interesting are the parser combinators that build larger parsers
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   842
out of smaller component parsers. For example the alternative 
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   843
parser combinator is as follows.
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   844
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   845
\begin{center}
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   846
\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
   847
class AltParser[I, T]
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   848
       (p: => Parser[I, T], 
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   849
        q: => Parser[I, T]) extends Parser[I, T] {
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   850
  def parse(sb: I) = p.parse(sb) ++ q.parse(sb)
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   851
}
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   852
\end{lstlisting}
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   853
\end{center}
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   854
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   855
\noindent
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   856
The types of this parser combinator are polymorphic (we just have \texttt{I}
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   857
for the input type, and \texttt{T} for the output type). The alternative parser
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   858
builds a new parser out of two existing parser combinator \texttt{p} and \texttt{q}.
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   859
Both need to be able to process input of type \texttt{I} and return the same
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   860
output type \texttt{Set[(T, I)]}. (There is an interesting detail of Scala, namely the 
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   861
\texttt{=>} in front of the types of \texttt{p} and \texttt{q}. They will prevent the
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   862
evaluation of the arguments before they are used. This is often called 
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   863
\emph{lazy evaluation} of the arguments.) The alternative parser should run
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   864
the input with the first parser \texttt{p} (producing a set of outputs) and then
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   865
run the same input with \texttt{q}. The result should be then just the union
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   866
of both sets, which is the operation \texttt{++} in Scala.
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   867
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   868
This parser combinator already allows us to construct a parser that either 
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   869
a character \texttt{a} or \texttt{b}, as
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   870
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   871
\begin{center}
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   872
\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
   873
new AltParser(CharParser('a'), CharParser('b'))
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   874
\end{lstlisting}
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   875
\end{center}
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   876
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   877
\noindent
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   878
Scala allows us to introduce some more readable shorthand notation for this, like \texttt{'a' || 'b'}. 
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   879
We can call this parser combinator with the strings
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   880
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   881
\begin{center}
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   882
\begin{tabular}{rcl}
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   883
input string & & output\medskip\\
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   884
\texttt{\Grid{ac}} & $\rightarrow$ & $\left\{(\texttt{\Grid{a}}, \texttt{\Grid{c}})\right\}$\\
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   885
\texttt{\Grid{bc}} & $\rightarrow$ & $\left\{(\texttt{\Grid{b}}, \texttt{\Grid{c}})\right\}$\\
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   886
\texttt{\Grid{cc}} & $\rightarrow$ & $\varnothing$
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   887
\end{tabular}
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   888
\end{center}
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   889
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   890
\noindent
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   891
We receive in the first two cases a successful output (that is a non-empty set).
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   892
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   893
A bit more interesting is the \emph{sequence parser combinator} implemented in
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   894
Scala as follows:
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   895
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   896
\begin{center}
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   897
\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
   898
class SeqParser[I, T, S]
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   899
       (p: => Parser[I, T], 
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   900
        q: => Parser[I, S]) extends Parser[I, (T, S)] {
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   901
  def parse(sb: I) = 
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   902
    for ((head1, tail1) <- p.parse(sb); 
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   903
         (head2, tail2) <- q.parse(tail1)) 
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   904
            yield ((head1, head2), tail2)
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   905
}
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   906
\end{lstlisting}
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   907
\end{center}
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   908
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   909
\noindent
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   910
This parser takes as input two parsers, \texttt{p} and \texttt{q}. It implements \texttt{parse} 
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   911
as follows: let first run the parser \texttt{p} on the input producing a set of pairs (\texttt{head1}, \texttt{tail1}).
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   912
The \texttt{tail1} stands for the unprocessed parts left over by \texttt{p}. 
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   913
Let \texttt{q} run on these unprocessed parts
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   914
producing again a set of pairs. The output of the sequence parser combinator is then a set
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   915
containing pairs where the first components are again pairs, namely what the first parser could parse
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   916
together with what the second parser could parse; the second component is the unprocessed
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   917
part left over after running the second parser \texttt{q}. Therefore the input type of
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   918
the sequence parser combinator is as usual \texttt{I}, but the output type is
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   919
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   920
\begin{center}
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   921
\texttt{Set[((T, S), I)]}
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   922
\end{center}
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   923
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   924
Scala allows us to provide some
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   925
shorthand notation for the sequence parser combinator. So we can write for 
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   926
example \texttt{'a'  $\sim$ 'b'}, which is the
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   927
parser combinator that first consumes the character \texttt{a} from a string and then \texttt{b}.
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   928
Calling this parser combinator with the strings
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   929
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   930
\begin{center}
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   931
\begin{tabular}{rcl}
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   932
input string & & output\medskip\\
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   933
\texttt{\Grid{abc}} & $\rightarrow$ & $\left\{((\texttt{\Grid{a}}, \texttt{\Grid{b}}), \texttt{\Grid{c}})\right\}$\\
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   934
\texttt{\Grid{bac}} & $\rightarrow$ & $\varnothing$\\
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   935
\texttt{\Grid{ccc}} & $\rightarrow$ & $\varnothing$
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   936
\end{tabular}
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   937
\end{center}
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   938
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   939
\noindent
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   940
A slightly more complicated parser is \texttt{('a'  || 'b') $\sim$ 'b'} which parses as first character either
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   941
an \texttt{a} or \texttt{b} followed by a \texttt{b}. This parser produces the following results.
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   942
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   943
\begin{center}
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   944
\begin{tabular}{rcl}
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   945
input string & & output\medskip\\
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   946
\texttt{\Grid{abc}} & $\rightarrow$ & $\left\{((\texttt{\Grid{a}}, \texttt{\Grid{b}}), \texttt{\Grid{c}})\right\}$\\
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   947
\texttt{\Grid{bbc}} & $\rightarrow$ & $\left\{((\texttt{\Grid{b}}, \texttt{\Grid{b}}), \texttt{\Grid{c}})\right\}$\\
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   948
\texttt{\Grid{aac}} & $\rightarrow$ & $\varnothing$
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   949
\end{tabular}
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   950
\end{center}
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   951
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   952
Note carefully that constructing the parser \texttt{'a' || ('a' $\sim$ 'b')} will result in a tying error.
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   953
The first parser has as output type a single character (recall the type of \texttt{CharParser}),
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   954
but the second parser produces a pair of characters as output. The alternative parser is however
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   955
required to have both component parsers to have the same type. We will see later how we can 
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   956
build this parser without the typing error.
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   957
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   958
The next parser combinator does not actually combine smaller parsers, but applies
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   959
a function to the result of the parser. It is implemented in Scala as follows
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   960
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   961
\begin{center}
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   962
\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
   963
class FunParser[I, T, S]
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   964
         (p: => Parser[I, T], 
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   965
          f: T => S) extends Parser[I, S] {
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   966
  def parse(sb: I) = 
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   967
    for ((head, tail) <- p.parse(sb)) yield (f(head), tail)
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   968
}
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   969
\end{lstlisting}
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   970
\end{center}
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   971
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   972
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   973
\noindent
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   974
This parser combinator takes a parser \texttt{p} with output type \texttt{T} as
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   975
input as well as a function \texttt{f} with type \texttt{T => S}. The parser \texttt{p}
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   976
produces sets of type \texttt{(T, I)}. The \texttt{FunParser} combinator then
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   977
applies the function \texttt{f} to all the parer outputs. Since this function
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   978
is of type \texttt{T => S}, we obtain a parser with output type \texttt{S}.
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   979
Again Scala lets us introduce some shorthand notation for this parser combinator. 
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   980
Therefore we will write \texttt{p ==> f} for it.
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   981
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   982
%\bigskip
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   983
%takes advantage of the full generality---have a look
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   984
%what it produces if we call it with the string \texttt{abc}
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   985
%
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   986
%\begin{center}
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   987
%\begin{tabular}{rcl}
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   988
%input string & & output\medskip\\
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   989
%\texttt{\Grid{abc}} & $\rightarrow$ & $\left\{((\texttt{\Grid{a}}, \texttt{\Grid{b}}), \texttt{\Grid{c}})\right\}$\\
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   990
%\texttt{\Grid{bbc}} & $\rightarrow$ & $\left\{((\texttt{\Grid{b}}, \texttt{\Grid{b}}), \texttt{\Grid{c}})\right\}$\\
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   991
%\texttt{\Grid{aac}} & $\rightarrow$ & $\varnothing$
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   992
%\end{tabular}
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   993
%\end{center}
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   994
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 177
diff changeset
   995
173
7cfb7a6f7c99 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   996
680
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   997
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
   998
173
7cfb7a6f7c99 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   999
7cfb7a6f7c99 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
  1000
%%% Local Variables: 
7cfb7a6f7c99 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
  1001
%%% mode: latex  
7cfb7a6f7c99 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
  1002
%%% TeX-master: t
7cfb7a6f7c99 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
  1003
%%% End: 
680
242c1f0e60df updated
Christian Urban <urbanc@in.tum.de>
parents: 665
diff changeset
  1004