142 Of$\!$fice: & S1.27 (1st floor Strand Building)\\ |
138 Of$\!$fice: & S1.27 (1st floor Strand Building)\\ |
143 Slides: & KEATS (also home work is there)\\ |
139 Slides: & KEATS (also home work is there)\\ |
144 \end{tabular} |
140 \end{tabular} |
145 \end{center} |
141 \end{center} |
146 |
142 |
147 |
143 \end{frame}} |
148 \end{frame}} |
144 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
149 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
150 |
|
151 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
152 \mode<presentation>{ |
|
153 \begin{frame}[c] |
|
154 \frametitle{\begin{tabular}{c}Building a ``Web Browser''\end{tabular}} |
|
155 |
|
156 Using a lexer: assume the following regular expressions |
|
157 |
|
158 \begin{center} |
|
159 \bl{\begin{tabular}{lcl} |
|
160 $SY\!M$ & $\dn$ & $(\text{a}..\text{zA}..\text{Z0}..\text{9}..)$\\ |
|
161 $W\!O\!RD$ & $\dn$ & $SY\!M^+$\\ |
|
162 $BT\!AG$ & $\dn$ & $<\!W\!O\!RD\!>$\\ |
|
163 $ET\!AG$ & $\dn$ & $<\!/W\!O\!RD\!>$\\ |
|
164 $W\!HIT\!E$ & $\dn$ & $\texttt{" "} + \texttt{"}\slash{}n\texttt{"}$\\ |
|
165 \end{tabular}} |
|
166 \end{center} |
|
167 |
|
168 \end{frame}} |
|
169 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
170 |
|
171 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
172 \mode<presentation>{ |
|
173 \begin{frame}[c] |
|
174 \frametitle{\begin{tabular}{c}Interpreting a List of Tokens\end{tabular}} |
|
175 |
|
176 \begin{itemize} |
|
177 \item the text should be formatted consistently up to a specified width, say 60 characters |
|
178 \item potential linebreaks are inserted by the formatter (not the input) |
|
179 \item repeated whitespaces are ``condensed'' to a single whitepace |
|
180 \item \bl{$<\!p\!>$} \bl{$<\!\slash{}p\!>$} start/end paragraph |
|
181 \item \bl{$<\!b\!>$} \bl{$<\!\slash{}b\!>$} start/end bold |
|
182 \item \bl{$<\!red\!>$} \bl{$<\!\slash{}red\!>$} start/end red (cyan, etc) |
|
183 |
|
184 |
|
185 \end{itemize} |
|
186 |
|
187 \end{frame}} |
|
188 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
189 |
|
190 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
191 \mode<presentation>{ |
|
192 \begin{frame}[c] |
|
193 \frametitle{\begin{tabular}{c}Interpreting a List of Tokens\end{tabular}} |
|
194 |
|
195 The lexer cannot prevent errors like |
|
196 |
|
197 \begin{center} |
|
198 \bl{$<\!b\!>$} \ldots \bl{$<\!p\!>$} \ldots \bl{$<\!\slash{}b\!>$} \ldots \bl{$<\!\slash{}p\!>$} |
|
199 \end{center} |
|
200 |
|
201 or |
|
202 |
|
203 \begin{center} |
|
204 \bl{$<\!\slash{}b\!>$} \ldots \bl{$<\!b\!>$} |
|
205 \end{center} |
|
206 |
|
207 |
|
208 \end{frame}} |
|
209 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
210 |
|
211 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
212 \mode<presentation>{ |
|
213 \begin{frame}[c] |
|
214 \frametitle{\begin{tabular}{c}Parser Combinators\end{tabular}} |
|
215 |
|
216 Parser combinators: \bigskip |
|
217 |
|
218 \begin{minipage}{1.1\textwidth} |
|
219 \begin{center} |
|
220 \mbox{}\hspace{-12mm}\mbox{}$\underbrace{\text{list of tokens}}_{\text{input}}$ \bl{$\Rightarrow$} |
|
221 $\underbrace{\text{set of (parsed input, unparsed input)}}_{\text{output}}$ |
|
222 \end{center} |
|
223 \end{minipage}\bigskip |
|
224 |
|
225 \begin{itemize} |
|
226 \item sequencing |
|
227 \item alternative |
|
228 \item semantic action |
|
229 \end{itemize} |
|
230 |
|
231 \end{frame}} |
|
232 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
233 |
|
234 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
235 \mode<presentation>{ |
|
236 \begin{frame}[c] |
|
237 |
|
238 Alternative parser (code \bl{$p\;||\;q$})\bigskip |
|
239 |
|
240 \begin{itemize} |
|
241 \item apply \bl{$p$} and also \bl{$q$}; then combine the outputs |
|
242 \end{itemize} |
|
243 |
|
244 \begin{center} |
|
245 \large \bl{$p(\text{input}) \cup q(\text{input})$} |
|
246 \end{center} |
|
247 |
|
248 |
|
249 \end{frame}} |
|
250 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
251 |
|
252 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
253 \mode<presentation>{ |
|
254 \begin{frame}[c] |
|
255 |
|
256 Sequence parser (code \bl{$p\sim q$})\bigskip |
|
257 |
|
258 \begin{itemize} |
|
259 \item apply first \bl{$p$} producing a set of pairs |
|
260 \item then apply \bl{$q$} to the unparsed parts |
|
261 \item then combine the results:\\ \mbox{}\;\;((output$_1$, output$_2$), unparsed part) |
|
262 \end{itemize} |
|
263 |
|
264 \begin{center} |
|
265 \begin{tabular}{l} |
|
266 \large \bl{$\{((o_1, o_2), u_2) \;|\;$}\\[2mm] |
|
267 \large\mbox{}\hspace{15mm} \bl{$(o_1, u_1) \in p(\text{input}) \wedge$}\\[2mm] |
|
268 \large\mbox{}\hspace{15mm} \bl{$(o_2, u_2) \in q(u_1)\}$} |
|
269 \end{tabular} |
|
270 \end{center} |
|
271 |
|
272 |
|
273 \end{frame}} |
|
274 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
275 |
|
276 |
|
277 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
278 \mode<presentation>{ |
|
279 \begin{frame}[c] |
|
280 |
|
281 Function parser (code \bl{$p \Longrightarrow f$})\bigskip |
|
282 |
|
283 \begin{itemize} |
|
284 \item apply \bl{$p$} producing a set of pairs |
|
285 \item then apply the function \bl{$f$} to each first component |
|
286 \end{itemize} |
|
287 |
|
288 \begin{center} |
|
289 \begin{tabular}{l} |
|
290 \large \bl{$\{(f(o_1), u_1) \;|\; (o_1, u_1) \in p(\text{input})\}$} |
|
291 \end{tabular} |
|
292 \end{center}\bigskip\bigskip\pause |
|
293 |
|
294 \bl{$f$} is the semantic action (``what to do with the parsed input'') |
|
295 |
|
296 \end{frame}} |
|
297 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
298 |
|
299 |
|
300 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
301 \mode<presentation>{ |
|
302 \begin{frame}[c] |
|
303 |
|
304 Token parser:\bigskip |
|
305 |
|
306 \begin{itemize} |
|
307 \item if the input is |
|
308 |
|
309 \begin{center} |
|
310 \large \bl{$tok_1:: tok_2 :: \ldots :: tok_n$} |
|
311 \end{center} |
|
312 |
|
313 then return |
|
314 |
|
315 \begin{center} |
|
316 \large \bl{$\{(tok_1,\; tok_2 :: \ldots :: tok_n)\}$} |
|
317 \end{center} |
|
318 |
|
319 or |
|
320 |
|
321 \begin{center} |
|
322 \large \bl{$\{\}$} |
|
323 \end{center} |
|
324 |
|
325 if \bl{$tok_1$} is not the right token we are looking for |
|
326 \end{itemize} |
|
327 |
|
328 |
|
329 |
|
330 \end{frame}} |
|
331 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
332 |
|
333 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
334 \mode<presentation>{ |
|
335 \begin{frame}[c] |
|
336 |
|
337 Number-Token parser:\bigskip |
|
338 |
|
339 \begin{itemize} |
|
340 \item if the input is |
|
341 |
|
342 \begin{center} |
|
343 \large \bl{$num\_tok(42):: tok_2 :: \ldots :: tok_n$} |
|
344 \end{center} |
|
345 |
|
346 then return |
|
347 |
|
348 \begin{center} |
|
349 \large \bl{$\{(42,\; tok_2 :: \ldots :: tok_n)\}$} |
|
350 \end{center} |
|
351 |
|
352 or |
|
353 |
|
354 \begin{center} |
|
355 \large \bl{$\{\}$} |
|
356 \end{center} |
|
357 |
|
358 if \bl{$tok_1$} is not the right token we are looking for |
|
359 \end{itemize}\pause |
|
360 |
|
361 \begin{center} |
|
362 list of tokens \bl{$\Rightarrow$} set of (\alert{int}, list of tokens) |
|
363 \end{center} |
|
364 |
|
365 \end{frame}} |
|
366 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
367 |
|
368 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
369 \mode<presentation>{ |
|
370 \begin{frame}[c] |
|
371 |
|
372 \begin{itemize} |
|
373 \item if the input is |
|
374 |
|
375 \begin{center} |
|
376 \begin{tabular}{l} |
|
377 \large \bl{$num\_tok(42)::$}\\ |
|
378 \hspace{7mm}\large \bl{$num\_tok(3) ::$}\\ |
|
379 \hspace{14mm}\large \bl{$tok_3 :: \ldots :: tok_n$} |
|
380 \end{tabular} |
|
381 \end{center} |
|
382 |
|
383 and the parser is |
|
384 |
|
385 \begin{center} |
|
386 \bl{$ntp \sim ntp$} |
|
387 \end{center} |
|
388 |
|
389 the successful output will be |
|
390 |
|
391 \begin{center} |
|
392 \large \bl{$\{((42, 3),\; tok_2 :: \ldots :: tok_n)\}$} |
|
393 \end{center}\pause |
|
394 |
|
395 Now we can form |
|
396 \begin{center} |
|
397 \bl{$(ntp \sim ntp) \Longrightarrow f$} |
|
398 \end{center} |
|
399 |
|
400 where \bl{$f$} is the semantic action (``what to do with the pair'') |
|
401 |
|
402 \end{itemize} |
|
403 |
|
404 \end{frame}} |
|
405 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
406 |
|
407 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
408 \mode<presentation>{ |
|
409 \begin{frame}[c] |
|
410 \frametitle{\begin{tabular}{c}Semantic Actions\end{tabular}} |
|
411 |
|
412 Addition |
|
413 |
|
414 \begin{center} |
|
415 \bl{$T \sim + \sim E \Longrightarrow \underbrace{f((x,y), z) \Rightarrow x + z}_{\text{semantic action}}$} |
|
416 \end{center}\pause |
|
417 |
|
418 Multiplication |
|
419 |
|
420 \begin{center} |
|
421 \bl{$F \sim * \sim T \Longrightarrow f((x,y), z) \Rightarrow x * z$} |
|
422 \end{center}\pause |
|
423 |
|
424 Parenthesis |
|
425 |
|
426 \begin{center} |
|
427 \bl{$\text{(} \sim E \sim \text{)} \Longrightarrow f((x,y), z) \Rightarrow y$} |
|
428 \end{center} |
|
429 |
|
430 \end{frame}} |
|
431 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
432 |
|
433 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
434 \mode<presentation>{ |
|
435 \begin{frame}[c] |
|
436 \frametitle{\begin{tabular}{c}Types of Parsers\end{tabular}} |
|
437 |
|
438 \begin{itemize} |
|
439 \item {\bf Sequencing}: if \bl{$p$} returns results of type \bl{$T$}, and \bl{$q$} results of type \bl{$S$}, |
|
440 then \bl{$p \sim q$} returns results of type |
|
441 |
|
442 \begin{center} |
|
443 \bl{$T \times S$} |
|
444 \end{center}\pause |
|
445 |
|
446 \item {\bf Alternative}: if \bl{$p$} returns results of type \bl{$T$} then \bl{$q$} \alert{must} also have results of type \bl{$T$}, |
|
447 and \bl{$p \;||\; q$} returns results of type |
|
448 |
|
449 \begin{center} |
|
450 \bl{$T$} |
|
451 \end{center}\pause |
|
452 |
|
453 \item {\bf Semantic Action}: if \bl{$p$} returns results of type \bl{$T$} and \bl{$f$} is a function from |
|
454 \bl{$T$} to \bl{$S$}, then |
|
455 \bl{$p \Longrightarrow f$} returns results of type |
|
456 |
|
457 \begin{center} |
|
458 \bl{$S$} |
|
459 \end{center} |
|
460 |
|
461 \end{itemize} |
|
462 |
|
463 \end{frame}} |
|
464 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
465 |
|
466 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
467 \mode<presentation>{ |
|
468 \begin{frame}[c] |
|
469 \frametitle{\begin{tabular}{c}Input Types of Parsers\end{tabular}} |
|
470 |
|
471 \begin{itemize} |
|
472 \item input: \alert{list of tokens} |
|
473 \item output: set of (output\_type, \alert{list of tokens}) |
|
474 \end{itemize}\bigskip\pause |
|
475 |
|
476 actually it can be any input type as long as it is a kind of sequence |
|
477 (for example a string) |
|
478 |
|
479 \end{frame}} |
|
480 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
481 |
|
482 |
|
483 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
484 \mode<presentation>{ |
|
485 \begin{frame}[c] |
|
486 \frametitle{\begin{tabular}{c}Scannerless Parsers\end{tabular}} |
|
487 |
|
488 \begin{itemize} |
|
489 \item input: \alert{string} |
|
490 \item output: set of (output\_type, \alert{string}) |
|
491 \end{itemize}\bigskip |
|
492 |
|
493 but lexers are better when whitespaces or comments need to be filtered out |
|
494 |
|
495 \end{frame}} |
|
496 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
497 |
|
498 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
499 \mode<presentation>{ |
|
500 \begin{frame}[c] |
|
501 \frametitle{\begin{tabular}{c}Successful Parses\end{tabular}} |
|
502 |
|
503 \begin{itemize} |
|
504 \item input: string |
|
505 \item output: \alert{set of} (output\_type, string) |
|
506 \end{itemize}\bigskip |
|
507 |
|
508 a parse is successful whenever the input has been |
|
509 fully ``consumed'' (that is the second component is empty) |
|
510 |
|
511 |
|
512 \end{frame}} |
|
513 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
514 |
|
515 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
516 \mode<presentation>{ |
|
517 \begin{frame}[c] |
|
518 {\lstset{language=Scala}\fontsize{10}{12}\selectfont |
|
519 \texttt{\lstinputlisting{app7.scala}}} |
|
520 \end{frame}} |
|
521 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
522 |
|
523 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
524 \mode<presentation>{ |
|
525 \begin{frame}[c] |
|
526 {\lstset{language=Scala}\fontsize{10}{12}\selectfont |
|
527 \texttt{\lstinputlisting{app7.scala}}} |
|
528 \end{frame}} |
|
529 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
530 |
|
531 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
532 \mode<presentation>{ |
|
533 \begin{frame}[c] |
|
534 {\lstset{language=Scala}\fontsize{10}{12}\selectfont |
|
535 \texttt{\lstinputlisting{app8.scala}}} |
|
536 \end{frame}} |
|
537 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
538 |
|
539 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
540 \mode<presentation>{ |
|
541 \begin{frame}[c] |
|
542 \frametitle{\begin{tabular}{c}Two Grammars\end{tabular}} |
|
543 |
|
544 Which languages are recognised by the following two grammars? |
|
545 |
|
546 \begin{center} |
|
547 \bl{\begin{tabular}{lcl} |
|
548 $S$ & $\rightarrow$ & $1 \cdot S \cdot S$\\ |
|
549 & $|$ & $\epsilon$ |
|
550 \end{tabular}} |
|
551 \end{center}\bigskip |
|
552 |
|
553 \begin{center} |
|
554 \bl{\begin{tabular}{lcl} |
|
555 $U$ & $\rightarrow$ & $1 \cdot U$\\ |
|
556 & $|$ & $\epsilon$ |
|
557 \end{tabular}} |
|
558 \end{center} |
|
559 |
|
560 \end{frame}} |
|
561 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
562 |
|
563 |
|
564 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
565 \mode<presentation>{ |
|
566 \begin{frame}[t] |
|
567 \frametitle{\begin{tabular}{c}Ambiguous Grammars\end{tabular}} |
|
568 |
|
569 \mbox{}\\[-25mm]\mbox{} |
|
570 |
|
571 \begin{center} |
|
572 \begin{tikzpicture}[y=.2cm, x=.009cm] |
|
573 %axis |
|
574 \draw (0,0) -- coordinate (x axis mid) (1000,0); |
|
575 \draw (0,0) -- coordinate (y axis mid) (0,30); |
|
576 %ticks |
|
577 \foreach \x in {0, 20, 100, 200,...,1000} |
|
578 \draw (\x,1pt) -- (\x,-3pt) |
|
579 node[anchor=north] {\small \x}; |
|
580 \foreach \y in {0,5,...,30} |
|
581 \draw (1pt,\y) -- (-3pt,\y) |
|
582 node[anchor=east] {\small\y}; |
|
583 %labels |
|
584 \node[below=0.6cm] at (x axis mid) {\bl{1}s}; |
|
585 \node[rotate=90, left=1.2cm] at (y axis mid) {secs}; |
|
586 |
|
587 %plots |
|
588 \draw[color=blue] plot[mark=*, mark options={fill=white}] |
|
589 file {s-grammar1.data}; |
|
590 \only<2->{\draw[color=red] plot[mark=triangle*, mark options={fill=white} ] |
|
591 file {s-grammar2.data};} |
|
592 %legend |
|
593 \begin{scope}[shift={(400,20)}] |
|
594 \draw[color=blue] (0,0) -- |
|
595 plot[mark=*, mark options={fill=white}] (0.25,0) -- (0.5,0) |
|
596 node[right]{\small unambiguous}; |
|
597 \only<2->{\draw[yshift=\baselineskip, color=red] (0,0) -- |
|
598 plot[mark=triangle*, mark options={fill=white}] (0.25,0) -- (0.5,0) |
|
599 node[right]{\small ambiguous};} |
|
600 \end{scope} |
|
601 \end{tikzpicture} |
|
602 \end{center} |
|
603 |
|
604 \end{frame}} |
|
605 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
606 |
|
607 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
608 \mode<presentation>{ |
|
609 \begin{frame}[c] |
|
610 \frametitle{\begin{tabular}{c}What about Left-Recursion?\end{tabular}} |
|
611 |
|
612 \begin{itemize} |
|
613 \item we record when we recursively called a parser\bigskip |
|
614 \item whenever the is a recursion, the parser must have consumed something --- so |
|
615 we can decrease the input string/list of token by one (at the end) |
|
616 \end{itemize} |
|
617 \end{frame}} |
|
618 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
619 |
145 |
620 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
146 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
621 \mode<presentation>{ |
147 \mode<presentation>{ |
622 \begin{frame}[c] |
148 \begin{frame}[c] |
623 \frametitle{\begin{tabular}{c}While-Language\end{tabular}} |
149 \frametitle{\begin{tabular}{c}While-Language\end{tabular}} |
643 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
170 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
644 |
171 |
645 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
172 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
646 \mode<presentation>{ |
173 \mode<presentation>{ |
647 \begin{frame}[c] |
174 \begin{frame}[c] |
648 \frametitle{\begin{tabular}{c}An Interpreter\end{tabular}} |
175 \frametitle{\begin{tabular}{c}Fibonacci Numbers\end{tabular}} |
649 |
176 |
650 \begin{center} |
177 \mbox{}\\[-18mm]\mbox{} |
651 \bl{\begin{tabular}{l} |
178 |
652 $\{$\\ |
179 {\lstset{language=While}\fontsize{10}{12}\selectfont |
653 \;\;$x := 5 \text{;}$\\ |
180 \texttt{\lstinputlisting{app9.while}}} |
654 \;\;$y := x * 3\text{;}$\\ |
181 |
655 \;\;$y := x * 4\text{;}$\\ |
182 \end{frame}} |
656 \;\;$x := u * 3$\\ |
183 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
657 $\}$ |
184 |
658 \end{tabular}} |
185 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
659 \end{center} |
186 \mode<presentation>{ |
660 |
187 \begin{frame}[c] |
661 \begin{itemize} |
188 \frametitle{\begin{tabular}{c}Interpreter\end{tabular}} |
662 \item the interpreter has to record the value of \bl{$x$} before assigning a value to \bl{$y$}\pause |
189 |
663 \item \bl{\text{eval}(stmt, env)} |
190 |
664 \end{itemize} |
191 |
665 |
192 \end{frame}} |
666 |
193 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
667 \end{frame}} |
194 |
668 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
195 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
196 \mode<presentation>{ |
|
197 \begin{frame}[c] |
|
198 \frametitle{\begin{tabular}{c}Interpreting a List of Tokens\end{tabular}} |
|
199 |
|
200 The lexer cannot prevent errors like |
|
201 |
|
202 \begin{center} |
|
203 \bl{$<\!b\!>$} \ldots \bl{$<\!p\!>$} \ldots \bl{$<\!\slash{}b\!>$} \ldots \bl{$<\!\slash{}p\!>$} |
|
204 \end{center} |
|
205 |
|
206 or |
|
207 |
|
208 \begin{center} |
|
209 \bl{$<\!\slash{}b\!>$} \ldots \bl{$<\!b\!>$} |
|
210 \end{center} |
|
211 |
|
212 |
|
213 \end{frame}} |
|
214 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
215 |
|
216 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
217 \mode<presentation>{ |
|
218 \begin{frame}[c] |
|
219 \frametitle{\begin{tabular}{c}Parser Combinators\end{tabular}} |
|
220 |
|
221 Parser combinators: \bigskip |
|
222 |
|
223 \begin{minipage}{1.1\textwidth} |
|
224 \begin{center} |
|
225 \mbox{}\hspace{-12mm}\mbox{}$\underbrace{\text{list of tokens}}_{\text{input}}$ \bl{$\Rightarrow$} |
|
226 $\underbrace{\text{set of (parsed input, unparsed input)}}_{\text{output}}$ |
|
227 \end{center} |
|
228 \end{minipage}\bigskip |
|
229 |
|
230 \begin{itemize} |
|
231 \item sequencing |
|
232 \item alternative |
|
233 \item semantic action |
|
234 \end{itemize} |
|
235 |
|
236 \end{frame}} |
|
237 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
238 |
|
239 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
240 \mode<presentation>{ |
|
241 \begin{frame}[c] |
|
242 |
|
243 Alternative parser (code \bl{$p\;||\;q$})\bigskip |
|
244 |
|
245 \begin{itemize} |
|
246 \item apply \bl{$p$} and also \bl{$q$}; then combine the outputs |
|
247 \end{itemize} |
|
248 |
|
249 \begin{center} |
|
250 \large \bl{$p(\text{input}) \cup q(\text{input})$} |
|
251 \end{center} |
|
252 |
|
253 |
|
254 \end{frame}} |
|
255 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
256 |
|
257 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
258 \mode<presentation>{ |
|
259 \begin{frame}[c] |
|
260 |
|
261 Sequence parser (code \bl{$p\sim q$})\bigskip |
|
262 |
|
263 \begin{itemize} |
|
264 \item apply first \bl{$p$} producing a set of pairs |
|
265 \item then apply \bl{$q$} to the unparsed parts |
|
266 \item then combine the results:\\ \mbox{}\;\;((output$_1$, output$_2$), unparsed part) |
|
267 \end{itemize} |
|
268 |
|
269 \begin{center} |
|
270 \begin{tabular}{l} |
|
271 \large \bl{$\{((o_1, o_2), u_2) \;|\;$}\\[2mm] |
|
272 \large\mbox{}\hspace{15mm} \bl{$(o_1, u_1) \in p(\text{input}) \wedge$}\\[2mm] |
|
273 \large\mbox{}\hspace{15mm} \bl{$(o_2, u_2) \in q(u_1)\}$} |
|
274 \end{tabular} |
|
275 \end{center} |
|
276 |
|
277 |
|
278 \end{frame}} |
|
279 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
280 |
|
281 |
|
282 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
283 \mode<presentation>{ |
|
284 \begin{frame}[c] |
|
285 |
|
286 Function parser (code \bl{$p \Longrightarrow f$})\bigskip |
|
287 |
|
288 \begin{itemize} |
|
289 \item apply \bl{$p$} producing a set of pairs |
|
290 \item then apply the function \bl{$f$} to each first component |
|
291 \end{itemize} |
|
292 |
|
293 \begin{center} |
|
294 \begin{tabular}{l} |
|
295 \large \bl{$\{(f(o_1), u_1) \;|\; (o_1, u_1) \in p(\text{input})\}$} |
|
296 \end{tabular} |
|
297 \end{center}\bigskip\bigskip\pause |
|
298 |
|
299 \bl{$f$} is the semantic action (``what to do with the parsed input'') |
|
300 |
|
301 \end{frame}} |
|
302 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
303 |
|
304 |
|
305 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
306 \mode<presentation>{ |
|
307 \begin{frame}[c] |
|
308 |
|
309 Token parser:\bigskip |
|
310 |
|
311 \begin{itemize} |
|
312 \item if the input is |
|
313 |
|
314 \begin{center} |
|
315 \large \bl{$tok_1:: tok_2 :: \ldots :: tok_n$} |
|
316 \end{center} |
|
317 |
|
318 then return |
|
319 |
|
320 \begin{center} |
|
321 \large \bl{$\{(tok_1,\; tok_2 :: \ldots :: tok_n)\}$} |
|
322 \end{center} |
|
323 |
|
324 or |
|
325 |
|
326 \begin{center} |
|
327 \large \bl{$\{\}$} |
|
328 \end{center} |
|
329 |
|
330 if \bl{$tok_1$} is not the right token we are looking for |
|
331 \end{itemize} |
|
332 |
|
333 |
|
334 |
|
335 \end{frame}} |
|
336 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
337 |
|
338 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
339 \mode<presentation>{ |
|
340 \begin{frame}[c] |
|
341 |
|
342 Number-Token parser:\bigskip |
|
343 |
|
344 \begin{itemize} |
|
345 \item if the input is |
|
346 |
|
347 \begin{center} |
|
348 \large \bl{$num\_tok(42):: tok_2 :: \ldots :: tok_n$} |
|
349 \end{center} |
|
350 |
|
351 then return |
|
352 |
|
353 \begin{center} |
|
354 \large \bl{$\{(42,\; tok_2 :: \ldots :: tok_n)\}$} |
|
355 \end{center} |
|
356 |
|
357 or |
|
358 |
|
359 \begin{center} |
|
360 \large \bl{$\{\}$} |
|
361 \end{center} |
|
362 |
|
363 if \bl{$tok_1$} is not the right token we are looking for |
|
364 \end{itemize}\pause |
|
365 |
|
366 \begin{center} |
|
367 list of tokens \bl{$\Rightarrow$} set of (\alert{int}, list of tokens) |
|
368 \end{center} |
|
369 |
|
370 \end{frame}} |
|
371 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
372 |
|
373 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
374 \mode<presentation>{ |
|
375 \begin{frame}[c] |
|
376 |
|
377 \begin{itemize} |
|
378 \item if the input is |
|
379 |
|
380 \begin{center} |
|
381 \begin{tabular}{l} |
|
382 \large \bl{$num\_tok(42)::$}\\ |
|
383 \hspace{7mm}\large \bl{$num\_tok(3) ::$}\\ |
|
384 \hspace{14mm}\large \bl{$tok_3 :: \ldots :: tok_n$} |
|
385 \end{tabular} |
|
386 \end{center} |
|
387 |
|
388 and the parser is |
|
389 |
|
390 \begin{center} |
|
391 \bl{$ntp \sim ntp$} |
|
392 \end{center} |
|
393 |
|
394 the successful output will be |
|
395 |
|
396 \begin{center} |
|
397 \large \bl{$\{((42, 3),\; tok_2 :: \ldots :: tok_n)\}$} |
|
398 \end{center}\pause |
|
399 |
|
400 Now we can form |
|
401 \begin{center} |
|
402 \bl{$(ntp \sim ntp) \Longrightarrow f$} |
|
403 \end{center} |
|
404 |
|
405 where \bl{$f$} is the semantic action (``what to do with the pair'') |
|
406 |
|
407 \end{itemize} |
|
408 |
|
409 \end{frame}} |
|
410 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
411 |
|
412 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
413 \mode<presentation>{ |
|
414 \begin{frame}[c] |
|
415 \frametitle{\begin{tabular}{c}Semantic Actions\end{tabular}} |
|
416 |
|
417 Addition |
|
418 |
|
419 \begin{center} |
|
420 \bl{$T \sim + \sim E \Longrightarrow \underbrace{f((x,y), z) \Rightarrow x + z}_{\text{semantic action}}$} |
|
421 \end{center}\pause |
|
422 |
|
423 Multiplication |
|
424 |
|
425 \begin{center} |
|
426 \bl{$F \sim * \sim T \Longrightarrow f((x,y), z) \Rightarrow x * z$} |
|
427 \end{center}\pause |
|
428 |
|
429 Parenthesis |
|
430 |
|
431 \begin{center} |
|
432 \bl{$\text{(} \sim E \sim \text{)} \Longrightarrow f((x,y), z) \Rightarrow y$} |
|
433 \end{center} |
|
434 |
|
435 \end{frame}} |
|
436 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
437 |
|
438 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
439 \mode<presentation>{ |
|
440 \begin{frame}[c] |
|
441 \frametitle{\begin{tabular}{c}Types of Parsers\end{tabular}} |
|
442 |
|
443 \begin{itemize} |
|
444 \item {\bf Sequencing}: if \bl{$p$} returns results of type \bl{$T$}, and \bl{$q$} results of type \bl{$S$}, |
|
445 then \bl{$p \sim q$} returns results of type |
|
446 |
|
447 \begin{center} |
|
448 \bl{$T \times S$} |
|
449 \end{center}\pause |
|
450 |
|
451 \item {\bf Alternative}: if \bl{$p$} returns results of type \bl{$T$} then \bl{$q$} \alert{must} also have results of type \bl{$T$}, |
|
452 and \bl{$p \;||\; q$} returns results of type |
|
453 |
|
454 \begin{center} |
|
455 \bl{$T$} |
|
456 \end{center}\pause |
|
457 |
|
458 \item {\bf Semantic Action}: if \bl{$p$} returns results of type \bl{$T$} and \bl{$f$} is a function from |
|
459 \bl{$T$} to \bl{$S$}, then |
|
460 \bl{$p \Longrightarrow f$} returns results of type |
|
461 |
|
462 \begin{center} |
|
463 \bl{$S$} |
|
464 \end{center} |
|
465 |
|
466 \end{itemize} |
|
467 |
|
468 \end{frame}} |
|
469 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
470 |
|
471 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
472 \mode<presentation>{ |
|
473 \begin{frame}[c] |
|
474 \frametitle{\begin{tabular}{c}Input Types of Parsers\end{tabular}} |
|
475 |
|
476 \begin{itemize} |
|
477 \item input: \alert{list of tokens} |
|
478 \item output: set of (output\_type, \alert{list of tokens}) |
|
479 \end{itemize}\bigskip\pause |
|
480 |
|
481 actually it can be any input type as long as it is a kind of sequence |
|
482 (for example a string) |
|
483 |
|
484 \end{frame}} |
|
485 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
486 |
|
487 |
|
488 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
489 \mode<presentation>{ |
|
490 \begin{frame}[c] |
|
491 \frametitle{\begin{tabular}{c}Scannerless Parsers\end{tabular}} |
|
492 |
|
493 \begin{itemize} |
|
494 \item input: \alert{string} |
|
495 \item output: set of (output\_type, \alert{string}) |
|
496 \end{itemize}\bigskip |
|
497 |
|
498 but lexers are better when whitespaces or comments need to be filtered out |
|
499 |
|
500 \end{frame}} |
|
501 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
502 |
|
503 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
504 \mode<presentation>{ |
|
505 \begin{frame}[t] |
|
506 \frametitle{\begin{tabular}{c}Compiled vs.~Interpreted Code\end{tabular}} |
|
507 |
|
508 \begin{center} |
|
509 \begin{tikzpicture} |
|
510 \begin{axis}[axis x line=bottom, axis y line=left, xlabel=n, ylabel=mins] |
|
511 \addplot file {compiled.data}; |
|
512 \end{axis} |
|
513 \end{tikzpicture} |
|
514 \end{center} |
|
515 |
|
516 \end{frame}} |
|
517 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
518 |
|
519 |
|
520 |
|
521 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
522 \mode<presentation>{ |
|
523 \begin{frame}[t] |
|
524 \frametitle{\begin{tabular}{c}Compiled vs.~Interpreted Code\end{tabular}} |
|
525 |
|
526 \mbox{}\\[-25mm]\mbox{} |
|
527 |
|
528 \begin{center} |
|
529 \begin{tikzpicture}[y=.4cm, x=.00000009cm] |
|
530 %axis |
|
531 \draw (0,0) -- coordinate (x axis mid) (5,0); |
|
532 \draw (0,0) -- coordinate (y axis mid) (0,5); |
|
533 %ticks |
|
534 \foreach \x in {0, 1000,...,10000} |
|
535 \draw (\x,1pt) -- (\x,-3pt) |
|
536 node[anchor=north] {\small \x{}00}; |
|
537 \foreach \y in {0,0.5,1, ..., 5.5} |
|
538 \draw (1pt,\y) -- (-3pt,\y) |
|
539 node[anchor=east] {\small\y}; |
|
540 %labels |
|
541 \node[below=0.6cm] at (x axis mid) {\bl{1}s}; |
|
542 \node[rotate=90, left=1.2cm] at (y axis mid) {secs}; |
|
543 |
|
544 %plots |
|
545 %\draw[color=blue] plot[mark=*, mark options={fill=white}] |
|
546 % file {compiled.data}; |
|
547 %\only<2->{\draw[color=red] plot[mark=triangle*, mark options={fill=white} ] |
|
548 % file {interpreted.data};} |
|
549 %legend |
|
550 %\begin{scope}[shift={(400,20)}] |
|
551 %\draw[color=blue] (0,0) -- |
|
552 % plot[mark=*, mark options={fill=white}] (0.25,0) -- (0.5,0) |
|
553 % node[right]{\small unambiguous}; |
|
554 %\only<2->{\draw[yshift=\baselineskip, color=red] (0,0) -- |
|
555 % plot[mark=triangle*, mark options={fill=white}] (0.25,0) -- (0.5,0) |
|
556 % node[right]{\small ambiguous};} |
|
557 %\end{scope} |
|
558 \end{tikzpicture} |
|
559 \end{center} |
|
560 |
|
561 \end{frame}} |
|
562 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
563 |
|
564 |
|
565 |
|
566 |
669 |
567 |
670 \end{document} |
568 \end{document} |
671 |
569 |
672 %%% Local Variables: |
570 %%% Local Variables: |
673 %%% mode: latex |
571 %%% mode: latex |