Tom Copeland's Recent Posts

RSS Feeds

« Tracking down a Rails app memory leak | Main | Generating Parsers With JavaCC reviewed »

An LOLCODE interpreter using JJTree

Here's something that's lingered in my blog TODO queue for far too long.  Brian Egge, formerly of ThoughtWorks and now of Macquarie Bank Limited, wrote an LOLCODE interpreter with JavaCC and JJTree.  Behold:

$ bin/lol.sh test/samples/hello_world.LOL 
HAI WORLD!

I salute his efforts in this crucial field and humbly submit a small patch:

Index: src/com/lolcode/parser/LolCode.jjt
===================================================================
--- src/com/lolcode/parser/LolCode.jjt  (revision 20)
+++ src/com/lolcode/parser/LolCode.jjt  (working copy)
@@ -141,7 +141,7 @@
{}
{
   <IM> <IN> <YR> <LOOP> <EOL>
-  ( LOOKAHEAD(2147483647) Statement() )+
+  ( LOOKAHEAD(Statement()) Statement() )+
   <IM> <OUTTA> <YR> <LOOP> <EOL>
}

@@ -157,7 +157,7 @@
{}
{
   IncrFunction()
-|  LOOKAHEAD(2147483647) BreakFunction()
+|  LOOKAHEAD(BreakFunction()) BreakFunction()
|  <IZ> BoolExpression() <O_RLY> <QUESTION> <EOL> then() ( Else() )? <KTHX> <EOL>
| <OPEN> <IDENT> <QUOTED_STRING>
}
@@ -245,4 +245,4 @@
    return t.image.substring(1, t.image.length() - 1);
  }
}

I'm just happy to be able to contribute anything to this work.  Go Brian!

TrackBack

TrackBack URL for this entry:
http://www.typepad.com/services/trackback/6a00d83451d3c069e200e5505cd9078833

Listed below are links to weblogs that reference An LOLCODE interpreter using JJTree:

Comments

Thanks Tom! I applied the patch. I wrote this part of the code before I read your book. I always felt that specifying a lookahead of 2147483647 was a hack, but it's a common pattern that you see in older JavaCC grammars. Clearly, semantic lookaheads are better than fixed, allowing for as much recursion as is needed.

By the way, last week I gave a five minute talk at the Sydney Java Users Group comparing JavaCC and ANTLR. Sadly, I didn't win the Apple TV prize, but I had fun presenting.

Verify your Comment

Previewing your Comment

This is only a preview. Your comment has not yet been posted.

Working...
Your comment could not be posted. Error type:
Your comment has been saved. Comments are moderated and will not appear until approved by the author. Post another comment

The letters and numbers you entered did not match the image. Please try again.

As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

Having trouble reading this image? View an alternate.

Working...

Post a comment

Comments are moderated, and will not appear until the author has approved them.