Lab 5
9/30 @ 11:59 PM
Not accepted late
The Lab Assignment

For this lab, you'll use the concepts of polymorphism to build an abstract syntax tree for our simple calculator language, and implement a means to pretty-print the program. Pretty-printing refers to outputting a "canonical", formatted version of the code. One way to pretty-print a program is to extract it's AST, then recursively output concrete syntax for each node. This is the approach that will be taken by the project, and in this lab for the simple calculator language.

The calculator language has been enhanced somewhat, as you'll see in the parser.yy file. The goal is to make it so that ASTs in the language have a reasonable class hierarchy. How you do this is up to you, but it should be the case that you can run the code in calc.cpp and get the output:

calculate v 1+2*3
orate v

You should also be able to run the code:

IDNode * id = new IDNode("v");
NumNode * one = new NumNode(1);
NumNode * two = new NumNode(2);
NumNode * three = new NumNode(3);

MathNode * m = new MultNode(two, three);
MathNode * a = new MultNode(one, m);
OpNode * op1 = new CalculateNode(id, a);
OpNode * op2 = new OrateNode(id);

std::list nodes = new std::list();

ProgramNode * p = new ProgramNode(nodes);
p->print();

and get the same output as above. This largely means that you should update ast.hpp in order to declare these node types. You'll also need to update the parser.yy file to set the translation types of the nonterminals.

Finally, you should use concepts from the video appropriately:

  • Subclass functions should override superclass functions. At the very least, all classes should have a virtual print function, and subclasses should override the superclass print
  • The OpNode class should be pure virtual (i.e. it cannot be instantiated)

Starter code is available here: lab.tgz

Submission Instructions

Create a tarball consisting of a single directory with all of your work in it, such as code or written answers. If you have any auxiliary files (READMEs, Makefiles, etc.), be sure to include those as well. Name your work directory l5 and name the tarball l5.tgz. Upload your tarball to the L5 Canvas assignment.

Labs will be graded under one of following criteria:

  1. Effort: You will automatically be given full credit for the labwork if the GTA determines that your participation in the lab was meaningful - i.e. you attended the lab session and used the time to make a good-faith attempt to complete the work. It is the sole discretion of the GTA to determine if you put in sufficient effort. Even if you expect an effort-based grade, you should turn in your (possibly incomplete) work.
  2. Correctness: If you do not participant meaningfully in lab (i.e. you do not attend the lab session), your grade will be assessed based on the correctness of your lab submission.

Advice: How to Approach Labs

The two-criteria grading scheme above is designed to avoid wasting your time. You should not feel obligated to attend the lab, and in fact should only do so if you want help from the GTA on the labwork assignment or whatever project is currently in progress. Here's a handy flowchart for how I suggest you approach lab: