|
| 1 | +\documentclass[10pt, oneside, letterpaper, titlepage]{article} |
| 2 | + |
| 3 | +\usepackage{amsmath} |
| 4 | +\usepackage[ampersand]{easylist} |
| 5 | + \ListProperties( |
| 6 | + Progressive*=5ex, |
| 7 | + Space=5pt, |
| 8 | + Space*=5pt, |
| 9 | + Style1*=\textbullet\ \ , |
| 10 | + Style2*=\begin{normalfont}\begin{bfseries}\textendash\end{bfseries}\end{normalfont} \ \ , |
| 11 | + Style3*=\textasteriskcentered\ \ , |
| 12 | + Style4*=\begin{normalfont}\begin{bfseries}\textperiodcentered\end{bfseries}\end{normalfont}\ \ , |
| 13 | + Style5*=\textbullet\ \ , |
| 14 | + Style6*=\begin{normalfont}\begin{bfseries}\textendash\end{bfseries}\end{normalfont}\ \ , |
| 15 | + Style7*=\textasteriskcentered\ \ , |
| 16 | + Style8*=\begin{normalfont}\begin{bfseries}\textperiodcentered\end{bfseries}\end{normalfont}\ \ , |
| 17 | + Hide1=1, |
| 18 | + Hide2=2, |
| 19 | + Hide3=3, |
| 20 | + Hide4=4, |
| 21 | + Hide5=5, |
| 22 | + Hide6=6, |
| 23 | + Hide7=7, |
| 24 | + Hide8=8 ) |
| 25 | +\usepackage[T1]{fontenc} |
| 26 | +\usepackage{forest} |
| 27 | +\usepackage{geometry} |
| 28 | + \geometry{margin=1.2in} |
| 29 | +\usepackage{graphicx} |
| 30 | + \graphicspath{ {img/} } |
| 31 | +\usepackage[colorlinks=true, linkcolor=blue]{hyperref} |
| 32 | +\usepackage{listings} |
| 33 | +\usepackage{lmodern} % Allows the use of symbols in font size 10; http://ctan.org/pkg/lm |
| 34 | +\usepackage{multirow} |
| 35 | +\usepackage{textcomp} % Allows the use of \textbullet with the font |
| 36 | +\usepackage{verbatim} |
| 37 | + |
| 38 | +\renewcommand{\arraystretch}{1.2} |
| 39 | +\renewcommand{\familydefault}{\sfdefault} |
| 40 | + |
| 41 | +\begin{document} |
| 42 | + |
| 43 | +\section{LL(1) Parsing} |
| 44 | +\subsection{Introduction} |
| 45 | +\begin{easylist} |
| 46 | + |
| 47 | +& \textbf{LL(1) parsing:} Top-down parsing algorithm which creates a left-to-right leftmost derivation requiring only 1 symbol of lookahead |
| 48 | + && Requires a grammar which is: |
| 49 | + &&& Unambiguous |
| 50 | + &&& Left-factored |
| 51 | + &&& Free of left-recursion |
| 52 | + && Only one possible production can exist given the next token |
| 53 | + |
| 54 | +& Grammar validity: |
| 55 | + && A grammar is LL(1) if and only if, for each pairing of a non-terminal symbol with a terminal symbol, only one possible production rule can be applied |
| 56 | + && A grammar is not LL(1) if and only if there exists at least one pairing of a non-terminal symbol and a terminal symbol which can expand to multiple production rules (e.g. $A \rightarrow bc; A \rightarrow bd$) |
| 57 | + |
| 58 | +\end{easylist} |
| 59 | +\subsection{Predictive Parsing Tables} |
| 60 | +\begin{easylist} |
| 61 | + |
| 62 | +& \textbf{Predictive parsing table:} Table which correlates the leftmost non-terminal symbol and the next terminal symbol with the only possible production |
| 63 | + && No cell can have more than one string as the expansion to the rule |
| 64 | + && Empty cells mean parsing errors |
| 65 | + && Uses lookahead to resolve conflicts between multiple possible production rule applications for the same symbol |
| 66 | + && Layout: See figure~\ref{tab:predictive-parsing-table-layout} |
| 67 | + |
| 68 | +\end{easylist} |
| 69 | +\begin{figure}[!htb] |
| 70 | + \caption{Predictive Parsing Table Layout} |
| 71 | + \label{tab:predictive-parsing-table-layout} |
| 72 | + \begin{center} |
| 73 | + \begin{tabular}{ r r | l l } |
| 74 | + & & \textbf{Terminal symbols} & \$ \\ |
| 75 | + \hline |
| 76 | + \textbf{Non-terminal Symbols} & \textit{Symbol} & \textit{Production result} |
| 77 | + \end{tabular} |
| 78 | + \end{center} |
| 79 | +\end{figure} |
| 80 | +\begin{easylist} |
| 81 | + |
| 82 | +& To create a predictive parsing table: |
| 83 | + && For each non-terminal symbol $X$ on the left column: |
| 84 | + &&& If there is a production rule where $X$ expands to epsilon ($\epsilon$), then: |
| 85 | + &&&& For each terminal symbol which is in the $FOLLOW$ of symbol $X$, write $\epsilon$ |
| 86 | + &&& If there is a production rule where $X$ expands to a string which is not epsilon ($\epsilon$), then: |
| 87 | + &&&& For each terminal symbol which is in the $FIRST$ of symbol $X$, write the expansion of the valid production rule |
| 88 | + |
| 89 | +& E.g. In figure~\ref{tab:ll1-conflict-resolution}, $Y \rightarrow \epsilon$ is the valid production rule to be chosen when $+, ),$ or $\$$ are the lookahead symbol |
| 90 | + |
| 91 | +\end{easylist} |
| 92 | +\begin{figure}[!htb] |
| 93 | + \caption{LL(1) Conflict Resolution with $FOLLOW$} |
| 94 | + \label{tab:ll1-conflict-resolution} |
| 95 | + \begin{center} |
| 96 | + \begin{tabular}{ r l } |
| 97 | + Production Rules |
| 98 | + & $E \rightarrow TX$ \\ |
| 99 | + & $X \rightarrow \epsilon$ \\ |
| 100 | + & $X \rightarrow +E$ \\ |
| 101 | + & $T \rightarrow (E)$ \\ |
| 102 | + & $T \rightarrow id Y$ \\ |
| 103 | + & $Y \rightarrow *T$ \\ |
| 104 | + & $Y \rightarrow \epsilon $ \\ |
| 105 | + \hline |
| 106 | + $FOLLOW(Y)$ |
| 107 | + & $= FOLLOW(T)$ \\ |
| 108 | + & $= ( FIRST(X) - \{\epsilon\} ) + FOLLOW(E)$ \\ |
| 109 | + & $= \{ +, ), \$ \}$ |
| 110 | + \end{tabular} |
| 111 | + \end{center} |
| 112 | +\end{figure} |
| 113 | +\begin{easylist} |
| 114 | + |
| 115 | +\clearpage |
| 116 | + |
| 117 | +\end{easylist} |
| 118 | +\subsection{LL(1) Parsing Trace} |
| 119 | +\begin{easylist} |
| 120 | + |
| 121 | +& LL(1) parsing trace: |
| 122 | + && Stack is used to keep track of non-terminal symbols |
| 123 | + && Stack and input strings are terminated with the end of input symbol (\$) |
| 124 | + && Layout: See figure~\ref{tab:ll1-parsing-trace-example} |
| 125 | + |
| 126 | +\end{easylist} |
| 127 | +\begin{figure}[!htb] |
| 128 | + \caption{LL(1) Parsing Trace Example} |
| 129 | + \label{tab:ll1-parsing-trace-example} |
| 130 | + \begin{center} |
| 131 | + \begin{tabular}{ l | l | l } |
| 132 | + \textbf{Stack} & \textbf{Input} & \textbf{Action} \\ |
| 133 | + \hline |
| 134 | + \textit{string \$} & \textit{string \$} & \textit{Production rule to expand OR} \\ |
| 135 | + && \textit{$\epsilon$ OR} \\ |
| 136 | + && \textit{Terminal $\alpha$ OR} \\ |
| 137 | + && \textit{acc} |
| 138 | + \end{tabular} |
| 139 | + \end{center} |
| 140 | +\end{figure} |
| 141 | +\begin{easylist} |
| 142 | + |
| 143 | +& To create an LL(1) parsing trace: |
| 144 | + && For the leftmost input symbol and the leftmost stack symbol: |
| 145 | + &&& If the leftmost stack symbol is a non-terminal symbol, then use the leftmost input and stack symbols to find the corresponding production rule expansion from the predictive parsing table, and: |
| 146 | + &&&& If the expansion is not an epsilon, then replace the leftmost stack symbol with the expansion |
| 147 | + &&&& If the expansion is an epsilon ($\epsilon$), then remove the leftmost stack symbol |
| 148 | + &&& If the leftmost stack symbol is a terminal symbol and the leftmost stack and input symbols match, then consume both of them with the action \textit{Terminal} |
| 149 | + &&& If the leftmost stack and input symbols are the end of input (\$), then accept the parse |
| 150 | + |
| 151 | +\end{easylist} |
| 152 | + |
| 153 | +\end{document} |
0 commit comments