\documentclass[12pt]{exam}
\renewcommand{\solutiontitle}{\noindent\textbf{Answer:}\par\noindent}
\pagestyle{headandfoot}
\header{CS 460/660}{Handout WA3}{}
\footrule
\headrule
\footer{}{Page \thepage\ of \numpages}
  {\iflastpage{End of Assignment}{Please go on to the next page\ldots}}
  
  \usepackage{graphicx}
  
%\printanswers

\begin{document}

This assignment asks you to prepare written answers to questions on
top-down parsing.  Each question has a short answer.  
You may discuss this assignment with other students and work
the problems together.  However, your writeup should be your own
individual work.  Remember written assignments are to be turned in in
class on the date due.


Consider the following grammar for a subset of English sentences.  The
nonterminals are $S$, $NP$, $VP$, $AP$.  The terminals are $the$,
$noun$, $adjective$, $verb$.  The starts symbol is $S$.
\begin{center}
{\tt
\begin{tabular}{lll}
S 	&$\rightarrow$ 	& NP VP \\
   	& | 		& NP VP NP \\
 \\
NP 	&$\rightarrow$ 	& the AP noun \\
 \\
AP 	&$\rightarrow$ 	& AP adjective \\
	& | 		& $\epsilon$\\
\\
VP	&$\rightarrow$ 	& verb
\end{tabular}
}
\end{center}


\begin{questions}

\question Left-factor this grammar.
	\begin{solution}
		{\bf 4 pts}		
		{\tt
			
			\begin{tabular}{lll}
				S 	&$\rightarrow$ 	& NP VP  S'\\
				S' 	&$\rightarrow$ 	& NP \\
				   	& | 		& $\epsilon$ \\
				NP 	&$\rightarrow$ 	& the AP noun \\
				AP 	&$\rightarrow$ 	& AP adjective \\
					& | 		& $\epsilon$\\
				VP	&$\rightarrow$ 	& verb
			\end{tabular}
		}
	\end{solution}
\question Eliminate left-recursion from your answer to part (1).
	\begin{solution}
		{\bf 2 pts}		
		{\tt
			\begin{tabular}{lll}
				S 	&$\rightarrow$ 	& NP VP  S'\\
				S' 	&$\rightarrow$ 	& NP \\
				   	& | 		& $\epsilon$ \\
				NP 	&$\rightarrow$ 	& the AP noun \\
				AP	&$\rightarrow$	& AP' \\
				AP' 	&$\rightarrow$ 	& adjective AP'\\
					& | 		& $\epsilon$\\
				VP	&$\rightarrow$ 	& verb
			\end{tabular}
		}
	\end{solution}

	\ifprintanswers
		\newpage
	\else
	\fi
\question Give the {\it First} and {\it Follow} sets for each
		grammar symbol for your answer to part (2).
	\begin{solution}
		{\bf 12 pts}		
	\begin{itemize}
	\item $First(S) = First(NP) - \{ \epsilon \} = \{ the \}$
	\item $First(S') = { \epsilon } + (First(NP)- { \epsilon } ) 
		= \{ \epsilon , the \}$
	\item $First(NP) = \{ the \} $
	\item $First(AP) = \{ \epsilon \} (because AP=*=> \epsilon) + ( First(AP') - \{ \epsilon \} ) 
		= \{ \epsilon, adjective \}$
	\item $First(AP') 
		= \{ \epsilon, adjective \}$
	\item $First(VP) = \{ verb \}$
	\end{itemize}

	\begin{itemize}
	\item $ Follow(S) = \{ \$ \} $
	\item $ Follow(S') =  Follow(S) = \{ \$ \} $
	\item $ Follow(NP) =  Follow(S') + ( First(VP)- \{ \epsilon \} )
		= \{ \$ , verb \}$
	\item $ Follow(AP) =  \{ noun \} $
	\item $ Follow(AP') =  Follow(AP) = \{ noun \}$
	\item $ Follow(VP) = (First(S')- \{ \epsilon \} ) + Follow(S) 
		= \{ \$ , the \}$
	\end{itemize}
	\end{solution}

\question Give an LL(1) parsing table for your answer to part (2).
	\begin{solution}
		{\bf 10 pts}		
	\begin{tabular}{|l|c|c|c|c|c|} \hline
		& the & noun & verb & adjective & \$ \\ \hline
	S	& NP VP S' & & & &  \\
	S'	& NP & & & & $\epsilon$ \\
	NP	& the AP noun & & & & \\
	AP	& & AP' & & AP' & \\
	AP'	& & $\epsilon$ & & adjective AP' & \\
	VP	& & & verb & & \\ \hline
	\end{tabular}
	\end{solution}

	\ifprintanswers
		\newpage
	\else
	\fi

\question Show the sequence of moves of your LL(1) parser on the
input:
	\begin{center}
	The silly professor invented the awkward obscure sentence.
	\end{center}

	(Note that you need to tokenize the input first.	)
	\begin{solution}
			{\bf 2 pts}		
	Tokens: \\
	\begin{tabular}{llllllll}
	The & silly     & professor & invented & the & awkward   & obscure   & sentence. \\
	the & adjective & noun      & verb     & the & adjective & adjective & noun
	\end{tabular}

	{\bf 6 pts}		
	\begin{itemize}
	\item \$ S
	\item \$ S' VP NP
	\item \$ S' VP noun AP {\bf the}
	\item \$ S' VP noun AP
	\item \$ S' VP noun AP'
	\item \$ S' Vp noun AP' {\bf adjective}
	\item \$ S' VP noun AP' 
	\item \$ S' VP {\bf noun}
	\item \$ S' VP
	\item \$ S' {\bf verb}
	\item \$ S'
	\item \$ NP
	\item \$ noun AP {\bf the}
	\item \$ noun AP
	\item \$ noun AP'
	\item \$ noun AP' {\bf adjective}
	\item \$ noun AP'
	\item \$ noun AP' {\bf adjective}
	\item \$ noun AP'
	\item \$ {\bf noun}
	\item \$
	\end{itemize}
	
	\end{solution}

\question Show the parse tree that your parser traces for this
	sentence.
	\begin{solution}
			{\bf 4 pts}		
	Here is the tree without the lines.....
	\begin{itemize}
	\item S
	\item NP VP S'
	\item the AP noun verb NP
	\item AP' the AP noun
	\item adjective AP' AP'
	\item $\epsilon$ adjective AP'
	\item adjective AP'
	\item $\epsilon$
	\end{itemize}
	\end{solution}
\end{questions}
\end{document}

