slides/slides07.tex
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Tue, 12 Nov 2013 22:54:41 +0000
changeset 187 9471a0325773
parent 186 fab34204d08e
child 188 12ef150273ce
permissions -rw-r--r--
added slides

\documentclass[dvipsnames,14pt,t]{beamer}
\usepackage{beamerthemeplaincu}
%\usepackage[T1]{fontenc}
%\usepackage[latin1]{inputenc}
\usepackage{mathpartir}
\usepackage[absolute,overlay]{textpos}
\usepackage{ifthen}
\usepackage{tikz}
\usepackage{pgf}
\usepackage{calc} 
\usepackage{ulem}
\usepackage{courier}
\usepackage{listings}
\renewcommand{\uline}[1]{#1}
\usetikzlibrary{arrows}
\usetikzlibrary{automata}
\usetikzlibrary{shapes}
\usetikzlibrary{shadows}
\usetikzlibrary{positioning}
\usetikzlibrary{calc}
\usetikzlibrary{plotmarks}
\usepackage{graphicx} 

\definecolor{javared}{rgb}{0.6,0,0} % for strings
\definecolor{javagreen}{rgb}{0.25,0.5,0.35} % comments
\definecolor{javapurple}{rgb}{0.5,0,0.35} % keywords
\definecolor{javadocblue}{rgb}{0.25,0.35,0.75} % javadoc

\lstset{language=Java,
	basicstyle=\ttfamily,
	keywordstyle=\color{javapurple}\bfseries,
	stringstyle=\color{javagreen},
	commentstyle=\color{javagreen},
	morecomment=[s][\color{javadocblue}]{/**}{*/},
	numbers=left,
	numberstyle=\tiny\color{black},
	stepnumber=1,
	numbersep=10pt,
	tabsize=2,
	showspaces=false,
	showstringspaces=false}

\lstdefinelanguage{scala}{
  morekeywords={abstract,case,catch,class,def,%
    do,else,extends,false,final,finally,%
    for,if,implicit,import,match,mixin,%
    new,null,object,override,package,%
    private,protected,requires,return,sealed,%
    super,this,throw,trait,true,try,%
    type,val,var,while,with,yield},
  otherkeywords={=>,<-,<\%,<:,>:,\#,@},
  sensitive=true,
  morecomment=[l]{//},
  morecomment=[n]{/*}{*/},
  morestring=[b]",
  morestring=[b]',
  morestring=[b]"""
}

\lstset{language=Scala,
	basicstyle=\ttfamily,
	keywordstyle=\color{javapurple}\bfseries,
	stringstyle=\color{javagreen},
	commentstyle=\color{javagreen},
	morecomment=[s][\color{javadocblue}]{/**}{*/},
	numbers=left,
	numberstyle=\tiny\color{black},
	stepnumber=1,
	numbersep=10pt,
	tabsize=2,
	showspaces=false,
	showstringspaces=false}

\setmonofont{Consolas}

% beamer stuff 
\renewcommand{\slidecaption}{AFL 07, King's College London, 13.~November 2013}
\newcommand{\bl}[1]{\textcolor{blue}{#1}}       
\newcommand{\dn}{\stackrel{\mbox{\scriptsize def}}{=}}% for definitions

\begin{document}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\mode<presentation>{
\begin{frame}<1>[t]
\frametitle{%
  \begin{tabular}{@ {}c@ {}}
  \\[-3mm]
  \LARGE Automata and \\[-2mm] 
  \LARGE Formal Languages (7)\\[3mm] 
  \end{tabular}}

  \normalsize
  \begin{center}
  \begin{tabular}{ll}
  Email:  & christian.urban at kcl.ac.uk\\
  Office: & S1.27 (1st floor Strand Building)\\
  Slides: & KEATS (also home work is there)\\
  \end{tabular}
  \end{center}


\end{frame}}
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     


\newcommand{\qq}{\mbox{\texttt{"}}}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\mode<presentation>{
\begin{frame}[c]
\frametitle{\begin{tabular}{c}Two Weeks Ago: CFGs\end{tabular}}

A \alert{context-free} grammar (CFG) \bl{$G$} consists of:

\begin{itemize}
\item a finite set of nonterminal symbols (upper case)
\item a finite terminal symbols or tokens (lower case)
\item a start symbol (which must be a nonterminal)
\item a set of rules
\begin{center}
\bl{$A \rightarrow \text{rhs}_1 | \text{rhs}_2 | \ldots$}
\end{center}

where \bl{rhs} are sequences involving terminals and nonterminals (can also be empty).\medskip\pause

\end{itemize}

\end{frame}}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\mode<presentation>{
\begin{frame}[c]
\frametitle{\begin{tabular}{c}Hierarchy of Languages\end{tabular}}

Recall that languages are sets of strings.

\begin{center}
\begin{tikzpicture}
[rect/.style={draw=black!50, top color=white,bottom color=black!20, rectangle, very thick, rounded corners}]

\draw (0,0) node [rect, text depth=39mm, text width=68mm] {all languages};
\draw (0,-0.4) node [rect, text depth=28.5mm, text width=64mm] {decidable languages};
\draw (0,-0.85) node [rect, text depth=17mm] {context sensitive languages};
\draw (0,-1.14) node [rect, text depth=9mm, text width=50mm] {context-free languages};
\draw (0,-1.4) node [rect] {regular languages};
\end{tikzpicture}

\end{center}

\end{frame}}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\mode<presentation>{
\begin{frame}[c]
\frametitle{\begin{tabular}{c}Arithmetic Expressions\end{tabular}}

A grammar for arithmetic expressions and numbers:

\begin{center}
\bl{\begin{tabular}{lcl}
$E$ & $\rightarrow$ &  $E \cdot + \cdot E \;|\;E \cdot * \cdot E \;|\;( \cdot E \cdot ) \;|\;N$ \\
$N$ & $\rightarrow$ & $N \cdot N \;|\; 0 \;|\; 1 \;|\: \ldots \;|\; 9$ 
\end{tabular}}
\end{center}

Unfortunately it is left-recursive (and ambiguous).\medskip\\
A problem for \alert{recursive descent parsers} (e.g.~parser combinators).
\bigskip\pause

\end{frame}}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\mode<presentation>{
\begin{frame}[t]
\frametitle{\begin{tabular}{c}Numbers\end{tabular}}



\begin{center}
\bl{\begin{tabular}{lcl}
$N$ & $\rightarrow$ &  $N \cdot N \;|\; 0 \;|\; 1 \;|\; \ldots \;|\; 9$\\
\end{tabular}}
\end{center}

A non-left-recursive, non-ambiguous grammar for numbers:

\begin{center}
\bl{\begin{tabular}{lcl}
$N$ & $\rightarrow$ &  $0 \cdot N \;|\;1 \cdot N \;|\;\ldots\;|\; 0 \;|\; 1 \;|\; \ldots \;|\; 9$\\
\end{tabular}}
\end{center}


\end{frame}}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\mode<presentation>{
\begin{frame}[c]
\frametitle{\begin{tabular}{c}Operator Precedences\end{tabular}}


To disambiguate

\begin{center}
\bl{\begin{tabular}{lcl}
$E$ & $\rightarrow$ &  $E \cdot + \cdot E \;|\;E \cdot * \cdot E \;|\;( \cdot E \cdot ) \;|\;N$ \\
\end{tabular}}
\end{center}

Decide on how many precedence levels, say\medskip\\
\hspace{5mm}highest for \bl{$()$}, medium for \bl{*}, lowest for \bl{+}

\begin{center}
\bl{\begin{tabular}{lcl}
$E_{low}$ & $\rightarrow$ & $E_{med} \cdot + \cdot E_{low} \;|\; E_{med}$ \\
$E_{med}$ & $\rightarrow$ & $E_{hi} \cdot * \cdot E_{med} \;|\; E_{hi}$\\
$E_{hi}$ & $\rightarrow$ &  $( \cdot E_{low} \cdot ) \;|\;N$ \\
\end{tabular}}
\end{center}\pause

\small What happens with \bl{$1 + 3  + 4$}?
\end{frame}}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\mode<presentation>{
\begin{frame}[c]
\frametitle{\begin{tabular}{c}Removing Left-Recursion\end{tabular}}

The rule for numbers is directly left-recursive:

\begin{center}
\bl{\begin{tabular}{lcl}
$N$ & $\rightarrow$ & $N \cdot N \;|\; 0 \;|\; 1\;\;\;\;(\ldots)$ 
\end{tabular}}
\end{center}

Translate

\begin{center}
\begin{tabular}{ccc}
\bl{\begin{tabular}{lcl}
$N$ & $\rightarrow$ & $N \cdot \alpha$\\
 &  $\;|\;$ & $\beta$\\
 \\ 
\end{tabular}} 
& {\Large$\Rightarrow$} &
\bl{\begin{tabular}{lcl}
$N$ & $\rightarrow$ & $\beta \cdot N'$\\
$N'$ & $\rightarrow$ & $\alpha \cdot N'$\\
 &  $\;|\;$ & $\epsilon$ 
\end{tabular}}
\end{tabular}
\end{center}\pause

Which means

\begin{center}
\bl{\begin{tabular}{lcl}
$N$ & $\rightarrow$ & $0 \cdot N' \;|\; 1 \cdot N'$\\
$N'$ & $\rightarrow$ & $N \cdot N' \;|\; \epsilon$\\
\end{tabular}}
\end{center}


\end{frame}}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\mode<presentation>{
\begin{frame}[c]
\frametitle{\begin{tabular}{c}Chomsky Normal Form\end{tabular}}

All rules must be of the form

\begin{center}
\bl{$A \rightarrow a$}
\end{center}

or

\begin{center}
\bl{$A \rightarrow B\cdot C$}
\end{center}

No rule can contain \bl{$\epsilon$}.

\end{frame}}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\mode<presentation>{
\begin{frame}[c]
\frametitle{\begin{tabular}{c}$\epsilon$-Removal\end{tabular}}

\begin{enumerate}
\item If \bl{$A\rightarrow \alpha \cdot B \cdot \beta$} and \bl{$B \rightarrow \epsilon$} are in the grammar,
then add \bl{$A\rightarrow \alpha \cdot \beta$} (iterate if necessary).
\item Throw out all \bl{$B \rightarrow \epsilon$}.
\end{enumerate}

\small
\begin{center}
\begin{tabular}{ccc}
\bl{\begin{tabular}{l@{\hspace{1mm}}c@{\hspace{1mm}}l}
$N$ & $\rightarrow$ & $0 \cdot N' \;|\; 1\cdot N'$\\
$N'$ & $\rightarrow$ & $N \cdot N'\;|\;\epsilon$\\
\\ 
\\
\\
\\
\\
\end{tabular}} &
\bl{\begin{tabular}{l@{\hspace{1mm}}c@{\hspace{1mm}}l}
\\
$N$ & $\rightarrow$ & $0 \cdot N' \;|\; 1\cdot N'\;|\;0\;|\;1$\\
$N'$ & $\rightarrow$ & $N \cdot N'\;|\;N\;|\;\epsilon$\\
\\
$N$ & $\rightarrow$ & $0 \cdot N' \;|\; 1\cdot N'\;|\;0\;|\;1$\\
$N'$ & $\rightarrow$ & $N \cdot N'\;|\;N$\\
\end{tabular}}
\end{tabular}
\end{center}

\pause\normalsize
\begin{center}
\bl{\begin{tabular}{l@{\hspace{1mm}}c@{\hspace{1mm}}l}
$N$ & $\rightarrow$ & $0 \cdot N\;|\; 1\cdot N\;|\;0\;|\;1$\\
\end{tabular}}

\end{center}
\end{frame}}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\mode<presentation>{
\begin{frame}[c]
\frametitle{\begin{tabular}{c}CYK Algorithm\end{tabular}}

If grammar is in Chomsky normalform \ldots

\begin{center}
\bl{\begin{tabular}{@ {}lcl@ {}}
$S$ & $\rightarrow$ &  $N\cdot P$ \\
$P$ & $\rightarrow$ &  $V\cdot N$ \\
$N$ & $\rightarrow$ &  $N\cdot N$ \\
$N$ & $\rightarrow$ &  $\texttt{students} \;|\; \texttt{Jeff} \;|\; \texttt{geometry} \;|\; \texttt{trains} $ \\
$V$ & $\rightarrow$ &  $\texttt{trains}$ 
\end{tabular}}
\end{center}

\bl{\texttt{Jeff trains geometry students}}

\end{frame}}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\mode<presentation>{
\begin{frame}[c]
\frametitle{\begin{tabular}{c}CYK Algorithm\end{tabular}}


\begin{itemize}
\item fastest possible algorithm for recognition problem
\item runtime is \bl{$O(n^3)$}\bigskip
\item grammars need to be transferred into CNF
\end{itemize}

\end{frame}}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\mode<presentation>{
\begin{frame}[c]
\frametitle{\begin{tabular}{c}Hierarchy of Languages\end{tabular}}

Recall that languages are sets of strings.

\begin{center}
\begin{tikzpicture}
[rect/.style={draw=black!50, top color=white,bottom color=black!20, rectangle, very thick, rounded corners}]

\draw (0,0) node [rect, text depth=39mm, text width=68mm] {all languages};
\draw (0,-0.4) node [rect, text depth=28.5mm, text width=64mm] {decidable languages};
\draw (0,-0.85) node [rect, text depth=17mm] {context sensitive languages};
\draw (0,-1.14) node [rect, text depth=9mm, text width=50mm] {context-free languages};
\draw (0,-1.4) node [rect] {regular languages};
\end{tikzpicture}

\end{center}

\end{frame}}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\mode<presentation>{
\begin{frame}[c]
\frametitle{\begin{tabular}{c}Context Sensitive Grms\end{tabular}}


\begin{center}
\bl{\begin{tabular}{lcl}
$S$ & $\Rightarrow$ &  $bSAA\;|\; \epsilon$\\
$A$ & $\Rightarrow$ & $a$\\
$bA$ & $\Rightarrow$ & $Ab$\\
\end{tabular}}
\end{center}\pause

\begin{center}
\bl{$S \Rightarrow\ldots\Rightarrow^? "ababaa"$}
\end{center}

\end{frame}}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\mode<presentation>{
\begin{frame}[c]
\frametitle{\begin{tabular}{c}Parse Trees\end{tabular}}
\begin{center}
\begin{tabular}{@{}lcl@{}}
\textit{Stmt} & $\rightarrow$ &  $\texttt{skip}$\\
              & $|$ & \textit{Id}\;\texttt{:=}\;\textit{AExp}\\
              & $|$ & \texttt{if}\; \textit{BExp} \;\texttt{then}\; \textit{Block} \;\texttt{else}\; \textit{Block}\\
              & $|$ & \texttt{while}\; \textit{BExp} \;\texttt{do}\; \textit{Block}\\
              & $|$ & \texttt{read}\;\textit{Id}\\
              & $|$ & \texttt{write}\;\textit{Id}\\
              & $|$ & \texttt{write}\;\textit{String}\medskip\\
\textit{Stmts} & $\rightarrow$ &  \textit{Stmt} \;\texttt{;}\; \textit{Stmts}\\
              & $|$ & \textit{Stmt}\medskip\\
\textit{Block} & $\rightarrow$ &  \texttt{\{}\,\textit{Stmts}\,\texttt{\}}\\
                & $|$ & \textit{Stmt}\medskip\\
\textit{AExp} & $\rightarrow$ & \ldots\\
\textit{BExp} & $\rightarrow$ & \ldots\\
\end{tabular}
\end{center}
\end{frame}}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\mode<presentation>{
\begin{frame}[c]
\frametitle{\begin{tabular}{c}Parse Trees\end{tabular}}

\begin{center}
\bl{\begin{tabular}{lcl}
$E$ & $\rightarrow$ &  $F \;|\; F \cdot * \cdot F$\\
$F$ & $\rightarrow$ & $T \;|\; T \cdot + \cdot T \;|\; T \cdot - \cdot T$\\
$T$ & $\rightarrow$ & $num\_token \;|\; ( \cdot E \cdot )$\\
\end{tabular}}
\end{center}

\begin{center}
\begin{tikzpicture}[level distance=8mm, blue]
  \node {$E$}
    child {node {$F$} 
     child {node {$T$} 
                 child {node {(\,$E$\,)}
                            child {node{$F$ *{} $F$}
                                  child {node {$T$} child {node {2}}}
                                  child {node {$T$} child {node {3}}} 
                               }
                          }
              }
     child {node {+}}
     child {node {$T$}
       child {node {(\,$E$\,)} 
       child {node {$F$}
       child {node {$T$ +{} $T$}
                    child {node {3}}
                    child {node {4}} 
                 }
                 }}
    }};
\end{tikzpicture}
\end{center}

\begin{textblock}{5}(1, 6.5)
\bl{\texttt{(2*3)+(3+4)}}
\end{textblock}

\end{frame}}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\mode<presentation>{
\begin{frame}[c]
\frametitle{\begin{tabular}{c}Ambiguous Grammars\end{tabular}}

A CFG is \alert{ambiguous} if there is a string that has at least parse trees.


\begin{center}
\bl{\begin{tabular}{lcl}
$E$ & $\rightarrow$ &  $num\_token$ \\
$E$ & $\rightarrow$ &  $E \cdot + \cdot E$ \\
$E$ & $\rightarrow$ &  $E \cdot - \cdot E$ \\
$E$ & $\rightarrow$ &  $E \cdot * \cdot E$ \\
$E$ & $\rightarrow$ &  $( \cdot E \cdot )$ 
\end{tabular}}
\end{center}

\bl{\texttt{1 + 2 * 3 + 4}}

\end{frame}}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\mode<presentation>{
\begin{frame}[c]
\frametitle{\begin{tabular}{c}Dangling Else\end{tabular}}

Another ambiguous grammar:\bigskip

\begin{center}
\bl{\begin{tabular}{lcl}
$E$ & $\rightarrow$ &  if $E$ then $E$\\
 & $|$ &  if $E$ then $E$ else $E$ \\
 & $|$ &  id 
\end{tabular}}
\end{center}\bigskip

\bl{\texttt{if a then if x then y else c}}

\end{frame}}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\mode<presentation>{
\begin{frame}[c]
\frametitle{\begin{tabular}{c}A CFG Derivation\end{tabular}}

\begin{enumerate}
\item Begin with a string with only the start symbol \bl{$S$}\bigskip
\item Replace any non-terminal \bl{$X$} in the string by the
right-hand side of some production \bl{$X \rightarrow \text{rhs}$}\bigskip
\item Repeat 2 until there are no non-terminals
\end{enumerate}

\begin{center}
\bl{$S \rightarrow \ldots \rightarrow \ldots  \rightarrow \ldots  \rightarrow \ldots $}
\end{center}

\end{frame}}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   


\end{document}

%%% Local Variables:  
%%% mode: latex
%%% TeX-master: t
%%% End: