List/Sublist Projects: HW 13, 15, 16 ...
These projects revolve around the program for reading and printing
lists and sublists, as discussed in class.
(Note: this incudes an updated Tokenizer -- please use the new copy).
(Note: requires Java 1.5 at least)
Overview:
- First learn about how the provided program and data structure
works
- Provide a mechanism for handling the allocation of Token objects,
leading eventually to a kind of "garbage-collection" scheme.
- Implement some arithmetic functions, leading eventually to a
simple programming language with variables.
Due dates:
- #13: phase 1, week of Nov 26
- #15: phase 2, Wednesday Dec 5
- #16 phase 3, Wednesday Dec 12
- Final date for any homework: Friday, Dec 14
After Friday, you should be concentrating on exams for this and your
other classes. No homework will be accepted after Friday, Dec 14,
2007.
- Final exam, Tuesday Dec. 18, 3-6 pm, M-1-619
Assignment 13 (part one of the larger product) is due
Monday Nov
26.
unpack/download the program, run it, figure out what's
happening.
Add (recursive) functions to calculate the length of a list, the
number of Items in a whole structure (list and sublists)
Main part: pick up the program I've supplied -- use all of what's
there, this is a fresh start (though some of the pieces are
familiar). Run it, and learn how it's going about building
a
list/sublist structure.
Notice that it has some debugging output turned on.
- Main loop: read in an Item (Atom or list with
possible
sublists), and then print it
- Debugging: the loop under construction is printed each time a new
Token appears.
Also notice the use of Java interface and implements in
this program. You'll need to understand how this mechanism works!
SUGGESTION: Run the program, and start with simple things: atoms
apple
beet grape
then a simple list:
(how are you)
and when you feel you're beginning to understand that, try some more
complex ones:
(here is a (list with (several sublists)) really)
(a b c (d e (f g)) h)
(1 2 () 3 ((4 5)) 6)
You might want to add some more print statements to get out more
info.
Did
they print what you thought they would?
The second part of assignment 13 is to add to the program, a function
to sweep through and count how many atoms there are.
Assignment 15, due Wednesday Dec 5
Create a mechanism for providing objects of type Token, which will
replace the use of "new Token(...)" throughout the rest of the
program.
Old:
t = new Token(stringVal);
New:
t = TokenPool.createToken(stringVal);
(createToken is a static factory function inside class TokenPool)
Your program will most likely include the following portions.
(see
class notes for Wednesday, Nov 28)
- A framework for the TokenPool, including a place to hang on to
the "free" Tokens who are waiting for someone to request them.
- Initialization logic to actually allocate all the Tokens you'll
be managing (allocate them using "new Token"), and place them into the
free list.
- Factory functions, createToken(...), which pick a Token off the
free list, initialize it just as the constructor had been doing (using
the arguments provided), and return it.
- If the pool runs out of Tokens, print a message and let the
program
die.
There are several ways to handle the issue of hanging on to the pool of
free Tokens.
(Of course, there will be a loop in Initialization which repeatedly
calls for a "new Token()"; the reference to each such Token has
to be held somewhere, available when a caller asks to createToken(...) )
- You could have an array of Tokens, then keep a pointer "nextFree"
which is incremented each time you deliver a Token.
- You could use a java.util.Collection mechanism -- like
ArrayList<Token> -- add() Tokens at Initialization, remove() at
createToken.
- You could make your own linked list, running links through each
Token object. Putting things on the head of a list isn't too
hard, nor is taking off the first item.
Any of these will work fine: for this phase, it doesn't matter
much -- use whichever you're most comfortable with.
For phase 3 of the project, however, it will helpful to use a
combination of (1) and (3). Have an array which keeps track of
all Tokens, but also create a simple linked list, and use that for
providing Tokens to createToken().
(If you've used a different approach at first, you'll not have much
work to retool it: all these are pretty easy).
Notice that the Item/list reader program is providing a customer for
Tokens. As it reads input, it constructs an Item/list data
structure
containing Tokens. After printing this list (and perhaps
evaluating
it), it discards the list -- thus potentially making the Tokens
available for garbage collection -- and reads another Item/list.
Calculator sub-project:
Also, please add an "eval" (evaluation) function. Add it to the
interface Item, as well as to Atom and IList. This will provide a
simple calculator functionality. Just "plus" and "times" are
enough,
thus
(plus 2 3) will result in 5
(plus 2 (times 3 4)) will result in 14
(times 2 3 4) will
result in 24
Since we have parentheses to help , we can build sublists of any
length. Might as well pass that along to the user, so you can
type (plus 7 8 5 11) instead of (plus 7 (plus 8 (plus
5 11)))
Allowing variable numbers of operands also avoids having to report an
error when there aren't the right number of operands. But what to
do if there are no operands at all? We'll need to define a
reasonable answer. If you do this right, you might not even need
a special-case check, it can just fall out of the normal processing.
What to do if the function name is not recognized? (it might be
easier to return a value, like 0). Your choice -- though you
should document what you decide.
---- More to come for phase 3 ----