Parser for expressions with "let"


I have provided a lexer and parser for depython on github.

Your task is to modify my lexer and parser to recognize a small language described in Lecture 3.

           add1 ( ‹expr› )
           sub1 ( ‹expr› )
           let IDENTIFIER = <expr> in <expr>

Your parser should produce an abstract syntax as shown in Lecture 3 as well:

type expr =
  | Num of int64
  | Add1 of expr
  | Sub1 of expr
  | Id of string
  | Let of string * expr * expr

Example code

There's sample code in github. To run it you will need menhir, ocamlfind and ocamlbuild installed. I used opam to install mine.

You can parse a sample depython program using:

$ make main.byte
$ ./main.byte tests/112.dp

After modifying the lexer and parser you should be able to parse a program like today's.

  a = 3 in let
  b = 2 in

If you want, you can load the parser, lexer, and main into ocaml/utop using the provided .ocamlinit file and parse a program string using the parse function to see the AST produced:

$ rlwrap ocaml
        OCaml version 4.05.0

# parse "3 * 4 + 32";;
- : Syntax.prog =
Module [Expr (BinOp (BinOp (Num 3, Mult, Num 4), Add, Num 32))]


You should turn in your modified lexer.mll, parser.mly, and You shouldn't need to make changes to other files.