Tuesday, March 01, 2011

Creating your own programming language with ANTLR.

Hi, and welcome to my blog. I am Bart Kiers, a software developer from the Netherlands.

From time to time I answer questions about ANTLR on Stackoverflow, and while doing so, I see many people using ANTLR up to a certain point and then ask themselves: "Okay, how do I proceed from here on? What do I do after creating an AST from my source code?". I hope to answer those questions by creating a small programming language called Tiny Language (TL henceforth) that will be parsed by ANTLR and then interpreted using plain Java code.

Don't worry if certain things are (already) a bit vague: I'll write the coming blogs in such a way that people without prior ANTLR experience and without a Computer Science background can follow them. Or, better said: I hope they will! The only thing you need to know is Java.

This tutorial consists of the following parts:
  1. TL specification
  2. Introduction to ANTLR
  3. Lexical analysis of TL
  4. Syntactic analysis of TL
  5. Building an AST
  6. Creating a tree grammar
  7. Interpreting and evaluating TL I
  8. Interpreting and evaluating TL II
  9. Room for improvement & recommendations
Note that this tutorial uses ANTLR 3. For an implementation of TL using ANTLR 4, see this Github project: https://github.com/bkiers/tiny-language-antlr4


Rishabh Garg said...

This is an amazing tutorial. Helped me a great deal.

The only thing I am unable to understand is how to take a rule alternative on the basis of a boolean condition. I have posted my question on SO and it would be great if you could take a peek at it:


Bart Kiers said...

Hi Rishabh, thank you.
I already saw you've been answered on SO.



MiniScience said...
This comment has been removed by the author.
MiniScience said...

One of the special ANTLR and CC (Compiler-compiler)s tutorials. You're the best Bart! Best programming blog that is ANTLR-begginer-freindly.

Klaus Havelund said...

how come I have to look here in order to use ANTLR? Why is this not documented on the ANTLR page?

navneet Singh said...

I want to implement the ‘AST’ for C can somebody help me by giving the link to the grammar for C language.

Srishthi B said...

That is a brilliant article on dot net training in Chennai that I was searching for. Helps us a lot in referring at our dot net training institute in Chennai. Thanks a lot. Keep writing more on dot net course in Chennai, would love to follow your posts and refer to others in dot net training institutes in Chennai.