Building the compiler
The Phun compiler is currently written in Java and Scala; not as strange as it sounds in light of the following:
- The lexicon and grammar are written in ANTLR 4, which at this time only supports lexer/parser generation into Java.
- When forced into the JVM, I prefer Scala which is millions of times better than Java. :)
There are two SBT projects,
compiler. You must first compile and run the
GrammarCompiler Scala app:
$ sbt grammar/compile $ sbt grammar/run
What this does is bootstrap the ANTLR 4 Tool to compile Phun.g4—the Phun lexicon and grammar—into lexer and parser
Java classes. Those Java classes are stuffed into the
compiler project under
Then you can use SBT to compile the
compiler project into a JAR:
$ sbt compiler/package
Here is a great StackOverflow post about how the compiler works with the generated lexer/parser.
Developing on the grammar (Phun.g4)
To ease development on the grammar, you can either use ANTLRWorks 2 or you can enter the following commands into SBT:
> project grammar > ~ ; grammar/run; compiler/compile; compiler/run
This sets up a watch so that when you change
Phun.g4, the following automatically happen:
grammaris automatically recompiled (i.e. ANTLR v4 generates lexer and parser)
compileris recompiled with the new lexer/parser
compileris run in test mode.