[back]

Lab 5: S-expressions

This week you'll be writing a parser for S-expressions. Technically speaking, we've already given you the solution to this problem (and if you peeked, no worries). The goal here is to try to do it yourself (with your lab-mates).

S-expressions represent hierarchical collections of strings (called atoms) using parentheses. S-expressions themselves are represented in code by the following ADT:

type sexpr =
  | Atom of String
  | List of sexpr list

The way that S-expressions represent hierarchical data is best understood by example. The S-expression:

(abc (d e f) ((g ((h i)) jk) lm))

is represented by the following value:

List
  [
    Atom "abc";
    List [Atom "d"; Atom "e"; Atom "f"];
    List
      [
        List
          [
            Atom "g";
            List [List [Atom "h"; Atom "i"]];
            Atom "jk";
          ];
        Atom "lm"
      ]
  ]

S-expressions are commonly used to serialize data (e.g., dune files consist of S-expression) and are sometimes used as syntax for programming languages (e.g., Lisp, or see Assignment 4).

There are many ways to parse S-expressions. The course staff will help you think through an approach. A couple hints:

As usual, please use this opportunity to clear up any confusions you have by asking the course staff questions. They're very smart folks!