slides/slides06.tex
changeset 169 57df3d7b4a25
parent 168 e60c4a9ba340
child 170 fa187fa5b642
equal deleted inserted replaced
168:e60c4a9ba340 169:57df3d7b4a25
    80 % beamer stuff 
    80 % beamer stuff 
    81 \renewcommand{\slidecaption}{AFL 06, King's College London, 30.~October 2013}
    81 \renewcommand{\slidecaption}{AFL 06, King's College London, 30.~October 2013}
    82 \newcommand{\bl}[1]{\textcolor{blue}{#1}}       
    82 \newcommand{\bl}[1]{\textcolor{blue}{#1}}       
    83 \newcommand{\dn}{\stackrel{\mbox{\scriptsize def}}{=}}% for definitions
    83 \newcommand{\dn}{\stackrel{\mbox{\scriptsize def}}{=}}% for definitions
    84 
    84 
    85 % The data files, written on the first run.
    85 
    86 \begin{filecontents}{re-python.data}
    86 \begin{filecontents}{s-grammar1.data}
    87 1 0.029
    87 1 0.01152
    88 5 0.029
    88 51 0.07973
    89 10 0.029
    89 101 0.09726
    90 15 0.032
    90 151 0.09320
    91 16 0.042
    91 201 0.10010
    92 17 0.042
    92 251 0.16997
    93 18 0.055
    93 301 0.26662
    94 19 0.084
    94 351 0.46118
    95 20 0.136
    95 401 0.62516
    96 21 0.248
    96 451 0.87247
    97 22 0.464
    97 501 1.16334
    98 23 0.899
    98 551 1.71152
    99 24 1.773
    99 601 2.10958
   100 25 3.505
   100 651 2.44360
   101 26 6.993
   101 701 2.98488
   102 27 14.503
   102 751 3.50326
   103 28 29.307
   103 801 4.11036
   104 #29 58.886
   104 851 4.93394
       
   105 901 5.77465
       
   106 951 7.39123
   105 \end{filecontents}
   107 \end{filecontents}
   106 
   108 
   107 \begin{filecontents}{re1.data}
   109 \begin{filecontents}{s-grammar2.data}
   108 1 0.00179
   110 1 0.01280
   109 2 0.00011
   111 2 0.00064
   110 3 0.00014
   112 3 0.00173
   111 4 0.00026
   113 4 0.00355
   112 5 0.00050
   114 5 0.00965
   113 6 0.00095
   115 6 0.02674
   114 7 0.00190
   116 7 0.06953
   115 8 0.00287
   117 8 0.11166
   116 9 0.00779
   118 9 0.18707
   117 10 0.01399
   119 10 0.09189
   118 11 0.01894
   120 11 0.12724
   119 12 0.03666
   121 12 0.24337
   120 13 0.07994
   122 13 0.59304
   121 14 0.08944
   123 14 1.53594
   122 15 0.02377
   124 15 4.01195
   123 16 0.07392
   125 16 10.73582
   124 17 0.22798
   126 17 29.51587
   125 18 0.65310
   127 #18 73.14163
   126 19 2.11360
       
   127 20 6.31606
       
   128 21 21.46013
       
   129 \end{filecontents}
   128 \end{filecontents}
   130 
   129 
   131 \begin{filecontents}{re2.data}
   130 
   132 1  0.00240
       
   133 2  0.00013
       
   134 3  0.00020
       
   135 4  0.00030
       
   136 5  0.00049
       
   137 6  0.00083
       
   138 7  0.00146
       
   139 8  0.00228
       
   140 9  0.00351
       
   141 10  0.00640
       
   142 11  0.01217
       
   143 12  0.02565
       
   144 13  0.01382
       
   145 14  0.02423
       
   146 15  0.05065 
       
   147 16  0.06522
       
   148 17  0.02140
       
   149 18  0.05176
       
   150 19  0.18254
       
   151 20  0.51898
       
   152 21  1.39631
       
   153 22  2.69501
       
   154 23  8.07952
       
   155 \end{filecontents}
       
   156 
       
   157 \begin{filecontents}{re-internal.data}
       
   158 1 0.00069
       
   159 301 0.00700
       
   160 601 0.00297
       
   161 901 0.00470
       
   162 1201 0.01301
       
   163 1501 0.01175
       
   164 1801 0.01761
       
   165 2101 0.01787
       
   166 2401 0.02717
       
   167 2701 0.03932
       
   168 3001 0.03470
       
   169 3301 0.04349
       
   170 3601 0.05411
       
   171 3901 0.06181
       
   172 4201 0.07119
       
   173 4501 0.08578
       
   174 \end{filecontents}
       
   175 
       
   176 \begin{filecontents}{re3.data}
       
   177 1 0.001605
       
   178 501 0.131066
       
   179 1001 0.057885
       
   180 1501 0.136875
       
   181 2001 0.176238
       
   182 2501 0.254363
       
   183 3001 0.37262
       
   184 3501 0.500946
       
   185 4001 0.638384
       
   186 4501 0.816605
       
   187 5001 1.00491
       
   188 5501 1.232505
       
   189 6001 1.525672
       
   190 6501 1.757502
       
   191 7001 2.092784
       
   192 7501 2.429224
       
   193 8001 2.803037
       
   194 8501 3.463045
       
   195 9001 3.609
       
   196 9501 4.081504
       
   197 10001 4.54569
       
   198 \end{filecontents}
       
   199 \begin{document}
   131 \begin{document}
   200 
   132 
   201 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   133 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   202 \mode<presentation>{
   134 \mode<presentation>{
   203 \begin{frame}<1>[t]
   135 \begin{frame}<1>[t]
   237 \item a set of rules
   169 \item a set of rules
   238 \begin{center}
   170 \begin{center}
   239 \bl{$A \rightarrow \text{rhs}$}
   171 \bl{$A \rightarrow \text{rhs}$}
   240 \end{center}
   172 \end{center}
   241 
   173 
   242 where \bl{rhs} are sequences involving terminals and nonterminals.\medskip\pause
   174 where \bl{rhs} are sequences involving terminals and nonterminals,
   243 
   175 including the empty sequence \bl{$\epsilon$}.\medskip\pause
   244 We can also allow rules
   176 
       
   177 We also allow rules
   245 \begin{center}
   178 \begin{center}
   246 \bl{$A \rightarrow \text{rhs}_1 | \text{rhs}_2 | \ldots$}
   179 \bl{$A \rightarrow \text{rhs}_1 | \text{rhs}_2 | \ldots$}
   247 \end{center}
   180 \end{center}
   248 \end{itemize}
   181 \end{itemize}
   249 
   182 
   296 \bl{\texttt{1 + 2 * 3 + 4}}
   229 \bl{\texttt{1 + 2 * 3 + 4}}
   297 
   230 
   298 \end{frame}}
   231 \end{frame}}
   299 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
   232 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
   300 
   233 
       
   234 
       
   235 
       
   236 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   237 \mode<presentation>{
       
   238 \begin{frame}[c]
       
   239 \frametitle{\begin{tabular}{c}A CFG Derivation\end{tabular}}
       
   240 
       
   241 \begin{enumerate}
       
   242 \item Begin with a string containing only the start symbol, say \bl{$S$}\bigskip
       
   243 \item Replace any nonterminal \bl{$X$} in the string by the
       
   244 right-hand side of some production \bl{$X \rightarrow \text{rhs}$}\bigskip
       
   245 \item Repeat 2 until there are no nonterminals
       
   246 \end{enumerate}
       
   247 
       
   248 \begin{center}
       
   249 \bl{$S \rightarrow \ldots \rightarrow \ldots  \rightarrow \ldots  \rightarrow \ldots $}
       
   250 \end{center}
       
   251 
       
   252 \end{frame}}
       
   253 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
       
   254   
       
   255 
       
   256 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   257 \mode<presentation>{
       
   258 \begin{frame}[c]
       
   259 \frametitle{\begin{tabular}{c}Example Derivation\end{tabular}}
       
   260 
       
   261 \begin{center}
       
   262 \bl{\begin{tabular}{lcl}
       
   263 $S$ & $\rightarrow$ &  $\epsilon \;|\; a\cdot S\cdot a \;|\;b\cdot S\cdot b$ \\
       
   264 \end{tabular}}
       
   265 \end{center}\bigskip
       
   266 
       
   267 
       
   268 \begin{center}
       
   269 \begin{tabular}{lcl}
       
   270 \bl{$S$} & \bl{$\rightarrow$} & \bl{$aSa$}\\
       
   271               & \bl{$\rightarrow$} & \bl{$abSba$}\\
       
   272               & \bl{$\rightarrow$} & \bl{$abaSaba$}\\
       
   273               & \bl{$\rightarrow$} & \bl{$abaaba$}\\
       
   274 
       
   275  
       
   276 \end{tabular}
       
   277 \end{center}
       
   278 
       
   279 \end{frame}}
       
   280 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
       
   281 
       
   282 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   283 \mode<presentation>{
       
   284 \begin{frame}[c]
       
   285 \frametitle{\begin{tabular}{c}Example Derivation\end{tabular}}
       
   286 
       
   287 \begin{center}
       
   288 \bl{\begin{tabular}{lcl}
       
   289 $E$ & $\rightarrow$ &  $num\_token$ \\
       
   290 $E$ & $\rightarrow$ &  $E \cdot + \cdot E$ \\
       
   291 $E$ & $\rightarrow$ &  $E \cdot - \cdot E$ \\
       
   292 $E$ & $\rightarrow$ &  $E \cdot * \cdot E$ \\
       
   293 $E$ & $\rightarrow$ &  $( \cdot E \cdot )$ 
       
   294 \end{tabular}}
       
   295 \end{center}\bigskip
       
   296 
       
   297 
       
   298 \begin{center}
       
   299 \begin{tabular}{@{}c@{}c@{}}
       
   300 \begin{tabular}{l@{\hspace{1mm}}l@{\hspace{1mm}}l}
       
   301 \bl{$E$} & \bl{$\rightarrow$} & \bl{$E*E$}\\
       
   302               & \bl{$\rightarrow$} & \bl{$E+E*E$}\\
       
   303               & \bl{$\rightarrow$} & \bl{$E+E*E+E$}\\
       
   304               & \bl{$\rightarrow^+$} & \bl{$1+2*3+4$}\\
       
   305 \end{tabular} &\pause
       
   306 \begin{tabular}{l@{\hspace{1mm}}l@{\hspace{1mm}}l}
       
   307 \bl{$E$} & \bl{$\rightarrow$} & \bl{$E+E$}\\
       
   308               & \bl{$\rightarrow$} & \bl{$E+E+E$}\\
       
   309               & \bl{$\rightarrow$} & \bl{$E+E*E+E$}\\
       
   310               & \bl{$\rightarrow^+$} & \bl{$1+2*3+4$}\\
       
   311 \end{tabular}
       
   312 \end{tabular}
       
   313 \end{center}
       
   314 
       
   315 \end{frame}}
       
   316 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
       
   317 
       
   318 
       
   319 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   320 \mode<presentation>{
       
   321 \begin{frame}[c]
       
   322 \frametitle{\begin{tabular}{c}Language of a CFG\end{tabular}}
       
   323 
       
   324 Let \bl{$G$} be a context-free grammar with start symbol \bl{$S$}. 
       
   325 Then the language \bl{$L(G)$} is:
       
   326 
       
   327 \begin{center}
       
   328 \bl{$\{c_1\ldots c_n \;|\; \forall i.\; c_i \in T \wedge S \rightarrow^* c_1\ldots c_n \}$}
       
   329 \end{center}\pause
       
   330 
       
   331 \begin{itemize}
       
   332 \item Terminals, because there are no rules for replacing them.
       
   333 \item Once generated, terminals are ``permanent''.
       
   334 \item Terminals ought to be tokens of the language\\
       
   335 (but can also be strings).
       
   336 \end{itemize}
       
   337 
       
   338 \end{frame}}
       
   339 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
   301 
   340 
   302 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   341 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   303 \mode<presentation>{
   342 \mode<presentation>{
   304 \begin{frame}[c]
   343 \begin{frame}[c]
   305 \frametitle{\begin{tabular}{c}Parse Trees\end{tabular}}
   344 \frametitle{\begin{tabular}{c}Parse Trees\end{tabular}}
   341 \bl{\texttt{(2*3)+(3+4)}}
   380 \bl{\texttt{(2*3)+(3+4)}}
   342 \end{textblock}
   381 \end{textblock}
   343 
   382 
   344 \end{frame}}
   383 \end{frame}}
   345 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
   384 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
   346 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   385 
   347 \mode<presentation>{
   386 
   348 \begin{frame}[c]
   387 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   349 \frametitle{\begin{tabular}{c}Ambiguous Grammars\end{tabular}}
   388 \mode<presentation>{
   350 
   389 \begin{frame}[c]
   351 A grammar is \alert{ambiguous} if there is a string that has at least parse trees.
   390 \frametitle{\begin{tabular}{c}Arithmetic Expressions\end{tabular}}
   352 
       
   353 
   391 
   354 \begin{center}
   392 \begin{center}
   355 \bl{\begin{tabular}{lcl}
   393 \bl{\begin{tabular}{lcl}
   356 $E$ & $\rightarrow$ &  $num\_token$ \\
   394 $E$ & $\rightarrow$ &  $num\_token$ \\
   357 $E$ & $\rightarrow$ &  $E \cdot + \cdot E$ \\
   395 $E$ & $\rightarrow$ &  $E \cdot + \cdot E$ \\
   358 $E$ & $\rightarrow$ &  $E \cdot - \cdot E$ \\
   396 $E$ & $\rightarrow$ &  $E \cdot - \cdot E$ \\
   359 $E$ & $\rightarrow$ &  $E \cdot * \cdot E$ \\
   397 $E$ & $\rightarrow$ &  $E \cdot * \cdot E$ \\
   360 $E$ & $\rightarrow$ &  $( \cdot E \cdot )$ 
   398 $E$ & $\rightarrow$ &  $( \cdot E \cdot )$ 
   361 \end{tabular}}
   399 \end{tabular}}
       
   400 \end{center}\pause\bigskip
       
   401 
       
   402 A CFG is \alert{left-recursive} if it has a nonterminal \bl{$E$} such
       
   403 that \bl{$E \rightarrow^+ E\cdot \ldots$}
       
   404 
       
   405 \end{frame}}
       
   406 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
       
   407 
       
   408 
       
   409 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   410 \mode<presentation>{
       
   411 \begin{frame}[c]
       
   412 \frametitle{\begin{tabular}{c}Ambiguous Grammars\end{tabular}}
       
   413 
       
   414 A grammar is \alert{ambiguous} if there is a string that has at least two different parse trees.
       
   415 
       
   416 
       
   417 \begin{center}
       
   418 \bl{\begin{tabular}{lcl}
       
   419 $E$ & $\rightarrow$ &  $num\_token$ \\
       
   420 $E$ & $\rightarrow$ &  $E \cdot + \cdot E$ \\
       
   421 $E$ & $\rightarrow$ &  $E \cdot - \cdot E$ \\
       
   422 $E$ & $\rightarrow$ &  $E \cdot * \cdot E$ \\
       
   423 $E$ & $\rightarrow$ &  $( \cdot E \cdot )$ 
       
   424 \end{tabular}}
   362 \end{center}
   425 \end{center}
   363 
   426 
   364 \bl{\texttt{1 + 2 * 3 + 4}}
   427 \bl{\texttt{1 + 2 * 3 + 4}}
       
   428 
       
   429 \end{frame}}
       
   430 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
       
   431 
       
   432 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   433 \mode<presentation>{
       
   434 \begin{frame}[c]
       
   435 \frametitle{\begin{tabular}{c}Dangling Else\end{tabular}}
       
   436 
       
   437 Another ambiguous grammar:\bigskip
       
   438 
       
   439 \begin{center}
       
   440 \bl{\begin{tabular}{lcl}
       
   441 $E$ & $\rightarrow$ &  if $E$ then $E$\\
       
   442  & $|$ &  if $E$ then $E$ else $E$ \\
       
   443  & $|$ &  \ldots
       
   444 \end{tabular}}
       
   445 \end{center}\bigskip
       
   446 
       
   447 \bl{\texttt{if a then if x then y else c}}
       
   448 
       
   449 \end{frame}}
       
   450 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
       
   451 
       
   452 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   453 \mode<presentation>{
       
   454 \begin{frame}[c]
       
   455 \frametitle{\begin{tabular}{c}Parser Combinators\end{tabular}}
       
   456 
       
   457 Parser combinators: \bigskip
       
   458 
       
   459 \begin{minipage}{1.1\textwidth}
       
   460 \begin{center}
       
   461 \mbox{}\hspace{-12mm}\mbox{}$\underbrace{\text{list of tokens}}_{\text{input}}$ \bl{$\Rightarrow$} 
       
   462 $\underbrace{\text{set of (parsed input, unparsed input)}}_{\text{output}}$
       
   463 \end{center}
       
   464 \end{minipage}\bigskip
       
   465 
       
   466 \begin{itemize}
       
   467 \item sequencing
       
   468 \item alternative
       
   469 \item semantic action
       
   470 \end{itemize}
       
   471 
       
   472 \end{frame}}
       
   473 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
       
   474 
       
   475 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   476 \mode<presentation>{
       
   477 \begin{frame}[c]
       
   478 
       
   479 Alternative parser (code \bl{$p\;||\;q$})\bigskip
       
   480 
       
   481 \begin{itemize}
       
   482 \item apply \bl{$p$} and also \bl{$q$}; then combine the outputs
       
   483 \end{itemize}
       
   484 
       
   485 \begin{center}
       
   486 \large \bl{$p(\text{input}) \cup q(\text{input})$}
       
   487 \end{center}
       
   488 
       
   489 
       
   490 \end{frame}}
       
   491 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
       
   492 
       
   493 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   494 \mode<presentation>{
       
   495 \begin{frame}[c]
       
   496 
       
   497 Sequence parser (code \bl{$p\sim q$})\bigskip
       
   498 
       
   499 \begin{itemize}
       
   500 \item apply first \bl{$p$} producing a set of pairs
       
   501 \item then apply \bl{$q$} to the unparsed parts
       
   502 \item then combine the results:\\ \mbox{}\;\;((output$_1$, output$_2$), unparsed part)
       
   503 \end{itemize}
       
   504 
       
   505 \begin{center}
       
   506 \begin{tabular}{l}
       
   507 \large \bl{$\{((o_1, o_2), u_2) \;|\;$}\\[2mm] 
       
   508 \large\mbox{}\hspace{15mm} \bl{$(o_1, u_1) \in p(\text{input}) \wedge$}\\[2mm]
       
   509 \large\mbox{}\hspace{15mm} \bl{$(o_2, u_2) \in q(u_1)\}$}
       
   510 \end{tabular}
       
   511 \end{center}
       
   512 
       
   513 
       
   514 \end{frame}}
       
   515 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
       
   516 
       
   517 
       
   518 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   519 \mode<presentation>{
       
   520 \begin{frame}[c]
       
   521 
       
   522 Function parser (code \bl{$p \Rightarrow f$})\bigskip
       
   523 
       
   524 \begin{itemize}
       
   525 \item apply \bl{$p$} producing a set of pairs
       
   526 \item then apply the function \bl{$f$} to each first component
       
   527 \end{itemize}
       
   528 
       
   529 \begin{center}
       
   530 \begin{tabular}{l}
       
   531 \large \bl{$\{(f(o_1), u_1) \;|\; (o_1, u_1) \in p(\text{input})\}$}
       
   532 \end{tabular}
       
   533 \end{center}\bigskip\bigskip\pause
       
   534 
       
   535 \bl{$f$} is the semantic action (``what to do with the parsed input'')
       
   536 
       
   537 \end{frame}}
       
   538 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
       
   539 
       
   540 
       
   541 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   542 \mode<presentation>{
       
   543 \begin{frame}[c]
       
   544 \frametitle{\begin{tabular}{c}Semantic Actions\end{tabular}}
       
   545 
       
   546 Addition
       
   547 
       
   548 \begin{center}
       
   549 \bl{$T \sim + \sim E \Rightarrow \underbrace{f((x,y), z) \Rightarrow x + z}_{\text{semantic action}}$}
       
   550 \end{center}\pause
       
   551 
       
   552 Multiplication
       
   553 
       
   554 \begin{center}
       
   555 \bl{$F \sim * \sim T \Rightarrow f((x,y), z) \Rightarrow x * z$}
       
   556 \end{center}\pause
       
   557 
       
   558 Parenthesis
       
   559 
       
   560 \begin{center}
       
   561 \bl{$\text{(} \sim E \sim \text{)} \Rightarrow f((x,y), z) \Rightarrow y$}
       
   562 \end{center}
       
   563 
       
   564 \end{frame}}
       
   565 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
       
   566 
       
   567 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   568 \mode<presentation>{
       
   569 \begin{frame}[c]
       
   570 \frametitle{\begin{tabular}{c}Types of Parsers\end{tabular}}
       
   571 
       
   572 \begin{itemize}
       
   573 \item {\bf Sequencing}: if \bl{$p$} returns results of type \bl{$T$}, and \bl{$q$} results of type \bl{$S$},
       
   574 then \bl{$p \sim q$} returns results of type
       
   575 
       
   576 \begin{center}
       
   577 \bl{$T \times S$}
       
   578 \end{center}\pause
       
   579 
       
   580 \item {\bf Alternative}: if \bl{$p$} returns results of type \bl{$T$} then  \bl{$q$} \alert{must} also have results of type \bl{$T$},
       
   581 and \bl{$p \;||\; q$} returns results of type
       
   582 
       
   583 \begin{center}
       
   584 \bl{$T$}
       
   585 \end{center}\pause
       
   586 
       
   587 \item {\bf Semantic Action}: if \bl{$p$} returns results of type \bl{$T$} and \bl{$f$} is a function from
       
   588 \bl{$T$} to \bl{$S$}, then
       
   589 \bl{$p \Rightarrow f$} returns results of type
       
   590 
       
   591 \begin{center}
       
   592 \bl{$S$}
       
   593 \end{center}
       
   594 
       
   595 \end{itemize}
       
   596 
       
   597 \end{frame}}
       
   598 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  
       
   599 
       
   600 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   601 \mode<presentation>{
       
   602 \begin{frame}[c]
       
   603 \frametitle{\begin{tabular}{c}Input Types of Parsers\end{tabular}}
       
   604 
       
   605 \begin{itemize}
       
   606 \item input: \alert{string}
       
   607 \item output: set of (output\_type, \alert{string})
       
   608 \end{itemize}\bigskip\pause
       
   609 
       
   610 actually it can be any input type as long as it is a kind of sequence
       
   611 (for example a string)
       
   612 
       
   613 \end{frame}}
       
   614 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  
       
   615 
       
   616 
       
   617 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   618 \mode<presentation>{
       
   619 \begin{frame}[c]
       
   620 \frametitle{\begin{tabular}{c}Scannerless Parsers\end{tabular}}
       
   621 
       
   622 \begin{itemize}
       
   623 \item input: \alert{string}
       
   624 \item output: set of (output\_type, \alert{string})
       
   625 \end{itemize}\bigskip
       
   626 
       
   627 but lexers are better when whitespaces or comments need to be filtered out;
       
   628 then input is a sequence of tokens
       
   629 
       
   630 \end{frame}}
       
   631 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  
       
   632 
       
   633 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   634 \mode<presentation>{
       
   635 \begin{frame}[c]
       
   636 \frametitle{\begin{tabular}{c}Successful Parses\end{tabular}}
       
   637 
       
   638 \begin{itemize}
       
   639 \item input: string
       
   640 \item output: \alert{set of} (output\_type, string)
       
   641 \end{itemize}\bigskip
       
   642 
       
   643 a parse is successful whenever the input has been
       
   644 fully ``consumed'' (that is the second component is empty)
       
   645 
       
   646 
       
   647 \end{frame}}
       
   648 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  
       
   649 
       
   650 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   651 \mode<presentation>{
       
   652 \begin{frame}[c]
       
   653 \frametitle{Abstract Parsers}
       
   654 
       
   655 \mbox{\lstset{language=Scala}\fontsize{10}{12}\selectfont
       
   656 \texttt{\lstinputlisting{../progs/app7.scala}}}
       
   657 \end{frame}}
       
   658 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  
       
   659 
       
   660 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   661 \mode<presentation>{
       
   662 \begin{frame}[c]
       
   663 {\lstset{language=Scala}\fontsize{10}{12}\selectfont
       
   664 \texttt{\lstinputlisting{../progs/app8.scala}}}
       
   665 \end{frame}}
       
   666 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
       
   667 
       
   668 
       
   669 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   670 \mode<presentation>{
       
   671 \begin{frame}[c]
       
   672 \frametitle{\begin{tabular}{c}Two Grammars\end{tabular}}
       
   673 
       
   674 Which languages are recognised by the following two grammars?
       
   675 
       
   676 \begin{center}
       
   677 \bl{\begin{tabular}{lcl}
       
   678 $S$ & $\rightarrow$ &  $1 \cdot S \cdot S$\\
       
   679         & $|$ & $\epsilon$
       
   680 \end{tabular}}
       
   681 \end{center}\bigskip
       
   682 
       
   683 \begin{center}
       
   684 \bl{\begin{tabular}{lcl}
       
   685 $U$ & $\rightarrow$ &  $1 \cdot U$\\
       
   686         & $|$ & $\epsilon$
       
   687 \end{tabular}}
       
   688 \end{center}
       
   689 
       
   690 \end{frame}}
       
   691 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
       
   692 
       
   693 
       
   694 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   695 \mode<presentation>{
       
   696 \begin{frame}[t]
       
   697 \frametitle{\begin{tabular}{c}Ambiguous Grammars\end{tabular}}
       
   698 
       
   699 \mbox{}\\[-25mm]\mbox{}
       
   700 
       
   701 \begin{center}
       
   702 \begin{tikzpicture}[y=.2cm, x=.009cm]
       
   703  	%axis
       
   704 	\draw (0,0) -- coordinate (x axis mid) (1000,0);
       
   705     	\draw (0,0) -- coordinate (y axis mid) (0,30);
       
   706     	%ticks
       
   707     	\foreach \x in {0, 20, 100, 200,...,1000}
       
   708      		\draw (\x,1pt) -- (\x,-3pt)
       
   709 			node[anchor=north] {\small \x};
       
   710     	\foreach \y in {0,5,...,30}
       
   711      		\draw (1pt,\y) -- (-3pt,\y) 
       
   712      			node[anchor=east] {\small\y}; 
       
   713 	%labels      
       
   714 	\node[below=0.6cm] at (x axis mid) {\bl{1}s};
       
   715 	\node[rotate=90, left=1.2cm] at (y axis mid) {secs};
       
   716 
       
   717 	%plots
       
   718 	\draw[color=blue] plot[mark=*, mark options={fill=white}] 
       
   719 		file {s-grammar1.data};
       
   720          \only<2->{\draw[color=red] plot[mark=triangle*, mark options={fill=white} ] 
       
   721                   file {s-grammar2.data};}
       
   722 	%legend
       
   723 	\begin{scope}[shift={(400,20)}] 
       
   724 	\draw[color=blue] (0,0) -- 
       
   725 		plot[mark=*, mark options={fill=white}] (0.25,0) -- (0.5,0) 
       
   726 		node[right]{\small unambiguous};
       
   727 	\only<2->{\draw[yshift=\baselineskip, color=red] (0,0) -- 
       
   728                 plot[mark=triangle*, mark options={fill=white}] (0.25,0) -- (0.5,0)
       
   729                 node[right]{\small ambiguous};}  
       
   730 	\end{scope}
       
   731 \end{tikzpicture}
       
   732 \end{center}
       
   733 
       
   734 \end{frame}}
       
   735 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
       
   736 
       
   737 
       
   738 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   739 \mode<presentation>{
       
   740 \begin{frame}[c]
       
   741 \frametitle{\begin{tabular}{c}While-Language\end{tabular}}
       
   742 
       
   743 
       
   744 \begin{center}
       
   745 \bl{\begin{tabular}{@{}lcl@{}}
       
   746 $Stmt$ & $\rightarrow$ &  $\text{skip}$\\
       
   747               & $|$ & $Id := AExp$\\
       
   748               & $|$ & $\text{if}\; B\!Exp \;\text{then}\; Block \;\text{else}\; Block$\\
       
   749               & $|$ & $\text{while}\; B\!Exp \;\text{do}\; Block$\medskip\\
       
   750 $Stmts$ & $\rightarrow$ &  $Stmt \;\text{;}\; Stmts$\\
       
   751               & $|$ & $Stmt$\medskip\\
       
   752 $Block$ & $\rightarrow$ &  $\{ Stmts \}$\\
       
   753                 & $|$ & $Stmt$\medskip\\
       
   754 $AExp$ & $\rightarrow$ & \ldots\\
       
   755 $BExp$ & $\rightarrow$ & \ldots\\
       
   756 \end{tabular}}
       
   757 \end{center}
       
   758 
       
   759 
       
   760 \end{frame}}
       
   761 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
       
   762 
       
   763 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   764 \mode<presentation>{
       
   765 \begin{frame}[c]
       
   766 \frametitle{\begin{tabular}{c}An Interpreter\end{tabular}}
       
   767 
       
   768 \begin{center}
       
   769 \bl{\begin{tabular}{l}
       
   770 $\{$\\
       
   771 \;\;$x := 5 \text{;}$\\
       
   772 \;\;$y := x * 3\text{;}$\\
       
   773 \;\;$y := x * 4\text{;}$\\
       
   774 \;\;$x := u * 3$\\
       
   775 $\}$
       
   776 \end{tabular}}
       
   777 \end{center}
       
   778 
       
   779 \begin{itemize}
       
   780 \item the interpreter has to record the value of \bl{$x$} before assigning a value to \bl{$y$}\pause
       
   781 \item \bl{\text{eval}(stmt, env)}
       
   782 \end{itemize}
       
   783 
   365 
   784 
   366 \end{frame}}
   785 \end{frame}}
   367 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
   786 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
   368 
   787 
   369 
   788