author | Christian Urban <christian.urban@kcl.ac.uk> |
Fri, 13 Nov 2020 15:03:48 +0000 | |
changeset 803 | d4fb8c7fc3bf |
parent 799 | 85267be9a5ed |
child 809 | 2b9956d29038 |
permissions | -rw-r--r-- |
666 | 1 |
% !TEX program = xelatex |
744 | 2 |
\documentclass[dvipsnames,14pt,t,xelatex,aspectratio=169,xcolor={table}]{beamer} |
295
19f23c4c2167
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
215
diff
changeset
|
3 |
\usepackage{../slides} |
19f23c4c2167
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
215
diff
changeset
|
4 |
\usepackage{../graphics} |
215
828303e8e4af
updated slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
184
diff
changeset
|
5 |
\usepackage{../langs} |
828303e8e4af
updated slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
184
diff
changeset
|
6 |
\usepackage{../data} |
597 | 7 |
\usepackage{../grammar} |
49 | 8 |
|
9 |
% beamer stuff |
|
445 | 10 |
\renewcommand{\slidecaption}{CFL 06, King's College London} |
11 |
||
12 |
||
365
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
13 |
\newcommand{\bl}[1]{\textcolor{blue}{#1}} |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
14 |
%\newcommand{\dn}{\stackrel{\mbox{\scriptsize def}}{=}}% for definitions |
366
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
15 |
\newcommand{\qq}{\mbox{\texttt{"}}} |
49 | 16 |
|
17 |
\begin{document} |
|
18 |
||
19 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
295
19f23c4c2167
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
215
diff
changeset
|
20 |
\begin{frame}[t] |
49 | 21 |
\frametitle{% |
22 |
\begin{tabular}{@ {}c@ {}} |
|
23 |
\\[-3mm] |
|
445 | 24 |
\LARGE Compilers and \\[-2mm] |
744 | 25 |
\LARGE Formal Languages\\[3mm] |
49 | 26 |
\end{tabular}} |
27 |
||
28 |
\normalsize |
|
29 |
\begin{center} |
|
30 |
\begin{tabular}{ll} |
|
683 | 31 |
Email: & christian.urban at kcl.ac.uk\\ |
744 | 32 |
%Office Hours: & Thursdays 12 -- 14\\ |
33 |
%Location: & N7.07 (North Wing, Bush House)\\ |
|
683 | 34 |
Slides \& Progs: & KEATS (also homework is there)\\ |
49 | 35 |
\end{tabular} |
36 |
\end{center} |
|
37 |
||
744 | 38 |
\begin{center} |
39 |
\begin{tikzpicture} |
|
40 |
\node[drop shadow,fill=white,inner sep=0pt] |
|
41 |
{\footnotesize\rowcolors{1}{capri!10}{white} |
|
42 |
\begin{tabular}{|p{4.8cm}|p{4.8cm}|}\hline |
|
43 |
1 Introduction, Languages & \cellcolor{blue!50} 6 While-Language \\ |
|
44 |
2 Regular Expressions, Derivatives & 7 Compilation, JVM \\ |
|
45 |
3 Automata, Regular Languages & 8 Compiling Functional Languages \\ |
|
46 |
4 Lexing, Tokenising & 9 Optimisations \\ |
|
47 |
5 Grammars, Parsing & 10 LLVM \\ \hline |
|
48 |
\end{tabular}% |
|
49 |
}; |
|
50 |
\end{tikzpicture} |
|
51 |
\end{center} |
|
52 |
||
295
19f23c4c2167
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
215
diff
changeset
|
53 |
\end{frame} |
19f23c4c2167
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
215
diff
changeset
|
54 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
49 | 55 |
|
799 | 56 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
57 |
\begin{frame}[c] |
|
803 | 58 |
%%\frametitle{Test Program} |
799 | 59 |
|
803 | 60 |
\mbox{}\\[-18mm]\mbox{} |
799 | 61 |
|
803 | 62 |
{\lstset{language=While}%%\fontsize{10}{12}\selectfont |
63 |
\texttt{\lstinputlisting{../progs/while-tests/loops.while}}} |
|
799 | 64 |
|
65 |
\end{frame} |
|
66 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
67 |
||
68 |
||
69 |
||
70 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
71 |
\begin{frame} |
|
72 |
\frametitle{While-Language} |
|
73 |
\mbox{}\\[-23mm]\mbox{} |
|
74 |
||
75 |
\bl{\begin{plstx}[rhs style=,one per line]: \meta{Stmt} ::= skip |
|
76 |
| \meta{Id} := \meta{AExp} |
|
77 |
| if \meta{BExp} then \meta{Block} else \meta{Block} |
|
78 |
| while \meta{BExp} do \meta{Block}\\ |
|
79 |
: \meta{Stmts} ::= \meta{Stmt} ; \meta{Stmts} |
|
80 |
| \meta{Stmt}\\ |
|
81 |
: \meta{Block} ::= \{ \meta{Stmts} \} |
|
82 |
| \meta{Stmt}\\ |
|
83 |
: \meta{AExp} ::= \ldots\\ |
|
84 |
: \meta{BExp} ::= \ldots\\\end{plstx}} |
|
85 |
||
86 |
\end{frame} |
|
87 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
88 |
||
89 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
803 | 90 |
\mode<presentation>{ |
799 | 91 |
\begin{frame}[c] |
803 | 92 |
\frametitle{\begin{tabular}{c}Aexps\end{tabular}} |
799 | 93 |
|
94 |
\begin{center} |
|
803 | 95 |
\bl{\begin{tabular}{@{}lcl@{}} |
96 |
$\text{eval}(n)$ & $\dn$ & $n$\\ |
|
97 |
$\text{eval}(a_1 + a_2)$ & $\dn$ & $\text{eval}(a_1) + \text{eval}(a_2)$\\ |
|
98 |
$\text{eval}(a_1 - a_2)$ & $\dn$ & $\text{eval}(a_1) - \text{eval}(a_2)$\\ |
|
99 |
$\text{eval}(a_1 * a_2)$ & $\dn$ & $\text{eval}(a_1) * \text{eval}(a_2)$\bigskip\\ |
|
100 |
$\text{eval}(x)$ & $\dn$ & $???$\\ |
|
799 | 101 |
\end{tabular}} |
102 |
\end{center} |
|
103 |
||
803 | 104 |
\end{frame}} |
105 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
799 | 106 |
|
107 |
||
108 |
||
109 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
110 |
\mode<presentation>{ |
|
111 |
\begin{frame}[c] |
|
112 |
\frametitle{\begin{tabular}{c}Interpreter\end{tabular}} |
|
113 |
||
114 |
\begin{center} |
|
115 |
\bl{\begin{tabular}{@{}lcl@{}} |
|
116 |
$\text{eval}(n, E)$ & $\dn$ & $n$\\ |
|
117 |
$\text{eval}(x, E)$ & $\dn$ & $E(x)$ \;\;\;\textcolor{black}{lookup \bl{$x$} in \bl{$E$}}\\ |
|
118 |
$\text{eval}(a_1 + a_2, E)$ & $\dn$ & $\text{eval}(a_1, E) + \text{eval}(a_2, E)$\\ |
|
119 |
$\text{eval}(a_1 - a_2, E)$ & $\dn$ & $\text{eval}(a_1, E) - \text{eval}(a_2, E)$\\ |
|
120 |
$\text{eval}(a_1 * a_2, E)$ & $\dn$ & $\text{eval}(a_1, E) * \text{eval}(a_2, E)$\bigskip\\ |
|
121 |
$\text{eval}(a_1 = a_2, E)$ & $\dn$ & $\text{eval}(a_1, E) = \text{eval}(a_2, E)$\\ |
|
122 |
$\text{eval}(a_1\,!\!= a_2, E)$ & $\dn$ & $\neg(\text{eval}(a_1, E) = \text{eval}(a_2, E))$\\ |
|
123 |
$\text{eval}(a_1 < a_2, E)$ & $\dn$ & $\text{eval}(a_1, E) < \text{eval}(a_2, E)$\ |
|
124 |
\end{tabular}} |
|
125 |
\end{center} |
|
126 |
||
127 |
\end{frame}} |
|
803 | 128 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
129 |
||
130 |
||
131 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
132 |
\begin{frame}[c] |
|
133 |
\frametitle{An Interpreter (1)} |
|
134 |
||
135 |
\begin{center} |
|
136 |
\bl{\begin{tabular}{l} |
|
137 |
$\{$\\ |
|
138 |
\;\;$x := 5 \text{;}$\\ |
|
139 |
\;\;$y := x * 3\text{;}$\\ |
|
140 |
\;\;$y := x * 4\text{;}$\\ |
|
141 |
\;\;$x := u * 3$\\ |
|
142 |
$\}$ |
|
143 |
\end{tabular}} |
|
144 |
\end{center} |
|
145 |
||
146 |
\begin{itemize} |
|
147 |
\item the interpreter has to record the value of \bl{$x$} before assigning a value to \bl{$y$}\pause |
|
148 |
\item \bl{\texttt{eval(stmt, env)}} |
|
149 |
\end{itemize} |
|
150 |
||
151 |
\end{frame} |
|
799 | 152 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
803 | 153 |
|
799 | 154 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
155 |
\mode<presentation>{ |
|
156 |
\begin{frame}[c] |
|
157 |
\frametitle{\begin{tabular}{c}Interpreter (2)\end{tabular}} |
|
158 |
||
159 |
\begin{center} |
|
160 |
\bl{\begin{tabular}{@{}lcl@{}} |
|
161 |
$\text{eval}(\text{skip}, E)$ & $\dn$ & $E$\\ |
|
162 |
$\text{eval}(x:=a, E)$ & $\dn$ & \bl{$E(x \mapsto \text{eval}(a, E))$}\\ |
|
163 |
\multicolumn{3}{@{}l@{}}{$\text{eval}(\text{if}\;b\;\text{then}\;cs_1\;\text{else}\;cs_2 , E) \dn$}\\ |
|
164 |
\multicolumn{3}{@{}l@{}}{\hspace{2cm}$\text{if}\;\text{eval}(b,E)\;\text{then}\; |
|
165 |
\text{eval}(cs_1,E)$}\\ |
|
166 |
\multicolumn{3}{@{}l@{}}{\hspace{2cm}$\phantom{\text{if}\;\text{eval}(b,E)\;}\text{else}\;\text{eval}(cs_2,E)$}\\ |
|
167 |
\multicolumn{3}{@{}l@{}}{$\text{eval}(\text{while}\;b\;\text{do}\;cs, E) \dn$}\\ |
|
168 |
\multicolumn{3}{@{}l@{}}{\hspace{2cm}$\text{if}\;\text{eval}(b,E)$}\\ |
|
169 |
\multicolumn{3}{@{}l@{}}{\hspace{2cm}$\text{then}\; |
|
170 |
\text{eval}(\text{while}\;b\;\text{do}\;cs, \text{eval}(cs,E))$}\\ |
|
171 |
\multicolumn{3}{@{}l@{}}{\hspace{2cm}$\text{else}\; E$}\\ |
|
172 |
$\text{eval}(\text{write}\; x, E)$ & $\dn$ & $\{\;\text{println}(E(x))\; ;\;E\;\}$\\ |
|
173 |
\end{tabular}} |
|
174 |
\end{center} |
|
175 |
||
176 |
\end{frame}} |
|
177 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
178 |
||
179 |
||
180 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
181 |
\mode<presentation>{ |
|
182 |
\begin{frame}[t] |
|
183 |
\frametitle{\begin{tabular}{c}Interpreted Code\end{tabular}} |
|
184 |
||
185 |
\begin{center} |
|
186 |
\begin{tikzpicture} |
|
187 |
\begin{axis}[axis x line=bottom, axis y line=left, xlabel=n, ylabel=secs, legend style=small] |
|
188 |
\addplot+[smooth] file {interpreted.data}; |
|
189 |
\end{axis} |
|
190 |
\end{tikzpicture} |
|
191 |
\end{center} |
|
192 |
||
193 |
\end{frame}} |
|
194 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
195 |
||
803 | 196 |
\end{document} |
197 |
||
198 |
||
799 | 199 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
200 |
\mode<presentation>{ |
|
201 |
\begin{frame}[c] |
|
202 |
\frametitle{\begin{tabular}{c}Java Virtual Machine\end{tabular}} |
|
203 |
||
204 |
\begin{itemize} |
|
205 |
\item introduced in 1995 |
|
206 |
\item is a stack-based VM (like Postscript, CLR of .Net) |
|
207 |
\item contains a JIT compiler |
|
208 |
\item many languages take advantage of JVM's infrastructure (JRE) |
|
209 |
\item is garbage collected $\Rightarrow$ no buffer overflows |
|
210 |
\item some languages compile to the JVM: Scala, Clojure\ldots |
|
211 |
\end{itemize} |
|
212 |
||
213 |
\end{frame}} |
|
214 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
215 |
||
216 |
||
217 |
||
803 | 218 |
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
219 |
% \begin{frame}[t] |
|
220 |
% \frametitle{% |
|
221 |
% \begin{tabular}{@ {}c@ {}} |
|
222 |
% \\[-3mm] |
|
223 |
% \LARGE Compilers and \\[-2mm] |
|
224 |
% \LARGE Formal Languages\\[3mm] |
|
225 |
% \end{tabular}} |
|
799 | 226 |
|
803 | 227 |
% \normalsize |
228 |
% \begin{center} |
|
229 |
% \begin{tabular}{ll} |
|
230 |
% Email: & christian.urban at kcl.ac.uk\\ |
|
231 |
% %Office Hours: & Thursdays 12 -- 14\\ |
|
232 |
% %Location: & N7.07 (North Wing, Bush House)\\ |
|
233 |
% Slides \& Progs: & KEATS (also homework is there)\\ |
|
234 |
% \end{tabular} |
|
235 |
% \end{center} |
|
799 | 236 |
|
803 | 237 |
% \begin{center} |
238 |
% \begin{tikzpicture} |
|
239 |
% \node[drop shadow,fill=white,inner sep=0pt] |
|
240 |
% {\footnotesize\rowcolors{1}{capri!10}{white} |
|
241 |
% \begin{tabular}{|p{4.8cm}|p{4.8cm}|}\hline |
|
242 |
% 1 Introduction, Languages & \cellcolor{blue!50} 6 While-Language \\ |
|
243 |
% 2 Regular Expressions, Derivatives & 7 Compilation, JVM \\ |
|
244 |
% 3 Automata, Regular Languages & 8 Compiling Functional Languages \\ |
|
245 |
% 4 Lexing, Tokenising & 9 Optimisations \\ |
|
246 |
% 5 Grammars, Parsing & 10 LLVM \\ \hline |
|
247 |
% \end{tabular}% |
|
248 |
% }; |
|
249 |
% \end{tikzpicture} |
|
250 |
% \end{center} |
|
799 | 251 |
|
803 | 252 |
% \end{frame} |
253 |
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
799 | 254 |
|
255 |
||
256 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
531 | 257 |
% \begin{frame}[c] |
258 |
% \small |
|
259 |
% \mbox{}\\[5mm] |
|
260 |
% %\begin{textblock}{10}(3,5) |
|
261 |
% \begin{tikzpicture}[scale=1.5, |
|
262 |
% node distance=1cm, |
|
263 |
% every node/.style={minimum size=7mm}] |
|
264 |
% \node (r1) {\bl{$r_1$}}; |
|
265 |
% \node (r2) [right=of r1] {\bl{$r_2$}}; |
|
266 |
% \draw[->,line width=1mm] (r1) -- (r2) node[above,midway] {\bl{$der\,a$}}; |
|
267 |
% \node (r3) [right=of r2] {\bl{$r_3$}}; |
|
268 |
% \draw[->,line width=1mm] (r2) -- (r3) node[above,midway] {\bl{$der\,b$}}; |
|
269 |
% \node (r4) [right=of r3] {\bl{$r_4$}}; |
|
270 |
% \draw[->,line width=1mm] (r3) -- (r4) node[above,midway] {\bl{$der\,c$}}; |
|
271 |
% \draw (r4) node[anchor=west] {\;\raisebox{3mm}{\bl{$nullable$}}}; |
|
272 |
% \node (v4) [below=of r4] {\bl{$v_4$}}; |
|
273 |
% \draw[->,line width=1mm] (r4) -- (v4); |
|
274 |
% \node (v3) [left=of v4] {\bl{$v_3$}}; |
|
275 |
% \draw[->,line width=1mm] (v4) -- (v3) node[below,midway] {\bl{$inj\,c$}}; |
|
276 |
% \node (v2) [left=of v3] {\bl{$v_2$}}; |
|
277 |
% \draw[->,line width=1mm] (v3) -- (v2) node[below,midway] {\bl{$inj\,b$}}; |
|
278 |
% \node (v1) [left=of v2] {\bl{$v_1$}}; |
|
279 |
% \draw[->,line width=1mm] (v2) -- (v1) node[below,midway] {\bl{$inj\,a$}}; |
|
280 |
% \draw[->,line width=0.5mm] (r3) -- (v3); |
|
281 |
% \draw[->,line width=0.5mm] (r2) -- (v2); |
|
282 |
% \draw[->,line width=0.5mm] (r1) -- (v1); |
|
283 |
% \draw (r4) node[anchor=north west] {\;\raisebox{-8mm}{\bl{$mkeps$}}}; |
|
284 |
% \end{tikzpicture} |
|
285 |
% %\end{textblock} |
|
365
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
286 |
|
531 | 287 |
% \begin{center} |
288 |
% \begin{tabular}{l@{\hspace{1mm}}c@{\hspace{1mm}}l} |
|
289 |
% \\[-10mm] |
|
290 |
% \bl{$inj\,(c)\,c\,Empty$} & \bl{$\dn$} & \bl{$Char\,c$}\\ |
|
291 |
% \bl{$inj\,(r_1 + r_2)\,c\,Left(v)$} & \bl{$\dn$} & \bl{$Left(inj\,r_1\,c\,v)$}\\ |
|
292 |
% \bl{$inj\,(r_1 + r_2)\,c\,Right(v)$} & \bl{$\dn$} & \bl{$Right(inj\,r_2\,c\,v)$}\\ |
|
293 |
% \bl{$inj\,(r_1 \cdot r_2)\,c\,Seq(v_1,v_2)$} & \bl{$\dn$} & \bl{$Seq(inj\,r_1\,c\,v_1,v_2)$}\\ |
|
294 |
% \bl{$inj\,(r_1 \cdot r_2)\,c\,Left(Seq(v_1,v_2))$} & \bl{$\dn$} & \bl{$Seq(inj\,r_1\,c\,v_1,v_2)$}\\ |
|
295 |
% \bl{$inj\,(r_1 \cdot r_2)\,c\,Right(v)$} & \bl{$\dn$} & \bl{$Seq(mkeps(r_1),inj\,r_2\,c\,v)$}\\ |
|
296 |
% \bl{$inj\,(r^*)\,c\,Seq(v,vs)$} & \bl{$\dn$} & \bl{$inj\,r\,c\,v\,::\,vs$}\\ |
|
297 |
% \end{tabular} |
|
298 |
% \end{center} |
|
365
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
299 |
|
531 | 300 |
% \end{frame} |
301 |
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
365
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
302 |
|
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
303 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
683 | 304 |
\begin{frame}[t] |
305 |
\frametitle{Starting Symbol} |
|
306 |
||
307 |
\bl{\begin{plstx}[margin=1cm] |
|
308 |
: \meta{S} ::= \meta{A}\cdot\meta{S}\cdot\meta{B} | |
|
309 |
\meta{B}\cdot\meta{S}\cdot\meta{A} | \epsilon\\ |
|
310 |
: \meta{A} ::= a | \epsilon\\ |
|
311 |
: \meta{B} ::= b\\ |
|
312 |
\end{plstx}} |
|
313 |
||
750 | 314 |
|
315 |
TODO: Testcases for math expressions |
|
316 |
\url{https://github.com/ArashPartow/math-parser-benchmark-project} |
|
317 |
||
683 | 318 |
\end{frame} |
319 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
320 |
||
321 |
||
322 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
365
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
323 |
\begin{frame}[c] |
683 | 324 |
\frametitle{Hierarchy of Languages} |
365
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
325 |
|
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
326 |
Recall that languages are sets of strings. |
295
19f23c4c2167
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
215
diff
changeset
|
327 |
|
19f23c4c2167
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
215
diff
changeset
|
328 |
\begin{center} |
19f23c4c2167
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
215
diff
changeset
|
329 |
\begin{tikzpicture} |
365
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
330 |
[rect/.style={draw=black!50, top color=white,bottom color=black!20, rectangle, very thick, rounded corners}] |
295
19f23c4c2167
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
215
diff
changeset
|
331 |
|
19f23c4c2167
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
215
diff
changeset
|
332 |
\draw (0,0) node [rect, text depth=39mm, text width=68mm] {all languages}; |
19f23c4c2167
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
215
diff
changeset
|
333 |
\draw (0,-0.4) node [rect, text depth=28.5mm, text width=64mm] {decidable languages}; |
19f23c4c2167
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
215
diff
changeset
|
334 |
\draw (0,-0.85) node [rect, text depth=17mm] {context sensitive languages}; |
19f23c4c2167
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
215
diff
changeset
|
335 |
\draw (0,-1.14) node [rect, text depth=9mm, text width=50mm] {context-free languages}; |
19f23c4c2167
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
215
diff
changeset
|
336 |
\draw (0,-1.4) node [rect] {regular languages}; |
19f23c4c2167
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
215
diff
changeset
|
337 |
\end{tikzpicture} |
19f23c4c2167
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
215
diff
changeset
|
338 |
|
19f23c4c2167
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
215
diff
changeset
|
339 |
\end{center} |
19f23c4c2167
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
215
diff
changeset
|
340 |
|
366
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
341 |
\end{frame} |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
342 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
343 |
|
531 | 344 |
|
366
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
345 |
|
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
346 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
347 |
\begin{frame}[c] |
598 | 348 |
\frametitle{Parser Combinators} |
349 |
||
366
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
350 |
Atomic parsers, for example |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
351 |
|
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
352 |
\begin{center} |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
353 |
\bl{$1::rest \;\Rightarrow\; \{(1, rest)\}$} |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
354 |
\end{center}\bigskip |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
355 |
|
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
356 |
\begin{itemize} |
597 | 357 |
\item you consume one or more tokens from the\\ |
366
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
358 |
input (stream) |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
359 |
\item also works for characters and strings |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
360 |
\end{itemize} |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
361 |
\end{frame} |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
362 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
363 |
|
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
364 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
365 |
\begin{frame}[c] |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
366 |
|
686 | 367 |
Alternative parser (code \bl{$p\;|\;q$})\bigskip |
366
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
368 |
|
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
369 |
\begin{itemize} |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
370 |
\item apply \bl{$p$} and also \bl{$q$}; then combine |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
371 |
the outputs |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
372 |
\end{itemize} |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
373 |
|
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
374 |
\begin{center} |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
375 |
\large \bl{$p(\text{input}) \cup q(\text{input})$} |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
376 |
\end{center} |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
377 |
|
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
378 |
\end{frame} |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
379 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
380 |
|
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
381 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
382 |
\begin{frame}[c] |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
383 |
|
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
384 |
Sequence parser (code \bl{$p\sim q$})\bigskip |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
385 |
|
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
386 |
\begin{itemize} |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
387 |
\item apply first \bl{$p$} producing a set of pairs |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
388 |
\item then apply \bl{$q$} to the unparsed parts |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
389 |
\item then combine the results:\medskip |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
390 |
\begin{center} |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
391 |
((output$_1$, output$_2$), unparsed part) |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
392 |
\end{center} |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
393 |
\end{itemize} |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
394 |
|
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
395 |
\begin{center} |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
396 |
\begin{tabular}{l} |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
397 |
\large \bl{$\{((o_1, o_2), u_2) \;|\;$}\\[2mm] |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
398 |
\large\mbox{}\hspace{15mm} \bl{$(o_1, u_1) \in p(\text{input}) \wedge$}\\[2mm] |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
399 |
\large\mbox{}\hspace{15mm} \bl{$(o_2, u_2) \in q(u_1)\}$} |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
400 |
\end{tabular} |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
401 |
\end{center} |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
402 |
|
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
403 |
|
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
404 |
\end{frame} |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
405 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
406 |
|
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
407 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
408 |
\begin{frame}[c] |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
409 |
|
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
410 |
Function parser (code \bl{$p \Rightarrow f\;$})\bigskip |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
411 |
|
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
412 |
\begin{itemize} |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
413 |
\item apply \bl{$p$} producing a set of pairs |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
414 |
\item then apply the function \bl{$f$} to each first component |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
415 |
\end{itemize} |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
416 |
|
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
417 |
\begin{center} |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
418 |
\begin{tabular}{l} |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
419 |
\large \bl{$\{(f(o_1), u_1) \;|\; (o_1, u_1) \in p(\text{input})\}$} |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
420 |
\end{tabular} |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
421 |
\end{center} |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
422 |
|
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
423 |
\end{frame} |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
424 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
425 |
|
531 | 426 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
427 |
\begin{frame}[c] |
|
428 |
\frametitle{Types of Parsers} |
|
429 |
||
430 |
\begin{itemize} |
|
431 |
\item {\bf Sequencing}: if \bl{$p$} returns results of type \bl{$T$}, and \bl{$q$} results of type \bl{$S$}, |
|
432 |
then \bl{$p \sim q$} returns results of type |
|
433 |
||
434 |
\begin{center} |
|
435 |
\bl{$T \times S$} |
|
436 |
\end{center}\pause |
|
437 |
||
438 |
\item {\bf Alternative}: if \bl{$p$} returns results of type \bl{$T$} then \bl{$q$} \alert{must} also have results of type \bl{$T$}, |
|
686 | 439 |
and \bl{$p \;|\; q$} returns results of type |
531 | 440 |
|
441 |
\begin{center} |
|
442 |
\bl{$T$} |
|
443 |
\end{center}\pause |
|
444 |
||
445 |
\item {\bf Semantic Action}: if \bl{$p$} returns results of type \bl{$T$} and \bl{$f$} is a function from |
|
446 |
\bl{$T$} to \bl{$S$}, then |
|
447 |
\bl{$p \Rightarrow f$} returns results of type |
|
448 |
||
449 |
\begin{center} |
|
450 |
\bl{$S$} |
|
451 |
\end{center} |
|
452 |
||
453 |
\end{itemize} |
|
454 |
||
455 |
\end{frame} |
|
456 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
457 |
||
458 |
||
459 |
||
460 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
461 |
\begin{frame}[c] |
|
462 |
\frametitle{Two Grammars} |
|
463 |
||
464 |
Which languages are recognised by the following two grammars? |
|
465 |
||
597 | 466 |
\bl{\begin{plstx}[margin=3cm] |
467 |
: \meta{S} ::= \liningnums{1}\cdot\meta{S}\cdot \meta{S} | \epsilon\\ |
|
468 |
\end{plstx}} |
|
531 | 469 |
|
597 | 470 |
\bl{\begin{plstx}[margin=3cm] |
471 |
: \meta{U} ::= \liningnums{1}\cdot\meta{U} | \epsilon\\ |
|
472 |
\end{plstx}} |
|
531 | 473 |
|
474 |
\end{frame} |
|
475 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
366
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
476 |
|
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
477 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
478 |
\begin{frame}[t] |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
479 |
\frametitle{Ambiguous Grammars} |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
480 |
|
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
481 |
\begin{center} |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
482 |
\begin{tikzpicture} |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
483 |
\begin{axis}[xlabel={\pcode{1}s},ylabel={time in secs}, |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
484 |
enlargelimits=false, |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
485 |
xtick={0,100,...,1000}, |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
486 |
xmax=1050, |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
487 |
ymax=33, |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
488 |
ytick={0,5,...,30}, |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
489 |
scaled ticks=false, |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
490 |
axis lines=left, |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
491 |
width=11cm, |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
492 |
height=7cm, |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
493 |
legend entries={unambiguous,ambiguous}, |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
494 |
legend pos=north east, |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
495 |
legend cell align=left, |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
496 |
x tick label style={font=\small,/pgf/number format/1000 sep={}}] |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
497 |
\addplot[blue,mark=*, mark options={fill=white}] |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
498 |
table {s-grammar1.data}; |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
499 |
\only<2>{ |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
500 |
\addplot[red,mark=triangle*, mark options={fill=white}] |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
501 |
table {s-grammar2.data};} |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
502 |
\end{axis} |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
503 |
\end{tikzpicture} |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
504 |
\end{center} |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
505 |
|
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
506 |
\end{frame} |
295
19f23c4c2167
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
215
diff
changeset
|
507 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
19f23c4c2167
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
215
diff
changeset
|
508 |
|
19f23c4c2167
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
215
diff
changeset
|
509 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
19f23c4c2167
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
215
diff
changeset
|
510 |
\begin{frame}[c] |
597 | 511 |
\frametitle{Arithmetic Expressions} |
365
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
512 |
|
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
513 |
A grammar for arithmetic expressions and numbers: |
50 | 514 |
|
597 | 515 |
\bl{\begin{plstx}[margin=1cm] |
516 |
: \meta{E} ::= \meta{E} \cdot + \cdot \meta{E} |
|
517 |
| \meta{E} \cdot * \cdot \meta{E} |
|
518 |
| ( \cdot \meta{E} \cdot ) | \meta{N}\\ |
|
519 |
: \meta{N} ::= \meta{N} \cdot \meta{N} | |
|
520 |
0 | 1 | \ldots | 9\\ |
|
521 |
\end{plstx}} |
|
49 | 522 |
|
365
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
523 |
Unfortunately it is left-recursive (and ambiguous).\medskip\\ |
597 | 524 |
A problem for \alert{recursive descent parsers} (e.g.~parser |
525 |
combinators). \bigskip\pause |
|
365
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
526 |
|
597 | 527 |
\end{frame} |
365
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
528 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
529 |
|
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
530 |
|
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
531 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
532 |
\begin{frame}[t] |
597 | 533 |
\frametitle{Numbers} |
365
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
534 |
|
597 | 535 |
\bl{\begin{plstx}[margin=1cm] |
536 |
: \meta{N} ::= \meta{N} \cdot \meta{N} | |
|
537 |
0 | 1 | \ldots | 9\\ |
|
538 |
\end{plstx}} |
|
49 | 539 |
|
365
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
540 |
A non-left-recursive, non-ambiguous grammar for numbers: |
49 | 541 |
|
597 | 542 |
\bl{\begin{plstx}[margin=1cm] |
543 |
: \meta{N} ::= 0 \cdot \meta{N} | 1 \cdot \meta{N} | \ldots | |
|
544 |
0 | 1 | \ldots | 9\\ |
|
545 |
\end{plstx}} |
|
365
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
546 |
|
597 | 547 |
\end{frame} |
365
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
548 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
549 |
|
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
550 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
551 |
\begin{frame}[c] |
597 | 552 |
\frametitle{Removing Left-Recursion} |
295
19f23c4c2167
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
215
diff
changeset
|
553 |
|
365
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
554 |
The rule for numbers is directly left-recursive: |
49 | 555 |
|
556 |
\begin{center} |
|
50 | 557 |
\bl{\begin{tabular}{lcl} |
597 | 558 |
$\meta{N}$ & $::=$ & $\meta{N} \cdot \meta{N} \;|\; 0 \;|\; 1\;\;\;\;(\ldots)$ |
365
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
559 |
\end{tabular}} |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
560 |
\end{center} |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
561 |
|
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
562 |
Translate |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
563 |
|
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
564 |
\begin{center} |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
565 |
\begin{tabular}{ccc} |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
566 |
\bl{\begin{tabular}{lcl} |
597 | 567 |
$\meta{N}$ & $::=$ & $\meta{N} \cdot \alpha$\\ |
365
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
568 |
& $\;|\;$ & $\beta$\\ |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
569 |
\\ |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
570 |
\end{tabular}} |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
571 |
& {\Large$\Rightarrow$} & |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
572 |
\bl{\begin{tabular}{lcl} |
597 | 573 |
$\meta{N}$ & $::=$ & $\beta \cdot \meta{N}'$\\ |
574 |
$\meta{N}'$ & $::=$ & $\alpha \cdot \meta{N}'$\\ |
|
365
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
575 |
& $\;|\;$ & $\epsilon$ |
50 | 576 |
\end{tabular}} |
365
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
577 |
\end{tabular} |
50 | 578 |
\end{center}\pause |
579 |
||
597 | 580 |
Which means in this case: |
365
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
581 |
|
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
582 |
\begin{center} |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
583 |
\bl{\begin{tabular}{lcl} |
597 | 584 |
$\meta{N}$ & $\rightarrow$ & $0 \cdot \meta{N}' \;|\; 1 \cdot \meta{N}'$\\ |
585 |
$\meta{N}'$ & $\rightarrow$ & $\meta{N} \cdot \meta{N}' \;|\; \epsilon$\\ |
|
365
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
586 |
\end{tabular}} |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
587 |
\end{center} |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
588 |
|
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
589 |
|
597 | 590 |
\end{frame} |
365
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
591 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
592 |
|
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
593 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
685 | 594 |
%\begin{frame}[c] |
595 |
%\frametitle{Operator Precedences} |
|
596 |
% |
|
597 |
% |
|
598 |
%To disambiguate |
|
599 |
% |
|
600 |
%\begin{center} |
|
601 |
%\bl{\begin{tabular}{lcl} |
|
602 |
%$\meta{E}$ & $::=$ & $\meta{E} \cdot + \cdot \meta{E} \;|\;\meta{E} \cdot * \cdot \meta{E} \;|\;( \cdot \meta{E} \cdot ) \;|\;\meta{N}$ \\ |
|
603 |
%\end{tabular}} |
|
604 |
%\end{center} |
|
605 |
% |
|
606 |
%Decide on how many precedence levels, say\medskip\\ |
|
607 |
%highest for \bl{$()$}, medium for \bl{*}, lowest for \bl{+} |
|
608 |
% |
|
609 |
%\begin{center} |
|
610 |
%\bl{\begin{tabular}{lcl} |
|
611 |
%$\meta{E}_{low}$ & $::=$ & $\meta{E}_{med} \cdot + \cdot \meta{E}_{low} \;|\; \meta{E}_{med}$ \\ |
|
612 |
%$\meta{E}_{med}$ & $::=$ & $\meta{E}_{hi} \cdot * \cdot \meta{E}_{med} \;|\; \meta{E}_{hi}$\\ |
|
613 |
%$\meta{E}_{hi}$ & $::=$ & $( \cdot \meta{E}_{low} \cdot ) \;|\;\meta{N}$ \\ |
|
614 |
%\end{tabular}} |
|
615 |
%\end{center}\pause |
|
616 |
% |
|
617 |
%\small What happens with \bl{$1 + 3 + 4$}? |
|
618 |
%\end{frame} |
|
366
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
619 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
620 |
|
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
621 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
622 |
\begin{frame}[c] |
597 | 623 |
\frametitle{Chomsky Normal Form} |
365
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
624 |
|
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
625 |
All rules must be of the form |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
626 |
|
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
627 |
\begin{center} |
597 | 628 |
\bl{$\meta{A} ::= a$} |
365
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
629 |
\end{center} |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
630 |
|
50 | 631 |
or |
632 |
||
633 |
\begin{center} |
|
597 | 634 |
\bl{$\meta{A} ::= \meta{B}\cdot \meta{C}$} |
365
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
635 |
\end{center} |
49 | 636 |
|
365
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
637 |
No rule can contain \bl{$\epsilon$}. |
49 | 638 |
|
597 | 639 |
\end{frame} |
49 | 640 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
641 |
||
169
57df3d7b4a25
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
168
diff
changeset
|
642 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
57df3d7b4a25
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
168
diff
changeset
|
643 |
\begin{frame}[c] |
365
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
644 |
\frametitle{\begin{tabular}{c}$\epsilon$-Removal\end{tabular}} |
169
57df3d7b4a25
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
168
diff
changeset
|
645 |
|
57df3d7b4a25
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
168
diff
changeset
|
646 |
\begin{enumerate} |
597 | 647 |
\item If \bl{$A::= \alpha \cdot B \cdot \beta$} and \bl{$B ::= \epsilon$} are in the grammar, |
648 |
then add \bl{$A::= \alpha \cdot \beta$} (iterate if necessary). |
|
649 |
\item Throw out all \bl{$B ::= \epsilon$}. |
|
169
57df3d7b4a25
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
168
diff
changeset
|
650 |
\end{enumerate} |
57df3d7b4a25
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
168
diff
changeset
|
651 |
|
365
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
652 |
\small |
169
57df3d7b4a25
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
168
diff
changeset
|
653 |
\begin{center} |
365
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
654 |
\begin{tabular}{ccc} |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
655 |
\bl{\begin{tabular}{l@{\hspace{1mm}}c@{\hspace{1mm}}l} |
597 | 656 |
$N$ & $::=$ & $0 \cdot N' \;|\; 1\cdot N'$\\ |
657 |
$N'$ & $::=$ & $N \cdot N'\;|\;\epsilon$\\ |
|
365
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
658 |
\\ |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
659 |
\\ |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
660 |
\\ |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
661 |
\\ |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
662 |
\\ |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
663 |
\end{tabular}} & |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
664 |
\bl{\begin{tabular}{l@{\hspace{1mm}}c@{\hspace{1mm}}l} |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
665 |
\\ |
597 | 666 |
$N$ & $::=$ & $0 \cdot N' \;|\; 1\cdot N'\;|\;0\;|\;1$\\ |
667 |
$N'$ & $::=$ & $N \cdot N'\;|\;N\;|\;\epsilon$\\ |
|
365
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
668 |
\\ |
597 | 669 |
$N$ & $::=$ & $0 \cdot N' \;|\; 1\cdot N'\;|\;0\;|\;1$\\ |
670 |
$N'$ & $::=$ & $N \cdot N'\;|\;N$\\ |
|
169
57df3d7b4a25
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
168
diff
changeset
|
671 |
\end{tabular}} |
57df3d7b4a25
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
168
diff
changeset
|
672 |
\end{tabular} |
57df3d7b4a25
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
168
diff
changeset
|
673 |
\end{center} |
57df3d7b4a25
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
168
diff
changeset
|
674 |
|
365
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
675 |
\pause\normalsize |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
676 |
\begin{center} |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
677 |
\bl{\begin{tabular}{l@{\hspace{1mm}}c@{\hspace{1mm}}l} |
597 | 678 |
$N$ & $::=$ & $0 \cdot N\;|\; 1\cdot N\;|\;0\;|\;1$\\ |
365
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
679 |
\end{tabular}} |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
680 |
|
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
681 |
\end{center} |
368
a9911966c0df
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
366
diff
changeset
|
682 |
\end{frame} |
169
57df3d7b4a25
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
168
diff
changeset
|
683 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
57df3d7b4a25
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
168
diff
changeset
|
684 |
|
365
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
685 |
|
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
686 |
|
169
57df3d7b4a25
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
168
diff
changeset
|
687 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
57df3d7b4a25
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
168
diff
changeset
|
688 |
\begin{frame}[c] |
597 | 689 |
\frametitle{CYK Algorithm} |
169
57df3d7b4a25
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
168
diff
changeset
|
690 |
|
365
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
691 |
If grammar is in Chomsky normalform \ldots |
169
57df3d7b4a25
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
168
diff
changeset
|
692 |
|
57df3d7b4a25
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
168
diff
changeset
|
693 |
\begin{center} |
365
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
694 |
\bl{\begin{tabular}{@ {}lcl@ {}} |
597 | 695 |
$\meta{S}$ & $::=$ & $\meta{N}\cdot \meta{P}$ \\ |
696 |
$\meta{P}$ & $::=$ & $\meta{V}\cdot \meta{N}$ \\ |
|
697 |
$\meta{N}$ & $::=$ & $\meta{N}\cdot \meta{N}$ \\ |
|
698 |
$\meta{N}$ & $::=$ & $\texttt{students} \;|\; \texttt{Jeff} \;|\; \texttt{geometry} \;|\; \texttt{trains} $ \\ |
|
699 |
$\meta{V}$ & $::=$ & $\texttt{trains}$ |
|
49 | 700 |
\end{tabular}} |
701 |
\end{center} |
|
702 |
||
365
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
703 |
\bl{\texttt{Jeff trains geometry students}} |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
704 |
|
597 | 705 |
\end{frame} |
365
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
706 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
597 | 707 |
|
708 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
365
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
709 |
\begin{frame}[c] |
597 | 710 |
\frametitle{CYK Algorithm} |
49 | 711 |
|
712 |
||
365
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
713 |
\begin{itemize} |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
714 |
\item fastest possible algorithm for recognition problem |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
715 |
\item runtime is \bl{$O(n^3)$}\bigskip |
597 | 716 |
\item grammars need to be transformed into CNF |
365
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
717 |
\end{itemize} |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
718 |
|
597 | 719 |
\end{frame} |
365
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
720 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
169
57df3d7b4a25
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
168
diff
changeset
|
721 |
|
57df3d7b4a25
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
168
diff
changeset
|
722 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
366
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
723 |
\begin{frame}[c] |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
724 |
\frametitle{The Goal of this Course} |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
725 |
\mbox{}\\[-26mm]\mbox{} |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
726 |
|
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
727 |
\begin{center} |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
728 |
\begin{tikzpicture}[scale=1, |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
729 |
node/.style={ |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
730 |
rectangle,rounded corners=3mm, |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
731 |
very thick,draw=black!50, |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
732 |
minimum height=18mm, minimum width=20mm, |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
733 |
top color=white,bottom color=black!20}] |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
734 |
|
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
735 |
\node at (3.05, 1.8) {\Large\bf Write a Compiler}; |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
736 |
|
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
737 |
\node (0) at (-2.3,0) {}; |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
738 |
|
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
739 |
\node (A) at (0,0) [node] {}; |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
740 |
\node [below right] at (A.north west) {lexer}; |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
741 |
|
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
742 |
\node (B) at (3,0) [node] {}; |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
743 |
\node [below right=1mm] at (B.north west) |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
744 |
{\mbox{}\hspace{-1mm}parser}; |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
745 |
|
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
746 |
\node (C) at (6,0) [node] {}; |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
747 |
\node [below right] at (C.north west) |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
748 |
{\mbox{}\hspace{-1mm}code gen}; |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
749 |
|
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
750 |
\node (1) at (8.4,0) {}; |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
751 |
|
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
752 |
\draw [->,line width=4mm] (0) -- (A); |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
753 |
\draw [->,line width=4mm] (A) -- (B); |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
754 |
\draw [->,line width=4mm] (B) -- (C); |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
755 |
\draw [->,line width=4mm] (C) -- (1); |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
756 |
\end{tikzpicture} |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
757 |
\end{center} |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
758 |
|
598 | 759 |
We have a lexer and a parser\ldots |
366
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
760 |
|
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
761 |
\end{frame} |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
762 |
|
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
763 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
169
57df3d7b4a25
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
168
diff
changeset
|
764 |
\begin{frame}[c] |
57df3d7b4a25
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
168
diff
changeset
|
765 |
|
57df3d7b4a25
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
168
diff
changeset
|
766 |
\begin{center} |
365
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
767 |
\bl{\begin{tabular}{@{}lcl@{}} |
598 | 768 |
\meta{Stmt} & $::=$ & $\texttt{skip}$\\ |
769 |
& $|$ & \textit{Id}\;\texttt{:=}\;\meta{AExp}\\ |
|
770 |
& $|$ & \texttt{if}\; \meta{BExp} \;\texttt{then}\; \meta{Block} \;\texttt{else}\; \meta{Block}\\ |
|
771 |
& $|$ & \texttt{while}\; \meta{BExp} \;\texttt{do}\; \meta{Block}\\ |
|
365
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
772 |
& $|$ & \texttt{read}\;\textit{Id}\\ |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
773 |
& $|$ & \texttt{write}\;\textit{Id}\\ |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
774 |
& $|$ & \texttt{write}\;\textit{String}\medskip\\ |
598 | 775 |
\meta{Stmts} & $::=$ & \meta{Stmt} \;\texttt{;}\; \meta{Stmts}\\ |
776 |
& $|$ & \meta{Stmt}\medskip\\ |
|
777 |
\meta{Block} & $::=$ & \texttt{\{}\,\meta{Stmts}\,\texttt{\}}\\ |
|
778 |
& $|$ & \meta{Stmt}\medskip\\ |
|
779 |
\meta{AExp} & $::=$ & \ldots\\ |
|
780 |
\meta{BExp} & $::=$ & \ldots\\ |
|
50 | 781 |
\end{tabular}} |
782 |
\end{center} |
|
366
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
365
diff
changeset
|
783 |
\end{frame} |
169
57df3d7b4a25
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
168
diff
changeset
|
784 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
57df3d7b4a25
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
168
diff
changeset
|
785 |
|
57df3d7b4a25
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
168
diff
changeset
|
786 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
57df3d7b4a25
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
168
diff
changeset
|
787 |
\mode<presentation>{ |
57df3d7b4a25
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
168
diff
changeset
|
788 |
\begin{frame}[c] |
57df3d7b4a25
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
168
diff
changeset
|
789 |
|
744 | 790 |
??%%\mbox{\lstinputlisting[language=while]{../progs/fib.while}} |
169
57df3d7b4a25
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
168
diff
changeset
|
791 |
|
57df3d7b4a25
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
168
diff
changeset
|
792 |
\end{frame}} |
57df3d7b4a25
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
168
diff
changeset
|
793 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
57df3d7b4a25
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
168
diff
changeset
|
794 |
|
57df3d7b4a25
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
168
diff
changeset
|
795 |
|
57df3d7b4a25
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
168
diff
changeset
|
796 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
57df3d7b4a25
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
168
diff
changeset
|
797 |
\begin{frame}[c] |
598 | 798 |
\frametitle{An Interpreter} |
169
57df3d7b4a25
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
168
diff
changeset
|
799 |
|
57df3d7b4a25
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
168
diff
changeset
|
800 |
\begin{center} |
57df3d7b4a25
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
168
diff
changeset
|
801 |
\bl{\begin{tabular}{l} |
57df3d7b4a25
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
168
diff
changeset
|
802 |
$\{$\\ |
57df3d7b4a25
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
168
diff
changeset
|
803 |
\;\;$x := 5 \text{;}$\\ |
57df3d7b4a25
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
168
diff
changeset
|
804 |
\;\;$y := x * 3\text{;}$\\ |
57df3d7b4a25
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
168
diff
changeset
|
805 |
\;\;$y := x * 4\text{;}$\\ |
57df3d7b4a25
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
168
diff
changeset
|
806 |
\;\;$x := u * 3$\\ |
57df3d7b4a25
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
168
diff
changeset
|
807 |
$\}$ |
57df3d7b4a25
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
168
diff
changeset
|
808 |
\end{tabular}} |
57df3d7b4a25
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
168
diff
changeset
|
809 |
\end{center} |
57df3d7b4a25
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
168
diff
changeset
|
810 |
|
57df3d7b4a25
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
168
diff
changeset
|
811 |
\begin{itemize} |
57df3d7b4a25
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
168
diff
changeset
|
812 |
\item the interpreter has to record the value of \bl{$x$} before assigning a value to \bl{$y$}\pause |
365
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
813 |
\item \bl{\text{eval}(stmt, env)} |
169
57df3d7b4a25
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
168
diff
changeset
|
814 |
\end{itemize} |
57df3d7b4a25
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
168
diff
changeset
|
815 |
|
365
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
816 |
|
598 | 817 |
\end{frame} |
365
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
818 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
819 |
|
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
820 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
821 |
\begin{frame}[c] |
598 | 822 |
\frametitle{An Interpreter} |
365
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
823 |
|
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
824 |
\begin{center} |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
825 |
\bl{\begin{tabular}{@{}lcl@{}} |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
826 |
$\text{eval}(n, E)$ & $\dn$ & $n$\\ |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
827 |
$\text{eval}(x, E)$ & $\dn$ & $E(x)$ \;\;\;\textcolor{black}{lookup \bl{$x$} in \bl{$E$}}\\ |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
828 |
$\text{eval}(a_1 + a_2, E)$ & $\dn$ & $\text{eval}(a_1, E) + \text{eval}(a_2, E)$\\ |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
829 |
$\text{eval}(a_1 - a_2, E)$ & $\dn$ & $\text{eval}(a_1, E) - \text{eval}(a_2, E)$\\ |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
830 |
$\text{eval}(a_1 * a_2, E)$ & $\dn$ & $\text{eval}(a_1, E) * \text{eval}(a_2, E)$\bigskip\\ |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
831 |
$\text{eval}(a_1 = a_2, E)$ & $\dn$ & $\text{eval}(a_1, E) = \text{eval}(a_2, E)$\\ |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
832 |
$\text{eval}(a_1\,!\!= a_2, E)$ & $\dn$ & $\neg(\text{eval}(a_1, E) = \text{eval}(a_2, E))$\\ |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
833 |
$\text{eval}(a_1 < a_2, E)$ & $\dn$ & $\text{eval}(a_1, E) < \text{eval}(a_2, E)$\ |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
834 |
\end{tabular}} |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
835 |
\end{center} |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
836 |
|
598 | 837 |
\end{frame} |
365
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
838 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
598 | 839 |
|
840 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
365
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
841 |
\begin{frame}[c] |
598 | 842 |
\frametitle{An Interpreter (2)} |
365
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
843 |
|
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
844 |
\begin{center} |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
845 |
\bl{\begin{tabular}{@{}lcl@{}} |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
846 |
$\text{eval}(\text{skip}, E)$ & $\dn$ & $E$\\ |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
847 |
$\text{eval}(x:=a, E)$ & $\dn$ & \bl{$E(x \mapsto \text{eval}(a, E))$}\\ |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
848 |
\multicolumn{3}{@{}l@{}}{$\text{eval}(\text{if}\;b\;\text{then}\;cs_1\;\text{else}\;cs_2 , E) \dn$}\\ |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
849 |
\multicolumn{3}{@{}l@{}}{\hspace{2cm}$\text{if}\;\text{eval}(b,E)\;\text{then}\; |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
850 |
\text{eval}(cs_1,E)$}\\ |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
851 |
\multicolumn{3}{@{}l@{}}{\hspace{2cm}$\phantom{\text{if}\;\text{eval}(b,E)\;}\text{else}\;\text{eval}(cs_2,E)$}\\ |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
852 |
\multicolumn{3}{@{}l@{}}{$\text{eval}(\text{while}\;b\;\text{do}\;cs, E) \dn$}\\ |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
853 |
\multicolumn{3}{@{}l@{}}{\hspace{2cm}$\text{if}\;\text{eval}(b,E)$}\\ |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
854 |
\multicolumn{3}{@{}l@{}}{\hspace{2cm}$\text{then}\; |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
855 |
\text{eval}(\text{while}\;b\;\text{do}\;cs, \text{eval}(cs,E))$}\\ |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
856 |
\multicolumn{3}{@{}l@{}}{\hspace{2cm}$\text{else}\; E$}\\ |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
857 |
$\text{eval}(\text{write}\; x, E)$ & $\dn$ & $\{\;\text{println}(E(x))\; ;\;E\;\}$\\ |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
858 |
\end{tabular}} |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
859 |
\end{center} |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
860 |
|
598 | 861 |
\end{frame} |
365
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
862 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
863 |
|
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
864 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
865 |
\begin{frame}[c] |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
866 |
\frametitle{\begin{tabular}{c}Test Program\end{tabular}} |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
867 |
|
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
868 |
\mbox{}\\[-18mm]\mbox{} |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
869 |
|
744 | 870 |
?? |
871 |
%{\lstset{language=While}%%\fontsize{10}{12}\selectfont |
|
872 |
%\texttt{\lstinputlisting{../progs/loops.while}}} |
|
365
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
873 |
|
295
19f23c4c2167
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
215
diff
changeset
|
874 |
\end{frame} |
169
57df3d7b4a25
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
168
diff
changeset
|
875 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
57df3d7b4a25
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
168
diff
changeset
|
876 |
|
365
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
877 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
878 |
\mode<presentation>{ |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
879 |
\begin{frame}[t] |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
880 |
\frametitle{\begin{tabular}{c}Interpreted Code\end{tabular}} |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
881 |
|
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
882 |
\begin{center} |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
883 |
\begin{tikzpicture} |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
884 |
\begin{axis}[axis x line=bottom, axis y line=left, xlabel=n, ylabel=secs, legend style=small] |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
885 |
\addplot+[smooth] file {interpreted.data}; |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
886 |
\end{axis} |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
887 |
\end{tikzpicture} |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
888 |
\end{center} |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
889 |
|
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
890 |
\end{frame}} |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
891 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
892 |
|
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
893 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
894 |
\begin{frame}[c] |
685 | 895 |
\frametitle{Java Virtual Machine} |
365
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
896 |
|
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
897 |
\begin{itemize} |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
898 |
\item introduced in 1995 |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
899 |
\item is a stack-based VM (like Postscript, CLR of .Net) |
685 | 900 |
\item contains a JIT compiler\\ |
901 |
\begin{itemize} |
|
902 |
\item From the Cradle to the Holy Graal - the JDK Story |
|
903 |
\item \url{https://www.youtube.com/watch?v=h419kfbLhUI} |
|
904 |
\end{itemize} |
|
365
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
905 |
\item is garbage collected $\Rightarrow$ no buffer overflows |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
906 |
\item some languages compile to the JVM: Scala, Clojure\ldots |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
907 |
\end{itemize} |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
908 |
|
685 | 909 |
\end{frame} |
365
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
910 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
295
diff
changeset
|
911 |
|
685 | 912 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
913 |
\begin{frame}[c] |
|
914 |
\frametitle{LLVM} |
|
915 |
||
916 |
\begin{itemize} |
|
917 |
\item LLVM started by academics in 2000 (University of Illinois in |
|
918 |
Urbana-Champaign) |
|
919 |
\item suite of compiler tools |
|
920 |
\item SSA-based intermediate language |
|
921 |
\item no need to allocate registers |
|
686 | 922 |
\item source languages: C, C++, Rust, Go, Swift |
923 |
\item target CPUs: x86, ARM, PowerPC, \ldots |
|
685 | 924 |
\end{itemize} |
925 |
\end{frame} |
|
926 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
702 | 927 |
|
598 | 928 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
666 | 929 |
%\begin{frame}[c] |
930 |
% \frametitle{Coursework: MkEps} |
|
931 |
% |
|
932 |
%\begin{center} |
|
933 |
%\begin{tabular}{@ {}l@ {\hspace{2mm}}c@ {\hspace{2mm}}l@ {}} |
|
934 |
% \bl{$mkeps([c_1 c_2 \ldots c_n])$} & \bl{$\dn$} & $\bl{undefined}$\\ |
|
935 |
% \bl{$mkeps(r^*)$} & \bl{$\dn$} & $\bl{Stars\,[]}$\\ |
|
936 |
% \bl{$mkeps(r^{\{n\}})$} & \bl{$\dn$} & $\bl{Stars\,(mkeps(r))^n}$\\ |
|
937 |
% \bl{$mkeps(r^{\{n..\}})$} & \bl{$\dn$} & $\bl{Stars\,(mkeps(r))^n}$\\ |
|
938 |
% \bl{$mkeps(r^{\{..n\}})$} & \bl{$\dn$} & $\bl{Stars\,[]}$\\ |
|
939 |
% \bl{$mkeps(r^{\{n..m\}})$} & \bl{$\dn$} & $\bl{Stars\,(mkeps(r))^n}$\medskip\\ |
|
940 |
% |
|
941 |
% \bl{$mkeps(r^+)$} & \bl{$\dn$} & \bl{$mkeps(r^{\{1..\}})$}\\ |
|
942 |
% \bl{$mkeps(r^?)$} & \bl{$\dn$} & \bl{$mkeps(r^{\{..1\}})$}\\ |
|
943 |
%\end{tabular} |
|
944 |
%\end{center} |
|
945 |
% |
|
946 |
%\end{frame} |
|
947 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
948 |
% |
|
949 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
950 |
%\begin{frame}[c] |
|
951 |
% \frametitle{Coursework: Inj} |
|
952 |
% |
|
953 |
%\begin{center} |
|
954 |
%\begin{tabular}{@ {}l@ {\hspace{2mm}}c@ {\hspace{2mm}}l@ {}} |
|
955 |
% \bl{$inj([c_1 c_2 \ldots c_n])\,c\,Empty$} & \bl{$\dn$} & $\bl{Chr\,c}$\\ |
|
956 |
% \bl{$inj(r^*)\,c\;(Seq\,v\,(Stars\,vs))$} & \bl{$\dn$} & $\bl{Stars\,(inj\,r\,c\,v::vs)}$\\ |
|
957 |
% \bl{$inj(r^{\{n\}})\,c\;(Seq\,v\,(Stars\,vs))$} & \bl{$\dn$} & $\bl{Stars\,(inj\,r\,c\,v::vs)}$\\ |
|
958 |
% \bl{$inj(r^{\{n..\}})\,c\;(Seq\,v\,(Stars\,vs))$} & \bl{$\dn$} & $\bl{Stars\,(inj\,r\,c\,v::vs)}$\\ |
|
959 |
% \bl{$inj(r^{\{..n\}})\,c\;(Seq\,v\,(Stars\,vs))$} & \bl{$\dn$} & $\bl{Stars\,(inj\,r\,c\,v::vs)}$\\ |
|
960 |
% \bl{$inj(r^{\{n..m\}})\,c\;(Seq\,v\,(Stars\,vs))$} & \bl{$\dn$} & $\bl{Stars\,(inj\,r\,c\,v::vs)}$\medskip\\ |
|
961 |
% |
|
962 |
% \bl{$inj(r^+)\,c\,v$} & \bl{$\dn$} & \bl{$inj(r^{\{1..\}})\,c\,v$}\\ |
|
963 |
% \bl{$inj(r^?)\,c\,v$} & \bl{$\dn$} & \bl{$inj(r^{\{..1\}})\,c\,v$}\\ |
|
964 |
%\end{tabular} |
|
965 |
%\end{center} |
|
966 |
% |
|
967 |
%\end{frame} |
|
968 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
702 | 969 |
% |
598 | 970 |
|
49 | 971 |
\end{document} |
972 |
||
803 | 973 |
|
974 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
975 |
\begin{frame}[c] |
|
976 |
\frametitle{Parser Combinators} |
|
977 |
||
978 |
One of the simplest ways to implement a parser, see |
|
979 |
{\small\url{https://vimeo.com/142341803}} (by Haoyi Li)\bigskip |
|
980 |
||
981 |
\begin{itemize} |
|
982 |
\item[$\bullet$] build-in library in Scala |
|
983 |
\item[$\bullet$] fastparse (2) library by Haoyi Li; is part of Ammonite |
|
984 |
\item[$\bullet$] possible exponential runtime behaviour |
|
985 |
\end{itemize} |
|
986 |
||
987 |
\end{frame} |
|
988 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
989 |
||
990 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
991 |
\begin{frame}[c] |
|
992 |
\frametitle{Parser Combinators} |
|
993 |
||
994 |
Parser combinators: \bigskip |
|
995 |
||
996 |
\begin{minipage}{1.1\textwidth} |
|
997 |
\begin{center} |
|
998 |
\mbox{}\hspace{-12mm}\mbox{}$\underbrace{\text{list of tokens}}_{\text{input}}$ \bl{$\Rightarrow$} |
|
999 |
$\underbrace{\text{set of (parsed input, unparsed input)}}_{\text{output}}$ |
|
1000 |
\end{center} |
|
1001 |
\end{minipage}\bigskip |
|
1002 |
||
1003 |
\begin{itemize} |
|
1004 |
\item atomic parsers |
|
1005 |
\item sequencing |
|
1006 |
\item alternative |
|
1007 |
\item semantic action (map-parser) |
|
1008 |
\end{itemize} |
|
1009 |
||
1010 |
\end{frame} |
|
1011 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
1012 |
||
1013 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
1014 |
\begin{frame}[c] |
|
1015 |
||
1016 |
Atomic parsers, for example, number tokens |
|
1017 |
||
1018 |
\begin{center} |
|
1019 |
\bl{$\texttt{Num(123)}::rest \;\Rightarrow\; \{(\texttt{Num(123)}, rest)\}$} |
|
1020 |
\end{center}\bigskip |
|
1021 |
||
1022 |
\begin{itemize} |
|
1023 |
\item you consume one or more token from the\\ |
|
1024 |
input (stream) |
|
1025 |
\item also works for characters and strings |
|
1026 |
\end{itemize} |
|
1027 |
\end{frame} |
|
1028 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
1029 |
||
1030 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
1031 |
\begin{frame}[c] |
|
1032 |
||
1033 |
Alternative parser (code \bl{$p\;||\;q$})\bigskip |
|
1034 |
||
1035 |
\begin{itemize} |
|
1036 |
\item apply \bl{$p$} and also \bl{$q$}; then combine |
|
1037 |
the outputs |
|
1038 |
\end{itemize} |
|
1039 |
||
1040 |
\begin{center} |
|
1041 |
\large \bl{$p(\text{input}) \cup q(\text{input})$} |
|
1042 |
\end{center} |
|
1043 |
||
1044 |
\end{frame} |
|
1045 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
1046 |
||
1047 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
1048 |
\begin{frame}[c] |
|
1049 |
||
1050 |
Sequence parser (code \bl{$p\sim q$})\bigskip |
|
1051 |
||
1052 |
\begin{itemize} |
|
1053 |
\item apply first \bl{$p$} producing a set of pairs |
|
1054 |
\item then apply \bl{$q$} to the unparsed part |
|
1055 |
\item then combine the results:\medskip |
|
1056 |
\begin{center} |
|
1057 |
((output$_1$, output$_2$), unparsed part) |
|
1058 |
\end{center} |
|
1059 |
\end{itemize} |
|
1060 |
||
1061 |
\begin{center} |
|
1062 |
\begin{tabular}{l} |
|
1063 |
\large \bl{$\{((o_1, o_2), u_2) \;|\;$}\\[2mm] |
|
1064 |
\large\mbox{}\hspace{15mm} \bl{$(o_1, u_1) \in p(\text{input}) \wedge$}\\[2mm] |
|
1065 |
\large\mbox{}\hspace{15mm} \bl{$(o_2, u_2) \in q(u_1)\}$} |
|
1066 |
\end{tabular} |
|
1067 |
\end{center} |
|
1068 |
||
1069 |
||
1070 |
\end{frame} |
|
1071 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
1072 |
||
1073 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
1074 |
\begin{frame}[c] |
|
1075 |
||
1076 |
Map-parser (code \bl{$p.map(f)\;$})\bigskip |
|
1077 |
||
1078 |
\begin{itemize} |
|
1079 |
\item apply \bl{$p$} producing a set of pairs |
|
1080 |
\item then apply the function \bl{$f$} to each first component |
|
1081 |
\end{itemize} |
|
1082 |
||
1083 |
\begin{center} |
|
1084 |
\begin{tabular}{l} |
|
1085 |
\large \bl{$\{(f(o_1), u_1) \;|\; (o_1, u_1) \in p(\text{input})\}$} |
|
1086 |
\end{tabular} |
|
1087 |
\end{center}\bigskip\bigskip\pause |
|
1088 |
||
1089 |
\bl{$f$} is the semantic action (``what to do with the parsed input'') |
|
1090 |
||
1091 |
\end{frame} |
|
1092 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
1093 |
||
1094 |
||
1095 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
1096 |
\mode<presentation>{ |
|
1097 |
\begin{frame}[c] |
|
1098 |
\frametitle{\begin{tabular}{c}Semantic Actions\end{tabular}} |
|
1099 |
||
1100 |
Addition |
|
1101 |
||
1102 |
\begin{center} |
|
1103 |
\bl{$\meta{T} \sim + \sim \meta{E} \Rightarrow \underbrace{f\,((x,y), z) \Rightarrow x + z}_{\text{semantic action}}$} |
|
1104 |
\end{center}\pause |
|
1105 |
||
1106 |
Multiplication |
|
1107 |
||
1108 |
\begin{center} |
|
1109 |
\bl{$\meta{F} \sim * \sim \meta{T} \Rightarrow f\,((x,y), z) \Rightarrow x * z$} |
|
1110 |
\end{center}\pause |
|
1111 |
||
1112 |
Parenthesis |
|
1113 |
||
1114 |
\begin{center} |
|
1115 |
\bl{$\text{(} \sim \meta{E} \sim \text{)} \Rightarrow f\,((x,y), z) \Rightarrow y$} |
|
1116 |
\end{center} |
|
1117 |
||
1118 |
\end{frame}} |
|
1119 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
1120 |
||
1121 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
1122 |
\begin{frame}[c] |
|
1123 |
\frametitle{Types of Parsers} |
|
1124 |
||
1125 |
\begin{itemize} |
|
1126 |
\item {\bf Sequencing}: if \bl{$p$} returns results of type \bl{$T$}, and \bl{$q$} results of type \bl{$S$}, |
|
1127 |
then \bl{$p \sim q$} returns results of type |
|
1128 |
||
1129 |
\begin{center} |
|
1130 |
\bl{$T \times S$} |
|
1131 |
\end{center}\pause |
|
1132 |
||
1133 |
\item {\bf Alternative}: if \bl{$p$} returns results of type \bl{$T$} then \bl{$q$} \alert{must} also have results of type \bl{$T$}, |
|
1134 |
and \bl{$p \;||\; q$} returns results of type |
|
1135 |
||
1136 |
\begin{center} |
|
1137 |
\bl{$T$} |
|
1138 |
\end{center}\pause |
|
1139 |
||
1140 |
\item {\bf Semantic Action}: if \bl{$p$} returns results of type \bl{$T$} and \bl{$f$} is a function from |
|
1141 |
\bl{$T$} to \bl{$S$}, then |
|
1142 |
\bl{$p \Rightarrow f$} returns results of type |
|
1143 |
||
1144 |
\begin{center} |
|
1145 |
\bl{$S$} |
|
1146 |
\end{center} |
|
1147 |
||
1148 |
\end{itemize} |
|
1149 |
||
1150 |
\end{frame} |
|
1151 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
1152 |
||
1153 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
1154 |
\begin{frame}[c] |
|
1155 |
\frametitle{Input Types of Parsers} |
|
1156 |
||
1157 |
\begin{itemize} |
|
1158 |
\item input: \alert{token list} |
|
1159 |
\item output: set of (output\_type, \alert{token list}) |
|
1160 |
\end{itemize}\bigskip\pause |
|
1161 |
||
1162 |
actually it can be any input type as long as it is a kind of |
|
1163 |
sequence (for example a string) |
|
1164 |
||
1165 |
\end{frame} |
|
1166 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
1167 |
||
1168 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
1169 |
\begin{frame}[c] |
|
1170 |
\frametitle{Scannerless Parsers} |
|
1171 |
||
1172 |
\begin{itemize} |
|
1173 |
\item input: \alert{string} |
|
1174 |
\item output: set of (output\_type, \alert{string}) |
|
1175 |
\end{itemize}\bigskip\bigskip |
|
1176 |
||
1177 |
but using lexers is better because whitespaces or comments can be |
|
1178 |
filtered out; then input is a sequence of tokens |
|
1179 |
||
1180 |
\end{frame} |
|
1181 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
1182 |
||
1183 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
1184 |
\begin{frame}[c] |
|
1185 |
\frametitle{Successful Parses} |
|
1186 |
||
1187 |
\begin{itemize} |
|
1188 |
\item input: string |
|
1189 |
\item output: \alert{set of} (output\_type, string) |
|
1190 |
\end{itemize}\bigskip |
|
1191 |
||
1192 |
a parse is successful whenever the input has been fully |
|
1193 |
``consumed'' (that is the second component is empty) |
|
1194 |
||
1195 |
\end{frame} |
|
1196 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
1197 |
||
1198 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
1199 |
\begin{frame}[c] |
|
1200 |
\frametitle{Abstract Parser Class} |
|
1201 |
||
1202 |
\small |
|
1203 |
\lstinputlisting[language=Scala,xleftmargin=1mm] |
|
1204 |
{../progs/app7.scala} |
|
1205 |
\end{frame} |
|
1206 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
1207 |
||
1208 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
1209 |
\begin{frame}[c] |
|
1210 |
||
1211 |
\small |
|
1212 |
\fontsize{10}{12}\selectfont |
|
1213 |
\lstinputlisting[language=Scala,xleftmargin=1mm] |
|
1214 |
{../progs/app8.scala} |
|
1215 |
\end{frame} |
|
1216 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
1217 |
||
1218 |
||
1219 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
1220 |
\begin{frame}[c] |
|
1221 |
\frametitle{Two Grammars} |
|
1222 |
||
1223 |
Which languages are recognised by the following two grammars? |
|
1224 |
||
1225 |
\begin{center} |
|
1226 |
\bl{\begin{tabular}{lcl} |
|
1227 |
$\meta{S}$ & $\rightarrow$ & $1 \cdot \meta{S} \cdot \meta{S}$\\ |
|
1228 |
& $|$ & $\epsilon$ |
|
1229 |
\end{tabular}} |
|
1230 |
\end{center}\bigskip |
|
1231 |
||
1232 |
\begin{center} |
|
1233 |
\bl{\begin{tabular}{lcl} |
|
1234 |
$\meta{U}$ & $\rightarrow$ & $1 \cdot \meta{U}$\\ |
|
1235 |
& $|$ & $\epsilon$ |
|
1236 |
\end{tabular}} |
|
1237 |
\end{center} |
|
1238 |
||
1239 |
\end{frame} |
|
1240 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
1241 |
||
1242 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
1243 |
\begin{frame}[t] |
|
1244 |
\frametitle{Ambiguous Grammars} |
|
1245 |
||
1246 |
\begin{center} |
|
1247 |
\begin{tikzpicture} |
|
1248 |
\begin{axis}[xlabel={\pcode{1}s},ylabel={time in secs}, |
|
1249 |
enlargelimits=false, |
|
1250 |
xtick={0,100,...,1000}, |
|
1251 |
xmax=1050, |
|
1252 |
ymax=33, |
|
1253 |
ytick={0,5,...,30}, |
|
1254 |
scaled ticks=false, |
|
1255 |
axis lines=left, |
|
1256 |
width=11cm, |
|
1257 |
height=7cm, |
|
1258 |
legend entries={unambiguous,ambiguous}, |
|
1259 |
legend pos=north east, |
|
1260 |
legend cell align=left, |
|
1261 |
x tick label style={font=\small,/pgf/number format/1000 sep={}}] |
|
1262 |
\addplot[blue,mark=*, mark options={fill=white}] |
|
1263 |
table {s-grammar1.data}; |
|
1264 |
\only<2>{ |
|
1265 |
\addplot[red,mark=triangle*, mark options={fill=white}] |
|
1266 |
table {s-grammar2.data};} |
|
1267 |
\end{axis} |
|
1268 |
\end{tikzpicture} |
|
1269 |
\end{center} |
|
1270 |
||
1271 |
\end{frame} |
|
1272 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
1273 |
||
1274 |
||
1275 |
||
1276 |
||
49 | 1277 |
%%% Local Variables: |
1278 |
%%% mode: latex |
|
1279 |
%%% TeX-master: t |
|
1280 |
%%% End: |
|
1281 |