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.

   
The Grey Labyrinth Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups    RegisterRegister  
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Anyone know some good parsing code?

 
Reply to topic    The Grey Labyrinth Forum Index -> Science, Art, and Culture
View previous topic :: View next topic  
Author Message
Samadhi
+1



PostPosted: Thu Nov 08, 2007 3:48 pm    Post subject: 1 Reply with quote

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)
_________________
And he lived happily ever after. Except for the dieing at the end and the heartbreak in between.
Back to top
View user's profile Send private message Send e-mail MSN Messenger
Antrax
ESL Student



PostPosted: Thu Nov 08, 2007 5:38 pm    Post subject: 2 Reply with quote

http://en.wikipedia.org/wiki/Lexer
_________________
After years of disappointment with get rich quick schemes, I know I'm gonna get rich with this scheme. And quick!
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Samadhi
+1



PostPosted: Thu Nov 08, 2007 5:49 pm    Post subject: 3 Reply with quote

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).
_________________
And he lived happily ever after. Except for the dieing at the end and the heartbreak in between.
Back to top
View user's profile Send private message Send e-mail MSN Messenger
Samadhi
+1



PostPosted: Sun Nov 18, 2007 8:10 am    Post subject: 4 Reply with quote

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.
_________________
And he lived happily ever after. Except for the dieing at the end and the heartbreak in between.
Back to top
View user's profile Send private message Send e-mail MSN Messenger
Zag
Tired of his old title



PostPosted: Sat Feb 02, 2008 5:53 am    Post subject: 5 Reply with quote

Samadhi wrote:
(I'm using java)

Gaaah! Just use java.util.StringTokenizer. Also, recursive descent evaluators are the funnest thing you will ever write.
Back to top
View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger
Samadhi
+1



PostPosted: Sat Feb 02, 2008 10:28 am    Post subject: 6 Reply with quote

Oddly enough, that's exactly what I ended up using. I got an F. Yes, I am appealing.
_________________
And he lived happily ever after. Except for the dieing at the end and the heartbreak in between.
Back to top
View user's profile Send private message Send e-mail MSN Messenger
Zag
Tired of his old title



PostPosted: Tue Feb 05, 2008 4:53 pm    Post subject: 7 Reply with quote

Samadhi wrote:
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.
Back to top
View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger
Display posts from previous: by   
Reply to topic    The Grey Labyrinth Forum Index -> Science, Art, and Culture All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Site Design by Wx3