Its so frustrating when there are lots of things that you want to do but no time to do them in! I’ve got my parents in law’s garden to finish off (3 years and counting!) a bookcase in the bedroom, Java song or what ever I eventually call it and I have recently come across an application called Javacc. This app lets you define a language grammer and then generates the Java classes necessary to interpret and execute your new language. Which got me thinking, since there is an international speak like a pirate day why not create a language for pirates to program in? Consider the following Java:
if (2 > 3) {
System.out.println(“True”);
}
else {
System.out.println(“False”);
}
In Pirate this could be expressed as:
2 > 3 This be true?
Aye
Ahoy “True”
That it be
Nay
Ahoy “False”
It be not
Ok so its a bit verbose and probably has no real use but whats that got to do with it. All thats needed is to expand the syntax a bit and have a go with Javacc. Which comes back to not having any time……
Here’s a start!
$ cat pirate.jj
options {
IGNORE_CASE=true;
}
PARSER_BEGIN(Pirate)
import java.io.*;
public class Pirate {
public static void main(String[] args) throws ParseException{
Pirate p = new Pirate(new StringReader(args[0]));
p.ahoy();
}
}
PARSER_END(Pirate)
SKIP : {
” ”
}
TOKEN : {
|
}
void ahoy() : {
String s;
} {
s=.image
{System.out.println(“A pirate said: Ahoy ” + s + ” !”);}
}
$ javacc pirate.jj && javac *.java && java Pirate “Ahoy ‘matey’”
Java Compiler Compiler Version 4.0 (Parser Generator)
(type “javacc” with no arguments for help)
Reading from file pirate.jj . . .
Parser generated successfully.
Note: Pirate.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
A pirate said: Ahoy ‘matey’ !
Er, hm, let’s try that with some HTML escaping:
$ cat pirate.jj
options {
IGNORE_CASE=true;
}
PARSER_BEGIN(Pirate)
import java.io.*;
public class Pirate {
public static void main(String[] args) throws ParseException{
Pirate p = new Pirate(new StringReader(args[0]));
p.ahoy();
}
}
PARSER_END(Pirate)
SKIP : {
” ”
}
TOKEN : {
<AHOY : “ahoy”>
| <STRING : “‘” (["a"-"z"])+ “‘” >
}
void ahoy() : {
String s;
} {
<AHOY> s=<STRING>.image
{System.out.println(“A pirate said: Ahoy ” + s + ” !”);}
}
$ javacc pirate.jj && javac *.java && java Pirate “Ahoy ‘matey’”
Java Compiler Compiler Version 4.0 (Parser Generator)
(type “javacc” with no arguments for help)
Reading from file pirate.jj . . .
Parser generated successfully.
Note: Pirate.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
A pirate said: Ahoy ‘matey’ !
Presumably you’re aware of lolcode? http://www.lolcode.com …..
Thanks for the example code Tom. I’ll give it a go