Unix

Compiling antlr3 with gcj

ForceCore 2009. 8. 16. 17:35
I'm writing this article in English, as the information on GCJ + Antlr3 is quite rare, as of 2009.08.16

http://weblog.hypotheticalabs.com/?p=94
I saw an article from Hypothetical Labs that describes the method to compile antlr-3.0 into native code, but it is a little bit out dated now. I tried to compile more recent version of antlr3 and got it to work.

Tested with antlr-3.1.2.jar.
You need JUnit, too. I've tested and verified that Junit-4.7 works with antlr-3.1.2.
My test environment is Ubuntu 9.04 (jaunty), with gcj package installed.

First, we need to set CLASSPATH correctly:
$ export CLASSPATH=antlr-3.1.2.jar:junit-4.7.jar
Now we build junit first. I decided to avoid using dynamic linking version, since I don't want to go through all that LD path stuff.

$ gcj -c -O2 -o junit-4.7.o junit-4.7.jar
This command should work if CLASSPATH is set correctly.

Now for Antlr3,
$ gcj -c -O2 -o antlr-3.1.2.o antlr-3.1.2.jar
This will take an awful lot of time. Keep those junit*.o and antlr*.o as you own parser will link against, if you choose to native compile your parser too.

Finally for the executable file:
$ gcj --main=org.antlr.Tool -O2 -o antlr3 antlr-3.1.2.o junit-4.7.o
Antlr3 is ready!

$ ./antlr3
usage: java org.antlr.Tool [args] file.g [file2.g file3.g ...]
  -o outputDir          specify output directory where all output is generated
  -fo outputDir         same as -o but force even files with relative paths to dir
  -lib dir              specify location of token files
  -depend               generate file dependencies
  -report               print out a report about the grammar(s) processed
  -print                print out the grammar without actions
  -debug                generate a parser that emits debugging events
  -profile              generate a parser that computes profiling information
  -nfa                  generate an NFA for each rule
  -dfa                  generate a DFA for each decision point
  -message-format name  specify output style for messages
  -verbose              generate ANTLR version and other information
  -X                    display extended argument list

To compile your parser into native code, compile your parser code as an object file and link it against antlr and junit.

$ gcj -c -O2 -o test.o test.java TParser.java TLexer.java
$ gcj --main=test -O2 -o test test.o antlr-3.1.2.o junit-4.7.o

Voila!