Wayne Conrad's Blog

home

Rattler -- Yet another Parsing Expression Grammar for Ruby

19 Jun 2014

I just noticed this: Language Designers Wanted. It announces rattler, yet another PEG for Ruby.

A quick look shows that it is an external DSL. Those are prettier to work with than internal DSLs, but somewhat less flexible. As long as the DSL gives you a way to do everything you need to do, that’s not a problem. It also appears to generate useful parse trees pretty easily. But the most exciting thing about Rattler is that it handles left recursion. None of the other Ruby PEGs I’ve worked with (Treetop, Citrus, Parslet) will do that.

The acid test of any parser is how well it handles crazy things like Fortran’s Hollerith constant1, where you precede a string with the count of how many characters are in it, and then a H:

      PRINT *, 10
 10   FORMAT (1X, 5HHELLO)

This is how you might print the word “HELLO”. The “5HHELLO” breaks down like this:

This crazy syntax goes all the way back to Fortran I, and is still supported in Fortran77, so a FORTRAN compiler has to be able to handle it.

So, that’s the question. Can Rattler change its parsing behavior based on some prior parse node? Can it use the count to know how many characters are in the string? Not because every parser should be able to parse Fortran, but because being able to parse a Hollerith constant means that the parser has a way to do unusual things when needed.

1 When used in the FORMAT statement, The nHaaa syntax is not actually a Hollerith constant. It’s a Hollerith format specification. Whatever. I only mention this to keep any of the three people who have read the Fortran specification from bugging me about it.

comments powered by Disqus