author | Christian Urban <urbanc@in.tum.de> |
Thu, 14 Nov 2019 01:21:02 +0000 | |
changeset 686 | 05cfce0fdef7 |
parent 682 | 553b4d4e3719 |
child 691 | 991849dfbcb1 |
permissions | -rw-r--r-- |
665 | 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 | 5 |
\usepackage{../grammar} |
173
7cfb7a6f7c99
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
6 |
|
545 | 7 |
% epsilon and left-recursion elimination |
8 |
% http://www.mollypages.org/page/grammar/index.mp |
|
9 |
||
618 | 10 |
%% parsing scala files |
11 |
%%https://scalameta.org/ |
|
12 |
||
173
7cfb7a6f7c99
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
13 |
\begin{document} |
7cfb7a6f7c99
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
14 |
|
385
7f8516ff408d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
362
diff
changeset
|
15 |
\section*{Handout 5 (Grammars \& Parser)} |
173
7cfb7a6f7c99
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
16 |
|
680 | 17 |
While regular expressions are very useful for lexing and for recognising |
18 |
many patterns in strings (like email addresses), they have their |
|
19 |
limitations. For example there is no regular expression that can |
|
682 | 20 |
recognise the language $a^nb^n$ (where you have strings starting with $n$ $a$'s |
680 | 21 |
followed by the same amount of $b$'s). Another example for which there |
22 |
exists no regular expression is the language of well-parenthesised |
|
23 |
expressions. In languages like Lisp, which use parentheses rather |
|
24 |
extensively, it might be of interest to know whether the following two |
|
25 |
expressions are well-parenthesised or not (the left one is, the right |
|
26 |
one is not): |
|
173
7cfb7a6f7c99
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
27 |
|
7cfb7a6f7c99
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
28 |
\begin{center} |
7cfb7a6f7c99
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
29 |
$(((()()))())$ \hspace{10mm} $(((()()))()))$ |
7cfb7a6f7c99
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
30 |
\end{center} |
7cfb7a6f7c99
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
31 |
|
362
57ea439feaff
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
360
diff
changeset
|
32 |
\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
|
33 |
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
|
34 |
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
|
35 |
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
|
36 |
languages}. They include the regular languages as the picture |
582 | 37 |
below about language classes shows: |
173
7cfb7a6f7c99
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
38 |
|
7cfb7a6f7c99
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
39 |
|
7cfb7a6f7c99
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
40 |
\begin{center} |
7cfb7a6f7c99
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
41 |
\begin{tikzpicture} |
297
5c51839c88fd
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
42 |
[rect/.style={draw=black!50, |
5c51839c88fd
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
43 |
top color=white,bottom color=black!20, |
5c51839c88fd
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
44 |
rectangle, very thick, rounded corners}] |
173
7cfb7a6f7c99
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
45 |
|
297
5c51839c88fd
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
46 |
\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
|
47 |
\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
|
48 |
\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
|
49 |
\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
|
50 |
\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
|
51 |
\end{tikzpicture} |
7cfb7a6f7c99
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
52 |
\end{center} |
7cfb7a6f7c99
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
53 |
|
582 | 54 |
\noindent Each ``bubble'' stands for sets of languages (remember |
55 |
languages are sets of strings). As indicated the set of regular |
|
680 | 56 |
languages is fully included inside the context-free languages, |
582 | 57 |
meaning every regular language is also context-free, but not vice |
665 | 58 |
versa. Below I will let you think, for example, what the context-free |
582 | 59 |
grammar is for the language corresponding to the regular expression |
60 |
$(aaa)^*a$. |
|
61 |
||
62 |
Because of their convenience, context-free languages play an important |
|
63 |
role in `day-to-day' text processing and in programming |
|
64 |
languages. Context-free in this setting means that ``words'' have one |
|
680 | 65 |
meaning only and this meaning is independent from the context |
66 |
the ``words'' appear in. For example ambiguity issues like |
|
582 | 67 |
|
68 |
\begin{center} |
|
682 | 69 |
\tt Time flies like an arrow. Fruit flies like bananas. |
582 | 70 |
\end{center} |
71 |
||
72 |
\noindent |
|
680 | 73 |
from natural languages were the meaning of \emph{flies} depends on the |
686 | 74 |
surrounding \emph{context} are avoided as much as possible. Here is |
75 |
an interesting video about C++ being not a context-free language |
|
76 |
||
77 |
\begin{center} |
|
78 |
\url{https://www.youtube.com/watch?v=OzK8pUu4UfM} |
|
79 |
\end{center} |
|
582 | 80 |
|
81 |
Context-free languages are usually specified by grammars. For example |
|
82 |
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
|
83 |
|
459 | 84 |
\begin{plstx}[margin=3cm] |
85 |
: \meta{P} ::= ( \cdot \meta{P} \cdot ) \cdot \meta{P} |
|
86 |
| \epsilon\\ |
|
87 |
\end{plstx} |
|
88 |
||
362
57ea439feaff
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
360
diff
changeset
|
89 |
\noindent |
57ea439feaff
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
360
diff
changeset
|
90 |
or a grammar for recognising strings consisting of ones is |
57ea439feaff
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
360
diff
changeset
|
91 |
|
459 | 92 |
\begin{plstx}[margin=3cm] |
93 |
: \meta{O} ::= 1 \cdot \meta{O} |
|
94 |
| 1\\ |
|
95 |
\end{plstx} |
|
362
57ea439feaff
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
360
diff
changeset
|
96 |
|
57ea439feaff
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
360
diff
changeset
|
97 |
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
|
98 |
from \emph{terminal symbols} (usually lower-case letters) and |
582 | 99 |
\emph{non-terminal symbols} (upper-case letters written in |
100 |
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
|
101 |
the shape |
173
7cfb7a6f7c99
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
102 |
|
459 | 103 |
\begin{plstx}[margin=3cm] |
104 |
: \meta{NT} ::= rhs\\ |
|
105 |
\end{plstx} |
|
173
7cfb7a6f7c99
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
106 |
|
362
57ea439feaff
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
360
diff
changeset
|
107 |
\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
|
108 |
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
|
109 |
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
|
110 |
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
|
111 |
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
|
112 |
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
|
113 |
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
|
114 |
rules. For example |
173
7cfb7a6f7c99
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
115 |
|
459 | 116 |
\begin{plstx}[margin=3cm] |
117 |
: \meta{NT} ::= rhs_1 |
|
118 |
| rhs_2\\ |
|
119 |
\end{plstx} |
|
173
7cfb7a6f7c99
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
120 |
|
459 | 121 |
\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
|
122 |
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
|
123 |
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
|
124 |
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
|
125 |
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
|
126 |
can be given as follows |
7cfb7a6f7c99
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
127 |
|
459 | 128 |
\begin{plstx}[margin=3cm,one per line] |
129 |
\mbox{\rm (1)}: \meta{E} ::= \meta{N}\\ |
|
130 |
\mbox{\rm (2)}: \meta{E} ::= \meta{E} \cdot + \cdot \meta{E}\\ |
|
131 |
\mbox{\rm (3)}: \meta{E} ::= \meta{E} \cdot - \cdot \meta{E}\\ |
|
132 |
\mbox{\rm (4)}: \meta{E} ::= \meta{E} \cdot * \cdot \meta{E}\\ |
|
133 |
\mbox{\rm (5)}: \meta{E} ::= ( \cdot \meta{E} \cdot )\\ |
|
134 |
\mbox{\rm (6\ldots)}: \meta{N} ::= \meta{N} \cdot \meta{N} |
|
135 |
\mid 0 \mid 1 \mid \ldots \mid 9\\ |
|
136 |
\end{plstx} |
|
173
7cfb7a6f7c99
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
137 |
|
459 | 138 |
\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
|
139 |
\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
|
140 |
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
|
141 |
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
|
142 |
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
|
143 |
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
|
144 |
follows: |
175
5801e8c0e528
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
173
diff
changeset
|
145 |
|
5801e8c0e528
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
173
diff
changeset
|
146 |
\begin{center} |
362
57ea439feaff
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
360
diff
changeset
|
147 |
\begin{tabular}{lll@{\hspace{2cm}}l} |
459 | 148 |
\meta{E} & $\rightarrow$ & $\meta{E}+\meta{E}$ & by (2)\\ |
149 |
& $\rightarrow$ & $(\meta{E})+\meta{E}$ & by (5)\\ |
|
150 |
& $\rightarrow$ & $(\meta{E}+\meta{E})+\meta{E}$ & by (2)\\ |
|
151 |
& $\rightarrow$ & $(\meta{E}+\meta{E})+\meta{N}$ & by (1)\\ |
|
152 |
& $\rightarrow$ & $(\meta{E}+\meta{E})+3$ & by (6\dots)\\ |
|
153 |
& $\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
|
154 |
& $\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
|
155 |
\end{tabular} |
5801e8c0e528
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
173
diff
changeset
|
156 |
\end{center} |
5801e8c0e528
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
173
diff
changeset
|
157 |
|
362
57ea439feaff
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
360
diff
changeset
|
158 |
\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
|
159 |
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
|
160 |
merged several steps into one. |
57ea439feaff
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
360
diff
changeset
|
161 |
|
57ea439feaff
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
360
diff
changeset
|
162 |
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
|
163 |
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
|
164 |
derivable by a derivation, that is |
175
5801e8c0e528
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
173
diff
changeset
|
165 |
|
5801e8c0e528
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
173
diff
changeset
|
166 |
\begin{center} |
5801e8c0e528
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
173
diff
changeset
|
167 |
$\{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
|
168 |
\end{center} |
5801e8c0e528
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
173
diff
changeset
|
169 |
|
5801e8c0e528
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
173
diff
changeset
|
170 |
\noindent |
680 | 171 |
A \emph{parse-tree} encodes how a string is derived with the starting |
172 |
symbol on top and each non-terminal containing a subtree for how it is |
|
173 |
replaced in a derivation. The parse tree for the string $(1 + 23)+4$ is |
|
174 |
as follows: |
|
175
5801e8c0e528
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
173
diff
changeset
|
175 |
|
5801e8c0e528
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
173
diff
changeset
|
176 |
\begin{center} |
5801e8c0e528
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
173
diff
changeset
|
177 |
\begin{tikzpicture}[level distance=8mm, black] |
665 | 178 |
\node {\meta{E}} |
179 |
child {node {\meta{E} } |
|
175
5801e8c0e528
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
173
diff
changeset
|
180 |
child {node {$($}} |
665 | 181 |
child {node {\meta{E} } |
182 |
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
|
183 |
child {node {$+$}} |
665 | 184 |
child {node {\meta{E} } |
185 |
child {node {\meta{N} } child {node {$2$}}} |
|
186 |
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
|
187 |
} |
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 |
child {node {$)$}} |
5801e8c0e528
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
173
diff
changeset
|
190 |
} |
5801e8c0e528
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
173
diff
changeset
|
191 |
child {node {$+$}} |
665 | 192 |
child {node {\meta{E} } |
193 |
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
|
194 |
}; |
5801e8c0e528
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
173
diff
changeset
|
195 |
\end{tikzpicture} |
5801e8c0e528
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
173
diff
changeset
|
196 |
\end{center} |
5801e8c0e528
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
173
diff
changeset
|
197 |
|
362
57ea439feaff
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
360
diff
changeset
|
198 |
\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
|
199 |
they encode the structure of how a string is derived by a |
680 | 200 |
grammar. |
201 |
||
202 |
Before we come to the problem of constructing such parse-trees, we need |
|
203 |
to consider the following two properties of grammars. A grammar is |
|
204 |
\emph{left-recursive} if there is a derivation starting from a |
|
205 |
non-terminal, say \meta{NT} which leads to a string which again starts |
|
206 |
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
|
207 |
|
175
5801e8c0e528
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
173
diff
changeset
|
208 |
\begin{center} |
665 | 209 |
$\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
|
210 |
\end{center} |
5801e8c0e528
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
173
diff
changeset
|
211 |
|
680 | 212 |
\noindent It can be easily seen that the grammar above for arithmetic |
213 |
expressions is left-recursive: for example the rules $\meta{E} |
|
214 |
\rightarrow \meta{E}\cdot + \cdot \meta{E}$ and $\meta{N} \rightarrow |
|
215 |
\meta{N}\cdot \meta{N}$ show that this grammar is left-recursive. But |
|
216 |
note that left-recursiveness can involve more than one step in the |
|
217 |
derivation. The problem with left-recursive grammars is that some |
|
218 |
algorithms cannot cope with them: with left-recursive grammars they will |
|
219 |
fall into a loop. Fortunately every left-recursive grammar can be |
|
220 |
transformed into one that is not left-recursive, although this |
|
221 |
transformation might make the grammar less ``human-readable''. For |
|
222 |
example if we want to give a non-left-recursive grammar for numbers we |
|
223 |
might specify |
|
175
5801e8c0e528
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
173
diff
changeset
|
224 |
|
5801e8c0e528
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
173
diff
changeset
|
225 |
\begin{center} |
665 | 226 |
$\meta{N} \;\;\rightarrow\;\; 0\;|\;\ldots\;|\;9\;|\; |
227 |
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
|
228 |
\end{center} |
5801e8c0e528
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
173
diff
changeset
|
229 |
|
362
57ea439feaff
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
360
diff
changeset
|
230 |
\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
|
231 |
string, but we will never be able to derive a string of the |
665 | 232 |
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
|
233 |
|
362
57ea439feaff
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
360
diff
changeset
|
234 |
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
|
235 |
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
|
236 |
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
|
237 |
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
|
238 |
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
|
239 |
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
|
240 |
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
|
241 |
|
5801e8c0e528
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
173
diff
changeset
|
242 |
\begin{center} |
5801e8c0e528
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
173
diff
changeset
|
243 |
\begin{tabular}{c@{\hspace{10mm}}c} |
5801e8c0e528
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
173
diff
changeset
|
244 |
\begin{tikzpicture}[level distance=8mm, black] |
665 | 245 |
\node {\meta{E} } |
246 |
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
|
247 |
child {node {$+$}} |
665 | 248 |
child {node {\meta{E} } |
249 |
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
|
250 |
child {node {$+$}} |
665 | 251 |
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
|
252 |
} |
5801e8c0e528
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
173
diff
changeset
|
253 |
; |
5801e8c0e528
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
173
diff
changeset
|
254 |
\end{tikzpicture} |
5801e8c0e528
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
173
diff
changeset
|
255 |
& |
5801e8c0e528
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
173
diff
changeset
|
256 |
\begin{tikzpicture}[level distance=8mm, black] |
665 | 257 |
\node {\meta{E} } |
258 |
child {node {\meta{E} } |
|
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 | 261 |
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
|
262 |
} |
5801e8c0e528
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
173
diff
changeset
|
263 |
child {node {$+$}} |
665 | 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 |
\end{tikzpicture} |
5801e8c0e528
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
173
diff
changeset
|
267 |
\end{tabular} |
5801e8c0e528
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
173
diff
changeset
|
268 |
\end{center} |
5801e8c0e528
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
173
diff
changeset
|
269 |
|
680 | 270 |
\noindent In particular in programming languages we will try to avoid |
271 |
ambiguous grammars because two different parse-trees for a string mean a |
|
272 |
program can be interpreted in two different ways. In such cases we have |
|
273 |
to somehow make sure the two different ways do not matter, or |
|
274 |
disambiguate the grammar in some other way (for example making the $+$ |
|
275 |
left-associative). Unfortunately already the problem of deciding whether |
|
276 |
a grammar is ambiguous or not is in general undecidable. But in simple |
|
277 |
instance (the ones we deal with in this module) one can usually see when |
|
278 |
a grammar is ambiguous. |
|
279 |
||
280 |
\subsection*{Removing Left-Recursion} |
|
281 |
||
282 |
Let us come back to the problem of left-recursion and consider the |
|
283 |
following grammar for binary numbers: |
|
284 |
||
285 |
\begin{plstx}[margin=1cm] |
|
286 |
: \meta{B} ::= \meta{B} \cdot \meta{B} | 0 | 1\\ |
|
287 |
\end{plstx} |
|
288 |
||
289 |
\noindent |
|
290 |
It is clear that this grammar can create all binary numbers, but |
|
291 |
it is also clear that this grammar is left-recursive. Giving this |
|
292 |
grammar as is to parser combinators will result in an infinite |
|
293 |
loop. Fortunately, every left-recursive grammar can be translated |
|
294 |
into one that is not left-recursive with the help of some |
|
295 |
transformation rules. Suppose we identified the ``offensive'' |
|
296 |
rule, then we can separate the grammar into this offensive rule |
|
297 |
and the ``rest'': |
|
298 |
||
299 |
\begin{plstx}[margin=1cm] |
|
300 |
: \meta{B} ::= \underbrace{\meta{B} \cdot \meta{B}}_{\textit{lft-rec}} |
|
301 |
| \underbrace{0 \;\;|\;\; 1}_{\textit{rest}}\\ |
|
302 |
\end{plstx} |
|
303 |
||
304 |
\noindent |
|
305 |
To make the idea of the transformation clearer, suppose the left-recursive |
|
306 |
rule is of the form $\meta{B}\alpha$ (the left-recursive non-terminal |
|
307 |
followed by something called $\alpha$) and the ``rest'' is called $\beta$. |
|
308 |
That means our grammar looks schematically as follows |
|
309 |
||
310 |
\begin{plstx}[margin=1cm] |
|
311 |
: \meta{B} ::= \meta{B} \cdot \alpha | \beta\\ |
|
312 |
\end{plstx} |
|
313 |
||
314 |
\noindent |
|
315 |
To get rid of the left-recursion, we are required to introduce |
|
316 |
a new non-terminal, say $\meta{B'}$ and transform the rule |
|
317 |
as follows: |
|
318 |
||
319 |
\begin{plstx}[margin=1cm] |
|
320 |
: \meta{B} ::= \beta \cdot \meta{B'}\\ |
|
321 |
: \meta{B'} ::= \alpha \cdot \meta{B'} | \epsilon\\ |
|
322 |
\end{plstx} |
|
323 |
||
324 |
\noindent |
|
325 |
In our example of binary numbers we would after the transformation |
|
326 |
end up with the rules |
|
327 |
||
328 |
\begin{plstx}[margin=1cm] |
|
329 |
: \meta{B} ::= 0 \cdot \meta{B'} | 1 \cdot \meta{B'}\\ |
|
330 |
: \meta{B'} ::= \meta{B} \cdot \meta{B'} | \epsilon\\ |
|
331 |
\end{plstx} |
|
332 |
||
333 |
\noindent |
|
334 |
A little thought should convince you that this grammar still derives |
|
335 |
all the binary numbers (for example 0 and 1 are derivable because $\meta{B'}$ |
|
336 |
can be $\epsilon$). Less clear might be why this grammar is non-left recursive. |
|
337 |
For $\meta{B'}$ it is relatively clear because we will never be |
|
338 |
able to derive things like |
|
339 |
||
340 |
\begin{center} |
|
341 |
$\meta{B'} \rightarrow\ldots\rightarrow \meta{B'}\cdot\ldots$ |
|
342 |
\end{center} |
|
343 |
||
344 |
\noindent |
|
345 |
because there will always be a $\meta{B}$ in front of a $\meta{B'}$, and |
|
346 |
$\meta{B}$ now has always a $0$ or $1$ in front, so a $\meta{B'}$ can |
|
347 |
never be in the first place. The reasoning is similar for $\meta{B}$: |
|
348 |
the $0$ and $1$ in the rule for $\meta{B}$ ``protect'' it from becoming |
|
349 |
left-recursive. This transformation does not mean the grammar is the |
|
350 |
simplest left-recursive grammar for binary numbers. For example the |
|
351 |
following grammar would do as well |
|
352 |
||
353 |
\begin{plstx}[margin=1cm] |
|
354 |
: \meta{B} ::= 0 \cdot \meta{B} | 1 \cdot \meta{B} | 0 | 1\\ |
|
355 |
\end{plstx} |
|
356 |
||
357 |
\noindent |
|
358 |
The point is that we can in principle transform every left-recursive |
|
359 |
grammar into one that is non-left-recursive one. This explains why often |
|
360 |
the following grammar is used for arithmetic expressions: |
|
361 |
||
362 |
\begin{plstx}[margin=1cm] |
|
363 |
: \meta{E} ::= \meta{T} | \meta{T} \cdot + \cdot \meta{E} | \meta{T} \cdot - \cdot \meta{E}\\ |
|
364 |
: \meta{T} ::= \meta{F} | \meta{F} \cdot * \cdot \meta{T}\\ |
|
365 |
: \meta{F} ::= num\_token | ( \cdot \meta{E} \cdot )\\ |
|
366 |
\end{plstx} |
|
176
3c2653fc8b5a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
175
diff
changeset
|
367 |
|
680 | 368 |
\noindent |
369 |
In this grammar all $\meta{E}$xpressions, $\meta{T}$erms and $\meta{F}$actors |
|
370 |
are in some way protected from being left-recusive. For example if you |
|
371 |
start $\meta{E}$ you can derive another one by going through $\meta{T}$, then |
|
372 |
$\meta{F}$, but then $\meta{E}$ is protected by the open-parenthesis. |
|
373 |
||
374 |
\subsection*{Removing $\epsilon$-Rules and CYK-Algorithm} |
|
375 |
||
376 |
I showed above that the non-left-recursive grammar for binary numbers is |
|
377 |
||
378 |
\begin{plstx}[margin=1cm] |
|
379 |
: \meta{B} ::= 0 \cdot \meta{B'} | 1 \cdot \meta{B'}\\ |
|
380 |
: \meta{B'} ::= \meta{B} \cdot \meta{B'} | \epsilon\\ |
|
381 |
\end{plstx} |
|
382 |
||
383 |
\noindent |
|
384 |
The transformation made the original grammar non-left-recursive, but at |
|
385 |
the expense of introducing an $\epsilon$ in the second rule. Having an |
|
386 |
explicit $\epsilon$-rule is annoying to, not in terms of looping, but in |
|
387 |
terms of efficiency. The reason is that the $\epsilon$-rule always |
|
388 |
applies but since it recognises the empty string, it does not make any |
|
389 |
progress with recognising a string. Better are rules like $( \cdot |
|
390 |
\meta{E} \cdot )$ where something of the input is consumed. Getting |
|
391 |
rid of $\epsilon$-rules is also important for the CYK parsing algorithm, |
|
392 |
which can give us an insight into the complexity class of parsing. |
|
393 |
||
394 |
It turns out we can also by some generic transformations eliminate |
|
395 |
$\epsilon$-rules from grammars. Consider again the grammar above for |
|
396 |
binary numbers where have a rule $\meta{B'} ::= \epsilon$. In this case |
|
397 |
we look for rules of the (generic) form \mbox{$\meta{A} := |
|
398 |
\alpha\cdot\meta{B'}\cdot\beta$}. That is there are rules that use |
|
399 |
$\meta{B'}$ and something ($\alpha$) is in front of $\meta{B'}$ and |
|
400 |
something follows ($\beta$). Such rules need to be replaced by |
|
401 |
additional rules of the form \mbox{$\meta{A} := \alpha\cdot\beta$}. |
|
402 |
In our running example there are the two rules for $\meta{B}$ which |
|
403 |
fall into this category |
|
404 |
||
405 |
\begin{plstx}[margin=1cm] |
|
406 |
: \meta{B} ::= 0 \cdot \meta{B'} | 1 \cdot \meta{B'}\\ |
|
407 |
\end{plstx} |
|
408 |
||
409 |
\noindent To follow the general scheme of the transfromation, |
|
410 |
the $\alpha$ is either is either $0$ or $1$, and the $\beta$ happens |
|
411 |
to be empty. SO we need to generate new rules for the form |
|
412 |
\mbox{$\meta{A} := \alpha\cdot\beta$}, which in our particular |
|
413 |
example means we obtain |
|
414 |
||
415 |
\begin{plstx}[margin=1cm] |
|
416 |
: \meta{B} ::= 0 \cdot \meta{B'} | 1 \cdot \meta{B'} | 0 | 1\\ |
|
417 |
\end{plstx} |
|
418 |
||
419 |
\noindent |
|
420 |
Unfortunately $\meta{B'}$ is also used in the rule |
|
421 |
||
422 |
\begin{plstx}[margin=1cm] |
|
423 |
: \meta{B'} ::= \meta{B} \cdot \meta{B'}\\ |
|
424 |
\end{plstx} |
|
425 |
||
426 |
\noindent |
|
427 |
For this we repeat the transformation, giving |
|
428 |
||
429 |
\begin{plstx}[margin=1cm] |
|
430 |
: \meta{B'} ::= \meta{B} \cdot \meta{B'} | \meta{B}\\ |
|
431 |
\end{plstx} |
|
432 |
||
433 |
\noindent |
|
434 |
In this case $\alpha$ was substituted with $\meta{B}$ and $\beta$ |
|
435 |
was again empty. Once no rule is left over, we can simply throw |
|
436 |
away the $\epsilon$ rule. This gives the grammar |
|
437 |
||
438 |
\begin{plstx}[margin=1cm] |
|
439 |
: \meta{B} ::= 0 \cdot \meta{B'} | 1 \cdot \meta{B'} | 0 | 1\\ |
|
440 |
: \meta{B'} ::= \meta{B} \cdot \meta{B'} | \meta{B}\\ |
|
441 |
\end{plstx} |
|
442 |
||
443 |
\noindent |
|
444 |
I let you think about whether this grammar can still recognise all |
|
445 |
binary numbers and whether this grammar is non-left-recursive. The |
|
446 |
precise statement for the transformation of removing $\epsilon$-rules is |
|
447 |
that if the original grammar was able to recognise only non-empty |
|
448 |
strings, then the transformed grammar will be equivalent (matching the |
|
449 |
same set of strings); if the original grammar was able to match the |
|
450 |
empty string, then the transformed grammar will be able to match the |
|
451 |
same strings, \emph{except} the empty string. So the $\epsilon$-removal |
|
452 |
does not preserve equivalence of grammars, but the small defect with the |
|
453 |
empty string is not important for practical purposes. |
|
454 |
||
455 |
So why are these transformations all useful? Well apart from making the |
|
456 |
parser combinators work (remember they cannot deal with left-recursion and |
|
457 |
are inefficient with $\epsilon$-rules), a second reason is that they help |
|
458 |
with getting any insight into the complexity of the parsing problem. |
|
459 |
The parser combinators are very easy to implement, but are far from the |
|
460 |
most efficient way of processing input (they can blow up exponentially |
|
461 |
with ambiguous grammars). The question remains what is the best possible |
|
462 |
complexity for parsing? It turns out that this is $O(n^3)$ for context-free |
|
463 |
languages. |
|
464 |
||
465 |
To answer the question about complexity, let me describe next the CYK |
|
466 |
algorithm (named after the authors Cocke–Younger–Kasami). This algorithm |
|
681 | 467 |
works with grammars that are in \emph{Chomsky normalform}. In Chomsky |
468 |
normalform all rules must be of the form $\meta{A} ::= a$, where $a$ is |
|
469 |
a terminal, or $\meta{A} ::= \meta{B}\cdot \meta{C}$, where $\meta{B}$ and |
|
470 |
$\meta{B}$ need to be non-terminals. And no rule can contain $\epsilon$. |
|
471 |
The following grammar is in Chomsky normalform: |
|
472 |
||
473 |
\begin{plstx}[margin=1cm] |
|
682 | 474 |
: \meta{S} ::= \meta{N}\cdot \meta{P}\\ |
475 |
: \meta{P} ::= \meta{V}\cdot \meta{N}\\ |
|
476 |
: \meta{N} ::= \meta{N}\cdot \meta{N}\\ |
|
477 |
: \meta{N} ::= \meta{A}\cdot \meta{N}\\ |
|
478 |
: \meta{N} ::= \texttt{student} | \texttt{trainer} | \texttt{team} |
|
479 |
| \texttt{trains}\\ |
|
480 |
: \meta{V} ::= \texttt{trains} | \texttt{team}\\ |
|
481 |
: \meta{A} ::= \texttt{The} | \texttt{the}\\ |
|
681 | 482 |
\end{plstx} |
483 |
||
484 |
\noindent |
|
485 |
where $\meta{S}$ is the start symbol and $\meta{S}$, $\meta{P}$, |
|
486 |
$\meta{N}$, $\meta{V}$ and $\meta{A}$ are non-terminals. The ``words'' |
|
487 |
are terminals. The rough idea behind this grammar is that $\meta{S}$ |
|
488 |
stands for a sentence, $\meta{P}$ is a predicate, $\meta{N}$ is a noun |
|
489 |
and so on. For example the rule \mbox{$\meta{P} ::= \meta{V}\cdot |
|
490 |
\meta{N}$} states that a predicate can be a verb followed by a noun. |
|
491 |
Now the question is whether the string |
|
492 |
||
493 |
\begin{center} |
|
494 |
\texttt{The trainer trains the student team} |
|
495 |
\end{center} |
|
496 |
||
497 |
\noindent |
|
498 |
is recognised by the grammar. The CYK algorithm starts with the |
|
499 |
following triangular data structure. |
|
680 | 500 |
|
682 | 501 |
\begin{figure}[t] |
502 |
\begin{center} |
|
503 |
\begin{tikzpicture}[scale=0.8,line width=0.8mm] |
|
504 |
\draw (-2,0) -- (4,0); |
|
505 |
\draw (-2,1) -- (4,1); |
|
506 |
\draw (-2,2) -- (3,2); |
|
507 |
\draw (-2,3) -- (2,3); |
|
508 |
\draw (-2,4) -- (1,4); |
|
509 |
\draw (-2,5) -- (0,5); |
|
510 |
\draw (-2,6) -- (-1,6); |
|
511 |
||
512 |
\draw (0,0) -- (0, 5); |
|
513 |
\draw (1,0) -- (1, 4); |
|
514 |
\draw (2,0) -- (2, 3); |
|
515 |
\draw (3,0) -- (3, 2); |
|
516 |
\draw (4,0) -- (4, 1); |
|
517 |
\draw (-1,0) -- (-1, 6); |
|
518 |
\draw (-2,0) -- (-2, 6); |
|
519 |
||
520 |
\draw (-1.5,-0.5) node {\footnotesize{}\texttt{The}}; |
|
521 |
\draw (-0.5,-1.0) node {\footnotesize{}\texttt{trainer}}; |
|
522 |
\draw ( 0.5,-0.5) node {\footnotesize{}\texttt{trains}}; |
|
523 |
\draw ( 1.5,-1.0) node {\footnotesize{}\texttt{the}}; |
|
524 |
\draw ( 2.5,-0.5) node {\footnotesize{}\texttt{student}}; |
|
525 |
\draw ( 3.5,-1.0) node {\footnotesize{}\texttt{team}}; |
|
526 |
||
527 |
\draw (-1.5,0.5) node {$A$}; |
|
528 |
\draw (-0.5,0.5) node {$N$}; |
|
529 |
\draw ( 0.5,0.5) node {$N,V$}; |
|
530 |
\draw ( 1.5,0.5) node {$A$}; |
|
531 |
\draw ( 2.5,0.5) node {$N$}; |
|
532 |
\draw ( 3.5,0.5) node {$N,V$}; |
|
533 |
||
534 |
\draw (-2.4, 5.5) node {$1$}; |
|
535 |
\draw (-2.4, 4.5) node {$2$}; |
|
536 |
\draw (-2.4, 3.5) node {$3$}; |
|
537 |
\draw (-2.4, 2.5) node {$4$}; |
|
538 |
\draw (-2.4, 1.5) node {$5$}; |
|
539 |
\draw (-2.4, 0.5) node {$6$}; |
|
540 |
\end{tikzpicture} |
|
541 |
\end{center} |
|
542 |
\end{figure} |
|
680 | 543 |
|
544 |
\end{document} |
|
545 |
||
546 |
||
547 |
%%% Parser combinators are now part of handout 6 |
|
459 | 548 |
|
549 |
\subsection*{Parser Combinators} |
|
550 |
||
362
57ea439feaff
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
360
diff
changeset
|
551 |
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
|
552 |
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
|
553 |
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
|
554 |
resemble grammar rules. Imagine that a grammar describes the |
665 | 555 |
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
|
556 |
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
|
557 |
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
|
558 |
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
|
559 |
``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
|
560 |
be functions of type |
176
3c2653fc8b5a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
175
diff
changeset
|
561 |
|
3c2653fc8b5a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
175
diff
changeset
|
562 |
\begin{center} |
177
53def1fbf472
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
176
diff
changeset
|
563 |
\texttt{I $\Rightarrow$ Set[(T, I)]} |
176
3c2653fc8b5a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
175
diff
changeset
|
564 |
\end{center} |
3c2653fc8b5a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
175
diff
changeset
|
565 |
|
362
57ea439feaff
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
360
diff
changeset
|
566 |
\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
|
567 |
\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
|
568 |
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
|
569 |
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
|
570 |
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
|
571 |
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
|
572 |
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
|
573 |
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
|
574 |
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
|
575 |
say the string |
183
b17eff695c7f
added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
177
diff
changeset
|
576 |
|
b17eff695c7f
added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
177
diff
changeset
|
577 |
\begin{center} |
b17eff695c7f
added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
177
diff
changeset
|
578 |
\tt\Grid{iffoo\VS testbar} |
b17eff695c7f
added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
177
diff
changeset
|
579 |
\end{center} |
b17eff695c7f
added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
177
diff
changeset
|
580 |
|
362
57ea439feaff
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
360
diff
changeset
|
581 |
\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
|
582 |
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
|
583 |
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
|
584 |
|
183
b17eff695c7f
added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
177
diff
changeset
|
585 |
\begin{center} |
b17eff695c7f
added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
177
diff
changeset
|
586 |
$\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
|
587 |
\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
|
588 |
\end{center} |
b17eff695c7f
added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
177
diff
changeset
|
589 |
|
362
57ea439feaff
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
360
diff
changeset
|
590 |
\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
|
591 |
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
|
592 |
`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
|
593 |
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
|
594 |
\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
|
595 |
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
|
596 |
return the empty set $\{\}$. This will indicate |
57ea439feaff
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
360
diff
changeset
|
597 |
something ``went wrong''. |
183
b17eff695c7f
added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
177
diff
changeset
|
598 |
|
b17eff695c7f
added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
177
diff
changeset
|
599 |
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
|
600 |
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
|
601 |
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
|
602 |
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
|
603 |
\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
|
604 |
\mbox{\texttt{Set[(T, I)]}}. |
53def1fbf472
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
176
diff
changeset
|
605 |
|
53def1fbf472
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
176
diff
changeset
|
606 |
\begin{center} |
53def1fbf472
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
176
diff
changeset
|
607 |
\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
|
608 |
abstract class Parser[I, T] { |
53def1fbf472
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
176
diff
changeset
|
609 |
def parse(ts: I): Set[(T, I)] |
53def1fbf472
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
176
diff
changeset
|
610 |
|
53def1fbf472
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
176
diff
changeset
|
611 |
def parse_all(ts: I): Set[T] = |
53def1fbf472
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
176
diff
changeset
|
612 |
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
|
613 |
yield head |
53def1fbf472
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
176
diff
changeset
|
614 |
} |
53def1fbf472
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
176
diff
changeset
|
615 |
\end{lstlisting} |
53def1fbf472
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
176
diff
changeset
|
616 |
\end{center} |
176
3c2653fc8b5a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
175
diff
changeset
|
617 |
|
177
53def1fbf472
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
176
diff
changeset
|
618 |
\noindent |
183
b17eff695c7f
added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
177
diff
changeset
|
619 |
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
|
620 |
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
|
621 |
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
|
622 |
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
|
623 |
|
177
53def1fbf472
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
176
diff
changeset
|
624 |
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
|
625 |
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
|
626 |
|
177
53def1fbf472
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
176
diff
changeset
|
627 |
\begin{itemize} |
53def1fbf472
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
176
diff
changeset
|
628 |
\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
|
629 |
the set $\{(c, \textit{tail of}\; s)\}$ |
53def1fbf472
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
176
diff
changeset
|
630 |
\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
|
631 |
\end{itemize} |
53def1fbf472
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
176
diff
changeset
|
632 |
|
53def1fbf472
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
176
diff
changeset
|
633 |
\noindent |
53def1fbf472
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
176
diff
changeset
|
634 |
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
|
635 |
\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
|
636 |
The code in Scala is as follows: |
53def1fbf472
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
176
diff
changeset
|
637 |
|
53def1fbf472
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
176
diff
changeset
|
638 |
\begin{center} |
53def1fbf472
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
176
diff
changeset
|
639 |
\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
|
640 |
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
|
641 |
def parse(sb: String) = |
53def1fbf472
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
176
diff
changeset
|
642 |
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
|
643 |
} |
53def1fbf472
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
176
diff
changeset
|
644 |
\end{lstlisting} |
53def1fbf472
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
176
diff
changeset
|
645 |
\end{center} |
176
3c2653fc8b5a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
175
diff
changeset
|
646 |
|
183
b17eff695c7f
added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
177
diff
changeset
|
647 |
\noindent |
b17eff695c7f
added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
177
diff
changeset
|
648 |
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
|
649 |
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
|
650 |
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
|
651 |
\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
|
652 |
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
|
653 |
|
b17eff695c7f
added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
177
diff
changeset
|
654 |
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
|
655 |
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
|
656 |
parser combinator is as follows. |
b17eff695c7f
added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
177
diff
changeset
|
657 |
|
b17eff695c7f
added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
177
diff
changeset
|
658 |
\begin{center} |
b17eff695c7f
added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
177
diff
changeset
|
659 |
\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
|
660 |
class AltParser[I, T] |
b17eff695c7f
added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
177
diff
changeset
|
661 |
(p: => Parser[I, T], |
b17eff695c7f
added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
177
diff
changeset
|
662 |
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
|
663 |
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
|
664 |
} |
b17eff695c7f
added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
177
diff
changeset
|
665 |
\end{lstlisting} |
b17eff695c7f
added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
177
diff
changeset
|
666 |
\end{center} |
b17eff695c7f
added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
177
diff
changeset
|
667 |
|
b17eff695c7f
added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
177
diff
changeset
|
668 |
\noindent |
b17eff695c7f
added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
177
diff
changeset
|
669 |
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
|
670 |
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
|
671 |
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
|
672 |
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
|
673 |
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
|
674 |
\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
|
675 |
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
|
676 |
\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
|
677 |
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
|
678 |
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
|
679 |
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
|
680 |
|
b17eff695c7f
added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
177
diff
changeset
|
681 |
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
|
682 |
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
|
683 |
|
b17eff695c7f
added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
177
diff
changeset
|
684 |
\begin{center} |
b17eff695c7f
added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
177
diff
changeset
|
685 |
\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
|
686 |
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
|
687 |
\end{lstlisting} |
b17eff695c7f
added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
177
diff
changeset
|
688 |
\end{center} |
b17eff695c7f
added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
177
diff
changeset
|
689 |
|
b17eff695c7f
added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
177
diff
changeset
|
690 |
\noindent |
b17eff695c7f
added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
177
diff
changeset
|
691 |
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
|
692 |
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
|
693 |
|
b17eff695c7f
added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
177
diff
changeset
|
694 |
\begin{center} |
b17eff695c7f
added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
177
diff
changeset
|
695 |
\begin{tabular}{rcl} |
b17eff695c7f
added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
177
diff
changeset
|
696 |
input string & & output\medskip\\ |
b17eff695c7f
added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
177
diff
changeset
|
697 |
\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
|
698 |
\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
|
699 |
\texttt{\Grid{cc}} & $\rightarrow$ & $\varnothing$ |
b17eff695c7f
added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
177
diff
changeset
|
700 |
\end{tabular} |
b17eff695c7f
added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
177
diff
changeset
|
701 |
\end{center} |
b17eff695c7f
added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
177
diff
changeset
|
702 |
|
b17eff695c7f
added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
177
diff
changeset
|
703 |
\noindent |
b17eff695c7f
added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
177
diff
changeset
|
704 |
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
|
705 |
|
b17eff695c7f
added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
177
diff
changeset
|
706 |
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
|
707 |
Scala as follows: |
b17eff695c7f
added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
177
diff
changeset
|
708 |
|
b17eff695c7f
added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
177
diff
changeset
|
709 |
\begin{center} |
b17eff695c7f
added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
177
diff
changeset
|
710 |
\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
|
711 |
class SeqParser[I, T, S] |
b17eff695c7f
added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
177
diff
changeset
|
712 |
(p: => Parser[I, T], |
b17eff695c7f
added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
177
diff
changeset
|
713 |
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
|
714 |
def parse(sb: I) = |
b17eff695c7f
added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
177
diff
changeset
|
715 |
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
|
716 |
(head2, tail2) <- q.parse(tail1)) |
b17eff695c7f
added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
177
diff
changeset
|
717 |
yield ((head1, head2), tail2) |
b17eff695c7f
added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
177
diff
changeset
|
718 |
} |
b17eff695c7f
added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
177
diff
changeset
|
719 |
\end{lstlisting} |
b17eff695c7f
added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
177
diff
changeset
|
720 |
\end{center} |
b17eff695c7f
added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
177
diff
changeset
|
721 |
|
b17eff695c7f
added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
177
diff
changeset
|
722 |
\noindent |
b17eff695c7f
added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
177
diff
changeset
|
723 |
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
|
724 |
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
|
725 |
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
|
726 |
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
|
727 |
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
|
728 |
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
|
729 |
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
|
730 |
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
|
731 |
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
|
732 |
|
b17eff695c7f
added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
177
diff
changeset
|
733 |
\begin{center} |
b17eff695c7f
added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
177
diff
changeset
|
734 |
\texttt{Set[((T, S), I)]} |
b17eff695c7f
added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
177
diff
changeset
|
735 |
\end{center} |
b17eff695c7f
added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
177
diff
changeset
|
736 |
|
b17eff695c7f
added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
177
diff
changeset
|
737 |
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
|
738 |
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
|
739 |
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
|
740 |
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
|
741 |
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
|
742 |
|
b17eff695c7f
added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
177
diff
changeset
|
743 |
\begin{center} |
b17eff695c7f
added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
177
diff
changeset
|
744 |
\begin{tabular}{rcl} |
b17eff695c7f
added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
177
diff
changeset
|
745 |
input string & & output\medskip\\ |
b17eff695c7f
added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
177
diff
changeset
|
746 |
\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
|
747 |
\texttt{\Grid{bac}} & $\rightarrow$ & $\varnothing$\\ |
b17eff695c7f
added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
177
diff
changeset
|
748 |
\texttt{\Grid{ccc}} & $\rightarrow$ & $\varnothing$ |
b17eff695c7f
added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
177
diff
changeset
|
749 |
\end{tabular} |
b17eff695c7f
added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
177
diff
changeset
|
750 |
\end{center} |
b17eff695c7f
added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
177
diff
changeset
|
751 |
|
b17eff695c7f
added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
177
diff
changeset
|
752 |
\noindent |
b17eff695c7f
added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
177
diff
changeset
|
753 |
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
|
754 |
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
|
755 |
|
b17eff695c7f
added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
177
diff
changeset
|
756 |
\begin{center} |
b17eff695c7f
added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
177
diff
changeset
|
757 |
\begin{tabular}{rcl} |
b17eff695c7f
added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
177
diff
changeset
|
758 |
input string & & output\medskip\\ |
b17eff695c7f
added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
177
diff
changeset
|
759 |
\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
|
760 |
\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
|
761 |
\texttt{\Grid{aac}} & $\rightarrow$ & $\varnothing$ |
b17eff695c7f
added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
177
diff
changeset
|
762 |
\end{tabular} |
b17eff695c7f
added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
177
diff
changeset
|
763 |
\end{center} |
b17eff695c7f
added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
177
diff
changeset
|
764 |
|
b17eff695c7f
added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
177
diff
changeset
|
765 |
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
|
766 |
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
|
767 |
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
|
768 |
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
|
769 |
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
|
770 |
|
b17eff695c7f
added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
177
diff
changeset
|
771 |
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
|
772 |
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
|
773 |
|
b17eff695c7f
added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
177
diff
changeset
|
774 |
\begin{center} |
b17eff695c7f
added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
177
diff
changeset
|
775 |
\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
|
776 |
class FunParser[I, T, S] |
b17eff695c7f
added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
177
diff
changeset
|
777 |
(p: => Parser[I, T], |
b17eff695c7f
added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
177
diff
changeset
|
778 |
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
|
779 |
def parse(sb: I) = |
b17eff695c7f
added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
177
diff
changeset
|
780 |
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
|
781 |
} |
b17eff695c7f
added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
177
diff
changeset
|
782 |
\end{lstlisting} |
b17eff695c7f
added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
177
diff
changeset
|
783 |
\end{center} |
b17eff695c7f
added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
177
diff
changeset
|
784 |
|
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 |
\noindent |
b17eff695c7f
added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
177
diff
changeset
|
787 |
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
|
788 |
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
|
789 |
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
|
790 |
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
|
791 |
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
|
792 |
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
|
793 |
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
|
794 |
|
b17eff695c7f
added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
177
diff
changeset
|
795 |
%\bigskip |
b17eff695c7f
added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
177
diff
changeset
|
796 |
%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
|
797 |
%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
|
798 |
% |
b17eff695c7f
added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
177
diff
changeset
|
799 |
%\begin{center} |
b17eff695c7f
added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
177
diff
changeset
|
800 |
%\begin{tabular}{rcl} |
b17eff695c7f
added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
177
diff
changeset
|
801 |
%input string & & output\medskip\\ |
b17eff695c7f
added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
177
diff
changeset
|
802 |
%\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
|
803 |
%\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
|
804 |
%\texttt{\Grid{aac}} & $\rightarrow$ & $\varnothing$ |
b17eff695c7f
added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
177
diff
changeset
|
805 |
%\end{tabular} |
b17eff695c7f
added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
177
diff
changeset
|
806 |
%\end{center} |
b17eff695c7f
added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
177
diff
changeset
|
807 |
|
b17eff695c7f
added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
177
diff
changeset
|
808 |
|
173
7cfb7a6f7c99
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
809 |
|
680 | 810 |
|
811 |
||
173
7cfb7a6f7c99
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
812 |
|
7cfb7a6f7c99
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
813 |
%%% Local Variables: |
7cfb7a6f7c99
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
814 |
%%% mode: latex |
7cfb7a6f7c99
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
815 |
%%% TeX-master: t |
7cfb7a6f7c99
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
816 |
%%% End: |
680 | 817 |