diff --git a/info/exercises/ex-03-sol.pdf b/info/exercises/ex-03-sol.pdf index 6b54106a485de679167f1f6f084f4180b158270b..17e187b47360c3eba545dfd53a01f0b3f7d80b90 100644 Binary files a/info/exercises/ex-03-sol.pdf and b/info/exercises/ex-03-sol.pdf differ diff --git a/info/exercises/ex-03.pdf b/info/exercises/ex-03.pdf index 840f896161f7c97fa1de069582c065432d99d06a..cdaf7ef242c891093edfca9551ae32b0b4cb7d62 100644 Binary files a/info/exercises/ex-03.pdf and b/info/exercises/ex-03.pdf differ diff --git a/info/exercises/src/ex-03/ex/compute.tex b/info/exercises/src/ex-03/ex/compute.tex index e7792fd2ce6bfe72241192bbc3935588cdeefe16..41072624c821e6595aa6800bb18b7d9c0ace52ee 100644 --- a/info/exercises/src/ex-03/ex/compute.tex +++ b/info/exercises/src/ex-03/ex/compute.tex @@ -66,16 +66,16 @@ \begin{align*} S &::= Exp~\mathbf{EOF} \\ - Exp &::= Exp_2~ Exp_* \\ - Exp_* &::= +~ Exp_2~ Exp_* \\ - Exp_* &::= -~ Exp_2~ Exp_* \\ - Exp_* &::= \\ - Exp_2 &::= Exp_3~ Exp_{2*} \\ - Exp_{2*} &::= *~ Exp_3~ Exp_{2*} \\ - Exp_{2*} &::= /~ Exp_3~ Exp_{2*} \\ - Exp_{2*} &::= \\ - Exp_3 &::= \mathbf{num} \\ - Exp_3 &::= (Exp) \\ + Exp &::= Term~ Add \\ + Add &::= +~ Term~ Add \\ + Add &::= -~ Term~ Add \\ + Add &::= \\ + Term &::= Factor~ Mul \\ + Mul &::= *~ Factor~ Mul \\ + Mul &::= /~ Factor~ Mul \\ + Mul &::= \\ + Factor &::= \mathbf{num} \\ + Factor &::= (Exp) \\ \end{align*} \begin{enumerate} @@ -104,31 +104,34 @@ \item \(\nullable\): % \begin{align*} + \nullable(S) &= false \\ \nullable(Exp) &= false \\ - \nullable(Exp_*) &= true \\ - \nullable(Exp_2) &= false \\ - \nullable(Exp_{2*}) &= true \\ - \nullable(Exp_3) &= false + \nullable(Add) &= true \\ + \nullable(Term) &= false \\ + \nullable(Mul) &= true \\ + \nullable(Factor) &= false \end{align*} \item \(\first\): we have constraints: % \begin{align*} - \first(Exp) &= \first(Exp_2) \\ - \first(Exp_*) &= \{+\} \cup \{-\} \cup \emptyset \\ - \first(Exp_2) &= \first(Exp_3) \\ - \first(Exp_{2*}) &= \{*\} \cup \{/\} \cup \emptyset \\ - \first(Exp_3) &= \{\mathbf{num}\} \cup \{(\} + \first(S) &= \first(Exp) \\ + \first(Exp) &= \first(Term) \\ + \first(Add) &= \{+\} \cup \{-\} \cup \emptyset \\ + \first(Term) &= \first(Factor) \\ + \first(Mul) &= \{*\} \cup \{/\} \cup \emptyset \\ + \first(Factor) &= \{\mathbf{num}\} \cup \{(\} \end{align*} % which can be solved to get: % \begin{align*} + \first(S) &= \{\mathbf{num}, (\} \\ \first(Exp) &= \{\mathbf{num}, (\} \\ - \first(Exp_*) &= \{+, -\} \\ - \first(Exp_2) &= \{\mathbf{num}, (\} \\ - \first(Exp_{2*}) &= \{*, /\} \\ - \first(Exp_3) &= \{\mathbf{num}, (\} + \first(Add) &= \{+, -\} \\ + \first(Term) &= \{\mathbf{num}, (\} \\ + \first(Mul) &= \{*, /\} \\ + \first(Factor) &= \{\mathbf{num}, (\} \end{align*} \item \(\follow\): we have constraints (for each rule, except empty/terminal rules): @@ -137,25 +140,25 @@ \begin{align*} \{\mathbf{EOF}\} &\subseteq \follow(Exp) \\ &\\ - \first(Exp_*) &\subseteq \follow(Exp_2) \\ - \follow(Exp) &\subseteq \follow(Exp_2) \\ - \follow(Exp) &\subseteq \follow(Exp_*) \\ + \first(Add) &\subseteq \follow(Term) \\ + \follow(Exp) &\subseteq \follow(Term) \\ + \follow(Exp) &\subseteq \follow(Add) \\ &\\ - \first(Exp_*) &\subseteq \follow(Exp_2) \\ - \follow(Exp_*) &\subseteq \follow(Exp_2) \\ + \first(Add) &\subseteq \follow(Term) \\ + \follow(Add) &\subseteq \follow(Term) \\ &\\ - \first(Exp_*) &\subseteq \follow(Exp_2) \\ - \follow(Exp_*) &\subseteq \follow(Exp_2) \\ + \first(Add) &\subseteq \follow(Term) \\ + \follow(Add) &\subseteq \follow(Term) \\ &\\ - \first(Exp_{2*}) &\subseteq \follow(Exp_3) \\ - \follow(Exp_2) &\subseteq \follow(Exp_3) \\ - \follow(Exp_2) &\subseteq \follow(Exp_{2*}) \\ + \first(Mul) &\subseteq \follow(Factor) \\ + \follow(Term) &\subseteq \follow(Factor) \\ + \follow(Term) &\subseteq \follow(Mul) \\ &\\ - \first(Exp_{2*}) &\subseteq \follow(Exp_3) \\ - \follow(Exp_{2*}) &\subseteq \follow(Exp_3) \\ + \first(Mul) &\subseteq \follow(Factor) \\ + \follow(Mul) &\subseteq \follow(Factor) \\ &\\ - \first(Exp_{2*}) &\subseteq \follow(Exp_3) \\ - \follow(Exp_{2*}) &\subseteq \follow(Exp_3) \\ + \first(Mul) &\subseteq \follow(Factor) \\ + \follow(Mul) &\subseteq \follow(Factor) \\ &\\ \{)\} &\subseteq \follow(Exp) \\ \end{align*} @@ -165,10 +168,10 @@ \begin{align*} \follow(S) &= \{\} \\ \follow(Exp) &= \{), \mathbf{EOF}\} \\ - \follow(Exp_*) &= \{), \mathbf{EOF}\} \\ - \follow(Exp_2) &= \{+, -, ), \mathbf{EOF}\} \\ - \follow(Exp_{2*}) &= \{+, -, ), \mathbf{EOF}\} \\ - \follow(Exp_3) &= \{+, -, *, /, ), \mathbf{EOF}\} + \follow(Add) &= \{), \mathbf{EOF}\} \\ + \follow(Term) &= \{+, -, ), \mathbf{EOF}\} \\ + \follow(Mul) &= \{+, -, ), \mathbf{EOF}\} \\ + \follow(Factor) &= \{+, -, *, /, ), \mathbf{EOF}\} \end{align*} \end{enumerate} @@ -181,45 +184,45 @@ \hline \(S\) & 1 & & & & & 1 & &\\ \(Exp\) & 1 & & & & & 1 & &\\ - \(Exp_*\) & & 1 & 2 & & & & 3 & 3 \\ - \(Exp_2\) & 1 & & & & & 1 & & \\ - \(Exp_{2*}\) & & 3 & 3 & 1 & 2 & & 3 & 3 \\ - \(Exp_3\) & 1 & & & & & 2 & & \\ + \(Add\) & & 1 & 2 & & & & 3 & 3 \\ + \(Term\) & 1 & & & & & 1 & & \\ + \(Mul\) & & 3 & 3 & 1 & 2 & & 3 & 3 \\ + \(Factor\) & 1 & & & & & 2 & & \\ \end{tabular} \end{center} \item Parsing the strings: \begin{enumerate} \item \((3 + 4) * 5 ~\mathbf{EOF}\) \checkmark \item \(2 + + ~\mathbf{EOF}\) --- fails on the second \(+\). The - corresponding error cell in the parsing table is \((Exp_2, +)\). + corresponding error cell in the parsing table is \((Term, +)\). \item \(2 ~\mathbf{EOF}\) \checkmark \item \(2 * 3 + 4 ~\mathbf{EOF}\) \checkmark - \item \(2 + 3 * 4 ~\mathbf{EOF}\) fails on the \(*\). Error at \((Exp_*, *)\). + \item \(2 + 3 * 4 ~\mathbf{EOF}\) fails on the \(*\). Error at \((Add, *)\). \end{enumerate} Example step-by-step LL(1) parsing state for \(2 * 3 + 4\): \begin{center} - \begin{tabular}{c c} - Lookahead token & Stack \\ + \begin{tabular}{c c c} + Lookahead & Stack & Next Rule \\ \hline - \(2\) & \(S\) \\ - \(2\) & \(Exp ~ \mathbf{EOF}\) \\ - \(2\) & \(Exp_2 ~ Exp_* ~ \mathbf{EOF}\) \\ - \(2\) & \(Exp_3 ~ Exp_{2*} ~ Exp_* ~ \mathbf{EOF}\) \\ - \(2\) & \(\mathbf{num} ~ Exp_{2*} ~ Exp_* ~ \mathbf{EOF}\) \\ - \(*\) & \(Exp_{2*} ~ Exp_* ~ \mathbf{EOF}\) \\ - \(*\) & \(* ~Exp_3 ~ Exp_{2*} ~ Exp_* ~ \mathbf{EOF}\) \\ - \(3\) & \(Exp_3 ~ Exp_{2*} ~ Exp_* ~ \mathbf{EOF}\) \\ - \(3\) & \(\mathbf{num} ~ Exp_{2*} ~ Exp_* ~ \mathbf{EOF}\) \\ - \(+\) & \(Exp_{2*} ~ Exp_* ~ \mathbf{EOF}\) \\ - \(+\) & \(Exp_* ~ \mathbf{EOF}\) \\ - \(+\) & \(+ ~Exp_2 ~Exp_* ~ \mathbf{EOF}\) \\ - \(4\) & \(Exp_2 ~Exp_* ~ \mathbf{EOF}\) \\ - \(4\) & \(Exp_3 ~Exp_{2*} ~Exp_* ~ \mathbf{EOF}\) \\ - \(4\) & \(\mathbf{num} ~Exp_{2*} ~Exp_* ~ \mathbf{EOF}\) \\ - \(\mathbf{EOF}\) & \(Exp_{2*} ~Exp_* ~ \mathbf{EOF}\) \\ - \(\mathbf{EOF}\) & \(Exp_* ~ \mathbf{EOF}\) \\ - \(\mathbf{EOF}\) & \(\mathbf{EOF}\) \\ + \(2\) & \(S\) & \(S ::= Exp ~\mathbf{EOF}\)\\ + \(2\) & \(Exp ~ \mathbf{EOF}\) & \(Exp ::= Term~Add\)\\ + \(2\) & \(Term ~ Add ~ \mathbf{EOF}\) & \(Term ::= Factor~Mul\)\\ + \(2\) & \(Factor ~ Mul ~ Add ~ \mathbf{EOF}\) & \(Factor ::= \mathbf{num}\)\\ + \(2\) & \(\mathbf{num} ~ Mul ~ Add ~ \mathbf{EOF}\) & \(match(\mathbf{num})\)\\ + \(*\) & \(Mul ~ Add ~ \mathbf{EOF}\) & \(Mul ::= *~Factor~Mul\)\\ + \(*\) & \(* ~Factor ~ Mul ~ Add ~ \mathbf{EOF}\) & \(match(*)\)\\ + \(3\) & \(Factor ~ Mul ~ Add ~ \mathbf{EOF}\) & \(Factor ::= \mathbf{num}\)\\ + \(3\) & \(\mathbf{num} ~ Mul ~ Add ~ \mathbf{EOF}\) & \(match(\mathbf{num})\)\\ + \(+\) & \(Mul ~ Add ~ \mathbf{EOF}\) & \(Mul ::=\)\\ + \(+\) & \(Add ~ \mathbf{EOF}\) & \(Add ::= +~Term~Add\)\\ + \(+\) & \(+ ~Term ~Add ~ \mathbf{EOF}\) & \(match(+)\)\\ + \(4\) & \(Term ~Add ~ \mathbf{EOF}\) & \(Term ::= Factor~Term*\)\\ + \(4\) & \(Factor ~Mul ~Add ~ \mathbf{EOF}\) & \(Factor ::= \mathbf{num}\)\\ + \(4\) & \(\mathbf{num} ~Mul ~Add ~ \mathbf{EOF}\) & \(match(\mathbf{num})\)\\ + \(\mathbf{EOF}\) & \(Mul ~Add ~ \mathbf{EOF}\) & \(Mul ::= \)\\ + \(\mathbf{EOF}\) & \(Add ~ \mathbf{EOF}\) & \(Add ::= \)\\ + \(\mathbf{EOF}\) & \(\mathbf{EOF}\) & \(match(\mathbf{EOF})\)\\ \end{tabular} \end{center} \end{enumerate} diff --git a/info/exercises/src/ex-03/ex/table.tex b/info/exercises/src/ex-03/ex/table.tex index 1e3768b1df53b1d347de1a770aa219f2d4153ed5..aa1de12f5b9c57eddc61dabd02be26459569ead9 100644 --- a/info/exercises/src/ex-03/ex/table.tex +++ b/info/exercises/src/ex-03/ex/table.tex @@ -24,21 +24,21 @@ LL(1) grammar. \begin{equation*} - E ::= \mathbf{num} + E \mid \mathbf{num} - E + E ::= \mathbf{num} + E \mid \mathbf{num} - E \mid \mathbf{num} \end{equation*} \begin{solution} The language is clearly not LL(1), as on seeing a token \(\mathbf{num}\), we - cannot decide whether to continue parsing it as \(\mathbf{num} + E\) or - \(\mathbf{num} - E\). + cannot decide whether to continue parsing it as \(\mathbf{num} + E\), + \(\mathbf{num} - E\), or the end. - The notable problem is the common prefix between the two rules. We can - separate this out by introducing a new non-terminal \(T\). This is a - transformation known as \emph{left factorization}. + The notable problem is the common prefix between the rules. We can separate + this out by introducing a new non-terminal \(T\). This is a transformation + known as \emph{left factorization}. \begin{align*} E &::= \mathbf{num} ~T \\ - T &::= + E \mid - E + T &::= + E \mid - E \mid \epsilon \end{align*} % without changing the terms or the overall "structure" of the grammar, we