slides/slides07.tex
changeset 184 2e9134d25a2b
parent 173 7cfb7a6f7c99
child 185 ea8b94d4755e
equal deleted inserted replaced
183:b17eff695c7f 184:2e9134d25a2b
    74 % beamer stuff 
    74 % beamer stuff 
    75 \renewcommand{\slidecaption}{AFL 07, King's College London, 13.~November 2013}
    75 \renewcommand{\slidecaption}{AFL 07, King's College London, 13.~November 2013}
    76 \newcommand{\bl}[1]{\textcolor{blue}{#1}}       
    76 \newcommand{\bl}[1]{\textcolor{blue}{#1}}       
    77 \newcommand{\dn}{\stackrel{\mbox{\scriptsize def}}{=}}% for definitions
    77 \newcommand{\dn}{\stackrel{\mbox{\scriptsize def}}{=}}% for definitions
    78 
    78 
    79 
       
    80 % The data files, written on the first run.
       
    81 \begin{filecontents}{re-python.data}
       
    82 1 0.029
       
    83 5 0.029
       
    84 10 0.029
       
    85 15 0.032
       
    86 16 0.042
       
    87 17 0.042
       
    88 18 0.055
       
    89 19 0.084
       
    90 20 0.136
       
    91 21 0.248
       
    92 22 0.464
       
    93 23 0.899
       
    94 24 1.773
       
    95 25 3.505
       
    96 26 6.993
       
    97 27 14.503
       
    98 28 29.307
       
    99 #29 58.886
       
   100 \end{filecontents}
       
   101 
       
   102 \begin{filecontents}{re-ruby.data}
       
   103 1 0.00006
       
   104 2 0.00003
       
   105 3 0.00001
       
   106 4 0.00001
       
   107 5 0.00001
       
   108 6 0.00002
       
   109 7 0.00002
       
   110 8 0.00004
       
   111 9 0.00007
       
   112 10 0.00013
       
   113 11 0.00026
       
   114 12 0.00055
       
   115 13 0.00106
       
   116 14 0.00196
       
   117 15 0.00378
       
   118 16 0.00764
       
   119 17 0.01606
       
   120 18 0.03094
       
   121 19 0.06508
       
   122 20 0.12420
       
   123 21 0.25393
       
   124 22 0.51449
       
   125 23 1.02174
       
   126 24 2.05998
       
   127 25 4.22514
       
   128 26 8.42479
       
   129 27 16.88678
       
   130 28 34.79653
       
   131 \end{filecontents}
       
   132 
       
   133 \begin{filecontents}{re1.data}
       
   134 1 0.00179
       
   135 2 0.00011
       
   136 3 0.00014
       
   137 4 0.00026
       
   138 5 0.00050
       
   139 6 0.00095
       
   140 7 0.00190
       
   141 8 0.00287
       
   142 9 0.00779
       
   143 10 0.01399
       
   144 11 0.01894
       
   145 12 0.03666
       
   146 13 0.07994
       
   147 14 0.08944
       
   148 15 0.02377
       
   149 16 0.07392
       
   150 17 0.22798
       
   151 18 0.65310
       
   152 19 2.11360
       
   153 20 6.31606
       
   154 21 21.46013
       
   155 \end{filecontents}
       
   156 
       
   157 \begin{filecontents}{re2.data}
       
   158 1  0.00240
       
   159 2  0.00013
       
   160 3  0.00020
       
   161 4  0.00030
       
   162 5  0.00049
       
   163 6  0.00083
       
   164 7  0.00146
       
   165 8  0.00228
       
   166 9  0.00351
       
   167 10  0.00640
       
   168 11  0.01217
       
   169 12  0.02565
       
   170 13  0.01382
       
   171 14  0.02423
       
   172 15  0.05065 
       
   173 16  0.06522
       
   174 17  0.02140
       
   175 18  0.05176
       
   176 19  0.18254
       
   177 20  0.51898
       
   178 21  1.39631
       
   179 22  2.69501
       
   180 23  8.07952
       
   181 \end{filecontents}
       
   182 
       
   183 \begin{filecontents}{re-internal.data}
       
   184 1 0.00069
       
   185 301 0.00700
       
   186 601 0.00297
       
   187 901 0.00470
       
   188 1201 0.01301
       
   189 1501 0.01175
       
   190 1801 0.01761
       
   191 2101 0.01787
       
   192 2401 0.02717
       
   193 2701 0.03932
       
   194 3001 0.03470
       
   195 3301 0.04349
       
   196 3601 0.05411
       
   197 3901 0.06181
       
   198 4201 0.07119
       
   199 4501 0.08578
       
   200 \end{filecontents}
       
   201 
       
   202 \begin{filecontents}{re3.data}
       
   203 1 0.001605
       
   204 501 0.131066
       
   205 1001 0.057885
       
   206 1501 0.136875
       
   207 2001 0.176238
       
   208 2501 0.254363
       
   209 3001 0.37262
       
   210 3501 0.500946
       
   211 4001 0.638384
       
   212 4501 0.816605
       
   213 5001 1.00491
       
   214 5501 1.232505
       
   215 6001 1.525672
       
   216 6501 1.757502
       
   217 7001 2.092784
       
   218 7501 2.429224
       
   219 8001 2.803037
       
   220 8501 3.463045
       
   221 9001 3.609
       
   222 9501 4.081504
       
   223 10001 4.54569
       
   224 \end{filecontents}
       
   225 \begin{document}
    79 \begin{document}
   226 
    80 
   227 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    81 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   228 \mode<presentation>{
    82 \mode<presentation>{
   229 \begin{frame}<1>[t]
    83 \begin{frame}<1>[t]
   260 \item a finite set of nonterminal symbols (upper case)
   114 \item a finite set of nonterminal symbols (upper case)
   261 \item a finite terminal symbols or tokens (lower case)
   115 \item a finite terminal symbols or tokens (lower case)
   262 \item a start symbol (which must be a nonterminal)
   116 \item a start symbol (which must be a nonterminal)
   263 \item a set of rules
   117 \item a set of rules
   264 \begin{center}
   118 \begin{center}
   265 \bl{$A \rightarrow \text{rhs}$}
   119 \bl{$A \rightarrow \text{rhs}_1 | \text{rhs}_2 | \ldots$}
   266 \end{center}
   120 \end{center}
   267 
   121 
   268 where \bl{rhs} are sequences involving terminals and nonterminals (can also be empty).\medskip\pause
   122 where \bl{rhs} are sequences involving terminals and nonterminals (can also be empty).\medskip\pause
   269 
   123 
   270 We can also allow rules
       
   271 \begin{center}
       
   272 \bl{$A \rightarrow \text{rhs}_1 | \text{rhs}_2 | \ldots$}
       
   273 \end{center}
       
   274 \end{itemize}
   124 \end{itemize}
   275 
   125 
   276 
   126 \end{frame}}
   277 \end{frame}}
   127 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
   278 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
   128 
   279 
   129 
   280 
   130 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   281 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   131 \mode<presentation>{
   282 \mode<presentation>{
   132 \begin{frame}[c]
   283 \begin{frame}[c]
   133 \frametitle{\begin{tabular}{c}Hierarchie of Languages\end{tabular}}
   284 \frametitle{\begin{tabular}{c}A CFG Derivation\end{tabular}}
   134 
   285 
   135 Recall that languages are sets of strings.
   286 \begin{enumerate}
   136 
   287 \item Begin with a string with only the start symbol \bl{$S$}\bigskip
   137 \begin{center}
   288 \item Replace any non-terminal \bl{$X$} in the string by the
   138 \begin{tikzpicture}
   289 right-hand side of some production \bl{$X \rightarrow \text{rhs}$}\bigskip
   139 [rect/.style={draw=black!50, top color=white,bottom color=black!20, rectangle, very thick, rounded corners}]
   290 \item Repeat 2 until there are no non-terminals
   140 
   291 \end{enumerate}
   141 \draw (0,0) node [rect, text depth=39mm, text width=68mm] {all languages};
   292 
   142 \draw (0,-0.4) node [rect, text depth=28.5mm, text width=64mm] {decidable languages};
   293 \begin{center}
   143 \draw (0,-0.85) node [rect, text depth=17mm] {context sensitive languages};
   294 \bl{$S \rightarrow \ldots \rightarrow \ldots  \rightarrow \ldots  \rightarrow \ldots $}
   144 \draw (0,-1.14) node [rect, text depth=9mm, text width=50mm] {context-free languages};
   295 \end{center}
   145 \draw (0,-1.4) node [rect] {regular languages};
   296 
   146 \end{tikzpicture}
   297 \end{frame}}
   147 
   298 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
   148 \end{center}
   299 
   149 
   300 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   150 \end{frame}}
   301 \mode<presentation>{
   151 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
   302 \begin{frame}[c]
   152 
   303 \frametitle{\begin{tabular}{c}Language of a CFG\end{tabular}}
   153 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   304 
   154 \mode<presentation>{
   305 Let \bl{$G$} be a context-free grammar with start symbol \bl{$S$}. 
   155 \begin{frame}[t]
   306 Then the language \bl{$L(G)$} is:
   156 \frametitle{\begin{tabular}{c}Numbers\end{tabular}}
   307 
   157 
   308 \begin{center}
   158 A grammar for numbers:
   309 \bl{$\{c_1\ldots c_n \;|\; \forall i.\; c_i \in T \wedge S \rightarrow^* c_1\ldots c_n \}$}
   159 
   310 \end{center}\pause
   160 \begin{center}
       
   161 \bl{\begin{tabular}{lcl}
       
   162 $N$ & $\rightarrow$ &  $N \cdot N \;|\; 0 \;|\; 1 \;|\; \ldots \;|\; 9$\\
       
   163 \end{tabular}}
       
   164 \end{center}
       
   165 
       
   166 Unfortunately left-recursive (and ambiguous).\medskip\\
       
   167 A problem for \alert{recursive descent parsers} (e.g.~parser combinators).
       
   168 \bigskip\pause
       
   169 
       
   170 A non-left-recursive grammar for numbers
       
   171 
       
   172 \begin{center}
       
   173 \bl{\begin{tabular}{lcl}
       
   174 $N$ & $\rightarrow$ &  $0 \cdot N \;|\;1 \cdot N \;|\ldots\;|\; 0 \;|\; 1 \;|\; \ldots \;|\; 9$\\
       
   175 \end{tabular}}
       
   176 \end{center}
       
   177 
       
   178 
       
   179 \end{frame}}
       
   180 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
       
   181 
       
   182 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   183 \mode<presentation>{
       
   184 \begin{frame}[c]
       
   185 \frametitle{\begin{tabular}{c}Chomsky Normal Form\end{tabular}}
       
   186 
       
   187 All rules must be of the form
       
   188 
       
   189 \begin{center}
       
   190 \bl{$A \rightarrow a$}
       
   191 \end{center}
       
   192 
       
   193 or
       
   194 
       
   195 \begin{center}
       
   196 \bl{$A \rightarrow B\cdot C$}
       
   197 \end{center}
       
   198 
       
   199 
       
   200 
       
   201 \end{frame}}
       
   202 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
       
   203 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   204 \mode<presentation>{
       
   205 \begin{frame}[c]
       
   206 \frametitle{\begin{tabular}{c}CYK Algorithm\end{tabular}}
       
   207 
       
   208 
       
   209 \begin{center}
       
   210 \bl{\begin{tabular}{@ {}lcl}
       
   211 $S$ & $\rightarrow$ &  $N\cdot P$ \\
       
   212 $P$ & $\rightarrow$ &  $V\cdot N$ \\
       
   213 $N$ & $\rightarrow$ &  $N\cdot N$ \\
       
   214 $N$ & $\rightarrow$ &  $\texttt{students} \;|\; \texttt{Jeff} \;|\; \texttt{geometry} \;|\; \texttt{trains} $ \\
       
   215 $V$ & $\rightarrow$ &  $\texttt{trains}$ 
       
   216 \end{tabular}}
       
   217 \end{center}
       
   218 
       
   219 \bl{\texttt{Jeff trains geometry students}}
       
   220 
       
   221 \end{frame}}
       
   222 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
       
   223 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   224 \mode<presentation>{
       
   225 \begin{frame}[c]
       
   226 \frametitle{\begin{tabular}{c}CYK Algorithm\end{tabular}}
       
   227 
   311 
   228 
   312 \begin{itemize}
   229 \begin{itemize}
   313 \item Terminals are so-called because there are no rules for replacing them
   230 \item runtime is \bl{$O(n^3)$}\bigskip
   314 \item Once generated, terminals are ``permanent''
   231 \item grammars need to be transferred into CNF
   315 \item Terminals ought to be tokens of the language (at least in this course)
       
   316 \end{itemize}
   232 \end{itemize}
   317 
   233 
   318 \end{frame}}
   234 \end{frame}}
   319 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
   235 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  
   320 
       
   321 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   322 \mode<presentation>{
       
   323 \begin{frame}[c]
       
   324 \frametitle{\begin{tabular}{c}Arithmetic Expressions\end{tabular}}
       
   325 
       
   326 \begin{center}
       
   327 \bl{\begin{tabular}{lcl}
       
   328 $E$ & $\rightarrow$ &  $num\_token$ \\
       
   329 $E$ & $\rightarrow$ &  $E \cdot + \cdot E$ \\
       
   330 $E$ & $\rightarrow$ &  $E \cdot - \cdot E$ \\
       
   331 $E$ & $\rightarrow$ &  $E \cdot * \cdot E$ \\
       
   332 $E$ & $\rightarrow$ &  $( \cdot E \cdot )$ 
       
   333 \end{tabular}}
       
   334 \end{center}\pause\bigskip
       
   335 
       
   336 A CFG is \alert{left-recursive} if it has a nonterminal \bl{$E$} such
       
   337 that \bl{$E \rightarrow^+ E\cdot \ldots$}
       
   338 
       
   339 \end{frame}}
       
   340 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
       
   341 
   236 
   342 
   237 
   343 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   238 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   344 \mode<presentation>{
   239 \mode<presentation>{
   345 \begin{frame}[c]
   240 \begin{frame}[c]
   425 \bl{\texttt{if a then if x then y else c}}
   320 \bl{\texttt{if a then if x then y else c}}
   426 
   321 
   427 \end{frame}}
   322 \end{frame}}
   428 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
   323 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
   429 
   324 
       
   325 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   326 \mode<presentation>{
       
   327 \begin{frame}[c]
       
   328 \frametitle{\begin{tabular}{c}A CFG Derivation\end{tabular}}
       
   329 
       
   330 \begin{enumerate}
       
   331 \item Begin with a string with only the start symbol \bl{$S$}\bigskip
       
   332 \item Replace any non-terminal \bl{$X$} in the string by the
       
   333 right-hand side of some production \bl{$X \rightarrow \text{rhs}$}\bigskip
       
   334 \item Repeat 2 until there are no non-terminals
       
   335 \end{enumerate}
       
   336 
       
   337 \begin{center}
       
   338 \bl{$S \rightarrow \ldots \rightarrow \ldots  \rightarrow \ldots  \rightarrow \ldots $}
       
   339 \end{center}
       
   340 
       
   341 \end{frame}}
       
   342 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
       
   343 
       
   344 
   430 \end{document}
   345 \end{document}
   431 
   346 
   432 %%% Local Variables:  
   347 %%% Local Variables:  
   433 %%% mode: latex
   348 %%% mode: latex
   434 %%% TeX-master: t
   349 %%% TeX-master: t