Description
Problem 1. (4 points) Put your full name, UIN, and acknowledgements of any help received
in the head comment in your .hs file for this assignment.
Problem 2. (26 points) The parser for expressions discussed in section 13.8 (page 190) is
of type expr :: Parser Int, term :: Parser Int and factor :: Parser Int. That
is, the parsed result of an expression is an integer. For example, if the input string was
“2*(3+4)”, the parsed result (by parse expr “2*(3+4)”) would be [(14,””)] (note that
the eval function given in the end of section 13.8 extracts the integer value 14 out of
this singleton list). Your task is to explain the step-by-step working of the two example
expressions below.
1. (13 points)
> parse expr “2*(3+4)”
results in
[(14,””)]
2. (13 points)
> parse expr “1+2*3″
results in
[(7,””)]
Your explanation should be as specific as possible, strictly using the definitions of expr,
term and factor given in section 13.8 (page 190) in the textbook.
2
Problem 3. (30 points) Exercise 13.5 (Modified). Use the following type Expr.
data Expr = Val Int | Add Expr Expr | Mul Expr Expr
deriving Show
Your task in this problem is to modify the parser for expressions to have type
expr :: Parser Expr, term :: Parser Expr and factor :: Parser Expr.
Thus, the parsed result will be an abstract syntax tree, for example, if you do
> parse expr 2*(3+4)
it should output
[(Mul (Val 2) (Add (Val 3) (Val 4)),””)]
and
> parse expr “1+2*3″
should output
[(Add (Val 1) (Mul (Val 2) (Val 3)),””)]
Have fun!
3