5 ms·
Can anyone elaborate on the bottom up method with a small but concrete example? Say I want to parse a special format of strings, read part of each line and dump
by markus_zhang 7y ago
Can anyone elaborate on the bottom up method with a small but concrete example? Say I want to parse a special format of strings, read part of each line and dump into say. CSV, how would a Forth programmer approach the problem?
- rabidrat 7y agoOne of the things about Forth, is that the techniques have not proven to be amenable to generalization. Chuck Moore himself says[0]: > I wish I knew what to tell you that would lead you to write good Forth. I can demonstrate. I have demonstrated in the past, ad nauseam, applications where I can reduce the amount of code by 90% percent and in some cases 99%. It can be done, but in a case by case basis. The general principle still eludes me. --- I would have to start by asking, what is the essence of what has to happen to parse the string? How I would approach the problem depends a great deal on the specifics of the actual problem. So, your "concrete" example problem would have to be a lot more concrete before a Forth programmer could even start tackling it. That's the nature of a bottom-up method. [0] http://www.ultratechnology.com/moore4th.htm http://www.ultratechnology.com/moore4th.htm
- lifthrasiir 7y agoI feel the following line (from [1], quote original) is a way better summary: > Remember the freedom quote? "Forth is about the freedom to change the language, the compiler, the OS or even the hardware design". > …And the freedom to change the problem. [1] http://yosefk.com/blog/my-history-with-forth-stack-machines.html http://yosefk.com/blog/my-history-with-forth-stack-machines....
- kragen 7y agoYosef Kreinin is a smart guy and a good writer but probably not a reliable authority on Forth.
- lifthrasiir 7y agoYes, but I didn't need an authority. I just needed a good summary for what Moore tries to say but not.
- kragen 7y agoI don't think that what Yossi said is even correct, much less an accurate representation of Moore's point of view.
- kragen 7y agoIn Dercuano there is a simple program compared in C, assembly, Python 2, and ANS Forth, in the note “Forth assembling”, p. 3326 of the PDF. Is that what you're looking for? Myself, I'd probably try writing a recursive-descent parser before anything else in that case. For generating CSV I might have a word quote? which tells me if a string will need quoting, a word quoted which emits a quoted field, a word unquoted which emits an unquoted one, a word comma which emits a comma, and a word record which emits a CRLF. But I'm not a competent Forth programmer, and I think neither is anybody else commenting here. I'm willing to try the exercise if you want to give me a text format.
- mikekchar 7y agoIt's been ages since I did any Forth programming, but at the start of my career I spent about 2 years writing Forth code. I have always found that bottom up programming is very similar in nature to test first TDD. You start implementing one specific part, then you implement another specific part. You check to see if there is a better representation for it and refactor if you can. Rinse and repeat. This is very different than outside-in GOOSE style development, though. You start at the lowest level of abstraction and work your way up. It requires you to do some analysis of the problem up front so that you can see that low level of abstraction. WRT your example, I can't really work through it without a real problem, because the devil is in the details. But generally, if I have to parse some strings, I'll start with parsing an example of a string in the simplest way I can. Then I'll expand that code to include another string, refactoring the code as I go. Once I have some idea what the parsing interface is going to look like, I'll write some code to read a line from a file. Then I'll work my way up to call the code that reads the line and sends it to the parser. Then I'll take a look at the representation for the parsed string and implement a single CSV output. Then back up to hook that up. Finally, I'll move back down the abstraction layers and work on more strings. You want to move from that low level, gradually up refactoring as you go, and then dive back down repeatedly. Or at least that's how I do it. I don't pretend to be as skilled at it as Chuck Moore who is famous for discovering amazing abstractions. I should note that not all problems fit this style of development. Sometimes you have to work from top down. Normally you can discover this fairly quickly if you start chasing your tail. You'll implement something, work your way up, then move down only to discover that you have to change something fundamental. You do it, moving back up, only to discover that the first bit doesn't work any more. However, once you discover the top level abstraction you need, you can go back to a bottom up technique because the lower level abstractions will be more obvious.
- markus_zhang 7y agoThanks this is really interesting. I'm definitely not experienced enough to classify problems into top down or bottom up though guess that takes years of experience.