Harvey Mudd College

Computer Science 60

Spring 1998

Assignment 7

Due Wed. 1 April 1998

Unicalc Applet

(Link to a sample Unicalc applet)

This assignment is 125 points of your overall grade. (Note: Your solution need only contain the first four text fields in the applet; the rest are purely optional.)

The purpose of this assignment is to gain experience in constructing a parser. The parser will parse input typed by the user in an applet. The result of the Unicalc conversion of the input expression will be displayed in the applet. The parser will also be used to input the database, initially from a file, but ultimately through URL http://www.cs.hmc.edu/~keller/cs60/unicalcDB.txt . Information on reading from a URL will be provided.

A sample applet of the form we want is the tautology checker (source). This parses logical expression input and tells whether the result is a tautology. The tautology checker does not read anything from a file or URL however.

You may fashion your parser after the one in the tautology checker. The Unicalc grammar, given below, is a little different from that tautology-checker's grammar. For the front-end, you may adapt the one in the tautology checker; there is very little to change here.

You already have a working Unicalc kernel (or you may use mine). This will be used by your applet.

Overall then, this applet consists of:

The grammar specified below uses the following meta-symbols:

| denotes alternates

{ } denotes 0 or more repetitions of

[ ] denotes optional

Unicalc grammar:

    Q -> U  E                                         // Equation (in database)

    E -> P { '/' P }                                  // Expression

    P -> I {I}                                        // Product

    I -> B { '^' N }                                  // Item

    B -> U | F |  '(' E ')'                           // Basic Item

    U -> L{L}                                         // Unit

    F -> ( D{D} [ '.' {D} ] | '.' D{D} )['e' N ]      // Floating point numeral

    N -> S D{D}                                       // Numeral

    S -> [ '+' | '-' ]                                // optional Sign

    D -> '0'|'1'|'2'|'3'|'4'|'5'|'6'|'7'|'8'|'9'      // Digit

    L -> 'a'|'b'|'c'|'d'|'e'|'f'|'g'|'h'|'i'|'j'      // Letter
        |'k'|'l'|'m'|'n'|'o'|'p'|'q'|'r'|'s'|'t'
        |'u'|'v'|'w'|'x'|'y'|'z'|'A'|'B'|'C'|'D'
        |'E'|'F'|'G'|'H'|'I'|'J'|'K'|'L'|'M'|'N'
        |'O'|'P'|'Q'|'R'|'S'|'T'|'U'|'V'|'W'|'X'
        |'Y'|'Z'|'_'

Whitespace (space and tab) is allowed between any syntactic entities except among digits in an N or among letters in a U. Ends-of-line are not considered whitespace; they terminate equations (in the database) and expressions (from the user).

Interpretation of the Grammar:

An equation (Q) (which only appears in the database) is a unit (U) followed by an expression (E).

Example of a Q, which states the definition for a joule:

joule     1 kilogram meter^2 / second^2

An expression (E) is a product (P), followed by any number of slash-product combinations.

Example of an E:

1 kilogram meter^2 / second^2

A product (P) is an item (I) followed by 0 or more items.

Example of a P:

1 kilogram meter^2

An item (I) is a basic item (B) followed by 0 or more carat-numeral combinations.

Example of an I:

meter^2

A basic item (B) is either a unit, a floating point numeral, or an expression (E) in parentheses.

Examples of B's:

meter
4.56e-23
(1 kilogram meter^2 / second^2)

A unit (U) is a series of 1 or more letters.

A floating point numeral (F) is (either one or more digits, followed by an optional point and additional digits or a point and one or more digits), followed by an optional 'e' and a numeral.

Examples of F's:

1
1.2
1.2e4
 .2e4
1.2e-4
 .2e-4
12.34e56
.34e56

A numeral (N) is an optional sign followed by one or more digits.

Examples of N's:

0
1
123
+123
-123

An optional sign (S) is '+' or '-' or the empty string.

A digit (D) and a letter (L) are as defined in the grammar.