Parser Generators: ANTLR vs JavaCC
A performance review of two parser generators: Java CC and ANTLR. One of them is a clear winner according to the measurements.
ANTLR (ANother Tool for Language Recognition) is a parser generator developed by Terence Parr, it is extremely versatile and is capable of generating parsers in multiple programming languages, but requires a runtime library.
ANTLR Features:
- ALL(*) or ALL(k) Parser Generator (an adaptive top down parser with arbitrary token lookahead, ie there is no need to specify the lookahead k)
- Capable of generate parsers in a number of different programming languages including Java, C#, Python and JavaScript
- A runtime library is required.
- A good level of support in the most popular IDEs
- Version 4 JIT Compiles the parser
- The lexer, parser and abstract syntax tree can all be generated from a single grammar file.
- Parse tree is built automatically.
- Supports parsing modes for mixed language documents.
- Support for semantic and syntactic predicates.
- Grammar Definition is EBNF like.
- A book is available about ANTLR here
Example Syntax
A JSON parser written in ANTLR.
JavaCC
JavaCC (Java Compiler Compiler) was written originally by Dr. Sriram Sankar and Sreenivasa Viswanadha. It is only capable of generating parsers in Java but doesn’t require a runtime library and the parsers it generates are very performant for an LL(k) parsers.
JavaCC Features:
- LL(k) Parser Generator (a top down parser with arbitrary token lookahead)
- Can only generate parsers in Java or C/C++.
- No runtime is required, the generated parser is completely autonomous.
- The lexer, parser, abstract syntax tree and documentation can all be generated from a single grammar file.
- Supports parsing modes for mixed language documents.
- Supports ranges for example a colour in CSS could be defined as “#”(([“0”-“9”,“a”-“f”,“A”-“F”]){3}){1,2}
- Grammar Definition is Java like.
- A book is available about JAVACC here
Example Syntax
A JSON Parser written in JavaCC
Benchmarks
The JSON file being parsed during the benchmark is a free example from https://adobe.github.io/Spry/samples/ and is shown here below:
Both parsers where launched and profiled using the same machine, the same profiler and JVM. The results are displayed below:
Parser Generator
|
Time (ms)
|
Avg. Time (ms)
|
Invocations
|
ANTLR
|
179,105
|
1,791
|
100
|
JAVACC
|
3,312
|
33
|
100
|

Summary
JavaCC is clearly more performant than ANTLR however if performance is not an issue, both parser generators are valid tools. The syntax is very similar, both have books available to bring you up to speed. Each have pros and cons, your decision which one to use depends on your needs and on your personal preference. Obviously it also depends on what your target programming language will be for the project where you intend to use the parser.