0

So I dont have much experience working with Linked List in java and I got this assignment that requires to store values on a Linked List. In summary is creating class that deals with Lisp expressions. In particular, it creates 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


My professor told me this: "So, the sublist or list you have to do the same as the complete list, that means that the recognizer process can be recursive."

I have a Lisp generic class, a LinkedList class, and a class for breaking the expression into tokens.

Any idea?

2
  • Sounds more like a tree than a list. Commented Feb 14, 2014 at 23:23
  • @BrianRoach yeah, I thought the same too, but Im required to build a Linked List instead Commented Feb 14, 2014 at 23:27

1 Answer 1

1

I think what you want here is a Composite Pattern implementation. When you have two types that you want to treat similarly, you make a composite. Parsers typically have node and leaf classes, or compound and terminal expressions. If you have both implement the same interface, then you can easily make a list that describes the parsing process because each node in the list could be a collection of other nodes. So your instincts are right, that you are dealing with a tree. A tree that looks like a list is usually a Composite.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.