# The Grey Labyrinth is a collection of puzzles, riddles, mind games, paradoxes and other intellectually challenging diversions. Related topics: puzzle games, logic puzzles, lateral thinking puzzles, philosophy, mind benders, brain teasers, word problems, conundrums, 3d puzzles, spatial reasoning, intelligence tests, mathematical diversions, paradoxes, physics problems, reasoning, math, science.

Message body

 Emoticons View more Emoticons
 [quote="Samadhi"]I didn't want to read up on all that so I worked it out myself. Here's how I did it. Let me say that this is a [strike]compiler[/strike] link editor for assembly code. Very strict fields and such, so I could rely on that. I'd probably have to utilize some kind of binary tree to do anything with any higher level language. Also, my input is a file that has already parsed internal references and any immediate values. Steps 1. Eliminate the parentheses. Dealing with the possibility of nested parentheses and multiple groups was annoying. Recursion was key of course. I always did one of two things. Remove the parentheses. Or remove the parentheses and change the sign of everything inside. 2. Remove numerical operands (they've been taken care of already). 3. Remove internally defined variables, adding a plus or minus start hex when necessary. Method 1. relies on many of the same algorithms as 2 and 3 but has annoying case logic that I won't go into. 2. Since I'd removed the parentheses I knew that operands would be separated only by '+' or '-'. Here's the essence. [code]//z = 0 initially //s is the input string while (z64) blah = blah + temp; //this assumes good input so anything above 64 is not a number z = end; //this moves to the next set or makes the loop stop }[/code] 3. Basically the same algorithm but I had to keep track of internal negatives and positives. EG If AAA and BBB are defined variables in programZ, then AAA-BBB would always be the same number. But if it were AAA+SomeValueFromAnotherProgram then it you need to modify that line by +whatever address AAA has. So if it's AAA-AAA or something like that, no additions. Else add a +/- modifier.[/quote]
Options
HTML is OFF
BBCode is ON
Smilies are ON
 Disable BBCode in this post Disable Smilies in this post

 All times are GMT
 Jump to: Select a forum Puzzles and Games----------------Grey Labyrinth PuzzlesVisitor Submitted PuzzlesVisitor GamesMafia Games Miscellaneous----------------Off-TopicVisitor Submitted NewsScience, Art, and CulturePoll Tournaments Administration----------------Grey Labyrinth NewsFeature Requests / Site Problems
 Topic review
Author Message
Zag
Posted: Tue Feb 05, 2008 4:53 pm    Post subject: 1

Oddly enough, that's exactly what I ended up using. I got an F. Yes, I am appealing.

Tell the teacher that a friend of yours with over 20 years' experience as a software engineer said that you were insane to write your own parser unless the assignment specifically said you had to. The mark of good engineering includes not re-inventing the wheel every time, but using the tools that are available.

Then offer to re-implement a simplified version of StringTokenizer yourself, just to show you can, and re-run your program using your new version of it.
Posted: Sat Feb 02, 2008 10:28 am    Post subject: 0

Oddly enough, that's exactly what I ended up using. I got an F. Yes, I am appealing.
Zag
Posted: Sat Feb 02, 2008 5:53 am    Post subject: -1

(I'm using java)

Gaaah! Just use java.util.StringTokenizer. Also, recursive descent evaluators are the funnest thing you will ever write.
Posted: Sun Nov 18, 2007 8:10 am    Post subject: -2

I didn't want to read up on all that so I worked it out myself. Here's how I did it.

Let me say that this is a compiler link editor for assembly code. Very strict fields and such, so I could rely on that. I'd probably have to utilize some kind of binary tree to do anything with any higher level language.

Also, my input is a file that has already parsed internal references and any immediate values.

Steps
1. Eliminate the parentheses. Dealing with the possibility of nested parentheses and multiple groups was annoying. Recursion was key of course. I always did one of two things. Remove the parentheses. Or remove the parentheses and change the sign of everything inside.
2. Remove numerical operands (they've been taken care of already).
3. Remove internally defined variables, adding a plus or minus start hex when necessary.

Method
1. relies on many of the same algorithms as 2 and 3 but has annoying case logic that I won't go into.

2. Since I'd removed the parentheses I knew that operands would be separated only by '+' or '-'. Here's the essence.
Code:
//z = 0 initially
//s is the input string
while (z<s.length()){  //z is the step through operands
end1 = s.indexOf("+",z+1);
end2 = s.indexOf("-",z+1);
if (end1<0) end1 = end2;
else if (end2<0) end2 = end1;
end = Math.min(end1,end2);
if (end<0) end = s.length();
//the last six lines established the next '+' or '-'
//or if no next then it's the rest of the string
//there's got to be a better way to do this
temp = s.substring(z,end);
if ((int)temp.charAt(1)>64)   blah = blah + temp;
//this assumes good input so anything above 64 is not a number
z = end; //this moves to the next set or makes the loop stop
}

3. Basically the same algorithm but I had to keep track of internal negatives and positives. EG If AAA and BBB are defined variables in programZ, then AAA-BBB would always be the same number. But if it were AAA+SomeValueFromAnotherProgram then it you need to modify that line by +whatever address AAA has.
So if it's AAA-AAA or something like that, no additions. Else add a +/- modifier.
Posted: Thu Nov 08, 2007 5:49 pm    Post subject: -3

I saw this at the end of the page
Quote:
Compiling with C# and Java
That's a book I'd like to have, since that's exactly what I'm doing (Java).
Antrax
Posted: Thu Nov 08, 2007 5:38 pm    Post subject: -4

http://en.wikipedia.org/wiki/Lexer
Posted: Thu Nov 08, 2007 3:48 pm    Post subject: -5

For example, suppose I've got
ENDA-LISTA-(ENDB-LISTB) [or something like that, there won't be an * or /]
I basically need to split that into
+ENDA
-LISTA
-ENDB
+LISTB

There's some other crap I need to do with it but I'm not really worried about that.

(I'm using java)