|Functions for Fourth Assignment
Dave Matuszek, Spring 2003
If you have not completed the third assignment (Implementing Lisp, Part I) successfully, then you are in trouble for the fourth assignment (Implementing Lisp, Part II). The following should be of assistance.
The hardest methods to write for Part I are
. I am providing
LispUtil, with (almost) these methods in it.
There are two basic ways to use these methods:
Using a .class file:
.classfile is a compiled
.javafile. Just drop it into the folder with your other
.classfiles and start using it. You don't need the source code. I'm not giving out source code until the final deadline for Part I.
This should work fine with BlueJ. If it doesn't, try this: Delete BlueJ's
.ctxtfiles from your folder, leaving only the
.classfiles. Then use BlueJ's Open Non BlueJ... command. Something similar should work with any other IDE you are using.
public static Sexpr parse(String input)
Given an input string, parses it and returns it as an S-expression. Dotted pairs are handled correctly.
- If the input string contains multiple S-expressions, only the first one is returned.
- All atoms use the
Sexpr(String)constructor, even if they are composed entirely of digits. The
Sexpr(int)constructor is not used.
- Some illegal strings will be parsed as if they were legal. In particular, if a string has too few closing parentheses, more will be added automatically.
- Your class Sexpr contains
- Your constructors do any case coversions you deem necessary.
Sexpr mySexpr = LispUtil.parse("(a . (b c))");
public static String toString(Sexpr sexpr)
Given an S-expression, returns a printable representation of that S-expression. Dotted pairs are handled correctly.
Despite the name, this method does not and cannot override
toString()(because it's in my class, not your
Sexprclass), so it must be called like an ordinary method; it is not implicitly called when you give a Sexpr as an argument to
System.out.println(...)or when you concatenate a Sexpr with a String.
- Does almost no error checking.
- Result may be equivalent to what you expect, without being identical. For example,
NIL = ()
(a . (b . (c . NIL))) = (a b c)
Sexprclass contains the additional method
public static String getValue(Sexpr atom)
- Returns the String value of an atom (and is only used for atoms).
Lispclass contains the following methods, as defined in Part I:
public static Sexpr car(Sexpr sexpr)
public static Sexpr cdr(Sexpr sexpr)
public static boolean atom(Sexpr sexpr)
public static boolean isNull(Sexpr sexpr)
String myString = LispUtil.toString(mySexpr);