slides/slides06.tex
changeset 365 9b71dead1219
parent 295 19f23c4c2167
child 366 5a83336a9690
equal deleted inserted replaced
364:50ce3667c190 365:9b71dead1219
     1 \documentclass[dvipsnames,14pt,t]{beamer}
     1 \documentclass[dvipsnames,14pt,t]{beamer}
     2 \usepackage{../slides}
     2 \usepackage{../slides}
     3 \usepackage{../graphics}
     3 \usepackage{../graphics}
     4 \usepackage{../langs}
     4 \usepackage{../langs}
     5 \usepackage{../data}
     5 \usepackage{../data}
     6 \usepackage{../grammar}
     6 
     7 
       
     8 \hfuzz=220pt 
       
     9 
       
    10 \pgfplotsset{compat=1.11}
       
    11 
       
    12 \newcommand{\bl}[1]{\textcolor{blue}{#1}}  
       
    13 
     7 
    14 % beamer stuff 
     8 % beamer stuff 
    15 \renewcommand{\slidecaption}{AFL 06, King's College London}
     9 \renewcommand{\slidecaption}{AFL 06, King's College London}
       
    10 \newcommand{\bl}[1]{\textcolor{blue}{#1}}       
       
    11 %\newcommand{\dn}{\stackrel{\mbox{\scriptsize def}}{=}}% for definitions
    16 
    12 
    17 \begin{document}
    13 \begin{document}
    18 
       
    19 
       
    20 
    14 
    21 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    15 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    22 \begin{frame}[t]
    16 \begin{frame}[t]
    23 \frametitle{%
    17 \frametitle{%
    24   \begin{tabular}{@ {}c@ {}}
    18   \begin{tabular}{@ {}c@ {}}
    39 \end{frame}
    33 \end{frame}
    40 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
    34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
    41 
    35 
    42 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    36 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    43 \begin{frame}[c]
    37 \begin{frame}[c]
    44 \frametitle{Regular Languages}
    38 
    45 
    39 \mbox{}\\[-18mm]\mbox{}
    46 While regular expressions are very useful for lexing, there is
    40 
    47 no regular expression that can recognise the language
    41 {\lstset{language=Scala}\fontsize{10}{12}\selectfont
    48 \bl{$a^nb^n$}.\bigskip
    42 \texttt{\lstinputlisting[xleftmargin=0mm]{../progs/pow.scala}}}
    49 
    43 
    50 \begin{center}
    44 \end{frame}
    51 \bl{$(((()()))())$} \;\;vs.\;\; \bl{$(((()()))()))$}
    45 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
    52 \end{center}\bigskip\bigskip
    46 
    53 
    47 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
    48 \begin{frame}[c]
    54 \small
    49 \small
    55 \noindent So we cannot find out with regular expressions
    50 \mbox{}\\[5mm]
    56 whether parentheses are matched or unmatched.
    51 %\begin{textblock}{10}(3,5)
       
    52 \begin{tikzpicture}[scale=1.5,
       
    53                     node distance=1cm,
       
    54                     every node/.style={minimum size=7mm}]
       
    55 \node (r1)  {\bl{$r_1$}};
       
    56 \node (r2) [right=of r1] {\bl{$r_2$}};
       
    57 \draw[->,line width=1mm]  (r1) -- (r2) node[above,midway] {\bl{$der\,a$}};
       
    58 \node (r3) [right=of r2] {\bl{$r_3$}};
       
    59 \draw[->,line width=1mm]  (r2) -- (r3) node[above,midway] {\bl{$der\,b$}};
       
    60 \node (r4) [right=of r3] {\bl{$r_4$}};
       
    61 \draw[->,line width=1mm]  (r3) -- (r4) node[above,midway] {\bl{$der\,c$}};
       
    62 \draw (r4) node[anchor=west] {\;\raisebox{3mm}{\bl{$nullable$}}};
       
    63 \node (v4) [below=of r4] {\bl{$v_4$}};
       
    64 \draw[->,line width=1mm]  (r4) -- (v4);
       
    65 \node (v3) [left=of v4] {\bl{$v_3$}};
       
    66 \draw[->,line width=1mm]  (v4) -- (v3) node[below,midway] {\bl{$inj\,c$}};
       
    67 \node (v2) [left=of v3] {\bl{$v_2$}};
       
    68 \draw[->,line width=1mm]  (v3) -- (v2) node[below,midway] {\bl{$inj\,b$}};
       
    69 \node (v1) [left=of v2] {\bl{$v_1$}};
       
    70 \draw[->,line width=1mm]  (v2) -- (v1) node[below,midway] {\bl{$inj\,a$}};
       
    71 \draw[->,line width=0.5mm]  (r3) -- (v3);
       
    72 \draw[->,line width=0.5mm]  (r2) -- (v2);
       
    73 \draw[->,line width=0.5mm]  (r1) -- (v1);
       
    74 \draw (r4) node[anchor=north west] {\;\raisebox{-8mm}{\bl{$mkeps$}}};
       
    75 \end{tikzpicture}
       
    76 %\end{textblock}
       
    77 
       
    78 \begin{center}
       
    79 \begin{tabular}{l@{\hspace{1mm}}c@{\hspace{1mm}}l}
       
    80   \\[-10mm]
       
    81   \bl{$inj\,(c)\,c\,Empty$} & \bl{$\dn$}  & \bl{$Char\,c$}\\
       
    82   \bl{$inj\,(r_1 + r_2)\,c\,Left(v)$} & \bl{$\dn$}  & \bl{$Left(inj\,r_1\,c\,v)$}\\
       
    83   \bl{$inj\,(r_1 + r_2)\,c\,Right(v)$} & \bl{$\dn$}  & \bl{$Right(inj\,r_2\,c\,v)$}\\
       
    84   \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)$}\\
       
    85   \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)$}\\
       
    86   \bl{$inj\,(r_1 \cdot r_2)\,c\,Right(v)$} & \bl{$\dn$}  & \bl{$Seq(mkeps(r_1),inj\,r_2\,c\,v)$}\\
       
    87   \bl{$inj\,(r^*)\,c\,Seq(v,vs)$} & \bl{$\dn$}  & \bl{$inj\,r\,c\,v\,::\,vs$}\\
       
    88 \end{tabular}
       
    89 \end{center}
    57 
    90 
    58 \end{frame}
    91 \end{frame}
    59 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
    92 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
    60 
    93 
    61 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    94 
    62 \begin{frame}[c]
    95 
    63 \frametitle{Hierarchy of Languages}
    96 
       
    97 \newcommand{\qq}{\mbox{\texttt{"}}}
       
    98 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
    99 \begin{frame}[c]
       
   100 \frametitle{CFGs}
       
   101 
       
   102 A \alert{context-free} grammar (CFG) \bl{$G$} consists of:
       
   103 
       
   104 \begin{itemize}
       
   105 \item a finite set of nonterminal symbols (upper case)
       
   106 \item a finite terminal symbols or tokens (lower case)
       
   107 \item a start symbol (which must be a nonterminal)
       
   108 \item a set of rules
       
   109 \begin{center}
       
   110 \bl{$A \rightarrow \text{rhs}_1 | \text{rhs}_2 | \ldots$}
       
   111 \end{center}
       
   112 
       
   113 where \bl{rhs} are sequences involving terminals and nonterminals (can also be empty).\medskip\pause
       
   114 
       
   115 \end{itemize}
       
   116 
       
   117 \end{frame}
       
   118 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
       
   119 
       
   120 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   121 \mode<presentation>{
       
   122 \begin{frame}[c]
       
   123 \frametitle{\begin{tabular}{c}Hierarchy of Languages\end{tabular}}
       
   124 
       
   125 Recall that languages are sets of strings.
    64 
   126 
    65 \begin{center}
   127 \begin{center}
    66 \begin{tikzpicture}
   128 \begin{tikzpicture}
    67 [rect/.style={draw=black!50, 
   129 [rect/.style={draw=black!50, top color=white,bottom color=black!20, rectangle, very thick, rounded corners}]
    68               top color=white,
       
    69               bottom color=black!20, 
       
    70               rectangle, 
       
    71               very thick, 
       
    72               rounded corners}]
       
    73 
   130 
    74 \draw (0,0) node [rect, text depth=39mm, text width=68mm] {all languages};
   131 \draw (0,0) node [rect, text depth=39mm, text width=68mm] {all languages};
    75 \draw (0,-0.4) node [rect, text depth=28.5mm, text width=64mm] {decidable languages};
   132 \draw (0,-0.4) node [rect, text depth=28.5mm, text width=64mm] {decidable languages};
    76 \draw (0,-0.85) node [rect, text depth=17mm] {context sensitive languages};
   133 \draw (0,-0.85) node [rect, text depth=17mm] {context sensitive languages};
    77 \draw (0,-1.14) node [rect, text depth=9mm, text width=50mm] {context-free languages};
   134 \draw (0,-1.14) node [rect, text depth=9mm, text width=50mm] {context-free languages};
    78 \draw (0,-1.4) node [rect] {regular languages};
   135 \draw (0,-1.4) node [rect] {regular languages};
    79 \end{tikzpicture}
   136 \end{tikzpicture}
    80 
   137 
    81 \end{center}
   138 \end{center}
    82 
   139 
    83 \end{frame}
   140 \end{frame}}
    84 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
   141 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
    85 
   142 
    86 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   143 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    87 \begin{frame}[c]
   144 \mode<presentation>{
    88 \frametitle{Grammars}
   145 \begin{frame}[c]
    89 
   146 \frametitle{\begin{tabular}{c}Arithmetic Expressions\end{tabular}}
    90 A (context-free) grammar \bl{$G$} consists of
   147 
       
   148 A grammar for arithmetic expressions and numbers:
       
   149 
       
   150 \begin{center}
       
   151 \bl{\begin{tabular}{lcl}
       
   152 $E$ & $\rightarrow$ &  $E \cdot + \cdot E \;|\;E \cdot * \cdot E \;|\;( \cdot E \cdot ) \;|\;N$ \\
       
   153 $N$ & $\rightarrow$ & $N \cdot N \;|\; 0 \;|\; 1 \;|\: \ldots \;|\; 9$ 
       
   154 \end{tabular}}
       
   155 \end{center}
       
   156 
       
   157 Unfortunately it is left-recursive (and ambiguous).\medskip\\
       
   158 A problem for \alert{recursive descent parsers} (e.g.~parser combinators).
       
   159 \bigskip\pause
       
   160 
       
   161 \end{frame}}
       
   162 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
       
   163 
       
   164 
       
   165 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   166 \mode<presentation>{
       
   167 \begin{frame}[t]
       
   168 \frametitle{\begin{tabular}{c}Numbers\end{tabular}}
       
   169 
       
   170 
       
   171 
       
   172 \begin{center}
       
   173 \bl{\begin{tabular}{lcl}
       
   174 $N$ & $\rightarrow$ &  $N \cdot N \;|\; 0 \;|\; 1 \;|\; \ldots \;|\; 9$\\
       
   175 \end{tabular}}
       
   176 \end{center}
       
   177 
       
   178 A non-left-recursive, non-ambiguous grammar for numbers:
       
   179 
       
   180 \begin{center}
       
   181 \bl{\begin{tabular}{lcl}
       
   182 $N$ & $\rightarrow$ &  $0 \cdot N \;|\;1 \cdot N \;|\;\ldots\;|\; 0 \;|\; 1 \;|\; \ldots \;|\; 9$\\
       
   183 \end{tabular}}
       
   184 \end{center}
       
   185 
       
   186 
       
   187 \end{frame}}
       
   188 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  
       
   189 
       
   190 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   191 \mode<presentation>{
       
   192 \begin{frame}[c]
       
   193 \frametitle{\begin{tabular}{c}Operator Precedences\end{tabular}}
       
   194 
       
   195 
       
   196 To disambiguate
       
   197 
       
   198 \begin{center}
       
   199 \bl{\begin{tabular}{lcl}
       
   200 $E$ & $\rightarrow$ &  $E \cdot + \cdot E \;|\;E \cdot * \cdot E \;|\;( \cdot E \cdot ) \;|\;N$ \\
       
   201 \end{tabular}}
       
   202 \end{center}
       
   203 
       
   204 Decide on how many precedence levels, say\medskip\\
       
   205 \hspace{5mm}highest for \bl{$()$}, medium for \bl{*}, lowest for \bl{+}
       
   206 
       
   207 \begin{center}
       
   208 \bl{\begin{tabular}{lcl}
       
   209 $E_{low}$ & $\rightarrow$ & $E_{med} \cdot + \cdot E_{low} \;|\; E_{med}$ \\
       
   210 $E_{med}$ & $\rightarrow$ & $E_{hi} \cdot * \cdot E_{med} \;|\; E_{hi}$\\
       
   211 $E_{hi}$ & $\rightarrow$ &  $( \cdot E_{low} \cdot ) \;|\;N$ \\
       
   212 \end{tabular}}
       
   213 \end{center}\pause
       
   214 
       
   215 \small What happens with \bl{$1 + 3  + 4$}?
       
   216 \end{frame}}
       
   217 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
       
   218 
       
   219 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   220 \mode<presentation>{
       
   221 \begin{frame}[c]
       
   222 \frametitle{\begin{tabular}{c}Removing Left-Recursion\end{tabular}}
       
   223 
       
   224 The rule for numbers is directly left-recursive:
       
   225 
       
   226 \begin{center}
       
   227 \bl{\begin{tabular}{lcl}
       
   228 $N$ & $\rightarrow$ & $N \cdot N \;|\; 0 \;|\; 1\;\;\;\;(\ldots)$ 
       
   229 \end{tabular}}
       
   230 \end{center}
       
   231 
       
   232 Translate
       
   233 
       
   234 \begin{center}
       
   235 \begin{tabular}{ccc}
       
   236 \bl{\begin{tabular}{lcl}
       
   237 $N$ & $\rightarrow$ & $N \cdot \alpha$\\
       
   238  &  $\;|\;$ & $\beta$\\
       
   239  \\ 
       
   240 \end{tabular}} 
       
   241 & {\Large$\Rightarrow$} &
       
   242 \bl{\begin{tabular}{lcl}
       
   243 $N$ & $\rightarrow$ & $\beta \cdot N'$\\
       
   244 $N'$ & $\rightarrow$ & $\alpha \cdot N'$\\
       
   245  &  $\;|\;$ & $\epsilon$ 
       
   246 \end{tabular}}
       
   247 \end{tabular}
       
   248 \end{center}\pause
       
   249 
       
   250 Which means
       
   251 
       
   252 \begin{center}
       
   253 \bl{\begin{tabular}{lcl}
       
   254 $N$ & $\rightarrow$ & $0 \cdot N' \;|\; 1 \cdot N'$\\
       
   255 $N'$ & $\rightarrow$ & $N \cdot N' \;|\; \epsilon$\\
       
   256 \end{tabular}}
       
   257 \end{center}
       
   258 
       
   259 
       
   260 \end{frame}}
       
   261 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
       
   262 
       
   263 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   264 \mode<presentation>{
       
   265 \begin{frame}[c]
       
   266 \frametitle{\begin{tabular}{c}Chomsky Normal Form\end{tabular}}
       
   267 
       
   268 All rules must be of the form
       
   269 
       
   270 \begin{center}
       
   271 \bl{$A \rightarrow a$}
       
   272 \end{center}
       
   273 
       
   274 or
       
   275 
       
   276 \begin{center}
       
   277 \bl{$A \rightarrow B\cdot C$}
       
   278 \end{center}
       
   279 
       
   280 No rule can contain \bl{$\epsilon$}.
       
   281 
       
   282 \end{frame}}
       
   283 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
       
   284 
       
   285 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   286 \mode<presentation>{
       
   287 \begin{frame}[c]
       
   288 \frametitle{\begin{tabular}{c}$\epsilon$-Removal\end{tabular}}
       
   289 
       
   290 \begin{enumerate}
       
   291 \item If \bl{$A\rightarrow \alpha \cdot B \cdot \beta$} and \bl{$B \rightarrow \epsilon$} are in the grammar,
       
   292 then add \bl{$A\rightarrow \alpha \cdot \beta$} (iterate if necessary).
       
   293 \item Throw out all \bl{$B \rightarrow \epsilon$}.
       
   294 \end{enumerate}
       
   295 
       
   296 \small
       
   297 \begin{center}
       
   298 \begin{tabular}{ccc}
       
   299 \bl{\begin{tabular}{l@{\hspace{1mm}}c@{\hspace{1mm}}l}
       
   300 $N$ & $\rightarrow$ & $0 \cdot N' \;|\; 1\cdot N'$\\
       
   301 $N'$ & $\rightarrow$ & $N \cdot N'\;|\;\epsilon$\\
       
   302 \\ 
       
   303 \\
       
   304 \\
       
   305 \\
       
   306 \\
       
   307 \end{tabular}} &
       
   308 \bl{\begin{tabular}{l@{\hspace{1mm}}c@{\hspace{1mm}}l}
       
   309 \\
       
   310 $N$ & $\rightarrow$ & $0 \cdot N' \;|\; 1\cdot N'\;|\;0\;|\;1$\\
       
   311 $N'$ & $\rightarrow$ & $N \cdot N'\;|\;N\;|\;\epsilon$\\
       
   312 \\
       
   313 $N$ & $\rightarrow$ & $0 \cdot N' \;|\; 1\cdot N'\;|\;0\;|\;1$\\
       
   314 $N'$ & $\rightarrow$ & $N \cdot N'\;|\;N$\\
       
   315 \end{tabular}}
       
   316 \end{tabular}
       
   317 \end{center}
       
   318 
       
   319 \pause\normalsize
       
   320 \begin{center}
       
   321 \bl{\begin{tabular}{l@{\hspace{1mm}}c@{\hspace{1mm}}l}
       
   322 $N$ & $\rightarrow$ & $0 \cdot N\;|\; 1\cdot N\;|\;0\;|\;1$\\
       
   323 \end{tabular}}
       
   324 
       
   325 \end{center}
       
   326 \end{frame}}
       
   327 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
       
   328 
       
   329 
       
   330 
       
   331 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   332 \mode<presentation>{
       
   333 \begin{frame}[c]
       
   334 \frametitle{\begin{tabular}{c}CYK Algorithm\end{tabular}}
       
   335 
       
   336 If grammar is in Chomsky normalform \ldots
       
   337 
       
   338 \begin{center}
       
   339 \bl{\begin{tabular}{@ {}lcl@ {}}
       
   340 $S$ & $\rightarrow$ &  $N\cdot P$ \\
       
   341 $P$ & $\rightarrow$ &  $V\cdot N$ \\
       
   342 $N$ & $\rightarrow$ &  $N\cdot N$ \\
       
   343 $N$ & $\rightarrow$ &  $\texttt{students} \;|\; \texttt{Jeff} \;|\; \texttt{geometry} \;|\; \texttt{trains} $ \\
       
   344 $V$ & $\rightarrow$ &  $\texttt{trains}$ 
       
   345 \end{tabular}}
       
   346 \end{center}
       
   347 
       
   348 \bl{\texttt{Jeff trains geometry students}}
       
   349 
       
   350 \end{frame}}
       
   351 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
       
   352 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   353 \mode<presentation>{
       
   354 \begin{frame}[c]
       
   355 \frametitle{\begin{tabular}{c}CYK Algorithm\end{tabular}}
       
   356 
    91 
   357 
    92 \begin{itemize}
   358 \begin{itemize}
    93 \item a finite set of nonterminal symbols (upper case)
   359 \item fastest possible algorithm for recognition problem
    94 \item a finite terminal symbols or tokens (lower case)
   360 \item runtime is \bl{$O(n^3)$}\bigskip
    95 \item a start symbol (which must be a nonterminal)
   361 \item grammars need to be transferred into CNF
    96 \item a set of rules
       
    97 \begin{center}
       
    98 \bl{$A \rightarrow \text{rhs}$}
       
    99 \end{center}
       
   100 
       
   101 where \bl{rhs} are sequences involving terminals and nonterminals,
       
   102 including the empty sequence \bl{$\epsilon$}.\medskip\pause
       
   103 
       
   104 We also allow rules
       
   105 \begin{center}
       
   106 \bl{$A \rightarrow \text{rhs}_1 | \text{rhs}_2 | \ldots$}
       
   107 \end{center}
       
   108 \end{itemize}
   362 \end{itemize}
   109 
   363 
   110 \end{frame}
       
   111 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
       
   112 
       
   113 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   114 \begin{frame}[c]
       
   115 \frametitle{Palindromes}
       
   116 
       
   117 A grammar for palindromes over the alphabet~\bl{$\{a,b\}$}:
       
   118 
       
   119 \begin{center}
       
   120 \bl{\begin{tabular}{lcl}
       
   121 $S$ & $\rightarrow$ &  $\epsilon$ \\
       
   122 $S$ & $\rightarrow$ &  $a\cdot S\cdot a$ \\
       
   123 $S$ & $\rightarrow$ &  $b\cdot S\cdot b$ \\
       
   124 \end{tabular}}
       
   125 \end{center}\pause
       
   126 
       
   127 or
       
   128 
       
   129 \begin{center}
       
   130 \bl{\begin{tabular}{lcl}
       
   131 $S$ & $\rightarrow$ &  $\epsilon \;|\; a\cdot S\cdot a \;|\;b\cdot S\cdot b$ \\
       
   132 \end{tabular}}
       
   133 \end{center}\pause\bigskip
       
   134 
       
   135 \small
       
   136 Can you find the grammar rules for matched parentheses?
       
   137 
       
   138 \end{frame}
       
   139 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
       
   140 
       
   141 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   142 \begin{frame}[c]
       
   143 \frametitle{Arithmetic Expressions}
       
   144 
       
   145 \begin{center}
       
   146 \bl{\begin{tabular}{lcl}
       
   147 $E$ & $\rightarrow$ &  $num\_token$ \\
       
   148 $E$ & $\rightarrow$ &  $E \cdot + \cdot E$ \\
       
   149 $E$ & $\rightarrow$ &  $E \cdot - \cdot E$ \\
       
   150 $E$ & $\rightarrow$ &  $E \cdot * \cdot E$ \\
       
   151 $E$ & $\rightarrow$ &  $( \cdot E \cdot )$ 
       
   152 \end{tabular}}
       
   153 \end{center}\pause
       
   154 
       
   155 \bl{\texttt{1 + 2 * 3 + 4}}
       
   156 
       
   157 \end{frame}
       
   158 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
       
   159 
       
   160 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   161 \begin{frame}[c]
       
   162 \frametitle{A CFG Derivation}
       
   163 
       
   164 \begin{enumerate}
       
   165 \item Begin with a string containing only the start symbol, say \bl{$S$}\bigskip
       
   166 \item Replace any nonterminal \bl{$X$} in the string by the
       
   167 right-hand side of some production \bl{$X \rightarrow \text{rhs}$}\bigskip
       
   168 \item Repeat 2 until there are no nonterminals
       
   169 \end{enumerate}
       
   170 
       
   171 \begin{center}
       
   172 \bl{$S \rightarrow \ldots \rightarrow \ldots  \rightarrow \ldots  \rightarrow \ldots $}
       
   173 \end{center}
       
   174 
       
   175 \end{frame}
       
   176 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
       
   177   
       
   178 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   179 \begin{frame}[c]
       
   180 \frametitle{Example Derivation}
       
   181 
       
   182 \begin{center}
       
   183 \bl{\begin{tabular}{lcl}
       
   184 $S$ & $\rightarrow$ &  $\epsilon \;|\; a\cdot S\cdot a \;|\;b\cdot S\cdot b$ \\
       
   185 \end{tabular}}
       
   186 \end{center}\bigskip
       
   187 
       
   188 \begin{center}
       
   189 \begin{tabular}{lcl}
       
   190 \bl{$S$} & \bl{$\rightarrow$} & \bl{$aSa$}\\
       
   191               & \bl{$\rightarrow$} & \bl{$abSba$}\\
       
   192               & \bl{$\rightarrow$} & \bl{$abaSaba$}\\
       
   193               & \bl{$\rightarrow$} & \bl{$abaaba$}\\
       
   194 
       
   195  
       
   196 \end{tabular}
       
   197 \end{center}
       
   198 
       
   199 \end{frame}
       
   200 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
       
   201 
       
   202 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   203 \begin{frame}[c]
       
   204 \frametitle{Example Derivation}
       
   205 
       
   206 \begin{center}
       
   207 \bl{\begin{tabular}{lcl}
       
   208 $E$ & $\rightarrow$ &  $num\_token$ \\
       
   209 $E$ & $\rightarrow$ &  $E \cdot + \cdot E$ \\
       
   210 $E$ & $\rightarrow$ &  $E \cdot - \cdot E$ \\
       
   211 $E$ & $\rightarrow$ &  $E \cdot * \cdot E$ \\
       
   212 $E$ & $\rightarrow$ &  $( \cdot E \cdot )$ 
       
   213 \end{tabular}}
       
   214 \end{center}\bigskip
       
   215 
       
   216 
       
   217 \begin{center}
       
   218 \begin{tabular}{@{}c@{}c@{}}
       
   219 \begin{tabular}{l@{\hspace{1mm}}l@{\hspace{1mm}}l}
       
   220 \bl{$E$} & \bl{$\rightarrow$} & \bl{$E*E$}\\
       
   221               & \bl{$\rightarrow$} & \bl{$E+E*E$}\\
       
   222               & \bl{$\rightarrow$} & \bl{$E+E*E+E$}\\
       
   223               & \bl{$\rightarrow^+$} & \bl{$1+2*3+4$}\\
       
   224 \end{tabular} &\pause
       
   225 \begin{tabular}{l@{\hspace{1mm}}l@{\hspace{1mm}}l}
       
   226 \bl{$E$} & \bl{$\rightarrow$} & \bl{$E+E$}\\
       
   227               & \bl{$\rightarrow$} & \bl{$E+E+E$}\\
       
   228               & \bl{$\rightarrow$} & \bl{$E+E*E+E$}\\
       
   229               & \bl{$\rightarrow^+$} & \bl{$1+2*3+4$}\\
       
   230 \end{tabular}
       
   231 \end{tabular}
       
   232 \end{center}
       
   233 
       
   234 \end{frame}
       
   235 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
       
   236 
       
   237 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   238 \begin{frame}[c]
       
   239 \frametitle{Language of a CFG}
       
   240 
       
   241 Let \bl{$G$} be a context-free grammar with start symbol \bl{$S$}. 
       
   242 Then the language \bl{$L(G)$} is:
       
   243 
       
   244 \begin{center}
       
   245 \bl{$\{c_1\ldots c_n \;|\; \forall i.\; c_i \in T \wedge S \rightarrow^* c_1\ldots c_n \}$}
       
   246 \end{center}\pause
       
   247 
       
   248 \begin{itemize}
       
   249 \item Terminals, because there are no rules for replacing them.
       
   250 \item Once generated, terminals are ``permanent''.
       
   251 \item Terminals ought to be tokens of the language\\
       
   252 (but can also be strings).
       
   253 \end{itemize}
       
   254 
       
   255 \end{frame}
       
   256 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
       
   257 
       
   258 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   259 \begin{frame}[c]
       
   260 \frametitle{Parse Trees}
       
   261 
       
   262 \begin{center}
       
   263 \bl{\begin{tabular}{lcl}
       
   264 $E$ & $\rightarrow$ &  $F \;|\; F \cdot * \cdot F$\\
       
   265 $F$ & $\rightarrow$ & $T \;|\; T \cdot + \cdot T \;|\; T \cdot - \cdot T$\\
       
   266 $T$ & $\rightarrow$ & $num\_token \;|\; ( \cdot E \cdot )$\\
       
   267 \end{tabular}}
       
   268 \end{center}
       
   269 
       
   270 \begin{center}
       
   271 \begin{tikzpicture}[level distance=8mm, blue]
       
   272   \node {$E$}
       
   273     child {node {$F$} 
       
   274      child {node {$T$} 
       
   275                  child {node {(\,$E$\,)}
       
   276                             child {node{$F$ *{} $F$}
       
   277                                   child {node {$T$} child {node {2}}}
       
   278                                   child {node {$T$} child {node {3}}} 
       
   279                                }
       
   280                           }
       
   281               }
       
   282      child {node {+}}
       
   283      child {node {$T$}
       
   284        child {node {(\,$E$\,)} 
       
   285        child {node {$F$}
       
   286        child {node {$T$ +{} $T$}
       
   287                     child {node {3}}
       
   288                     child {node {4}} 
       
   289                  }
       
   290                  }}
       
   291     }};
       
   292 \end{tikzpicture}
       
   293 \end{center}
       
   294 
       
   295 \begin{textblock}{5}(1, 6.5)
       
   296 \bl{\texttt{(2*3)+(3+4)}}
       
   297 \end{textblock}
       
   298 
       
   299 \end{frame}
       
   300 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
       
   301 
       
   302 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   303 \begin{frame}[c]
       
   304 \frametitle{Arithmetic Expressions}
       
   305 
       
   306 \begin{center}
       
   307 \bl{\begin{tabular}{lcl}
       
   308 $E$ & $\rightarrow$ &  $num\_token$ \\
       
   309 $E$ & $\rightarrow$ &  $E \cdot + \cdot E$ \\
       
   310 $E$ & $\rightarrow$ &  $E \cdot - \cdot E$ \\
       
   311 $E$ & $\rightarrow$ &  $E \cdot * \cdot E$ \\
       
   312 $E$ & $\rightarrow$ &  $( \cdot E \cdot )$ 
       
   313 \end{tabular}}
       
   314 \end{center}\pause\bigskip
       
   315 
       
   316 A CFG is \alert{left-recursive} if it has a nonterminal \bl{$E$} such
       
   317 that \bl{$E \rightarrow^+ E\cdot \ldots$}
       
   318 
       
   319 \end{frame}
       
   320 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
       
   321 
       
   322 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   323 \begin{frame}[c]
       
   324 \frametitle{Ambiguous Grammars}
       
   325 
       
   326 A grammar is \alert{ambiguous} if there is a string that has
       
   327 at least two different parse trees.
       
   328 
       
   329 
       
   330 \begin{center}
       
   331 \bl{\begin{tabular}{lcl}
       
   332 $E$ & $\rightarrow$ &  $num\_token$ \\
       
   333 $E$ & $\rightarrow$ &  $E \cdot + \cdot E$ \\
       
   334 $E$ & $\rightarrow$ &  $E \cdot - \cdot E$ \\
       
   335 $E$ & $\rightarrow$ &  $E \cdot * \cdot E$ \\
       
   336 $E$ & $\rightarrow$ &  $( \cdot E \cdot )$ 
       
   337 \end{tabular}}
       
   338 \end{center}
       
   339 
       
   340 \bl{\texttt{1 + 2 * 3 + 4}}
       
   341 
       
   342 \end{frame}
       
   343 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
       
   344 
       
   345 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   346 \begin{frame}[c]
       
   347 \frametitle{Dangling Else}
       
   348 
       
   349 Another ambiguous grammar:\bigskip
       
   350 
       
   351 \begin{center}
       
   352 \bl{\begin{tabular}{lcl}
       
   353 $E$ & $\rightarrow$ &  if $E$ then $E$\\
       
   354  & $|$ &  if $E$ then $E$ else $E$ \\
       
   355  & $|$ &  \ldots
       
   356 \end{tabular}}
       
   357 \end{center}\bigskip
       
   358 
       
   359 \bl{\texttt{if a then if x then y else c}}
       
   360 
       
   361 \end{frame}
       
   362 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
       
   363 
       
   364 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   365 \begin{frame}[c]
       
   366 \frametitle{Parser Combinators}
       
   367 
       
   368 Parser combinators: \bigskip
       
   369 
       
   370 \begin{minipage}{1.1\textwidth}
       
   371 \begin{center}
       
   372 \mbox{}\hspace{-12mm}\mbox{}$\underbrace{\text{list of tokens}}_{\text{input}}$ \bl{$\Rightarrow$} 
       
   373 $\underbrace{\text{set of (parsed input, unparsed input)}}_{\text{output}}$
       
   374 \end{center}
       
   375 \end{minipage}\bigskip
       
   376 
       
   377 \begin{itemize}
       
   378 \item atomic parsers
       
   379 \item sequencing
       
   380 \item alternative
       
   381 \item semantic action
       
   382 \end{itemize}
       
   383 
       
   384 \end{frame}
       
   385 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
       
   386 
       
   387 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   388 \begin{frame}[c]
       
   389 
       
   390 Atomic parsers, for example, number tokens
       
   391 
       
   392 \begin{center}
       
   393 \bl{$\texttt{Num(123)}::rest \;\Rightarrow\; \{(\texttt{Num(123)}, rest)\}$} 
       
   394 \end{center}\bigskip
       
   395 
       
   396 \begin{itemize}
       
   397 \item you consume one or more token from the\\ 
       
   398   input (stream)
       
   399 \item also works for characters and strings
       
   400 \end{itemize}
       
   401 \end{frame}
       
   402 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
       
   403 
       
   404 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   405 \begin{frame}[c]
       
   406 
       
   407 Alternative parser (code \bl{$p\;||\;q$})\bigskip
       
   408 
       
   409 \begin{itemize}
       
   410 \item apply \bl{$p$} and also \bl{$q$}; then combine 
       
   411   the outputs
       
   412 \end{itemize}
       
   413 
       
   414 \begin{center}
       
   415 \large \bl{$p(\text{input}) \cup q(\text{input})$}
       
   416 \end{center}
       
   417 
       
   418 \end{frame}
       
   419 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
       
   420 
       
   421 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   422 \mode<presentation>{
       
   423 \begin{frame}[c]
       
   424 
       
   425 Sequence parser (code \bl{$p\sim q$})\bigskip
       
   426 
       
   427 \begin{itemize}
       
   428 \item apply first \bl{$p$} producing a set of pairs
       
   429 \item then apply \bl{$q$} to the unparsed parts
       
   430 \item then combine the results:\medskip 
       
   431 \begin{center}
       
   432 ((output$_1$, output$_2$), unparsed part)
       
   433 \end{center}
       
   434 \end{itemize}
       
   435 
       
   436 \begin{center}
       
   437 \begin{tabular}{l}
       
   438 \large \bl{$\{((o_1, o_2), u_2) \;|\;$}\\[2mm] 
       
   439 \large\mbox{}\hspace{15mm} \bl{$(o_1, u_1) \in p(\text{input}) \wedge$}\\[2mm]
       
   440 \large\mbox{}\hspace{15mm} \bl{$(o_2, u_2) \in q(u_1)\}$}
       
   441 \end{tabular}
       
   442 \end{center}
       
   443 
       
   444 
       
   445 \end{frame}}
       
   446 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
       
   447 
       
   448 
       
   449 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   450 \mode<presentation>{
       
   451 \begin{frame}[c]
       
   452 
       
   453 Function parser (code \bl{$p \Rightarrow f$})\bigskip
       
   454 
       
   455 \begin{itemize}
       
   456 \item apply \bl{$p$} producing a set of pairs
       
   457 \item then apply the function \bl{$f$} to each first component
       
   458 \end{itemize}
       
   459 
       
   460 \begin{center}
       
   461 \begin{tabular}{l}
       
   462 \large \bl{$\{(f(o_1), u_1) \;|\; (o_1, u_1) \in p(\text{input})\}$}
       
   463 \end{tabular}
       
   464 \end{center}\bigskip\bigskip\pause
       
   465 
       
   466 \bl{$f$} is the semantic action (``what to do with the parsed input'')
       
   467 
       
   468 \end{frame}}
       
   469 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
       
   470 
       
   471 
       
   472 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   473 \mode<presentation>{
       
   474 \begin{frame}[c]
       
   475 \frametitle{\begin{tabular}{c}Semantic Actions\end{tabular}}
       
   476 
       
   477 Addition
       
   478 
       
   479 \begin{center}
       
   480 \bl{$T \sim + \sim E \Rightarrow \underbrace{f((x,y), z) \Rightarrow x + z}_{\text{semantic action}}$}
       
   481 \end{center}\pause
       
   482 
       
   483 Multiplication
       
   484 
       
   485 \begin{center}
       
   486 \bl{$F \sim * \sim T \Rightarrow f((x,y), z) \Rightarrow x * z$}
       
   487 \end{center}\pause
       
   488 
       
   489 Parenthesis
       
   490 
       
   491 \begin{center}
       
   492 \bl{$\text{(} \sim E \sim \text{)} \Rightarrow f((x,y), z) \Rightarrow y$}
       
   493 \end{center}
       
   494 
       
   495 \end{frame}}
       
   496 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
       
   497 
       
   498 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   499 \mode<presentation>{
       
   500 \begin{frame}[c]
       
   501 \frametitle{\begin{tabular}{c}Types of Parsers\end{tabular}}
       
   502 
       
   503 \begin{itemize}
       
   504 \item {\bf Sequencing}: if \bl{$p$} returns results of type \bl{$T$}, and \bl{$q$} results of type \bl{$S$},
       
   505 then \bl{$p \sim q$} returns results of type
       
   506 
       
   507 \begin{center}
       
   508 \bl{$T \times S$}
       
   509 \end{center}\pause
       
   510 
       
   511 \item {\bf Alternative}: if \bl{$p$} returns results of type \bl{$T$} then  \bl{$q$} \alert{must} also have results of type \bl{$T$},
       
   512 and \bl{$p \;||\; q$} returns results of type
       
   513 
       
   514 \begin{center}
       
   515 \bl{$T$}
       
   516 \end{center}\pause
       
   517 
       
   518 \item {\bf Semantic Action}: if \bl{$p$} returns results of type \bl{$T$} and \bl{$f$} is a function from
       
   519 \bl{$T$} to \bl{$S$}, then
       
   520 \bl{$p \Rightarrow f$} returns results of type
       
   521 
       
   522 \begin{center}
       
   523 \bl{$S$}
       
   524 \end{center}
       
   525 
       
   526 \end{itemize}
       
   527 
       
   528 \end{frame}}
   364 \end{frame}}
   529 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  
   365 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  
   530 
   366 
   531 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   367 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   532 \begin{frame}[c]
   368 \mode<presentation>{
   533 \frametitle{Input Types of Parsers}
   369 \begin{frame}[c]
   534 
   370 
   535 \begin{itemize}
   371 \begin{center}
   536 \item input: \alert{token list}
   372 \bl{\begin{tabular}{@{}lcl@{}}
   537 \item output: set of (output\_type, \alert{token list})
   373 \textit{Stmt} & $\rightarrow$ &  $\texttt{skip}$\\
   538 \end{itemize}\bigskip\pause
   374               & $|$ & \textit{Id}\;\texttt{:=}\;\textit{AExp}\\
   539 
   375               & $|$ & \texttt{if}\; \textit{BExp} \;\texttt{then}\; \textit{Block} \;\texttt{else}\; \textit{Block}\\
   540 actually it can be any input type as long as it is a kind of
   376               & $|$ & \texttt{while}\; \textit{BExp} \;\texttt{do}\; \textit{Block}\\
   541 sequence (for example a string)
   377               & $|$ & \texttt{read}\;\textit{Id}\\
   542 
   378               & $|$ & \texttt{write}\;\textit{Id}\\
   543 \end{frame}
   379               & $|$ & \texttt{write}\;\textit{String}\medskip\\
   544 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  
   380 \textit{Stmts} & $\rightarrow$ &  \textit{Stmt} \;\texttt{;}\; \textit{Stmts}\\
   545 
   381               & $|$ & \textit{Stmt}\medskip\\
   546 
   382 \textit{Block} & $\rightarrow$ &  \texttt{\{}\,\textit{Stmts}\,\texttt{\}}\\
   547 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   383                 & $|$ & \textit{Stmt}\medskip\\
   548 \begin{frame}[c]
   384 \textit{AExp} & $\rightarrow$ & \ldots\\
   549 \frametitle{Scannerless Parsers}
   385 \textit{BExp} & $\rightarrow$ & \ldots\\
   550 
   386 \end{tabular}}
   551 \begin{itemize}
   387 \end{center}
   552 \item input: \alert{string}
   388 \end{frame}}
   553 \item output: set of (output\_type, \alert{string})
   389 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
   554 \end{itemize}\bigskip
   390 
   555 
   391 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   556 but lexers are better when whitespaces or comments need to be
   392 \mode<presentation>{
   557 filtered out; then input is a sequence of tokens
   393 \begin{frame}[c]
   558 
   394 
   559 \end{frame}
   395 \mbox{\lstinputlisting[language=while]{../progs/fib.while}}
   560 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  
   396 
   561 
   397 \end{frame}}
   562 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   398 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
   563 \begin{frame}[c]
   399 
   564 \frametitle{Successful Parses}
   400 
   565 
   401 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   566 \begin{itemize}
   402 \mode<presentation>{
   567 \item input: string
   403 \begin{frame}[c]
   568 \item output: \alert{set of} (output\_type, string)
   404 \frametitle{\begin{tabular}{c}An Interpreter\end{tabular}}
   569 \end{itemize}\bigskip
       
   570 
       
   571 a parse is successful whenever the input has been fully
       
   572 ``consumed'' (that is the second component is empty)
       
   573 
       
   574 \end{frame}
       
   575 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  
       
   576 
       
   577 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   578 \begin{frame}[c]
       
   579 \frametitle{Abstract Parser Class}
       
   580 
       
   581 \small
       
   582 \lstinputlisting[language=Scala,xleftmargin=1mm]{../progs/app7.scala}
       
   583 \end{frame}
       
   584 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  
       
   585 
       
   586 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   587 \begin{frame}[c]
       
   588 
       
   589 \small
       
   590 \fontsize{10}{12}\selectfont
       
   591 \lstinputlisting[language=Scala,xleftmargin=1mm]{../progs/app8.scala}
       
   592 \end{frame}
       
   593 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
       
   594 
       
   595 
       
   596 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   597 \begin{frame}[c]
       
   598 \frametitle{Two Grammars}
       
   599 
       
   600 Which languages are recognised by the following two grammars?
       
   601 
       
   602 \begin{center}
       
   603 \bl{\begin{tabular}{lcl}
       
   604 $S$ & $\rightarrow$ &  $1 \cdot S \cdot S$\\
       
   605         & $|$ & $\epsilon$
       
   606 \end{tabular}}
       
   607 \end{center}\bigskip
       
   608 
       
   609 \begin{center}
       
   610 \bl{\begin{tabular}{lcl}
       
   611 $U$ & $\rightarrow$ &  $1 \cdot U$\\
       
   612         & $|$ & $\epsilon$
       
   613 \end{tabular}}
       
   614 \end{center}
       
   615 
       
   616 \end{frame}
       
   617 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
       
   618 
       
   619 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   620 \begin{frame}[t]
       
   621 \frametitle{Ambiguous Grammars}
       
   622 
       
   623 \begin{center}
       
   624 \begin{tikzpicture}
       
   625 \begin{axis}[xlabel={\pcode{1}s},ylabel={time in secs},
       
   626     enlargelimits=false,
       
   627     xtick={0,100,...,1000},
       
   628     xmax=1050,
       
   629     ymax=33,
       
   630     ytick={0,5,...,30},
       
   631     scaled ticks=false,
       
   632     axis lines=left,
       
   633     width=11cm,
       
   634     height=7cm, 
       
   635     legend entries={unambiguous,ambiguous},  
       
   636     legend pos=north east,
       
   637     legend cell align=left,
       
   638     x tick label style={font=\small,/pgf/number format/1000 sep={}}]
       
   639 \addplot[blue,mark=*, mark options={fill=white}] 
       
   640   table {s-grammar1.data};
       
   641 \only<2>{
       
   642   \addplot[red,mark=triangle*, mark options={fill=white}] 
       
   643   table {s-grammar2.data};}  
       
   644 \end{axis}
       
   645 \end{tikzpicture}
       
   646 \end{center}
       
   647 
       
   648 \end{frame}
       
   649 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
       
   650 
       
   651 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
       
   652 \begin{frame}
       
   653 \frametitle{While-Language}
       
   654 \mbox{}\\[-23mm]\mbox{}
       
   655 
       
   656 \bl{\begin{plstx}[rhs style=,one per line]
       
   657 : \meta{Stmt} ::= skip
       
   658          | \meta{Id} := \meta{AExp}
       
   659          | if \meta{BExp} then \meta{Block} else \meta{Block}
       
   660          | while \meta{BExp} do \meta{Block}\\
       
   661 : \meta{Stmts} ::= \meta{Stmt} ; \meta{Stmts}
       
   662           | \meta{Stmt}\\
       
   663 : \meta{Block} ::= \{ \meta{Stmts} \}
       
   664           | \meta{Stmt}\\
       
   665 : \meta{AExp} ::= \ldots\\
       
   666 : \meta{BExp} ::= \ldots\\
       
   667 \end{plstx}}
       
   668 
       
   669 \end{frame}
       
   670 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
       
   671 
       
   672 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   673 \begin{frame}[c]
       
   674 \frametitle{An Interpreter}
       
   675 
   405 
   676 \begin{center}
   406 \begin{center}
   677 \bl{\begin{tabular}{l}
   407 \bl{\begin{tabular}{l}
   678 $\{$\\
   408 $\{$\\
   679 \;\;$x := 5 \text{;}$\\
   409 \;\;$x := 5 \text{;}$\\
   684 \end{tabular}}
   414 \end{tabular}}
   685 \end{center}
   415 \end{center}
   686 
   416 
   687 \begin{itemize}
   417 \begin{itemize}
   688 \item the interpreter has to record the value of \bl{$x$} before assigning a value to \bl{$y$}\pause
   418 \item the interpreter has to record the value of \bl{$x$} before assigning a value to \bl{$y$}\pause
   689 \item \bl{\texttt{eval(stmt, env)}}
   419 \item \bl{\text{eval}(stmt, env)}
   690 \end{itemize}
   420 \end{itemize}
   691 
   421 
       
   422 
       
   423 \end{frame}}
       
   424 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   425 
       
   426 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   427 \mode<presentation>{
       
   428 \begin{frame}[c]
       
   429 \frametitle{\begin{tabular}{c}Interpreter\end{tabular}}
       
   430 
       
   431 \begin{center}
       
   432 \bl{\begin{tabular}{@{}lcl@{}}
       
   433 $\text{eval}(n, E)$ & $\dn$ & $n$\\
       
   434 $\text{eval}(x, E)$ & $\dn$ & $E(x)$ \;\;\;\textcolor{black}{lookup \bl{$x$} in \bl{$E$}}\\
       
   435 $\text{eval}(a_1 + a_2, E)$ & $\dn$ & $\text{eval}(a_1, E) + \text{eval}(a_2, E)$\\
       
   436 $\text{eval}(a_1 - a_2, E)$ & $\dn$ & $\text{eval}(a_1, E) - \text{eval}(a_2, E)$\\
       
   437 $\text{eval}(a_1 * a_2, E)$ & $\dn$ & $\text{eval}(a_1, E) * \text{eval}(a_2, E)$\bigskip\\
       
   438 $\text{eval}(a_1 = a_2, E)$ & $\dn$ & $\text{eval}(a_1, E) = \text{eval}(a_2, E)$\\
       
   439 $\text{eval}(a_1\,!\!= a_2, E)$ & $\dn$ & $\neg(\text{eval}(a_1, E) = \text{eval}(a_2, E))$\\
       
   440 $\text{eval}(a_1 < a_2, E)$ & $\dn$ & $\text{eval}(a_1, E) < \text{eval}(a_2, E)$\
       
   441 \end{tabular}}
       
   442 \end{center}
       
   443 
       
   444 \end{frame}}
       
   445 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
       
   446 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   447 \mode<presentation>{
       
   448 \begin{frame}[c]
       
   449 \frametitle{\begin{tabular}{c}Interpreter (2)\end{tabular}}
       
   450 
       
   451 \begin{center}
       
   452 \bl{\begin{tabular}{@{}lcl@{}}
       
   453 $\text{eval}(\text{skip}, E)$ & $\dn$ & $E$\\
       
   454 $\text{eval}(x:=a, E)$ & $\dn$ & \bl{$E(x \mapsto \text{eval}(a, E))$}\\
       
   455 \multicolumn{3}{@{}l@{}}{$\text{eval}(\text{if}\;b\;\text{then}\;cs_1\;\text{else}\;cs_2 , E) \dn$}\\
       
   456 \multicolumn{3}{@{}l@{}}{\hspace{2cm}$\text{if}\;\text{eval}(b,E)\;\text{then}\;
       
   457 \text{eval}(cs_1,E)$}\\
       
   458 \multicolumn{3}{@{}l@{}}{\hspace{2cm}$\phantom{\text{if}\;\text{eval}(b,E)\;}\text{else}\;\text{eval}(cs_2,E)$}\\
       
   459 \multicolumn{3}{@{}l@{}}{$\text{eval}(\text{while}\;b\;\text{do}\;cs, E) \dn$}\\
       
   460 \multicolumn{3}{@{}l@{}}{\hspace{2cm}$\text{if}\;\text{eval}(b,E)$}\\
       
   461 \multicolumn{3}{@{}l@{}}{\hspace{2cm}$\text{then}\;
       
   462 \text{eval}(\text{while}\;b\;\text{do}\;cs, \text{eval}(cs,E))$}\\
       
   463 \multicolumn{3}{@{}l@{}}{\hspace{2cm}$\text{else}\; E$}\\
       
   464 $\text{eval}(\text{write}\; x, E)$ & $\dn$ & $\{\;\text{println}(E(x))\; ;\;E\;\}$\\
       
   465 \end{tabular}}
       
   466 \end{center}
       
   467 
       
   468 \end{frame}}
       
   469 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
       
   470 
       
   471 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   472 \begin{frame}[c]
       
   473 \frametitle{\begin{tabular}{c}Test Program\end{tabular}}
       
   474 
       
   475 \mbox{}\\[-18mm]\mbox{}
       
   476 
       
   477 {\lstset{language=While}%%\fontsize{10}{12}\selectfont
       
   478 \texttt{\lstinputlisting{../progs/loops.while}}}
       
   479 
   692 \end{frame}
   480 \end{frame}
       
   481 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
       
   482 
       
   483 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   484 \mode<presentation>{
       
   485 \begin{frame}[t]
       
   486 \frametitle{\begin{tabular}{c}Interpreted Code\end{tabular}}
       
   487 
       
   488 \begin{center}
       
   489 \begin{tikzpicture}
       
   490 \begin{axis}[axis x line=bottom, axis y line=left, xlabel=n, ylabel=secs, legend style=small]
       
   491 \addplot+[smooth] file {interpreted.data};
       
   492 \end{axis}
       
   493 \end{tikzpicture}
       
   494 \end{center}
       
   495 
       
   496 \end{frame}}
       
   497 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
       
   498 
       
   499 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   500 \mode<presentation>{
       
   501 \begin{frame}[c]
       
   502 \frametitle{\begin{tabular}{c}Java Virtual Machine\end{tabular}}
       
   503 
       
   504 \begin{itemize}
       
   505 \item introduced in 1995
       
   506 \item is a stack-based VM (like Postscript, CLR of .Net)
       
   507 \item contains a JIT compiler
       
   508 \item many languages take advantage of JVM's infrastructure (JRE)
       
   509 \item is garbage collected $\Rightarrow$ no buffer overflows
       
   510 \item some languages compile to the JVM: Scala, Clojure\ldots
       
   511 \end{itemize}
       
   512 
       
   513 \end{frame}}
   693 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
   514 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
   694 
   515 
   695 
   516 
   696 \end{document}
   517 \end{document}
   697 
   518