Showing posts with label Parsec. Show all posts
Showing posts with label Parsec. Show all posts

Wednesday, September 19, 2007

Parser Combinator in Java

Pffuii, I'm quite busy these days, hence the posting frequency has dropped. I'm in the process of playing with a functional Java based language (more on that in later posts, hopefully) and I decided to have a go at a little parser combinator in Java.
The end result works (I can parse things like Java class declaration) and looks a lot like parsec, but there is a lot of Java cruft that I'd like to remove, things that are hidden with the wonderful magic (I don't understand it well enough to call it otherwise) of Haskell monads.
For example:

try {
Character c=new Try(new Letter()).parse(input);
...
} catch (JCParseException jpe){
...


This code tries to match a letter in the input at its current position (the input variable) and does not consume any input if it fails. I don't like having to explicitly tell the combinator to parse the input, and I don't like using exceptions to indicate a parsing failure. Now, anybody has any great ideas?

Friday, August 10, 2007

The beauty of Haskell Combinators

Pffiuu, it's been a while since I've posted here, but don't think I've been idle. In addition to my day job, I'm still hacking away at Haskell. And still loving it!! I just wanted today to express my pleasure (yes, pleasure) at working with some cool APIs and libraries that are out there.
I'm doing a bit of research into language parsing and I'm using Parsec for parsing and the HughesPJ Pretty Printing library (included with GHC) to dump the parsed AST and check that I can output what I parse and reparse it into a equivalent structure. I have to say, working with combinators (monadic or otherwise) is actually fun and easy. I've developed code parser before, for example using some Java parser generators, but coding your parser directly in your favorite functional language is really sweet. And of course, I like the way that similar concepts are expressed the same in the two libraries, even down to the function names: parens() in Parsec will parse something surrounded but parentheses and parens() in HughesPJ will write something in between parenthesis.
I had also a exhilarating ah ah moment when I discovered in the Parsec doc makeTokenParser that suddenly made it so easy to accommodate with spaces and pesky littls things of this caliber.
Thanks to all contributors to these libraries for making my functional programming evenings fun!