Lab 2: A Calculator

Due: 10pm, Wednesday, Oct. 7

Learning Objectives

After this lab, you will be able to:

Overview

In this lab you will complete a calculator built with scup, the course’s LL(1) parsing library. The parser is a .scup spec file: a yacc-flavored grammar whose productions carry blocks of Scala to run when they apply — the same way we will build the parser for IC in PA 2.

The lab lives in your Calculator repository and complements the problems in HW 3: accept the GitHub invitation email to your Calculator repository if you have not already, and clone it. Read the scup tutorial first if you have not.

Tasks

Explore and work through this repository as you prepare for our HW 3 discussion. In lab, you’ll want to additionally do the following.

Complete the TODOs in calc.scup (the grammar spec at the root of the repository):

  1. Add subtraction and division.

  2. The starter grammar’s exprTail is right-recursive, so 9 - 2 - 3 evaluates to 10. Fix the grammar with a %left block and a %binary rule (the provided tests in CalcTests.scala will tell you when you have it right: 9 - 2 - 3 is 4).

  3. Extend the language so an input is a comma-separated list of expressions evaluating to a List[Int], using %list(expr, ","), and wire it to evalList in Calc.scala.

Submitting

Your hw/First.scala implementation from HW 3 lives in the same repository, so one submission covers both: commit and push your work, then submit the repository via Gradescope. All tests must pass under sbt test. (The autograder checks the last line your program prints and accepts either a bare value, 4, or a Scala list, List(4).)