| 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
and . I am providing
a class, LispUtil, with (almost) these methods in it.
There are two basic ways to use these methods:
Using a .class file:
A
.classfile is a compiled.javafile. Just drop it into the folder with your other.javaand.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
.pkg,pkh, and.ctxtfiles from your folder, leaving only the.javaand.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.
Limitations:
- 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. TheSexpr(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.
Assumptions:
- Your class Sexpr contains
Sexpr(String)andSexpr(int)constructors.- Your constructors do any case coversions you deem necessary.
Usage:
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 yourSexprclass), so it must be called like an ordinary method; it is not implicitly called when you give a Sexpr as an argument toSystem.out.println(...)or when you concatenate a Sexpr with a String.Limitations:
- Does almost no error checking.
- Result may be equivalent to what you expect, without being identical. For example,
, and NIL = (). (a . (b . (c . NIL))) = (a b c)Assumptions:
- Your
Sexprclass containsSexpr(String)andSexpr(Sexpr, Sexpr)constructors.- Your
Sexprclass contains the additional method
public static String getValue(Sexpr atom)
- Returns the String value of an atom (and is only used for atoms).
- Your
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)Usage:
String myString = LispUtil.toString(mySexpr);
File: LispUtil.zip