
FreeBASIC's Official Forums
|
| View previous topic :: View next topic |
| Author |
Message |
|
|
Posted: Dec 14, 2007 20:18 Post subject: Parsing 101 |
|
|
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:
and give you the words
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 |
|
 |
|
|
Posted: Dec 15, 2007 1:56 Post subject: |
|
|
| I hate to be a buzz-kill, but...strtok() |
| |
|
| Back to top |
|
 |
|
|
Posted: Dec 15, 2007 3:04 Post subject: |
|
|
strtok()
what the hell is that? |
| |
|
| Back to top |
|
 |
|
|
Posted: Dec 15, 2007 3:21 Post subject: |
|
|
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 |
|
 |
|
|
Posted: Dec 15, 2007 12:22 Post subject: |
|
|
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 |
|
 |
|
|
Posted: Dec 16, 2007 1:16 Post subject: |
|
|
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 |
|
 |
|
|
Posted: Dec 16, 2007 2:43 Post subject: |
|
|
| 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 |
|
 |
|
|
Posted: Dec 17, 2007 15:42 Post subject: |
|
|
| 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 |
|
 |
|
|
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
|
|