Assignment #4: Parsing
(Modified from Prof. Greenberg’s Homework #4).
For this assignment you will add to the parser that we discussed in class. As usual, you will proceed in stages:
-
- Test the existing parser code for arithmetic expressions, by simply running the program you downloaded.
- Add the tokens, lexer rules, and parsing functions to support parsing Boolean expressions.
- Add the tokens, lexer rules, and parsing functions to support the parsing of statements.
- Add a function
run :: Store -> String -> Store
that accepts a store (set of variable assignments) and a string (the text of a program) and returns the modified store made by parsing the statement. If you copy/paste youreval
code from the previous assignment, this function ends up being just one line of code.
Here are some hints that may save you time and frustration:
-
- The existing lexer rules have a single character (e.g.,
lexer ('+':s) = TPlus:lexer s
). To support rules that use strings (multiple characters), you can chain characters together using a colon:lexer ('f':'a':'l':'s':'e':s) = TFalse:lexer s
- To keep the code a little smaller, I’ve used the names
AExp
andBExp
instead ofArithExp
andBoolExp
. - Remember that the type for
BExp
includes anAExp
, and the type forStmt
includes anAExp
and aBExp
. Looking at your evaluator code for the previous assignment will help you see what I mean.
- The existing lexer rules have a single character (e.g.,