Hari's Corner

Humour, comics, tech, law, software, reviews, essays, articles and HOWTOs intermingled with random philosophy now and then

Writing a Toy Calculator scripting language with Java and ANTLR 4 - Part 1

Filed under: Tutorials and HOWTOs by Hari
Posted on Sat, Apr 4, 2020 at 22:16 IST (last updated: Sun, Apr 5, 2020 @ 12:24 IST)

In this series Next >

I have always been interested in pattern matching and parsing, and recently have re-kindled my interest in Java. So it is only natural that I came across ANTLR. ANTLR is a parser-generator that works with multiple languages and originally written to generate Java code. It certainly caught my interest, since it not only combines a lexer with a parser generator, it also builds a parse-tree object, ready for you to feed into further processing / compiling / code generation. You can certainly using the lex/yacc, flex/bison combination, but ANTLR certainly seems much more convenient. For the theoritically minded, ANTLR generates LL(k) parsers.

In this article, I will discuss how to set up ANTLR with NetBeans and Maven and how to build a simple toy calculator language with this wonderful tool. While pattern matching and parsing are complex subjects, ANTLR certainly does a lot of the heavy lifting and helps you focus purely on the grammar of the language you are designing and using the results of parsing that grammar.

Why this article (series)

This article is basically to document my own effort at learning ANTLR and also give an idea to others as to how to build a basic simple "language" with this tool. I found many tutorials / Stack Overflow answers etc, both advanced and simpe, and have tried to distill their essence into this.

Defining our scope for the First Cut

The grammar I am about to design is very simple, yet involved enough to get an idea of what it is about, and it should be easy enough to extend. Since this is as much a learning experience for me as for the reader, I appreciate corrections/comments if I have made any mistakes or errors.

I assume that you have a basic knowledge of pattern matching (or regular expressions) and a working knowledge of Java.

Before going into the technicalities, it is good to define our scope. Most of the tutorials I read dived straight into the code, which makes it somewhat hard to understand what the author is trying to achieve. Avoiding that temptation, before writing a single line of code/grammar construct, I wanted to explain the scope which makes both the features and limitations of our end result clear. So, here is what I intend to achieve in the first cut of our grammar of ToyCalc:

Conceptually:

Grammatically:

A sample program (of our first cut ToyCalc) will look like this:

/* A sample ToyCalc Script
hello world. */
SETVALUE -110;
/* Note that since we 
use statement separator ; we can have
multiple statements in one line */
MUL 5.5; ADD 10; DIV 3;
PRINT "The current value is: ";
GETVALUE;
DIV 48.32;
PRINT "The final value is: ";
GETVALUE;

So without further, ado, let us dive into the requirements.

You need:

That's about it. Other dependencies can be pulled in using Maven directly in Netbeans to build your project.

First steps

  1. Fire up Netbeans.
  2. Under the Tools menu, go to Java Platforms. Add the correct path to your JDK.
  3. Choose File -> New Project...
  4. Under the category, Java with Maven, choose Java Application.
  5. I named my Application ToyCalc giving the group ID org.harishankar.  You can give whatever name you want. NetBeans will automatically create the package org.harishankar.toycalc for this application.

You should now have an empty Maven project.

Will be continued in Part 2.

In this series

No comments yet

Leave a comment »

There are no comments for this article yet.

Leave a comment

First-time comments on this blog are moderated.
Your name*
Email ID*
(wont' be published)
Website
Your comments*
(No HTML allowed)
:-) :-D :biggrin: :-P ;-) 8-) :-( :mad: |-| :oops: :-/ :-| :roll:
bold italic quote code
Code* captcha Enter the code you see in the image
* required fields