|
[boo]
Towards Extensible Parsing
[
bamboo
]
Almost four years ago the first feature request was entered into the boo issue tracker.
Since then boo has improved a lot but with no solution for BOO-1 in the horizon. I think I'm getting close to solve it in a interesting way using PEGs implemented as a graph of expression objects. PEGs are very likable. Conceptually simple and composable. Take the PEG that recognizes integer expressions involving the + and * operators:
where:
The grammar can be translated to boo very simply using the peg macro from Boo.Pegs:
I had to be a little creative in mapping the PEG operators to valid boo expressions because as it must be clear by now boo doesn't allow the introduction of completely new syntax and that's what the fuss is all about here. I actually like the way it looks. Implementing something more useful such as expression evaluation on top of that requires a few semantic actions operating a stack:
Semantic actions are just closures that get executed as matching succeeds. $text returns the text matched so far by the current rule. Beautiful. The underlying implementation based on a graph of expression objects really shines when one considers what it takes to extend the grammar above with support for hexadecimal literals:
It's not yet clear how this extensibility mechanism will be exposed at the boo language level but the simplicity at the peg level is encouraging. One last feature worth pointing out is the ability to match based on a previously matched rule. For instance, the closing tag of a xml element must match the name in the starting tag:
I've found the idea for the last match operator @ first described in this article. Great idea. I've been also greatly inspired by conversations I've had with Massi who's exploring similar territory and Jb during the last Mono Meeting in Extensible parsing. Soon in a boo compiler close to you. Fantastic stuff - great work! --Stuart Carnie, May 9, 2008 03:07 AM
Wonderful! --Onur Gumus, May 9, 2008 05:40 AM
Great --网站推广, May 9, 2008 06:35 AM
Looks promising. I also love the extensive usage of the match macro in Boo.Pegs. --Avish, May 9, 2008 01:42 PM
I lagged in my feeds and just stumbled upon this post!
--Cedric, May 13, 2008 04:07 PM
Whoa--when did PEGs become available on .NET? I only read about a Java implementation. I've always wanted syntax extensibility in Boo. Fingers crossed... --David Piepgrass, January 28, 2009 06:27 PM
Post a comment
|