Due April 23, 2007

Assignment

You are write and turn in a Java program that meets the following
specification. You do not need to turn in any evidence of testing if
the program works correctly. However, if the program fails in some
way to meet its specification, you are expected to turn in a statement
regarding the shortcoming. If you fail to do so, your grade will be
reduced both for your failure to document the error as well as for the
error itself. If there is any aspect of the specification that isn't
clear to you, please ask about it.

Input

Your program should read text from the standard input that
constitutes a block, as described by the following BNF grammar:

A <name> is a token that starts with a letter
and then can contain zero or more additional characters that are
letters, digits, or underscores. No whitespace may occur within a
name or any of the keyword tokens (begin,
end, declare, or use). At
least one whitespace character must seperate each pair of consecutive
tokens. Additional whitespace may freely occur before the first
token, after the last token, or between tokens.

If the input does not conform to this specification, your program
should process the input until the point where the syntax error
becomes apparent and then print out the message "Syntax error" on the
standard error output and exit. This handling of syntactically
illegal inputs will count for 20% of the grade of the assignment.

Output

Your program should echo back the input on the standard output, but with two changes.
(You should only echo the input back a single time, with both changes
in place. Don't echo it back once with the first change and a second
time with the other change.)
First, although the whitespace in the input is largely irrelevant, the
output should be formatted to show its structure. Each line should
include exactly one begin, end,
declare or use. The indentation of the
lines should correspond to the block structure: two spaces of
indentation per enclosing block.
The second change your program should make in the output (as
compared to the input) is that each declaration statement or use
statement should be followed by an annotation as in the following example:

Notice that each legal declaration receives a number one higher
than the previous one. These numbers are stored for later reference
in the use statements. An illegal
redeclaration does not advance the number by one.

Data Structure

Your program should use a generic class, ScopedMap,
which you must write. That class should conform to the documentation
that I am linking to this assignment. This class will count for 70%
of the grade of the assignment.

Your job will be easier if you use some of the predefined generic
types in the Java API. Moreover, if you fail to do so, but rather
build the ScopedMap class from scratch, you will not get
full credit.

There are multiple plausible ways to store this data structure.
Some are more efficient than others. You will receive extra credit if
your design allows your program to process input of length
n in time that is big theta of n, not counting
the time spent indenting the output. (For some inputs, the
indentation may itself require quadratic time.)