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.
Your program should read text from the standard input that constitutes a block, as described by the following BNF grammar:
<block> ::= begin <stmts> end <stmts> ::= <empty> | <stmt> <stmts> <stmt> ::= declare <name> | use <name> | <block>
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.
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:
begin declare x {declaration 1} use y {illegal undeclared use} declare y {declaration 2} begin use x {references declaration 1} declare x {declaration 3} use x {references declaration 3} declare x {illegal redeclaration} use x {references declaration 3} declare y {declaration 4} end use x {references declaration 1} end
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.
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.)