• 10-19-2008, 03:56 PM
thekermo
Hey guys, i'm making a program that will read in a simple formula, decompose it into it's subformals, work out the truth values of these and then print out the results. Most of this I can do without any problem but i'm struggling decomposing the damn formulas. A basic example of a formula would be a(b(a,b)). I need to be able to split this down to a, b, (a,b) and b(a,b) and store those four values into an array. The storing i can do fine but I can't get the string to split up accurately. I've played around with stringTokenizer and streamTokenizer but i'm not sure if that's really the way to go. Any ideas? Any advice will be much appreciated and if I could, i'd give you all some cake.
• 10-19-2008, 05:08 PM
Nicholas Jordan
recrusive descent parser
This challenge is traditional computer science, called a recursive descent parser. Suggest to start google for just exacly that term, there is a list of resources at Wikipedia - Recursive descent parser See as well David J. Eck and Sedgewick's "Algorithms in Java".
Quote:

Originally Posted by thekermo
Hey guys, i'm making a program that will read in a simple formula, decompose it into it's subformals, work out the truth values of these and then print out the results.

Define "subformals" and truth values.
Quote:

Originally Posted by thekermo
Most of this I can do without any problem but i'm struggling decomposing the damn formulas.

Digraphic Algorithm Mutating Nuance - D.A.M.N. be sure to acronymize your Clear Understandable Student Studies - C.U.S.S. 'in. ( yuk-yuk !)
Quote:

Originally Posted by thekermo
A basic example of a formula would be a(b(a,b)). I need to be able to split this down to a, b, (a,b) and b(a,b) and store those four values into an array.

Combinatorics. Donald E. Kuth has fascicle 0 for vol-4 in print now, that may be of some utility in basics and is the definitive reference for the work you have to hand.
Quote:

Originally Posted by thekermo
The storing i can do fine but I can't get the string to split up accurately. I've played around with stringTokenizer and streamTokenizer but i'm not sure if that's really the way to go.

I would skip the stream stuff for basic studies, place the sample data in an Array or List directly in the sample code. eg:
Code:

```class SampleCombinatorics{ private static final String sampleData = "a(b(a,b))";```
Doesn't exactly do what I would think of as building a Tree but is much more useful during concepting and studies plus is easier to modifiy the sample data. You can go to a traditional io file at any time onces something appears to work.
Quote:

Originally Posted by thekermo
Any ideas? Any advice will be much appreciated and if I could, i'd give you all some cake.

I prefer Chocolate, heavy on the Chocolate and premium grade. Check shipping regulations and throw in one standard grade apple pie to cover a bet I have with a master, I have the flag already covered.

Whatever you do, don't send me any low-carb anything. Oh, and yes - Cay S. Horstmann has some remarkably clear writings on the subject. No shortage of study material - focus on Lists, Trees, Stacks and related matters - reading in the data is a distraction at this point.

I do not see any tokens in your sample data, why are you using StringTokenizer?
Code:

```String[] result = "this is a test".split("\\s"); for (int x=0; x<result.length; x++) System.out.println(result[x]);```
"a(b(a,b))" - there's nothing to split on. Try if(nextCharacter = "(")StackPush(); or something - the way this is done has factors in common with the size of the dataset and efficiency.
• 10-19-2008, 06:24 PM
Norm
Quote:

I can't get the string to split up accurately
Can you show us the code you are using to parse the expressions?