\documentclass{article}
\usepackage{fullpage}
\usepackage{array}
\title{Assignment 2: Parsing}
\author{15-411: Compiler Design\\
Nathan Snyder (npsnyder@andrew) and Anand Subramanian(asubrama@andrew)}
\date{Due: Tuesday, September 21, 2010 (1:30 pm)}
\begin{document}
\maketitle
\newcommand{\nonterm}[1]{\ensuremath{\langle{\mbox{#1}}\rangle}}
\newcommand{\term}[1]{{#1}}
\newcommand{\OR}{\ensuremath{\ | \ \ }}
\newcommand{\production}[3]{\ensuremath{\gamma_{#1}} & : & \nonterm{#2} & $\rightarrow$ & {#3} \\}
\noindent\textbf{Reminder:} Assignments are individual assignments,
not done in pairs. The work must be all your own.
\noindent
You may hand in a handwritten solution or a printout of a typeset
solution at the beginning of lecture on Tuesday, September 21. Please
read the late policy for written assignments on the course web page.
If you decide not to typeset your answers, make sure the text and
pictures are legible and clear.
\section*{Overview}
This assignment is designed to test your understanding of the concepts that
underly LL and LR parsing. In particular, it is here to ensure that you
understand the details that widely available tools such as parser
generators abstract away from you when you work on the Labs.
Being well-versed in the concepts behind LL and LR parsing can often be crucial for using parser generators effectively.
In this assignment, you will be working with the context free grammar $\Lambda$,
defined as follows:
\begin{tabular}{rclcl}
\production{1}{E}{\term{x}}
\production{2}{E}{\term{$\lambda$ x :} \nonterm{E}}
\production{3}{E}{\nonterm{E} \nonterm{E}}
\production{4}{E}{\term{$\oplus$} \nonterm{E}}
\production{5}{E}{\nonterm{E} \term{$\oplus$} \nonterm{E}}
\production{6}{E}{\term{(} \nonterm{E} \term{)}}
\\
\production{7}{A}{\term{x =} \nonterm{E}}
\\
\production{8}{S}{\term{$\epsilon$}}
\production{9}{S}{\term{let} \nonterm{A}}
\production{10}{S}{\term{let} \nonterm{A} \term{where} \nonterm{A}}
\\
\production{11}{P}{\term{\$}}
\production{12}{P}{\nonterm{S} \nonterm{P}}
\\
\end{tabular}
\noindent
Non-terminals are in \nonterm{angle brackets}.\\
``let'' and ``where'' are each considered
a single terminal. \term{$\epsilon$} stands for the empty string and \term{\$}
stands for the end-of-stream marker. \nonterm{P} is the start symbol. And we fix some specific operator $\oplus$ in the language.
Each production rule is named differently for convenience, even if
some of them may produce the same nonterminal.
This grammar could very well describe a simple programming language!
\section*{Problem 1: Warmup (10 points)}
\begin{itemize}
\item Give three derivations of the following string -- one
that is left-most, one that is right-most, and one that is neither. \\
\texttt{let x = $\oplus$ x $\oplus$ x where x = x $\oplus$ x $\oplus$ x}
\end{itemize}
\section*{Problem 2: Predictive (LL(1)) Parsing (30 points)}
\begin{enumerate}
\item [(a)] Compute the First and Follow sets for $\Lambda$.\\
Remember that First($\gamma$) stands for the set of all tokens that may
appear first in any string accepted by the production rule $\gamma$.
Through a slight abuse of notation, First(\nonterm{N}) can additionally
be defined as the set of all tokens that may appear in any
string from which the non-terminal \nonterm{N} can be derived.
\noindent It is useful to compute the values of First, both for
production rules and for non-terminals, because you can use them
as intermediate values to compute other First and Follow sets.
Therefore, you should write down both. Please show your work.
\item[(b)] Explain First-First conflicts (i.e. how this class of
conflicts manifest themselves in the execution of a predictive parser),
and how you would compute them using your answer to part (b).
List all First-First conflicts you find in $\Lambda$.
\item [(c)] Explain First-Follow conflicts,
and how you would compute them using your answer to part (b).
List all First-Follow conflicts you find in $\Lambda$.
\item[(d)] Demonstrate the correct use of Left-Factoring and Right-Recursion
to derive an equivalent grammar that resolves all conflicts you found in
parts (c) and (d).
Briefly explain why your transformations do not change the language.
\end{enumerate}
\section*{Problem 3: LR Parsing (20 points)}
\begin{enumerate}
\item [(a)] Explain shift-reduce and reduce-reduce conflicts (i.e. how
they manifest themselves in an LR(1) parser).
\item [(b)] An LR(0) parser is a special case of LR parsers characterized
by a zero token look-ahead. Reduce actions occur purely based on the
tokens that have already been pushed on to the stack.
List all the conflicts that an LR(0) parser for $\Lambda$ would encounter.
\item [(c)] An SLR parser is an LR(0) parser with one addition:
we perform a reduce action only if the next token is in the Follow set
of the nonterminal that the reduction would derive.
List all the conflicts that an SLR parser for $\Lambda$ would
encounter. Also explain what you would do to get the right-most
possible derivations for $\Lambda$ using an SLR parser if you had
the ability to manually pick a state transition where there is
a conflict.
\end{enumerate}
\end{document}