There are thousands of programming languages available today, and new ones show up every year. If you are a programmer, at some point in your life, you must have wondered if you too could ever create your very own language, one that conforms to your ideals. Well, thanks to ANTLR v4, doing so has become easier than ever. In this tutorial, I’ll show you how to create a very simple programming language using ANTLR4 and Java.
I’m going to assume that you already have Java 7 installed on your computer, along with Eclipse.
Use the following command to download ANTLR v4.5.3 as a JAR file:
After you place it in the lib folder of your project, in the Package Explorer view, right click on it and select Build Path > Add to Build Path.
To keep this tutorial short, we’ll be creating a very simple programming language. Let’s call it GYOO. Here’s a sample program in GYOO:
The program above demonstrates all the features that we are going to support in this language. It’s going to have three types of statements:
It can handle only positive numbers, and programs must begin and end with the
Accordingly, create a new file call GYOO.g4 inside the src folder and add the following grammar to it:
The grammar should be fairly intuitive to you if you are familiar with BNF.
Now that we have a grammar file, we can pass it as an input to the
org.antlr.v4.Tool class and generate a parser and lexer for it. You could use the ANTLR Eclipse plugin to do so. In this tutorial, however, I’ll do it manually on the command line:
Make sure that you specify your own classpath and package name while running the command. Once the command completes successfully, you’ll have several new Java classes in your src folder.
You must now create a new Java class that is a subclass of the
GYOOBaseListener class. Call it MyListener.
MyListener, you need to tell ANTLR’s parser what it should do every time it encounters a specific type of token. For example, every time it encounters an assign statement, it must assign a value to a variable. You can do so by overriding the
exitAssign() methods. There are similar methods for the print and add statements too.
MyListener also needs a
Map object that can store the names and values of all the variables.
Accordingly, add the following code to the class:
At this point, our programming language is almost ready. However, we still need to pass an input file to it and break it down into tokens. In order to that, add a new class called Main to your project and add a
main() method to it.
Inside the method, you must first create an
ANTLRInputStream object and pass a
FileInputStream to it. Next, you must create a
GYOOLexer object based on the
InputStream. You can now create a stream of tokens using the lexer, and pass it as an input to a
You must, of course, not forget to add the
MyListener class as a listener to the
At this point, you can call the
program() method to start the parsing.
And now, if you pass the sample GYOO program you saw earlier as the input file, you should see the following output:
You now know how to create a simple programming language using ANTLR v4. In a future tutorial, I’ll show you how to create a more complex language that can have conditions and loops. If you have any questions, please do write a comment.