slides/slides07.tex
changeset 188 12ef150273ce
parent 187 9471a0325773
child 215 828303e8e4af
equal deleted inserted replaced
187:9471a0325773 188:12ef150273ce
    18 \usetikzlibrary{shadows}
    18 \usetikzlibrary{shadows}
    19 \usetikzlibrary{positioning}
    19 \usetikzlibrary{positioning}
    20 \usetikzlibrary{calc}
    20 \usetikzlibrary{calc}
    21 \usetikzlibrary{plotmarks}
    21 \usetikzlibrary{plotmarks}
    22 \usepackage{graphicx} 
    22 \usepackage{graphicx} 
       
    23 \usepackage{pgfplots}
    23 
    24 
    24 \definecolor{javared}{rgb}{0.6,0,0} % for strings
    25 \definecolor{javared}{rgb}{0.6,0,0} % for strings
    25 \definecolor{javagreen}{rgb}{0.25,0.5,0.35} % comments
    26 \definecolor{javagreen}{rgb}{0.25,0.5,0.35} % comments
    26 \definecolor{javapurple}{rgb}{0.5,0,0.35} % keywords
    27 \definecolor{javapurple}{rgb}{0.5,0,0.35} % keywords
    27 \definecolor{javadocblue}{rgb}{0.25,0.35,0.75} % javadoc
    28 \definecolor{javadocblue}{rgb}{0.25,0.35,0.75} % javadoc
    69 	numbersep=10pt,
    70 	numbersep=10pt,
    70 	tabsize=2,
    71 	tabsize=2,
    71 	showspaces=false,
    72 	showspaces=false,
    72 	showstringspaces=false}
    73 	showstringspaces=false}
    73 
    74 
       
    75 \lstdefinelanguage{While}{
       
    76   morekeywords={while, if, then. else, read, write},
       
    77   otherkeywords={=>,<-,<\%,<:,>:,\#,@},
       
    78   sensitive=true,
       
    79   morecomment=[l]{//},
       
    80   morecomment=[n]{/*}{*/},
       
    81   morestring=[b]",
       
    82   morestring=[b]',
       
    83   morestring=[b]"""
       
    84 }
       
    85 
       
    86 
    74 \setmonofont{Consolas}
    87 \setmonofont{Consolas}
       
    88 
       
    89 \begin{filecontents}{interpreted.data}
       
    90 %1 0.00503
       
    91 200 1.005863
       
    92 400 7.8296765
       
    93 500 15.43106
       
    94 600 27.2321885
       
    95 800 65.249271
       
    96 1000 135.4493445
       
    97 1200 232.134097
       
    98 1400 382.527227
       
    99 \end{filecontents}
    75 
   100 
    76 % beamer stuff 
   101 % beamer stuff 
    77 \renewcommand{\slidecaption}{AFL 07, King's College London, 13.~November 2013}
   102 \renewcommand{\slidecaption}{AFL 07, King's College London, 13.~November 2013}
    78 \newcommand{\bl}[1]{\textcolor{blue}{#1}}       
   103 \newcommand{\bl}[1]{\textcolor{blue}{#1}}       
    79 \newcommand{\dn}{\stackrel{\mbox{\scriptsize def}}{=}}% for definitions
   104 \newcommand{\dn}{\stackrel{\mbox{\scriptsize def}}{=}}% for definitions
   422 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
   447 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
   423 
   448 
   424 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   449 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   425 \mode<presentation>{
   450 \mode<presentation>{
   426 \begin{frame}[c]
   451 \begin{frame}[c]
   427 \frametitle{\begin{tabular}{c}Parse Trees\end{tabular}}
   452 
   428 \begin{center}
   453 \begin{center}
   429 \begin{tabular}{@{}lcl@{}}
   454 \bl{\begin{tabular}{@{}lcl@{}}
   430 \textit{Stmt} & $\rightarrow$ &  $\texttt{skip}$\\
   455 \textit{Stmt} & $\rightarrow$ &  $\texttt{skip}$\\
   431               & $|$ & \textit{Id}\;\texttt{:=}\;\textit{AExp}\\
   456               & $|$ & \textit{Id}\;\texttt{:=}\;\textit{AExp}\\
   432               & $|$ & \texttt{if}\; \textit{BExp} \;\texttt{then}\; \textit{Block} \;\texttt{else}\; \textit{Block}\\
   457               & $|$ & \texttt{if}\; \textit{BExp} \;\texttt{then}\; \textit{Block} \;\texttt{else}\; \textit{Block}\\
   433               & $|$ & \texttt{while}\; \textit{BExp} \;\texttt{do}\; \textit{Block}\\
   458               & $|$ & \texttt{while}\; \textit{BExp} \;\texttt{do}\; \textit{Block}\\
   434               & $|$ & \texttt{read}\;\textit{Id}\\
   459               & $|$ & \texttt{read}\;\textit{Id}\\
   438               & $|$ & \textit{Stmt}\medskip\\
   463               & $|$ & \textit{Stmt}\medskip\\
   439 \textit{Block} & $\rightarrow$ &  \texttt{\{}\,\textit{Stmts}\,\texttt{\}}\\
   464 \textit{Block} & $\rightarrow$ &  \texttt{\{}\,\textit{Stmts}\,\texttt{\}}\\
   440                 & $|$ & \textit{Stmt}\medskip\\
   465                 & $|$ & \textit{Stmt}\medskip\\
   441 \textit{AExp} & $\rightarrow$ & \ldots\\
   466 \textit{AExp} & $\rightarrow$ & \ldots\\
   442 \textit{BExp} & $\rightarrow$ & \ldots\\
   467 \textit{BExp} & $\rightarrow$ & \ldots\\
   443 \end{tabular}
   468 \end{tabular}}
   444 \end{center}
   469 \end{center}
   445 \end{frame}}
   470 \end{frame}}
   446 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
   471 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
   447 
   472 
   448 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   473 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   449 \mode<presentation>{
   474 \mode<presentation>{
   450 \begin{frame}[c]
   475 \begin{frame}[c]
   451 \frametitle{\begin{tabular}{c}Parse Trees\end{tabular}}
   476 
   452 
   477 \mbox{\lstinputlisting[language=while]{../progs/fib.while}}
   453 \begin{center}
   478 
   454 \bl{\begin{tabular}{lcl}
   479 \end{frame}}
   455 $E$ & $\rightarrow$ &  $F \;|\; F \cdot * \cdot F$\\
   480 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
   456 $F$ & $\rightarrow$ & $T \;|\; T \cdot + \cdot T \;|\; T \cdot - \cdot T$\\
   481 
   457 $T$ & $\rightarrow$ & $num\_token \;|\; ( \cdot E \cdot )$\\
   482 
   458 \end{tabular}}
   483 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   459 \end{center}
   484 \mode<presentation>{
   460 
   485 \begin{frame}[c]
   461 \begin{center}
   486 \frametitle{\begin{tabular}{c}An Interpreter\end{tabular}}
   462 \begin{tikzpicture}[level distance=8mm, blue]
   487 
   463   \node {$E$}
   488 \begin{center}
   464     child {node {$F$} 
   489 \bl{\begin{tabular}{l}
   465      child {node {$T$} 
   490 $\{$\\
   466                  child {node {(\,$E$\,)}
   491 \;\;$x := 5 \text{;}$\\
   467                             child {node{$F$ *{} $F$}
   492 \;\;$y := x * 3\text{;}$\\
   468                                   child {node {$T$} child {node {2}}}
   493 \;\;$y := x * 4\text{;}$\\
   469                                   child {node {$T$} child {node {3}}} 
   494 \;\;$x := u * 3$\\
   470                                }
   495 $\}$
   471                           }
   496 \end{tabular}}
   472               }
   497 \end{center}
   473      child {node {+}}
   498 
   474      child {node {$T$}
   499 \begin{itemize}
   475        child {node {(\,$E$\,)} 
   500 \item the interpreter has to record the value of \bl{$x$} before assigning a value to \bl{$y$}\pause
   476        child {node {$F$}
   501 \item \bl{\text{eval}(stmt, env)}
   477        child {node {$T$ +{} $T$}
   502 \end{itemize}
   478                     child {node {3}}
   503 
   479                     child {node {4}} 
   504 
   480                  }
   505 \end{frame}}
   481                  }}
   506 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   482     }};
   507 
       
   508 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   509 \mode<presentation>{
       
   510 \begin{frame}[c]
       
   511 \frametitle{\begin{tabular}{c}Interpreter\end{tabular}}
       
   512 
       
   513 \begin{center}
       
   514 \bl{\begin{tabular}{@{}lcl@{}}
       
   515 $\text{eval}(n, E)$ & $\dn$ & $n$\\
       
   516 $\text{eval}(x, E)$ & $\dn$ & $E(x)$ \;\;\;\textcolor{black}{lookup \bl{$x$} in \bl{$E$}}\\
       
   517 $\text{eval}(a_1 + a_2, E)$ & $\dn$ & $\text{eval}(a_1, E) + \text{eval}(a_2, E)$\\
       
   518 $\text{eval}(a_1 - a_2, E)$ & $\dn$ & $\text{eval}(a_1, E) - \text{eval}(a_2, E)$\\
       
   519 $\text{eval}(a_1 * a_2, E)$ & $\dn$ & $\text{eval}(a_1, E) * \text{eval}(a_2, E)$\bigskip\\
       
   520 $\text{eval}(a_1 = a_2, E)$ & $\dn$ & $\text{eval}(a_1, E) = \text{eval}(a_2, E)$\\
       
   521 $\text{eval}(a_1\,!\!= a_2, E)$ & $\dn$ & $\neg(\text{eval}(a_1, E) = \text{eval}(a_2, E))$\\
       
   522 $\text{eval}(a_1 < a_2, E)$ & $\dn$ & $\text{eval}(a_1, E) < \text{eval}(a_2, E)$\
       
   523 \end{tabular}}
       
   524 \end{center}
       
   525 
       
   526 \end{frame}}
       
   527 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
       
   528 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   529 \mode<presentation>{
       
   530 \begin{frame}[c]
       
   531 \frametitle{\begin{tabular}{c}Interpreter (2)\end{tabular}}
       
   532 
       
   533 \begin{center}
       
   534 \bl{\begin{tabular}{@{}lcl@{}}
       
   535 $\text{eval}(\text{skip}, E)$ & $\dn$ & $E$\\
       
   536 $\text{eval}(x:=a, E)$ & $\dn$ & \bl{$E(x \mapsto \text{eval}(a, E))$}\\
       
   537 \multicolumn{3}{@{}l@{}}{$\text{eval}(\text{if}\;b\;\text{then}\;cs_1\;\text{else}\;cs_2 , E) \dn$}\\
       
   538 \multicolumn{3}{@{}l@{}}{\hspace{2cm}$\text{if}\;\text{eval}(b,E)\;\text{then}\;
       
   539 \text{eval}(cs_1,E)$}\\
       
   540 \multicolumn{3}{@{}l@{}}{\hspace{2cm}$\phantom{\text{if}\;\text{eval}(b,E)\;}\text{else}\;\text{eval}(cs_2,E)$}\\
       
   541 \multicolumn{3}{@{}l@{}}{$\text{eval}(\text{while}\;b\;\text{do}\;cs, E) \dn$}\\
       
   542 \multicolumn{3}{@{}l@{}}{\hspace{2cm}$\text{if}\;\text{eval}(b,E)$}\\
       
   543 \multicolumn{3}{@{}l@{}}{\hspace{2cm}$\text{then}\;
       
   544 \text{eval}(\text{while}\;b\;\text{do}\;cs, \text{eval}(cs,E))$}\\
       
   545 \multicolumn{3}{@{}l@{}}{\hspace{2cm}$\text{else}\; E$}\\
       
   546 $\text{eval}(\text{write}\; x, E)$ & $\dn$ & $\{\;\text{println}(E(x))\; ;\;E\;\}$\\
       
   547 \end{tabular}}
       
   548 \end{center}
       
   549 
       
   550 \end{frame}}
       
   551 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
       
   552 
       
   553 
       
   554 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   555 \mode<presentation>{
       
   556 \begin{frame}[c]
       
   557 \frametitle{\begin{tabular}{c}Test Program\end{tabular}}
       
   558 
       
   559 \mbox{}\\[-18mm]\mbox{}
       
   560 
       
   561 {\lstset{language=While}%%\fontsize{10}{12}\selectfont
       
   562 \texttt{\lstinputlisting{../progs/loops.while}}}
       
   563 
       
   564 \end{frame}}
       
   565 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
       
   566 
       
   567 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   568 \mode<presentation>{
       
   569 \begin{frame}[t]
       
   570 \frametitle{\begin{tabular}{c}Interpreted Code\end{tabular}}
       
   571 
       
   572 \begin{center}
       
   573 \begin{tikzpicture}
       
   574 \begin{axis}[axis x line=bottom, axis y line=left, xlabel=n, ylabel=secs, legend style=small]
       
   575 \addplot+[smooth] file {interpreted.data};
       
   576 \end{axis}
   483 \end{tikzpicture}
   577 \end{tikzpicture}
   484 \end{center}
   578 \end{center}
   485 
   579 
   486 \begin{textblock}{5}(1, 6.5)
       
   487 \bl{\texttt{(2*3)+(3+4)}}
       
   488 \end{textblock}
       
   489 
       
   490 \end{frame}}
       
   491 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
       
   492 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   493 \mode<presentation>{
       
   494 \begin{frame}[c]
       
   495 \frametitle{\begin{tabular}{c}Ambiguous Grammars\end{tabular}}
       
   496 
       
   497 A CFG is \alert{ambiguous} if there is a string that has at least parse trees.
       
   498 
       
   499 
       
   500 \begin{center}
       
   501 \bl{\begin{tabular}{lcl}
       
   502 $E$ & $\rightarrow$ &  $num\_token$ \\
       
   503 $E$ & $\rightarrow$ &  $E \cdot + \cdot E$ \\
       
   504 $E$ & $\rightarrow$ &  $E \cdot - \cdot E$ \\
       
   505 $E$ & $\rightarrow$ &  $E \cdot * \cdot E$ \\
       
   506 $E$ & $\rightarrow$ &  $( \cdot E \cdot )$ 
       
   507 \end{tabular}}
       
   508 \end{center}
       
   509 
       
   510 \bl{\texttt{1 + 2 * 3 + 4}}
       
   511 
       
   512 \end{frame}}
       
   513 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
       
   514 
       
   515 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   516 \mode<presentation>{
       
   517 \begin{frame}[c]
       
   518 \frametitle{\begin{tabular}{c}Dangling Else\end{tabular}}
       
   519 
       
   520 Another ambiguous grammar:\bigskip
       
   521 
       
   522 \begin{center}
       
   523 \bl{\begin{tabular}{lcl}
       
   524 $E$ & $\rightarrow$ &  if $E$ then $E$\\
       
   525  & $|$ &  if $E$ then $E$ else $E$ \\
       
   526  & $|$ &  id 
       
   527 \end{tabular}}
       
   528 \end{center}\bigskip
       
   529 
       
   530 \bl{\texttt{if a then if x then y else c}}
       
   531 
       
   532 \end{frame}}
   580 \end{frame}}
   533 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
   581 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
   534 
   582 
   535 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   583 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   536 \mode<presentation>{
   584 \mode<presentation>{
   537 \begin{frame}[c]
   585 \begin{frame}[c]
   538 \frametitle{\begin{tabular}{c}A CFG Derivation\end{tabular}}
   586 \frametitle{\begin{tabular}{c}Java Virtual Machine\end{tabular}}
   539 
   587 
   540 \begin{enumerate}
   588 \begin{itemize}
   541 \item Begin with a string with only the start symbol \bl{$S$}\bigskip
   589 \item introduced in 1995
   542 \item Replace any non-terminal \bl{$X$} in the string by the
   590 \item is a stack-based VM (like Postscript, CLR of .Net)
   543 right-hand side of some production \bl{$X \rightarrow \text{rhs}$}\bigskip
   591 \item contains a JIT compiler
   544 \item Repeat 2 until there are no non-terminals
   592 \item many languages take advantage of JVM's infrastructure (JRE)
   545 \end{enumerate}
   593 \item is garbage collected $\Rightarrow$ no buffer overflows
   546 
   594 \item some languages compile to the JVM: Scala, Clojure\ldots
   547 \begin{center}
   595 \end{itemize}
   548 \bl{$S \rightarrow \ldots \rightarrow \ldots  \rightarrow \ldots  \rightarrow \ldots $}
       
   549 \end{center}
       
   550 
   596 
   551 \end{frame}}
   597 \end{frame}}
   552 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
   598 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
   553 
   599 
   554 
   600