- Antlrworks
- Antlrworks 2
- Using ANTLRWorks With Java.g
- Antlrworks-1.4.3.jar
- Antlrworks 2 Download
- Antlrworks 2.1
- ANTLR - Wikipedia
Programming
- ANTLR gives tools to make parser development more comfortable. One of the tools is ANTLRworks. The tool can draw parse trees interactively. Though, I’ve experienced some problems with this functionality (with left recursion mostly) and abandoned using ANTLRworks.
- ANTLRWorks is a novel grammar development environment for ANTLR v3 grammars. It combines an excellent grammar-aware editor with an interpreter for rapid prototyping and a language-agnostic.
- ANTLRWorks 2 does not support Scala (it does not generate parser/lexer/visitor in Scala). However, ANTLRWorks 2 generates Java and interoperability between Scala and Java is easy. I decided to try it! Scala is a main language in which this application was developed. ANTLRWorks 2 was used for parsing regular expression.
- ANTLRWorks can also visualize the parser decision-making process (implemented with lookahead state machines) to help figure out which input sequences predict which alternative productions. ANTLRWorks is guaranteed to provide the same parsing information as ANTLR because it uses ANTLR as a library to obtain lookahead and nondeterminism information.
Antlrworks
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.
In computer-based language recognition, ANTLR (pronounced antler ), or ANother Tool for Language Recognition, is a parser generator that uses LL (.) for parsing. ANTLR is the successor to the Purdue Compiler Construction Tool Set ( PCCTS ), first developed in 1989, and is under active development.
Project Setup
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.
Create a Grammar File
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:
- an assign statement
- an add statement
- a print statement
It can handle only positive numbers, and programs must begin and end with the begin
and end
keywords.
Accordingly, create a new file call GYOO.g4 inside the src folder and add the following grammar to it:
Antlrworks 2
The grammar should be fairly intuitive to you if you are familiar with BNF.
Generate Parser and Lexer
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.
Create a Custom Listener
You must now create a new Java class that is a subclass of the GYOOBaseListener
class. Call it MyListener.
Inside 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 enterAssign()
and exitAssign()
methods. There are similar methods for the print and add statements too.
Using ANTLRWorks With Java.g
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:
Run the Parser
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 GYOOParser
object.
Antlrworks-1.4.3.jar
You must, of course, not forget to add the MyListener
class as a listener to the GYOOParser
object.
Antlrworks 2 Download
At this point, you can call the program()
method to start the parsing.
Antlrworks 2.1
And now, if you pass the sample GYOO program you saw earlier as the input file, you should see the following output:
ANTLR - Wikipedia
Conclusion
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.