Setup Antlr With IntelliJ IDE For Language Lexing And Parsing

Easy-to-Follow and Step-by-Step Guide to Built Your First Lexer and Parser with IntelliJ IDEA and Antlr.

If you are struggling to set up IntelliJ IDEA and Antlr and don't know where to start then this guide is for you. I followed several solutions to figure things our, and I was frustrated. I hope this guide will help you all and will make things easy. Let’s get started.


 Setting Up IntelliJ IDEA

Download the IntelliJ IDEA( and extract the content to ‘/opt/’ and install it by running the bellow commands.


cd /opt/ 
sudo tar -xvzf ~/Downloads/ideaI* #extract the files
sudo mv idea-IU-203.5981.155 idea # Rename the folder (optional)
./idea/bin/ #Run the installer


Follow the instructions to installer the IDE. Once done, close the IDE and create a file named jetbrains-idea.desktop and put below content in it


[Desktop Entry]
Name=IntelliJ IDEA Ultimate Edition
Exec="/opt/idea/bin/" %f
Comment=Capable and Ergonomic IDE for JVM


Finally, run the below command to make a launch icon in your applications' menu.


 sudo desktop-file-install jetbrains-idea.desktop


 Setting Up Antlr  

Setup Antlr by running below commands (Tested on Fedora Linux)

cd /usr/local/lib
export CLASSPATH=".:/usr/local/lib/antlr-4.9.2-complete.jar:$CLASSPATH"
alias antlr4='java -jar /usr/local/lib/antlr-4.9.2-complete.jar'
alias grun='java org.antlr.v4.gui.TestRig'

Finally, to test the Anltr installation, simply run ‘java org.antlr.v4.Tool in your terminal! The output should be as presented below



 Combining IntelliJ IDEA and Antlr for Language Lexer and Parsing

Run IntelliJ IDEA and choose the plugins tab and search for ANTLR v4 Plugin and install it.



Next, create a new console project named testProject. You can follow along as shown below.





Create a new folder in the root of your named assets and copy antlr jar file( and your target language grammar files ( I am using java8 Grammar files in my example. Your folder should be similar to mine if you are following along.


Next, add the antlr jar to the project's library.



We will generate java lexer and parser files from the grammar. Right-click on the Java8Lexer.g4 file and click on configure antlr. Your settings should be similar to mine as given below.



Generate the files by running Generate Antlr Recognizer (Ctrl+Shift+G)

Follow the same Process for the Java8Parser.g4. If everything went smoothly, your file structure should be similar to mine.



To test my setup, I ran a simple example. You can grab this example below and put it in your assets' folder. Next, you can simply copy the code I used and test your setup as shown below.

// HelloWorld Java Program

class HelloWorld {
 public static void main(String[] args) {
 System.out.println("Hello, World!");
 // Example Antlr parsing 

import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.tree.ParseTree;


public class Main {

public static void main(String[] args) throws IOException {
String fileName = "/home/hussain/IdeaProjects/javaAntlrParser/jars/";

System.out.println("Processing >> " + fileName);

CharStream in = CharStreams.fromFileName(fileName); lexer = new;

CommonTokenStream tokens = new CommonTokenStream(lexer); parser = new;
ParseTree tree = parser.compilationUnit();

Congratulations!You should have successfully built your first lexer and parser with IntelliJ IDEA and Antlr.

Leave a comment below if you have any question or suggestions.

Happy Posting! See you in the next one.



Buy me a coffee!    or    Show your support!

Related Posts

Function Keys Not Working In Linux

Solution for Logitech or Other Apple Kind Keyboards Function Keys Not Working In Linux

By Yasir Hussain | Posted 3 months ago

Setup Pycharm IDE With Global Package Manager

Easy To Follow And Step-By-Step Guide To Setup Pycharm IDE With Global Packages Manager

By Yasir Hussain | Posted 1 year ago

Setup Pycharm IDE In Linux

Easy To Follow And Step-By-Step Guide To Setup JetBraine Pycharm IDE In Your Linux Distro

By Yasir Hussain | Posted 1 year ago