diff -r 2e9134d25a2b -r ea8b94d4755e slides/slides07.tex --- a/slides/slides07.tex Mon Nov 11 15:26:20 2013 +0000 +++ b/slides/slides07.tex Mon Nov 11 23:24:38 2013 +0000 @@ -152,10 +152,32 @@ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \mode{ +\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 left-recursive (and ambiguous).\medskip\\ +A problem for \alert{recursive descent parsers} (e.g.~parser combinators). +\bigskip\pause + +\end{frame}} +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +\mode{ \begin{frame}[t] \frametitle{\begin{tabular}{c}Numbers\end{tabular}} -A grammar for numbers: + \begin{center} \bl{\begin{tabular}{lcl} @@ -163,15 +185,84 @@ \end{tabular}} \end{center} -Unfortunately left-recursive (and ambiguous).\medskip\\ -A problem for \alert{recursive descent parsers} (e.g.~parser combinators). -\bigskip\pause +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}} +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -A non-left-recursive grammar for numbers +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +\mode{ +\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 how many precedence levels, say\medskip\\ +\hspace{5mm}highest for \bl{$()$}, medium for \bl{*}, lowest for \bl{+} \begin{center} \bl{\begin{tabular}{lcl} -$N$ & $\rightarrow$ & $0 \cdot N \;|\;1 \cdot N \;|\ldots\;|\; 0 \;|\; 1 \;|\; \ldots \;|\; 9$\\ +$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{ +\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} @@ -196,10 +287,42 @@ \bl{$A \rightarrow B\cdot C$} \end{center} +No rule can contain \bl{$\epsilon$}. + +\end{frame}} +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +\mode{ +\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$ +\end{tabular}} +\end{tabular} +\end{center} \end{frame}} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + + + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \mode{ \begin{frame}[c]