Setting up ANTLR4 on Windows

| microsoft | programming | java |

I am using ANTLR for a Compilers course, and working on a windows box. Getting ANTLR to work is not difficult since it is just a Java jar file. The quick start instructions on the ANTLR website seem really straightforward, and they probably are if you know what you are doing. However, I had a bit of trouble getting the handy antlr4 and gruncommands to work properly in my environment. This is likely due to my Unix/Linux background and because I am not used to messing with environment variables and batch files on windows. Adding things to the PATH and creating bash aliases in Linux is a piece of cake. This is a bit trickier on Windows so I figured I would make a short tutorial to help anyone else who does not know what they are doing like me.

Download Antlr

You can get the latest ANTLR from here. Save this file to C:\Javalib

Add ANTLR to the CLASSPATH

CLASSPATH is an environmental variable. In order to set an environmental variable in Windows you will need to do the following.
  1. Open Up File Explorer
  2. Right Click on This PC
  3. Select Properties
  4. On the left hand side of the new window select Advanced system settings
  5. On the bottom of this screen select Environment Variables…
  6. This is a new computer with no CLASSPATH variable so I had to add a new one. If you have used other Java Libraries it is possible that you may already have CLASSPATH defined. If you see CLASSPATH under System Variables append it with C:\Javalib\antlr-4.5.3-complete.jar; If you do not have a CLASSPATH variable defined, select **New…* and then enter CLASSPATH for the Variable Name, andC:\Javalib\antlr-4.5.3-complete.jar for the Variable value.

Create batch commands for ANTLR Tool and TestRig

In order to make ANTLR easier to run from the command line we will need to add two simple batch files somewhere in the PATH. I decided to put these in C:\Javalib in order to keep them organized and together. C:\Javalib is not in the PATH by default so we must append the existing PATH variable just like we did in the previous step.
  1. Open Up File Explorer
  2. Right Click on This PC
  3. Select Properties
  4. On the left hand side of the new window select Advanced system settings
  5. On the bottom of this screen select Environment Variables…
  6. Find the PATH variable
  7. Append ;C:\Javalib\; to the existing PATH variable
Now we need to create the batch files.
  1. Create a new file in C:\Javalib called antlr4.bat
  2. In this file enter java org.antlr.v4.Tool %*
  3. Save this file
  4. Create a new file in C:\Javalib called grun.bat
  5. In this file enter java org.antlr.v4.runtime.misc.TestRig %*
  6. Save this file
This will now allow us to enter antrl4 and grun into a command prompt and launch ANTLR or the Testing Suite respectively.

Make Sure Everything Works

To make sure that all of this worked;
  1. Open up a Command Prompt or Powershell
  2. Enter the command antlr4
  3. You should see something like this:
[caption id="attachment_87" align="alignnone" width="660"]Output of antlr4 command Output of antlr4 command[/caption]
  1. Enter the command grun
  2. You should see somethign like this:
[caption id="attachment_88" align="alignnone" width="660"]Output of grun command Output of grun command[/caption]

Congrats, you are ready to hack on compilers now. Next, check out The Definitive ANTLR Reference book to learn everything there is to know about ANTLR.

Thank you for reading! Share your thoughts with me on mastodon or via email.

Check out some more stuff to read down below.

Most popular posts this month

Recent Favorite Blog Posts

This is a collection of the last 8 posts that I bookmarked.

Articles from blogs I follow around the net

Script Doctoring

I’ve been having a number of communications problems in my interactions with my doctors at Kaiser lately, and it’s becoming one of those things where the burden and onus entirely is placed upon me to sort out, and that’s exhausting for the actually autist…

via Bix Dot Blog October 22, 2024

Blockchain company Forte acquires games studios, demands secrecy, shuts them down

Sometime in 2023, blockchain firm Forte acquired game studios Phoenix Labs and Rumble Games. However, it would be a year before this came to light, because according to a report from Game Developer, Forte demanded secrecy from employ…

via Web3 is Going Just Great October 22, 2024

Initial explorations of Anthropic's new Computer Use capability

Two big announcements from Anthropic today: a new Claude 3.5 Sonnet model and a new API mode that they are calling computer use. (They also pre-announced Haiku 3.5, but that's not available yet so I'm ignoring it until I can try it out myself.) Comp…

via Simon Willison's Weblog: Entries October 22, 2024

Generated by openring