Question

How do you create a reference based linked list from an expression, and implements a number of methods to perform operations on the list. As provided by my professor, he use the tokenizer to break the expression. Example:

parse("(+ 3 5 ( 23.2 -1a 1/2 \"abc\" ) ) "); 

And my assignment is to add it to a simple linked list, but my problem is that for every open parentheses there's a new list and a parentheses inside this is a sublist.

For the case: (4 * (45 + 3) - (7/5))

(45 + 3) is a sublist or list. (7/5) is a sublist or list 4 is integer * symbol 45 integer + symbol 3 integer - symbol 7/5 ratio

So for I have successfully created a tokenizer to grab values and place them in a linked list. The problem comes when I have to make sublists.

I have tried to create an LispList object with its own "head" variable to hold the values in that sublist. And temporarily moving the tokenizers main list pointer to the sublists "head" until the token reaches a ")" token but I still get an empty sublist list.

Any ideas on how to create the sublists Would recursion be a better option? Thanks!

This is the LispConversion class that breaks down the list.

public class LispConversion{

public LispObject head;
public LispObject temphead;

public void parse(String values){  
    StreamTokenizer t = new StreamTokenizer(new BufferedReader(new StringReader(values)));
    t.resetSyntax();   
    t.wordChars(0,255); 
    t.whitespaceChars(0,' '); 
    t.commentChar(';'); 
    t.quoteChar('"');
    t.ordinaryChar('('); 
    t.ordinaryChar(')');

    try {
        t.nextToken();
    }
    catch (IOException e){
        System.out.println(e);
    }

    int counter = 0;
    while (t.ttype != StreamTokenizer.TT_EOF){

        switch (t.ttype) {

        case StreamTokenizer.TT_WORD: 
            try{ 
                int value = Integer.parseInt(t.sval);
                head = new LispInteger(head, value);
                } 

            catch (NumberFormatException e1){
                try{
                    double value = Double.parseDouble(t.sval);
                    head = new LispFloat(head, value);
                    } 
                catch (NumberFormatException e2){
                    String[] array = t.sval.split("/");
                    if (array.length != 2){ 
                        String value = t.sval;
                        head = new LispSymbol(head, value);
                    } 
                    else {
                        int numerator = Integer.parseInt(array[0]);
                        int denominator = Integer.parseInt(array[1]);
                        head = new LispRatio(head, numerator, denominator);
                    }
                }
            }
            break;

        case '"': 
            String value = t.sval;
            head = new LispString(head, value);

        break;

        default: 
        counter++; //So the first '(' is not read I use a counter.
        if((char)t.ttype == '(' && counter > 1){

            LispList newList = new LispList(head);
            head = newList;

            temphead = head;
            head = newList.listHead;    
            }
        if((char)t.ttype == ')'){
            head = temphead;
            System.out.println();
        }


        }

        try {
            t.nextToken();
        }
        catch (IOException e){
            System.out.println(e);
        }
    }
}

This is my LispList Object.

public class LispList extends LispObject {

public LispObject listHead;

public LispList(LispObject next){
    super(next);
}

public void printList(){
    System.out.print("List: ");
    if(listHead == null){
        System.out.println("The list is empty.");
        return;
    }
    LispObject temp = listHead;
    while(temp != null){
        System.out.print(temp + " ");
        temp = temp.next;
    }
    System.out.println();
}

No correct solution

OTHER TIPS

Generics in Java exist. They are represented by <> symbols. To use a Linked List, import java.util.LinkedList class in your program and use it like:

LinkedList<Integer> integerLinkedList = new LinkedList<Integer>();
integerLinedList.add(56);

LinkedList<Character> characterLinkedList = new LinkedList<Character>();
characterLinkedList.add('(');

Check out the LinkedList documentation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top