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