Install Antlr4 on WSL (Windows Subsystems for Linux)

Install Antlr4 on WSL
Install Antlr4 on WSL

Antlr4 on WSL (Windows Subsystems for Linux)

WSL is Linux based kernel developed for running on a Windows 10 operating system. This allows
running a Linux distribution in a windows environment. We recommend following the guide
created by Microsoft.

Installing WSL

Installation guide for WSL:

https://docs.microsoft.com/en-us/windows/wsl/install

To install WSL 2, you need to enable virtualization in your machine’s BIOS.

Installing a Linux Distribution:

After installing and enabling WSL, head over to the Windows Store and pick your Linux OS, we recommend using the latest distribution of Ubuntu found in the store.

After installing WSL with Ubuntu, update the repositories and upgrade Ubuntu.

$ sudo apt-get update

$ sudo apt-get upgrade

$ sudo apt-get install build-essential

Installing Java Development Kit

To find which version of the Java development kit is already ready for installation. Try the following command.

$ sudo apt search openjdk

If OpenJDK-14-JDK is on the list, install this. This is Java 14 and should be sufficient for ANTLR4 version 4.8.

$ sudo apt-get install openjdk-14-jdk

OpenJDK provides the Java development kit for Java 11. This is the Java Runtime Environment (JRE), Java Virtual Machine (JVM), and Java Compiler.

Installing ANTLR4 in Ubuntu

To install ANTLR4 from the Ubuntu terminal, your computer needs to have internet access. Then
use the following codes to

$ cd /usr/local/lib


$ sudo wget https://www.antlr.org/download/antlr-4.8-
complete.jar

After downloading, run these next commands.

$ export CLASSPATH=”.:/usr/local/lib/antlr-4.8-
complete.jar:$CLASSPATH”
$ alias antlr4='java -Xmx500M -cp "/usr/local/lib/antlr-4.8-
complete.jar:$CLASSPATH" org.antlr.v4.Tool'
$ alias grun='java -Xmx500M -cp "/usr/local/lib/antlr-4.8-
complete.jar:$CLASSPATH" org.antlr.v4.gui.TestRig'

Then edit the bash startup script. In Ubuntu, it’s located as a secret file in the home folder called
.bashrc. This is done, so the commands are useable whenever a new terminal is opened.

$ sudo nano ~/.bashrc

Append to the end of the file the commands from before.

export CLASSPATH=”.:/usr/local/lib/antlr-4.8-
complete.jar:$CLASSPATH”
alias antlr4='java -Xmx500M -cp "/usr/local/lib/antlr-4.8-
complete.jar:$CLASSPATH" org.antlr.v4.Tool'
alias grun='java -Xmx500M -cp "/usr/local/lib/antlr-4.8-
complete.jar:$CLASSPATH" org.antlr.v4.gui.TestRig'

Then to test if the installation works:

$ antlr4
$ grun

The installation has been successful if there is a valid response to the new commands.

Wokring with ANTLR4 on WSL:

An easy way to work on the exercises using WSL is to use WSL in combination with Visual Studio Code. Visual Studio Code contains a WSL extension, which helps with the workflow using WSL. In addition, visual Studio Code has an ANTLR extension. But for it to work with WSL, it needs to be installed in the WSL session.

Activating the extensions to Visual Studio Code:

Code contains a bunch of handy extensions and plugins. One excellent extension is their Remote Development extension. Which, in our case, can be used to have Code and get access to WSL.

After installing the extension, you can open the WSL in code by clicking the green button and selecting Remote-WSL: New Window.

In the WSL session, you can activate the Antlr4 plug-in for VS Code. It’s the one developed by Mike Lischke.

Working in VS Code.

Now we’re ready to begin working. The first thing we do is open the work path to be the home folder of WSL.

Through Code, you can drag and drop folders into WSL. The first example could be to lower the example folder from the first lecture into WSL.

To open an Ubuntu terminal in Visual Code, press the terminal panel and select the new terminal.

Terminal -> New Terminal

Next, you can navigate into the folder, and now your workspace is set up.

$ cd “Foldername”

Ex. cd lec01-regex

Setting up the makefile.

To use make to generate and compile the generated ANTLR files. We will use make to have an ordered compilation tool. For WSL, the changes to the make file have to match up with this example makefile.


The first is the ANTLRJAR variable. It has to match the location and filename of the ANTLR jar file. If you followed the guide, it should match this filename.


The second is to uncomment the export line. This makes an environment variable for make to work with ANTLR.
The third is to make grun testing generating an example tree in the terminal instead of a GUI application. This is done because WSL cannot run GUI applications since it is just a Linux terminal. 3’rd party software is available to create a Linux Desktop Environment using WSL. But it is not supported by Microsoft and, therefore, should be done at your own risk.
Reference for WSL Desktop environment:

https://solarianprogrammer.com/2017/04/16/windows-susbsystem-for-linux-xfce-4/

https://medium.com/@dhanar.santika/installing-wsl-with-gui-using-vcxsrv-6f307e96fac0

Warning may not work!!

Make functions:

There are three functions to use with the makefile:

$ make
$ make test
$ make grun

First, generate the java classes and compile them to create an executable program.
Make test tests your grammar file by running it using the input .txt file and producing a result based
on your implementation.
Make grun tests your grammar by trying to parse the input file into a tree structure. For WSL it
should have the -tree option instead of the -GUI option since WSL can’t run GUI applications