Date: 25 Sep 2003 22:15:04 -0000 Message-ID: <20030925221504.12396.qmail@plover.com> Subject: Chapter IX finished From: Mark Jason Dominus Organization: Plover Systems If you forgot what this list is about, or you don't know why you're getting this message, please see http://perl.plover.com/book/ To unsubscribe, send a blank message to mjd-book-unsubscribe@plover.com. ---------------------------------------------------------------- I've been working very hard on Chapter IX, and it's now finished, or at least finished enough that I can stop work on it. It turned out a lot longer than I expected---about 79 pages---but also a lot better. I'm really pleased with it. Most of the chapter is given over to the ins and outs of writing recursive descent parsers. One nice thing about this style of parser is that it's easy to build operators that put together small parsers into large ones. I show how to build complicated parsers from only five or six prefabricated parts. So, for example, if '$expression' is a parser for expressions, then 'star($expression)' builds a new parser that will accept an empty input, but prefers to find an expression, and if it succeeds, it looks for more expressions. It uses the '$expression' parser to recognize expressions. Similarly, the parser built by star(lookfor('COMMA'), $expression) tries to find zero or more repetitions of a comma token followed by an expression. So you can assemble a parser for lists of expressions by saying $list = concatenate(lookfor('LPAREN'), $expression, star(lookfor('COMMA'), $expression), lookfor('RPAREN')); which looks for a left parenthesis followed by an expression followed by zero or more repetitions of a comma and another expression, followed by a right parenthesis. Or, if your language has a lot of lists of this form, you can build a new parser operator: sub list_of { my ($element, $separator) = @_; $separator = lookfor('COMMA') unless defined $separator; return concatenate(lookfor('LPAREN'), $element, star($separator, $element), lookfor('RPAREN')); } and then build your expression list parser just by saying $list = list_of($expression); The part of the chapter I'm proudest of is probably the last section, in which I use operator overloading to replace functions like 'concatenate'. The result is Perl code that looks just like the grammar that it's intended to parse---if you're familiar with yacc, it looks a lot like that, except that it's pure Perl: $statement = _'PRINT' - $Expression - _'NEWLINES' >> sub { print "$_[1]\n" } | _'IDENTIFIER' - _['OP', '='] - $Expression - _'NEWLINES' >> sub { $VAR{$_[0]} = $_[2] } ; Writing about this was a real milestone for me, because I first mentioned the idea to Sean Burke when I met him at YAPC in 1999. The idea of overloading operators like '|' so that they work on parser objects is one I've been looking forward to ever since then, and I was beginning to think that the day would never come that I'd get write about it. Also, I wasn't entirely sure that it would work. But it works beautifully. The draft of Chapter IX is available on my web site at [ Sorry, freebies are available only to mailing list subscribers. Send mail to mjd-book-subscribe@plover.com to subscribe. ] As usual, I would like to remind you that you absolutely must not distribute these files to anyone else. This is for several reasons. Most important, the chapter is still a draft, and I would be very unhappy and embarrassed if my crappy old drafts were circulating on the Internet after the finished manuscript was published. There are sure to be errors, even in the final version, and I'm sure that these errors will continue to bother me for years and years. I'll be even more bothered if I know that the error-riddled draft files are circulating around, making me look bad. And you know that if they do circulate, some idiot will post on Slashdot saying that my book sucks because I made the following stupid error on page 297---an error which was removed prior to publication. So please don't torment me by distributing my drafts. They're for your personal enjoyment only. Also, because I want the draft chapters to be gifts for the kind people on my mailing list, please do not advertise these URLs to anyone else. Instead, please advertise the mailing list. People who subscribe between now and the completion of Chapter X will receive the Chapter IX draft URL in email when they subscribe. To subscribe, send email to mjd-book-subscribe@plover.com or visit the subscription form at http://perl.plover.com/book/#mlist I'd also like to remind everyone that when the book is finished, which I hope will be by the end of the year, I will post the entire finished text on my web site at http://perl.plover.com/book/. Thanks to all of you for your patience and interest. I expect to finish the book by the end of the year. I'll send another report when Chapter X is ready, and probably sooner. A final note, less happy than the rest. In the past, my writing progress has been slow. But I wrote about 45 pages in the past 11 days, which I think is great. I'm really into the book right now, and it's going wonderfully. But that's about to come to a stop, or at least a major slowdown, because I've run out of money. As you may know, I am a Perl trainer and consultant. Business is always irregular, and lately there has been less than usual. I need to find work right away. If you like my book and you want to see more of it as soon as possible, the best thing you could do for me would be to get your company to hire me to do some Perl training for them. Details about the classes I teach and my rates are at http://perl.plover.com/yak/comm.html I can also construct new classes to suit your company's needs. Everything is negotiable; please send me email if you think anyone you know might be interested. As a special offer for mailing list subscribers, I will extend a 20% discount to any company that takes me up on this offer as long as the training is completed by December 31. I'll also give a free autographed copy of my book to any person responsible for setting up the training job. If your company isn't interested in training, perhaps they have some consulting or programming work I could do. I can develop, modify, and debug Perl applications, and I have particular expertise with Apache plugin modules in both perl and C. Thanks again for your interest and kind support.