• Home
  • Textbooks
  • Programming in Haskell
  • Monadic parsing

Programming in Haskell

Graham Hutton

Chapter 13

Monadic parsing - all with Video Answers

Educators


Chapter Questions

Problem 1

Define a parser comment :: Parser () for ordinary Haskell comments that begin with the symbol - and extend to the end of the current line, which is represented by the control character ' $\backslash n^{\prime}$.

Check back soon!
09:48

Problem 2

Using our second grammar for arithmetic expressions, draw the two possible parse trees for the expression $2+3+4$.

Chris Trentman
Chris Trentman
Numerade Educator
09:48

Problem 3

Using our third grammar for arithmetic expressions, draw the parse trees for the expressions $2+3,2 \star 3 \star 4$ and $(2+3)+4$.

Chris Trentman
Chris Trentman
Numerade Educator
00:53

Problem 4

Explain why the final simplification of the grammar for arithmetic expressions has a dramatic effect on the efficiency of the resulting parser. Hint: begin by considering how an expression comprising a single number would be parsed if this simplification step had not been made.

Karly Williams
Karly Williams
Numerade Educator
06:18

Problem 5

Define a suitable type Expr for arithmetic expressions and modify the parser for expressions to have type expr :: Parser Expr.

Chris Trentman
Chris Trentman
Numerade Educator
06:18

Problem 6

Extend the parser expr :: Parser Int to support subtraction and division, and to use integer values rather than natural numbers, based upon the following revisions to the grammar:
$$
\begin{array}{ll}
\operatorname{expr} & :=\operatorname{term}(+\exp r|-\operatorname{expr}| \epsilon) \\
\text { term } & :=\operatorname{factor}(* \operatorname{term}|/ \operatorname{term}| \epsilon) \\
\text { factor }::=(\text { expr }) \mid \text { int } \\
\text { int } & ::=\cdots|-1| 0|1| \cdots
\end{array}
$$

Chris Trentman
Chris Trentman
Numerade Educator
01:47

Problem 7

Further extend the grammar and parser for arithmetic assumed to associate to the right and have higher priority than multiplication and division, but lower priority than parentheses and numbers. For example, $2^{\wedge} 3^* 4$ means $\left(2^{\wedge} 3\right) * 4$. Hint: the new level of priority requires a new rule in the grammar.

James Kiss
James Kiss
Numerade Educator

Problem 8

Consider expressions built up from natural numbers using a subtraction operator that is assumed to associate to the left.
a. Translate this description directly into a grammar.
b. Implement this grammar as a parser expr :: Parser Int.
c. What is the problem with this parser?
d. Show how it can be fixed. Hint: rewrite the parser using the repetition primitive many and the library function foldl.

Check back soon!
00:40

Problem 9

Modify the calculator program to indicate the approximate position of an error rather than just sounding a beep, by using the fact that the parser returns the unconsumed part of the input string.
Solutions to exercises 1-4 are given in appendix A.

Vanna Tran
Vanna Tran
Numerade Educator