After this lab, you will be able to:
- extend an LL(1) grammar spec with new operators, a
%left/%binaryprecedence ladder, and%listrepetition, and connect the parser’s actions to an evaluator.
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):
Add subtraction and division.
The starter grammar’s
exprTailis right-recursive, so9 - 2 - 3evaluates to 10. Fix the grammar with a%leftblock and a%binaryrule (the provided tests inCalcTests.scalawill tell you when you have it right:9 - 2 - 3is 4).Extend the language so an input is a comma-separated list of expressions evaluating to a
List[Int], using%list(expr, ","), and wire it toevalListinCalc.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).)