\documentclass[12pt]{exam}
\setlength{\topmargin}{0.25in}
\renewcommand{\solutiontitle}{\noindent\textbf{Answer:}\par\noindent}
\pagestyle{headandfoot}
\header{CS 460/660}{Handout WA2}{}
\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
context-free grammars.  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.

\begin{questions}

\question
	Given the Grammar G=
			\begin{itemize}
			\item $\left. A \rightarrow Ba \right| bC$
			\item $\left. B \rightarrow d \right| eBf$
			\item $\left. C \rightarrow gC \right| g$
			\end{itemize}
			
			Determine which of the following strings are in L(G).  
				Construct parse trees for those that are.
				\begin{itemize}
				\item bg
					\begin{solution}
					Yes
					\end{solution}
				\item bffd
					\begin{solution}
					No
					\end{solution}
				\item bggg
					\begin{solution}
					Yes
					\end{solution}
				\item edfa
					\begin{solution}
					Yes
					\end{solution}
				\item eedffa
					\begin{solution}
					Yes
					\end{solution}
				\item faae
					\begin{solution}
					No
					\end{solution}
				\item defa
					\begin{solution}
					Yes
					\end{solution}
				\end{itemize}

\question
	Given the following parse tree,
		
		% Insert figure pg 115 problem 3.4
		
	\begin{figure}[h]
	\centering
		\includegraphics[width=4in]{Capture-2.eps}
	%\label{fig:Capture-2}
\end{figure}

		\begin{parts}
		\part Construct the corresponding rightmost derivation
					\begin{solution}
					Note: A Rightmost derivation only expands one non-terminal at a time. 
					Therefore, you should have a list of sentential forms with one non-terminal
					expanded from the previous one
					\begin{itemize}
					\item $ S \rightarrow ABC $
					\item $  \rightarrow ABgCg $
					\item $  \rightarrow ABgAdg $
					\item $  \rightarrow ABghdg $
					\item $  \rightarrow Aefghdg $
					\item $  \rightarrow Bdefghdg $
					\item $  \rightarrow efdefghdg $
					\end{itemize}
					\end{solution}
		\part Construct as much of the grammar as can be determined from the parse tree
					\begin{solution}
					\begin{itemize}
					\item $ S \rightarrow ABC $
					\item $\left. A \rightarrow Bb \right| h $
					\item $ B \rightarrow ef $
					\item $\left. C \rightarrow gCg \right| Ad $

					\end{itemize}
					\end{solution}
		\end{parts}
		
%\newpage
\question
		Given the grammar
			\begin{itemize}
			\item $ S \rightarrow aS \left.\right| SB\left.\right| d$
			\item $ B \rightarrow Bb \left.\right| c$
			\end{itemize}
		Show that this grammar is ambiguous by showing two parse trees for the string aadccb
					\begin{solution}
					\end{solution}


\question
	Our usual expression grammar (Old Faithful)
	
			\begin{itemize}
				\item $ E \rightarrow E + T  \left.\right| E - T \left.\right| T $
				\item $ T \rightarrow T * F  \left.\right| T / F \left.\right| F $
				\item $ F \rightarrow (E)    \left.\right| i $
				\end{itemize}
			can't be used as is in a predictive parser because it has left recursion.  
			Someone had hte idea that he could remove left recursion from this grammar by changing it to 
				\begin{itemize}
				\item $ E \rightarrow T + E \left.\right| T - E \left.\right| T $
				\item $ T \rightarrow F * T  \left.\right| F / T \left.\right| F $
				\item $ F \rightarrow (E)    \left.\right| i $
				\end{itemize}

			Show that this isn't such a good idea by 
			\begin{parts}
			\part drawing parse trees for $a-b-c$ using both the original grammar and the revised grammar and
					\begin{solution}
					\end{solution}
			\part using the trees as a guide to determine the results of the corresponding computations if $a=3$, $b=5$, $c=8$.
					\begin{solution}
					Original = -10 \\
					Revised = + 6
					\end{solution}
			\end{parts}
			
			
\question
		Find the $FIRST$ and $FOLLOW$ sets for the grammar
				\begin{itemize}
				\item $ S \rightarrow ABC$
				\item $ A \rightarrow a  \left.\right| Cb \left.\right| \epsilon $
				\item $ B \rightarrow c  \left.\right| dA \left.\right| \epsilon $
				\item $ C \rightarrow e    \left.\right| f $
				\end{itemize}
					\begin{solution}
					First Sets:
						\begin{itemize}
						\item $First (S) = First(A) - \{ \epsilon \} = \{a,e,f\}$
						\item $First (A) = \{a, \epsilon \} + (First(C) - \{ \epsilon \}) = \{ a,e,f, \epsilon \}$
						\item $First (B) = \{ c,d, \epsilon \}$
						\item $First (C) = \{ e,f \}$
						\end{itemize}
					Follow Sets:
						\begin{itemize}
						\item $Follow(S) = \{ \verb+$+ \}$
						\item $Follow(A) = (First(B) - \epsilon) + Follow(B) = \{ c,d,e,f \}$
						\item $Follow(B) = First(C) - \epsilon = \{e,f \}   $
						\item $Follow(C) = Follow(S) + \{b\} = \{\verb+$+,b\}   $
						\end{itemize}
					\end{solution}


\end{questions}		
\end{document}

