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

5 comments:

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:

http://stackoverflow.com/questions/17054285/is-it-possible-to-lookahead-in-antlr4-without-actually-matching-a-token

Bart Kiers said...

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

Cheers,

Bart.

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?