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!
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.
Posted by: Brian E | September 30, 2007 at 08:25 AM