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(www.jetbrains.com/idea/download/) 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/idea.sh #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]
Version=1.0
Type=Application
Name=IntelliJ IDEA Ultimate Edition
Icon=/opt/idea/bin/idea.svg
Exec="/opt/idea/bin/idea.sh" %f
Comment=Capable and Ergonomic IDE for JVM
Categories=Development;IDE;
Terminal=false
StartupWMClass=jetbrains-idea

 

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
wget https://www.antlr.org/download/antlr-4.9.2-complete.jar
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(https://www.antlr.org/download/) and your target language grammar files (https://github.com/antlr/grammars-v4). 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 HelloWorld.java 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 
package com.company;

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;

import java.io.IOException;

public class Main {

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

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

CharStream in = CharStreams.fromFileName(fileName);

com.company.Java8Lexer lexer = new com.company.Java8Lexer(in);

CommonTokenStream tokens = new CommonTokenStream(lexer);

com.company.Java8Parser parser = new com.company.Java8Parser(tokens);
parser.setBuildParseTree(true);
ParseTree tree = parser.compilationUnit();
System.out.println(tree.toStringTree());
}
}




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


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 5 months 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 5 months ago

Setup IntelliJ IDE In Linux

Easy To Follow And Step-By-Step Guide To Setup JetBrains IntelliJ IDE In Your Linux Distro


By Yasir Hussain | Posted 5 months ago