slides/slides07.tex
changeset 187 9471a0325773
parent 186 fab34204d08e
child 188 12ef150273ce
equal deleted inserted replaced
186:fab34204d08e 187:9471a0325773
    69 	numbersep=10pt,
    69 	numbersep=10pt,
    70 	tabsize=2,
    70 	tabsize=2,
    71 	showspaces=false,
    71 	showspaces=false,
    72 	showstringspaces=false}
    72 	showstringspaces=false}
    73 
    73 
       
    74 \setmonofont{Consolas}
       
    75 
    74 % beamer stuff 
    76 % beamer stuff 
    75 \renewcommand{\slidecaption}{AFL 07, King's College London, 13.~November 2013}
    77 \renewcommand{\slidecaption}{AFL 07, King's College London, 13.~November 2013}
    76 \newcommand{\bl}[1]{\textcolor{blue}{#1}}       
    78 \newcommand{\bl}[1]{\textcolor{blue}{#1}}       
    77 \newcommand{\dn}{\stackrel{\mbox{\scriptsize def}}{=}}% for definitions
    79 \newcommand{\dn}{\stackrel{\mbox{\scriptsize def}}{=}}% for definitions
    78 
    80 
   162 $E$ & $\rightarrow$ &  $E \cdot + \cdot E \;|\;E \cdot * \cdot E \;|\;( \cdot E \cdot ) \;|\;N$ \\
   164 $E$ & $\rightarrow$ &  $E \cdot + \cdot E \;|\;E \cdot * \cdot E \;|\;( \cdot E \cdot ) \;|\;N$ \\
   163 $N$ & $\rightarrow$ & $N \cdot N \;|\; 0 \;|\; 1 \;|\: \ldots \;|\; 9$ 
   165 $N$ & $\rightarrow$ & $N \cdot N \;|\; 0 \;|\; 1 \;|\: \ldots \;|\; 9$ 
   164 \end{tabular}}
   166 \end{tabular}}
   165 \end{center}
   167 \end{center}
   166 
   168 
   167 Unfortunately left-recursive (and ambiguous).\medskip\\
   169 Unfortunately it is left-recursive (and ambiguous).\medskip\\
   168 A problem for \alert{recursive descent parsers} (e.g.~parser combinators).
   170 A problem for \alert{recursive descent parsers} (e.g.~parser combinators).
   169 \bigskip\pause
   171 \bigskip\pause
   170 
   172 
   171 \end{frame}}
   173 \end{frame}}
   172 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
   174 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
   183 \bl{\begin{tabular}{lcl}
   185 \bl{\begin{tabular}{lcl}
   184 $N$ & $\rightarrow$ &  $N \cdot N \;|\; 0 \;|\; 1 \;|\; \ldots \;|\; 9$\\
   186 $N$ & $\rightarrow$ &  $N \cdot N \;|\; 0 \;|\; 1 \;|\; \ldots \;|\; 9$\\
   185 \end{tabular}}
   187 \end{tabular}}
   186 \end{center}
   188 \end{center}
   187 
   189 
   188 A non-left-recursive, non-ambiguous grammar for numbers
   190 A non-left-recursive, non-ambiguous grammar for numbers:
   189 
   191 
   190 \begin{center}
   192 \begin{center}
   191 \bl{\begin{tabular}{lcl}
   193 \bl{\begin{tabular}{lcl}
   192 $N$ & $\rightarrow$ &  $0 \cdot N \;|\;1 \cdot N \;|\;\ldots\;|\; 0 \;|\; 1 \;|\; \ldots \;|\; 9$\\
   194 $N$ & $\rightarrow$ &  $0 \cdot N \;|\;1 \cdot N \;|\;\ldots\;|\; 0 \;|\; 1 \;|\; \ldots \;|\; 9$\\
   193 \end{tabular}}
   195 \end{tabular}}
   209 \bl{\begin{tabular}{lcl}
   211 \bl{\begin{tabular}{lcl}
   210 $E$ & $\rightarrow$ &  $E \cdot + \cdot E \;|\;E \cdot * \cdot E \;|\;( \cdot E \cdot ) \;|\;N$ \\
   212 $E$ & $\rightarrow$ &  $E \cdot + \cdot E \;|\;E \cdot * \cdot E \;|\;( \cdot E \cdot ) \;|\;N$ \\
   211 \end{tabular}}
   213 \end{tabular}}
   212 \end{center}
   214 \end{center}
   213 
   215 
   214 Decide how many precedence levels, say\medskip\\
   216 Decide on how many precedence levels, say\medskip\\
   215 \hspace{5mm}highest for \bl{$()$}, medium for \bl{*}, lowest for \bl{+}
   217 \hspace{5mm}highest for \bl{$()$}, medium for \bl{*}, lowest for \bl{+}
   216 
   218 
   217 \begin{center}
   219 \begin{center}
   218 \bl{\begin{tabular}{lcl}
   220 \bl{\begin{tabular}{lcl}
   219 $E_{low}$ & $\rightarrow$ & $E_{med} \cdot + \cdot E_{low} \;|\; E_{med}$ \\
   221 $E_{low}$ & $\rightarrow$ & $E_{med} \cdot + \cdot E_{low} \;|\; E_{med}$ \\
   306 \small
   308 \small
   307 \begin{center}
   309 \begin{center}
   308 \begin{tabular}{ccc}
   310 \begin{tabular}{ccc}
   309 \bl{\begin{tabular}{l@{\hspace{1mm}}c@{\hspace{1mm}}l}
   311 \bl{\begin{tabular}{l@{\hspace{1mm}}c@{\hspace{1mm}}l}
   310 $N$ & $\rightarrow$ & $0 \cdot N' \;|\; 1\cdot N'$\\
   312 $N$ & $\rightarrow$ & $0 \cdot N' \;|\; 1\cdot N'$\\
   311 $N'$ & $\rightarrow$ & $N \cdot N'\;|\;\epsilon$ 
   313 $N'$ & $\rightarrow$ & $N \cdot N'\;|\;\epsilon$\\
       
   314 \\ 
       
   315 \\
       
   316 \\
       
   317 \\
       
   318 \\
   312 \end{tabular}} &
   319 \end{tabular}} &
   313 \bl{\begin{tabular}{l@{\hspace{1mm}}c@{\hspace{1mm}}l}
   320 \bl{\begin{tabular}{l@{\hspace{1mm}}c@{\hspace{1mm}}l}
       
   321 \\
   314 $N$ & $\rightarrow$ & $0 \cdot N' \;|\; 1\cdot N'\;|\;0\;|\;1$\\
   322 $N$ & $\rightarrow$ & $0 \cdot N' \;|\; 1\cdot N'\;|\;0\;|\;1$\\
   315 $N'$ & $\rightarrow$ & $N \cdot N'\;|\;N\;|\;\epsilon$ 
   323 $N'$ & $\rightarrow$ & $N \cdot N'\;|\;N\;|\;\epsilon$\\
       
   324 \\
       
   325 $N$ & $\rightarrow$ & $0 \cdot N' \;|\; 1\cdot N'\;|\;0\;|\;1$\\
       
   326 $N'$ & $\rightarrow$ & $N \cdot N'\;|\;N$\\
   316 \end{tabular}}
   327 \end{tabular}}
   317 \end{tabular}
   328 \end{tabular}
   318 \end{center}
   329 \end{center}
   319 
   330 
   320 
   331 \pause\normalsize
       
   332 \begin{center}
       
   333 \bl{\begin{tabular}{l@{\hspace{1mm}}c@{\hspace{1mm}}l}
       
   334 $N$ & $\rightarrow$ & $0 \cdot N\;|\; 1\cdot N\;|\;0\;|\;1$\\
       
   335 \end{tabular}}
       
   336 
       
   337 \end{center}
   321 \end{frame}}
   338 \end{frame}}
   322 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
   339 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
   323 
   340 
   324 
   341 
   325 
   342 
   326 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   343 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   327 \mode<presentation>{
   344 \mode<presentation>{
   328 \begin{frame}[c]
   345 \begin{frame}[c]
   329 \frametitle{\begin{tabular}{c}CYK Algorithm\end{tabular}}
   346 \frametitle{\begin{tabular}{c}CYK Algorithm\end{tabular}}
   330 
   347 
   331 
   348 If grammar is in Chomsky normalform \ldots
   332 \begin{center}
   349 
   333 \bl{\begin{tabular}{@ {}lcl}
   350 \begin{center}
       
   351 \bl{\begin{tabular}{@ {}lcl@ {}}
   334 $S$ & $\rightarrow$ &  $N\cdot P$ \\
   352 $S$ & $\rightarrow$ &  $N\cdot P$ \\
   335 $P$ & $\rightarrow$ &  $V\cdot N$ \\
   353 $P$ & $\rightarrow$ &  $V\cdot N$ \\
   336 $N$ & $\rightarrow$ &  $N\cdot N$ \\
   354 $N$ & $\rightarrow$ &  $N\cdot N$ \\
   337 $N$ & $\rightarrow$ &  $\texttt{students} \;|\; \texttt{Jeff} \;|\; \texttt{geometry} \;|\; \texttt{trains} $ \\
   355 $N$ & $\rightarrow$ &  $\texttt{students} \;|\; \texttt{Jeff} \;|\; \texttt{geometry} \;|\; \texttt{trains} $ \\
   338 $V$ & $\rightarrow$ &  $\texttt{trains}$ 
   356 $V$ & $\rightarrow$ &  $\texttt{trains}$ 
   348 \begin{frame}[c]
   366 \begin{frame}[c]
   349 \frametitle{\begin{tabular}{c}CYK Algorithm\end{tabular}}
   367 \frametitle{\begin{tabular}{c}CYK Algorithm\end{tabular}}
   350 
   368 
   351 
   369 
   352 \begin{itemize}
   370 \begin{itemize}
       
   371 \item fastest possible algorithm for recognition problem
   353 \item runtime is \bl{$O(n^3)$}\bigskip
   372 \item runtime is \bl{$O(n^3)$}\bigskip
   354 \item grammars need to be transferred into CNF
   373 \item grammars need to be transferred into CNF
   355 \end{itemize}
   374 \end{itemize}
   356 
   375 
   357 \end{frame}}
   376 \end{frame}}
   358 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  
   377 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  
   359 
   378 
       
   379 
       
   380 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   381 \mode<presentation>{
       
   382 \begin{frame}[c]
       
   383 \frametitle{\begin{tabular}{c}Hierarchy of Languages\end{tabular}}
       
   384 
       
   385 Recall that languages are sets of strings.
       
   386 
       
   387 \begin{center}
       
   388 \begin{tikzpicture}
       
   389 [rect/.style={draw=black!50, top color=white,bottom color=black!20, rectangle, very thick, rounded corners}]
       
   390 
       
   391 \draw (0,0) node [rect, text depth=39mm, text width=68mm] {all languages};
       
   392 \draw (0,-0.4) node [rect, text depth=28.5mm, text width=64mm] {decidable languages};
       
   393 \draw (0,-0.85) node [rect, text depth=17mm] {context sensitive languages};
       
   394 \draw (0,-1.14) node [rect, text depth=9mm, text width=50mm] {context-free languages};
       
   395 \draw (0,-1.4) node [rect] {regular languages};
       
   396 \end{tikzpicture}
       
   397 
       
   398 \end{center}
       
   399 
       
   400 \end{frame}}
       
   401 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
       
   402 
       
   403 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   404 \mode<presentation>{
       
   405 \begin{frame}[c]
       
   406 \frametitle{\begin{tabular}{c}Context Sensitive Grms\end{tabular}}
       
   407 
       
   408 
       
   409 \begin{center}
       
   410 \bl{\begin{tabular}{lcl}
       
   411 $S$ & $\Rightarrow$ &  $bSAA\;|\; \epsilon$\\
       
   412 $A$ & $\Rightarrow$ & $a$\\
       
   413 $bA$ & $\Rightarrow$ & $Ab$\\
       
   414 \end{tabular}}
       
   415 \end{center}\pause
       
   416 
       
   417 \begin{center}
       
   418 \bl{$S \Rightarrow\ldots\Rightarrow^? "ababaa"$}
       
   419 \end{center}
       
   420 
       
   421 \end{frame}}
       
   422 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
       
   423 
       
   424 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   425 \mode<presentation>{
       
   426 \begin{frame}[c]
       
   427 \frametitle{\begin{tabular}{c}Parse Trees\end{tabular}}
       
   428 \begin{center}
       
   429 \begin{tabular}{@{}lcl@{}}
       
   430 \textit{Stmt} & $\rightarrow$ &  $\texttt{skip}$\\
       
   431               & $|$ & \textit{Id}\;\texttt{:=}\;\textit{AExp}\\
       
   432               & $|$ & \texttt{if}\; \textit{BExp} \;\texttt{then}\; \textit{Block} \;\texttt{else}\; \textit{Block}\\
       
   433               & $|$ & \texttt{while}\; \textit{BExp} \;\texttt{do}\; \textit{Block}\\
       
   434               & $|$ & \texttt{read}\;\textit{Id}\\
       
   435               & $|$ & \texttt{write}\;\textit{Id}\\
       
   436               & $|$ & \texttt{write}\;\textit{String}\medskip\\
       
   437 \textit{Stmts} & $\rightarrow$ &  \textit{Stmt} \;\texttt{;}\; \textit{Stmts}\\
       
   438               & $|$ & \textit{Stmt}\medskip\\
       
   439 \textit{Block} & $\rightarrow$ &  \texttt{\{}\,\textit{Stmts}\,\texttt{\}}\\
       
   440                 & $|$ & \textit{Stmt}\medskip\\
       
   441 \textit{AExp} & $\rightarrow$ & \ldots\\
       
   442 \textit{BExp} & $\rightarrow$ & \ldots\\
       
   443 \end{tabular}
       
   444 \end{center}
       
   445 \end{frame}}
       
   446 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
   360 
   447 
   361 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   448 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   362 \mode<presentation>{
   449 \mode<presentation>{
   363 \begin{frame}[c]
   450 \begin{frame}[c]
   364 \frametitle{\begin{tabular}{c}Parse Trees\end{tabular}}
   451 \frametitle{\begin{tabular}{c}Parse Trees\end{tabular}}