|
@@ -4346,7 +4346,7 @@ lang_int: (stmt NEWLINE)* -> module
|
|
\end{lstlisting}
|
|
\end{lstlisting}
|
|
\end{minipage}
|
|
\end{minipage}
|
|
\end{center}
|
|
\end{center}
|
|
-The resulting parse tree
|
|
|
|
|
|
+Here is the resulting parse tree.
|
|
\begin{lstlisting}
|
|
\begin{lstlisting}
|
|
Tree('module',
|
|
Tree('module',
|
|
[Tree('expr', [Tree('add', [Tree('int', [Token('INT', '1')]),
|
|
[Tree('expr', [Tree('add', [Tree('int', [Token('INT', '1')]),
|
|
@@ -4356,11 +4356,13 @@ Tree('module',
|
|
|
|
|
|
\subsection{Ambiguous Grammars}
|
|
\subsection{Ambiguous Grammars}
|
|
|
|
|
|
-A grammar is \emph{ambiguous}\index{subject}{ambiguous} when there are
|
|
|
|
-strings that can be parsed in more than one way. For example, consider
|
|
|
|
-the string \code{'1-2+3'}. This string can parsed in two different
|
|
|
|
-ways using our draft grammar, resulting in the two parse trees shown
|
|
|
|
-in figure~\ref{fig:ambig-parse-tree}.
|
|
|
|
|
|
+A grammar is \emph{ambiguous}\index{subject}{ambiguous} when a string
|
|
|
|
+can be parsed in more than one way. For example, consider the string
|
|
|
|
+\code{'1-2+3'}. This string can parsed in two different ways using
|
|
|
|
+our draft grammar, resulting in the two parse trees shown in
|
|
|
|
+figure~\ref{fig:ambig-parse-tree}. This example is problematic because
|
|
|
|
+interpreting the second parse tree would yield \code{-4} even through
|
|
|
|
+the correct answer is \code{2}.
|
|
|
|
|
|
\begin{figure}[tbp]
|
|
\begin{figure}[tbp]
|
|
\begin{tcolorbox}[colback=white]
|
|
\begin{tcolorbox}[colback=white]
|
|
@@ -4371,6 +4373,12 @@ in figure~\ref{fig:ambig-parse-tree}.
|
|
\label{fig:ambig-parse-tree}
|
|
\label{fig:ambig-parse-tree}
|
|
\end{figure}
|
|
\end{figure}
|
|
|
|
|
|
|
|
+To deal with this problem we can change the grammar to become
|
|
|
|
+unambiguous by categorizing the syntax in a more fine grained
|
|
|
|
+fashion. In this case we want to disallow the application of the rule
|
|
|
|
+\code{exp: exp "-" exp} when the child on the right is an addition.
|
|
|
|
+
|
|
|
|
+
|
|
|
|
|
|
|
|
|
|
|
|
|