freebasic.net Forum Index
FreeBASIC's Official Forums
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister   ProfileProfile   Log inLog in

Parsing 101

 
Post new topic   Reply to topic    freebasic.net Forum Index -> Tips and Tricks
View previous topic :: View next topic  
Author Message
notthecheatr
Master
PostPosted: Dec 14, 2007 20:18    Post subject: Parsing 101 Reply with quote

I wrote a simple object called words_list which takes a string and splits it into words. Its main use would probably be parsing, since for example it'll take something like this:

Code:
Dim a As Integer


and give you the words

Code:
Dim

Code:
a

Code:
As

Code:
Integer


It leaves line comments (since it works line-by-line, multi-line comments can't be supported) and whitespace out of the words it produces, and strings inside quotation marks are all one word.

May be useful to someone anyways, here's the ZIP with the code, example, and readme: http://notthecheatr.phatcode.net/downloads/words_list.zip
 
Back to top
View user's profile Send e-mail Visit poster's website
1000101
Hero
PostPosted: Dec 15, 2007 1:56    Post subject: Reply with quote

I hate to be a buzz-kill, but...strtok()
 
Back to top
View user's profile
duke4e
Sr. Member
PostPosted: Dec 15, 2007 3:04    Post subject: Reply with quote

strtok()
what the hell is that?
 
Back to top
View user's profile Send e-mail Visit poster's website AIM Address Yahoo Messenger MSN Messenger
Peter

PostPosted: Dec 15, 2007 3:21    Post subject: Reply with quote

strtok = String Tokenize, basically it breaks down a string into the component pieces i.e. words. It pretty much does what your trying to do with your code. That said, I think your code is great, it gives you the basis to make much more complex stuff that is (and I think this is important sometimes) completely independent of outside calls.

Back to lurkerdom for me now.
 
Back to top
View user's profile
MichaelW
Hero
PostPosted: Dec 15, 2007 12:22    Post subject: Reply with quote

For general-purpose use your whitespace should probably include tab characters. Strtok is fast and easy to use, but a byte scanner is probably a better choice where you need the length of each token and/or where you need to detect errors such as unmatched quotes.

Finding Tokens in a String
 
Back to top
View user's profile
notthecheatr
Master
PostPosted: Dec 16, 2007 1:16    Post subject: Reply with quote

Now I know why people complain.

Actually, it's mostly a learning thing. Like I said, the code's pretty much a hack, but it might be useful and that's why I wrote it. I don't really care if there's something else that already does that, as using that other tool won't really teach me anything. Sure, it's good to use other tools, but there's nothing wrong with writing your own thing, is there?


P.S. This is an object. I could write an object based on strtok() but in any case it doesn't matter... my point is that using an object makes it a much more natural interface.
 
Back to top
View user's profile Send e-mail Visit poster's website
Peter

PostPosted: Dec 16, 2007 2:43    Post subject: Reply with quote

Quote:
Sure, it's good to use other tools, but there's nothing wrong with writing your own thing, is there?

No, there's nothing wrong with it. Especially when your learning to program I feel it is important to do as much coding as possible. When you have to code to a deadline it is right nice to know that someone else did some of the work for you already though. That all said.... when I was a wee lad all excited because my aunt showed me a few commands on my parents BBC Micro I spent several days writing a way to perform a a set number of times. I was so proud of it and wanted to show it off. When I did I was shocked when told about this "FOR...NEXT" thing did the same blasted thing!
 
Back to top
View user's profile
yetifoot
Master
PostPosted: Dec 17, 2007 15:42    Post subject: Reply with quote

Peter wrote:
Quote:
Sure, it's good to use other tools, but there's nothing wrong with writing your own thing, is there?

No, there's nothing wrong with it. Especially when your learning to program I feel it is important to do as much coding as possible.


I also think it's good to learn how it's done, at least the first time. I prefer to hand write the lexer/scanner part, rather than use strtok, as strtok is not flexible enough sometimes.
 
Back to top
View user's profile Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    freebasic.net Forum Index -> Tips and Tricks All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot 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



sf.net phatcode