ANTLR Set up in Windows (First step of building your own programming language)

MD. ANAYTUL ISLAM ARPON
3 min readMay 22, 2021

For different purposes, a lot of programming languages are available nowadays. Every programming language has its own pattern which is called grammar, describes the rules and regulation of that language in the compiler.

The main work of a compiler is to translate source code from a particular language to machine language or “code” that a computer processor uses.

Basic Components of a Compiler :
Lexer: splits source code into tokens which are special keywords and structures of specific programming language

Parser: Identifies patterns of token set and builds Abstract Syntax Tree(AST).

Generator: Generates the syntax of the target language.

When there are new changes in the grammar above components need to be changed. Therefore writing a compiler from the scratch is somewhat difficult.

ANTLR
Another Tool For Language Recognition or ANTLR is making this task easy by giving formatting language for grammar. Also, the Lexer and Parser source codes will be generated automatically.

By following the below instructions you can easily setup ANTLR on your windows PC:

  1. Create a new folder in the C drive and name it as you wish.
    For this example, we will follow the folder name as JavaLib
  2. Create 3 files in the folder and renamed them as ( antlr.bat, class.bat, grun.bat)
  3. Download ANTLR (Complete ANTLR 4.9.2 Java binaries jar or the latest) from their official site (ANTLR) and save it in that folder.
  4. Download the latest java development kit and install it.
  5. Then go to the “Edit the system environment variables” and select “Environment variables”.

On the System variable, Select Path and edit it. Add the JDK destination path there. For example, my JDK path was “C:\Program Files\Java\jdk-16.0.1\bin

On the User variable, add the Classpath. For creating this, click the new button, write the variable name CLASSPATH and the variable value is “C:\JavaLib\antlr-4.9.2-complete.jar” (the folder you created previously with the ANTLR file)

Then select Path on the User variable and edit it. Add “%CLASSPATH%” and “C:\JavaLib”(the folder you created previously).
Then save and close all of it.

6. Open the antlr.bat from your folder and write “java org.antlr.v4.Tool %*” and save it.

7. Open the grun.bat from your folder and write “java org.antlr.v4.gui.TestRig %*” and save it.

8. Open the class.bat from your folder and write “ SET CLASSPATH=.;%CLASSPATH%” and save it.

All process is done. For checking it is successful or not, let’s run an example. For this create a folder named example and open cmd in there. Then run “antlr” and hit enter. You will see your cmd as below.

Then run “grun” and hit enter. You will see your cmd as below.

That means your PC knows what antlr is.
Follow the antlr official page create a grammar named “Expr.g4” (extension of a grammar file is .g4) and paste the following code

grammar Expr;
prog: (expr NEWLINE)* ;
expr: expr (‘*’|’/’) expr
| expr (‘+’|’-’) expr
| INT
| ‘(‘ expr ‘)’
;
NEWLINE : [\r\n]+ ;
INT : [0–9]+ ;

Now we have to compile our grammar. So run your CMD
antlr Expr.g4”. It will create some java files on the folder. Now we have to convert the java files into class files. For this run “javac Expr*.java” in your CMD.
Now create a sample file named example.txt and write “100+2*34”.
Run the command “grun Expr prog example.txt -gui” and see the parse tree as below-

Additional resources:

  1. https://pragprog.com/titles/tpantlr2/the-definitive-antlr-4-reference/
  2. https://www.antlr.org/

--

--