Installing ANTLR

Example run of ANTLR

Let us go through an example step by step. We assume a UNIX-like command-line and that the file ntlr-4.4-complete.jar is located in directory ~/ANTLR/.

The first step is to set the CLASSPATH environment variable:

% export CLASSPATH=".:~/ANTLR/antlr-4.4-complete.jar:$CLASSPATH"

In the working directory we have two files:

% ls 
SimpleExample.g4  
helloworld.greeting

The first file, written in ANTLR syntax that we will understand later, describes the language to be lexed/parsed, in this case a trivial language that describes a greeting of the form “Hello !":

grammar SimpleExample;

greeting: Hello Name Bang ;

// KEYWORDS
Hello:	'Hello' ;
Bang:	'!' ;
Name:   [A-Z][a-z]* ;

WS : [ \t\r\n]+ -> skip ; // skip spaces, tabs, newlines

The second file is “source code” written using the above language

Hello World !

We can now run ANTLR to generate the code of our lexer/parser:

% java  -cp .:antlr-4.4-complete.jar -jar ~/ANTLR/antlr-4.4-complete.jar SimpleExample.g4

The previous command has produced a set of .java files, which we now compile:

% javac  -cp .:antlr-4.4-complete.jar SimpleExample*.java

We can now finally run our lexer/parser on the source code

% java  -cp .:antlr-4.4-complete.jar org.antlr.v4.runtime.misc.TestRig SimpleExample greeting helloworld.greeting -tokens -tree
[@0,0:4='Hello',<1>,1:0]
[@1,6:10='World',<3>,1:6]
[@2,12:12='!',<2>,1:12]
[@3,14:13='<EOF>',<-1>,2:0]
(greeting Hello World !)

In the above we asked ANTLR to print the set of tokens and the parse tree… We’ll do much more.