CSE 124
2017 October 2: Homework 1: Framing and parsing

FAQ

The FAQ has been moved to its own post.

Overview

In this exercise you are going to get some practice working with protocols. In particular, you’re going to learn to frame and parse input to implement a very simple calculator. The calculator protocol specified below is intentionally designed to be similar to HTTP/1.1, and so once you complete this assignment you will be in good shape to write the framing and parsing code for your project 1 web server.

You are going to write a C or C++ program that reads commands from an input file, and prints out the appropriate responses. A few examples are given later in this document.

SimpleCalc specification

Each SimpleCalc instance has an Accumulator, which is simply an integer variable. The accumulator’s initial value is 0. This value can be changed via the following commands:

  • ADD: Adds an integer to the accumulator, and stores the result back in the accumulator
  • SUB: Subtracts an integer from the accumulator, storing the result back in the accumulator
  • MUL: Multiplies an integer by the value of the accumulator, storing the result back in the accumulator
  • SET: Sets the value of the accumulator to the given integer

You only have to support integers (no floating point numbers), and the accumulator’s value, and all arguments to the above four commands, should fit into the uint64_t datatype (you do not need to support values outside this range). You do not need to support overflow or other corner-case/exceptional conditions.

Each command consists of the command name, followed by a space, followed by the argument, followed by a CRLF terminator. Thus, each command is on a line by itself.

A sequence of commands is permitted, and the end of that sequence is signified by a blank line. The accumulator is reset to 0 after each sequence.

Your program should read and parse commands, keeping a running value in the accumulator, until you encounter the end of the sequence (e.g., a blank line). At that point, your program should print out the value of the accumulator. Your program should keep reading in sequences until you reach the end of the file. Thus, your program should print out one result per sequence of commands.

Examples

In the following examples, EOF indicates the end of the file. Although not shown, there is an implicit CRLF after each command.

Example 1

Given input:

ADD 3
SUB 4
ADD 10
<CRLF>
<EOF>

SimpleCalc should print out

9

Example 2

Given input:

SET 3
ADD 3
ADD 2
<CRLF>
ADD 10
SUB 3
<CRLF>
<EOF>

SimpleCalc should print out

8
7

Example 3

Given input

SET 5
MUL 4
<CRLF>
SET 2
ADD 5
SET 3
<CRLF>
<EOF>

SimpleCalc should print out

20
3

Notes about line feeds

By default, Mac and Unix computers (e.g., Linux) end a line with a LF line feed character. Windows/DOS traditionally ended lines with a CR then a LF. If you want to create your own input files, and you do so on the lab computers or a UNIX-based system (including Macs), you’ll need to make sure that each line ends in the CRLF delimiter. There is a tool called unix2dos.sh inside of the tools/ subdirectory that will do just this for you. There is also a tool in that same directory that will print out information about line endings for files you give it, in case you want to verify that any sample files you create are of the right kind.

For the example files that I’ve provided to you, I’ve already ensured that each line ends in a CRLF pair. If you want to create your own files to test with, you will need to run unix2dos.sh to ensure that each line ends in CRLF.

Grading

You can modify the simplecalc.h and simplecalc.c files to implement your solution. Please do not modify the main.c or Makefile.

Evaluation

We’re going to run your program against a set of example SimpleCalc “programs”, and assign points proportionally to the number of correct responses. For example, if there were 10 programs in the input file, then each would be worth 12 of a point.

Due date

October 11th, 2017, 5pm

FAQ

The FAQ has been moved to its own post.

GitHub invitation

https://classroom.github.com/a/Nm3Knmdx