From: Eric Mahurin Date: 2008-09-24T22:46:44+09:00 Subject: Re: ANTLR Target for Ruby ------=_Part_59476_24056085.1222264011128 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline On Wed, Sep 24, 2008 at 8:32 AM, James Gray wrote: > On Sep 24, 2008, at 7:23 AM, Eric Mahurin wrote: > > I'm sounds like I >> won't be able to convince you why a DSL is better than a specific format, >> and you vice-versa. A quick summary is: although you lose the exact >> syntax >> that you may want, you gain in power and flexibility. Using Grammar >> (directly), you could template Grammars (method/lambda could return a >> Grammar based on Grammar args and other params), use plain ruby for extra >> control when specifying a Grammar tree, or simply embed Grammar objects in >> a >> ruby program (like Regexp objects can be - although they parse the grammar >> from a string). >> > > When I wrote GhostWheel, I was convinced the DSL was better too. Then I > tried using it and changed my mind. :) > > The DSL adds a lot of Ruby noise that makes it harder to think about and > understand the language being parsed. It keeps you from focusing on the > right things. > > For example, compare this JSON parser using GhostWheel's DSL: > > http://ghostwheel.rubyforge.org/svn/trunk/test/example/tc_json_ruby.rb > > and this using a grammar: > > > http://ghostwheel.rubyforge.org/svn/trunk/test/example/tc_json_ghost_wheel.rb > > I'm not arguing my grammar is perfect either. I think it could get even > more readable. But have a look at how clear rules like value_space, > value_content, value, and value_with_array_sep play out. > > I also think most of the advantages you list can be gained with non-DSL > grammars. I mean, we can always dynamically build up a String in Ruby too > and that's the dumbest thing I could possibly think of. > > Anyway, the two don't have to be exclusive. As you see, GhostWheel > supports both. I think my DSL is still fairly clean because you can simply use variable assignments for "rules" and use the "+" and "|" operators for sequences and alternations. Those help a lot. Ideally I would use a space instead of "+" to match BNF, but it doesn't seem that bad. But, I think I'll also have converters (PEG, BNF, regex) to give the non-DSL approach. ------=_Part_59476_24056085.1222264011128--