Lab 5
9.3

Lab 5🔗

Course Homepage

  • Develop the method swap-adds, that accepts an ArithC and returns a new ArithC where the left and right terms of every addition are swapped.

  • Add a printf to your swap-adds function that prints each tree on entry to the function.

  • Develop a parser for the Arith language. It should map Sexp to ArithC. It should use the match form. It should signal an error, by calling error, when the input is not well-formed. Here’s a grammar for the Arith language:

  •   Arith = num
      | {+ Arith Arith}
      | {* Arith Arith}
  • Extend the parser and the interpreter to handle a new ^2 feature that squares a number. With this new feature, the grammar for the language is this:

  •   Arith = num
      | {+ Arith Arith}
      | {* Arith Arith}
      | {^2 Arith}
  • Let’s explore the space! More cowbell! Here’s an alternate grammar for the language, without all of those pesky parentheses:

     

    ‹expr›

     ::= 

    ‹num›

     

      |  

    ‹expr› + ‹expr›

     

      |  

    ‹expr› * ‹expr›

     

      |  

    ‹expr› ** 2

     

      |  

    { ‹expr› }

    Develop a parser called parse2 which takes a list of expressions that match this grammar, and returns a list of ExprCs.

    (NB: this part of this lab ... well ... it’s kind of hard. I probably shouldn’t have put it here. Is it optional? Ask your instructor.)

  • Develop the one-line function top-interp, that accepts an s-expression and calls the parser and then the interp function.

  • Develop the zip function, that consumes two lists of Number of the same length and returns a new list of lists where each element of the new list is a list containing both of the corresponding elements from the original lists. Read that last sentence carefully. Optionally: use the All parametric polymorphism feature to widen the type so it can handle any kinds of lists.