See the assignment turn-in page (last modified on 21 January 2003) for instructions on turning in your assignment.
The basic units can be used to define other units. For example, speed can be defined as
mps = m/s
Units can also be defined as a multiple (called a scale factor) of another unit:
cm = m/1000 foot = cm/30.38
Units can also be defined as a combination of scale factors and other units
acre = foot^2/43560
Over the years, many people have proposed adding SI (or similar) units to programming languages to provide another source of protection against programming errors. For example, given the variable declarations
int age year; int height m;
the statement
height = height + age
is legal in C++ (because both are integers) but would be illegal if units were taken into account because you can't meaningfully add years to meters. Units take part in operations along with their associated values. The following code
double distance m; double time s; double speed mps; speed = distance/time
is valid because dividing meters by seconds gives you the same units as mps (assuming the definition given above). Conversions between units, where defined, are done automatically. The code
double heighti inches; double heightf foot; heightf = heighti
is valid because (let us assume) a foot has been defined as 12 inches (and an inch, in turn, has been defined as 0.0254 meters).
A unit definition has the form:
is
unit-equation
A unit-name is a sequence of one or more letters (case doesn't matter). A
unit-name is unique; it must appear exactly once to the left of the
is
in a unit definition.
A unit-equation is either
unit-equation unit-operation unit-equation | or |
( unit-equation ) |
or |
unit |
A unit is one of the pre-defined units meters, grams or seconds, an already defined unit, or a number.
m
| g
| s
| unit-name | integer | double
A unit-operation is either multiplication, division, or exponentiation.
*
| /
| ^
The right-hand argument to the exponentiation operator must be a number; the
unit-equation m^0.5
is syntactically valid, while the unit-equation
m^s
is syntactically invalid. Exponentiation has higher precedence than
does multiplication and division and is left associative; multiplication and
division have the same precedence and are also left associative. Parenthesis
alter precedence in the usual way.
A variable declaration has the form
:
unit-spec
A var-name has the same syntax as a unit-name. A unit-spec is either a unit-name or a unit-equation
When defined, a variable contains an undefined, non-numeric value. Because the value is non-numeric, it cannot legally be used in a computation.
A variable-assignment has the form
=
equation
A var-name has the same syntax as a unit-name. An equation is either
equation operation equation | or |
( equation ) |
or |
value optional-unit-spec |
A value is either an integer, a double or a variable.
An operation is one of the four standard operations.
+
| -
| *
| /
An optional-unit-spec is either a unit-spec or nothing.
If var-name is a scalar value then the unit-spec will be missing. Each unit-spec should be in simplest form possible. A unit-spec is in simplest form if
inch*inch
is not in simplest form; inch^2
is in simplest form.
s*m/s
is not in
simplest form; m
is in simplest form.
acre
is defined to be foot^2/43560
, then foot^2/minute
is not
in simplest form; acre/minute
is in simplest form. Two units that are a
constant factor of one another do are of equivalent simplicity.
If the input contains errors, either in syntax or use, then the only output that should appear are error messages written to std-err. If any errors are found in unit definitions or variable declarations, then all such errors should be printed, and nothing should be printed about variable assignments. For example, if the input is
foot is m + s v: furlong time: s len: m v = time + len
The error messages about foot
's definition and v
's declaration should
be output, but no error about v
's assignment should be printed to std-err.
If both the unit definitions and variable declarations have no errors, then all errors discovered in the variable assignments should be printed to std-err and no variable assignments should be printed.
This page last modified on 7 April 2003.